xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 4667:2cb417b1d90c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213446Smrj 
220Sstevel@tonic-gate /*
233434Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/thread.h>
310Sstevel@tonic-gate #include <sys/cpuvar.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/disp.h>
360Sstevel@tonic-gate #include <sys/class.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/asm_linkage.h>
400Sstevel@tonic-gate #include <sys/x_call.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/var.h>
430Sstevel@tonic-gate #include <sys/vtrace.h>
440Sstevel@tonic-gate #include <vm/hat.h>
450Sstevel@tonic-gate #include <vm/as.h>
460Sstevel@tonic-gate #include <vm/seg_kmem.h>
473446Smrj #include <vm/seg_kp.h>
480Sstevel@tonic-gate #include <sys/segments.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/stack.h>
510Sstevel@tonic-gate #include <sys/smp_impldefs.h>
520Sstevel@tonic-gate #include <sys/x86_archext.h>
530Sstevel@tonic-gate #include <sys/machsystm.h>
540Sstevel@tonic-gate #include <sys/traptrace.h>
550Sstevel@tonic-gate #include <sys/clock.h>
560Sstevel@tonic-gate #include <sys/cpc_impl.h>
573434Sesaxe #include <sys/pg.h>
583434Sesaxe #include <sys/cmt.h>
590Sstevel@tonic-gate #include <sys/dtrace.h>
600Sstevel@tonic-gate #include <sys/archsystm.h>
610Sstevel@tonic-gate #include <sys/fp.h>
620Sstevel@tonic-gate #include <sys/reboot.h>
633446Smrj #include <sys/kdi_machimpl.h>
640Sstevel@tonic-gate #include <vm/hat_i86.h>
650Sstevel@tonic-gate #include <sys/memnode.h>
66938Sesaxe #include <sys/pci_cfgspace.h>
673446Smrj #include <sys/mach_mmu.h>
683446Smrj #include <sys/sysmacros.h>
691414Scindi #include <sys/cpu_module.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
720Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
730Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
763446Smrj  * Useful for disabling MP bring-up on a MP capable system.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate int use_mp = 1;
790Sstevel@tonic-gate 
802006Sandrei /*
813446Smrj  * to be set by a PSM to indicate what cpus
823446Smrj  * are sitting around on the system.
832006Sandrei  */
843446Smrj cpuset_t mp_cpus;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
880Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
890Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
900Sstevel@tonic-gate  * order to allow cross calls.
910Sstevel@tonic-gate  */
923446Smrj int flushes_require_xcalls;
933446Smrj cpuset_t cpu_ready_set = 1;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static 	void	mp_startup(void);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static void cpu_sep_enable(void);
980Sstevel@tonic-gate static void cpu_sep_disable(void);
990Sstevel@tonic-gate static void cpu_asysc_enable(void);
1000Sstevel@tonic-gate static void cpu_asysc_disable(void);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate extern int tsc_gethrtime_enable;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate void
1080Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1110Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1170Sstevel@tonic-gate 
118*4667Smh27603 	/*
119*4667Smh27603 	 * Current frequency in Hz.
120*4667Smh27603 	 */
121*4667Smh27603 	pi->pi_curr_clock = cpu_freq_hz;
122*4667Smh27603 
1230Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1240Sstevel@tonic-gate 	if (fpu_exists)
1250Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1300Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1350Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1360Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Configure syscall support on this CPU.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate /*ARGSUSED*/
1450Sstevel@tonic-gate static void
1460Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	kpreempt_disable();
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #if defined(__amd64)
1513446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #if !defined(__lint)
1540Sstevel@tonic-gate 		/*
1550Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1560Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1570Sstevel@tonic-gate 		 * here.
1580Sstevel@tonic-gate 		 */
1590Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1600Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1610Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate 		/*
1640Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1650Sstevel@tonic-gate 		 */
1660Sstevel@tonic-gate 		cpu_asysc_enable();
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		/*
1690Sstevel@tonic-gate 		 * Program the magic registers ..
1700Sstevel@tonic-gate 		 */
1713446Smrj 		wrmsr(MSR_AMD_STAR,
1723446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
173770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
174770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		/*
1770Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1780Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1790Sstevel@tonic-gate 		 */
180770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate #endif
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1860Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1870Sstevel@tonic-gate 	 *
1880Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1890Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1900Sstevel@tonic-gate 	 */
1913446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #if !defined(__lint)
1940Sstevel@tonic-gate 		/*
1950Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1960Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1970Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
1980Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
1990Sstevel@tonic-gate 		 * Instruction Set Reference"
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2040Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2070Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		cpu_sep_enable();
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2140Sstevel@tonic-gate 		 * via a context handler.
2150Sstevel@tonic-gate 		 */
2163446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
217770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	kpreempt_enable();
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Multiprocessor initialization.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2270Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2280Sstevel@tonic-gate  */
2293446Smrj struct cpu *
2300Sstevel@tonic-gate mp_startup_init(int cpun)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	struct cpu *cp;
2330Sstevel@tonic-gate 	kthread_id_t tp;
2340Sstevel@tonic-gate 	caddr_t	sp;
2350Sstevel@tonic-gate 	proc_t *procp;
2360Sstevel@tonic-gate 	extern void idle();
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate #ifdef TRAPTRACE
2390Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2430Sstevel@tonic-gate 
2443446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2454481Sbholler 	if (x86_feature & X86_MWAIT)
2464481Sbholler 		cp->cpu_m.mcpu_mwait = mach_alloc_mwait(CPU);
2474481Sbholler 
2480Sstevel@tonic-gate 	procp = curthread->t_procp;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	disp_cpu_init(cp);
2550Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2560Sstevel@tonic-gate 
257414Skchow 	cpu_vm_data_init(cp);
258414Skchow 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2610Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2620Sstevel@tonic-gate 	 * when the CPU starts running.
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2650Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2690Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2700Sstevel@tonic-gate 	 *
2710Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2720Sstevel@tonic-gate 	 * thread_create().
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2750Sstevel@tonic-gate 	tp->t_preempt = 1;
2760Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2770Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2780Sstevel@tonic-gate 	tp->t_cpu = cp;
2790Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	sp = tp->t_stk;
2850Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2860Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2873446Smrj #if defined(__amd64)
2883446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
2893446Smrj #endif
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	cp->cpu_id = cpun;
2920Sstevel@tonic-gate 	cp->cpu_self = cp;
2930Sstevel@tonic-gate 	cp->cpu_thread = tp;
2940Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
2950Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
2960Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2991482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3001482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3011482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3021482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3031482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3041482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3051482Ssethg 	 * proper value.
3061482Ssethg 	 */
3071482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3081482Ssethg 
3091482Ssethg 	/*
3100Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	tp->t_preempt = 1;
3170Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3180Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3190Sstevel@tonic-gate 	tp->t_cpu = cp;
3200Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3233434Sesaxe 	 * Bootstrap the CPU's PG data
32460Sesaxe 	 */
3253434Sesaxe 	pg_cpu_bootstrap(cp);
32660Sesaxe 
32760Sesaxe 	/*
3283446Smrj 	 * Perform CPC initialization on the new CPU.
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	kcpc_hw_init(cp);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3340Sstevel@tonic-gate 	 * for each CPU.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3393446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3400Sstevel@tonic-gate 	 */
3413446Smrj #if !defined(__lint)
3423446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3433446Smrj #endif
3443446Smrj 	cp->cpu_m.mcpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3453446Smrj 	bcopy(CPU->cpu_m.mcpu_gdt, cp->cpu_m.mcpu_gdt,
3463446Smrj 	    (sizeof (*cp->cpu_m.mcpu_gdt) * NGDT));
3471626Srab 
3483446Smrj #if defined(__i386)
3490Sstevel@tonic-gate 	/*
3500Sstevel@tonic-gate 	 * setup kernel %gs.
3510Sstevel@tonic-gate 	 */
3520Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3530Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3543446Smrj #endif
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3580Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3590Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3600Sstevel@tonic-gate 	 * cmpxchgl register bug
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
3633446Smrj 		struct machcpu *mcpu = &cp->cpu_m;
3643446Smrj 
3653446Smrj 		mcpu->mcpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
3663446Smrj 		bcopy(idt0, mcpu->mcpu_idt, sizeof (idt0));
3673446Smrj 	} else {
3683446Smrj 		cp->cpu_m.mcpu_idt = CPU->cpu_m.mcpu_idt;
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3723446Smrj 	 * Get interrupt priority data from cpu 0.
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3750Sstevel@tonic-gate 
3763446Smrj 	/*
3773446Smrj 	 * alloc space for cpuid info
3783446Smrj 	 */
3793446Smrj 	cpuid_alloc_space(cp);
3803446Smrj 
3814581Ssherrym 	/*
3824581Ssherrym 	 * alloc space for ucode_info
3834581Ssherrym 	 */
3844581Ssherrym 	ucode_alloc_space(cp);
3854581Ssherrym 
3860Sstevel@tonic-gate 	hat_cpu_online(cp);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate #ifdef TRAPTRACE
3890Sstevel@tonic-gate 	/*
3903446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
3910Sstevel@tonic-gate 	 */
3920Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
3930Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
3940Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
3950Sstevel@tonic-gate #endif
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * Record that we have another CPU.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4020Sstevel@tonic-gate 	 */
4031455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4040Sstevel@tonic-gate 	/*
4050Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4060Sstevel@tonic-gate 	 * after mp_startup().
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	cpu_add_unit(cp);
4090Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4103446Smrj 
4113446Smrj 	return (cp);
4123446Smrj }
4133446Smrj 
4143446Smrj /*
4153446Smrj  * Undo what was done in mp_startup_init
4163446Smrj  */
4173446Smrj static void
4183446Smrj mp_startup_fini(struct cpu *cp, int error)
4193446Smrj {
4203446Smrj 	mutex_enter(&cpu_lock);
4213446Smrj 
4223446Smrj 	/*
4233446Smrj 	 * Remove the CPU from the list of available CPUs.
4243446Smrj 	 */
4253446Smrj 	cpu_del_unit(cp->cpu_id);
4263446Smrj 
4273446Smrj 	if (error == ETIMEDOUT) {
4283446Smrj 		/*
4293446Smrj 		 * The cpu was started, but never *seemed* to run any
4303446Smrj 		 * code in the kernel; it's probably off spinning in its
4313446Smrj 		 * own private world, though with potential references to
4323446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4333446Smrj 		 *
4343446Smrj 		 * Worse still, it may actually wake up some time later,
4353446Smrj 		 * so rather than guess what it might or might not do, we
4363446Smrj 		 * leave the fundamental data structures intact.
4373446Smrj 		 */
4383446Smrj 		cp->cpu_flags = 0;
4393446Smrj 		mutex_exit(&cpu_lock);
4403446Smrj 		return;
4413446Smrj 	}
4423446Smrj 
4433446Smrj 	/*
4443446Smrj 	 * At this point, the only threads bound to this CPU should
4453446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4463446Smrj 	 * and it's interrupt threads.  Clean these up.
4473446Smrj 	 */
4483446Smrj 	cpu_destroy_bound_threads(cp);
4493446Smrj 	cp->cpu_idle_thread = NULL;
4503446Smrj 
4513446Smrj 	/*
4523446Smrj 	 * Free the interrupt stack.
4533446Smrj 	 */
4543446Smrj 	segkp_release(segkp,
4553446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
4563446Smrj 
4573446Smrj 	mutex_exit(&cpu_lock);
4583446Smrj 
4593446Smrj #ifdef TRAPTRACE
4603446Smrj 	/*
4613446Smrj 	 * Discard the trap trace buffer
4623446Smrj 	 */
4633446Smrj 	{
4643446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
4653446Smrj 
4663446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
4673446Smrj 		ttc->ttc_first = NULL;
4683446Smrj 	}
4693446Smrj #endif
4703446Smrj 
4713446Smrj 	hat_cpu_offline(cp);
4723446Smrj 
4733446Smrj 	cpuid_free_space(cp);
4743446Smrj 
4754581Ssherrym 	ucode_free_space(cp);
4764581Ssherrym 
4773446Smrj 	if (cp->cpu_m.mcpu_idt != CPU->cpu_m.mcpu_idt)
4783446Smrj 		kmem_free(cp->cpu_m.mcpu_idt, sizeof (idt0));
4793446Smrj 	cp->cpu_m.mcpu_idt = NULL;
4803446Smrj 
4813446Smrj 	kmem_free(cp->cpu_m.mcpu_gdt, PAGESIZE);
4823446Smrj 	cp->cpu_m.mcpu_gdt = NULL;
4833446Smrj 
4843446Smrj 	teardown_vaddr_for_ppcopy(cp);
4853446Smrj 
4863446Smrj 	kcpc_hw_fini(cp);
4873446Smrj 
4883446Smrj 	cp->cpu_dispthread = NULL;
4893446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
4903446Smrj 
4913446Smrj 	cpu_vm_data_destroy(cp);
4923446Smrj 
4933446Smrj 	mutex_enter(&cpu_lock);
4943446Smrj 	disp_cpu_fini(cp);
4953446Smrj 	mutex_exit(&cpu_lock);
4963446Smrj 
4973446Smrj 	kmem_free(cp, sizeof (*cp));
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5040Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5050Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5060Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5070Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5080Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5090Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5100Sstevel@tonic-gate  * system.
5110Sstevel@tonic-gate  *
512938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
513938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
514938Sesaxe  * to acknowledging their readiness to the master, so this routine will
515938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
516938Sesaxe  * global data safe.
517938Sesaxe  *
518359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
519359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5200Sstevel@tonic-gate  */
5210Sstevel@tonic-gate 
5223446Smrj #if defined(OPTERON_ERRATUM_88)
5233446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5243446Smrj #endif
5253446Smrj 
5260Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5270Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5280Sstevel@tonic-gate #endif
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5310Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5320Sstevel@tonic-gate #endif
5330Sstevel@tonic-gate 
5343446Smrj #if defined(OPTERON_ERRATUM_95)
5353446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
5363446Smrj #endif
5373446Smrj 
5380Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5390Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5400Sstevel@tonic-gate #endif
5410Sstevel@tonic-gate 
5423446Smrj #if defined(OPTERON_ERRATUM_108)
5433446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
5443446Smrj #endif
5453446Smrj 
5460Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5470Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5480Sstevel@tonic-gate #endif
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5510Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5550Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5560Sstevel@tonic-gate #endif
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5590Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5600Sstevel@tonic-gate #endif
5610Sstevel@tonic-gate 
562359Skucharsk #if defined(OPTERON_ERRATUM_131)
563359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
564359Skucharsk #endif
5650Sstevel@tonic-gate 
566938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
567938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
568938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
569938Sesaxe #endif
570938Sesaxe 
5711582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5721582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5731582Skchow #endif
5741582Skchow 
5753446Smrj static void
5763446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
5773446Smrj {
5783446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
5793446Smrj 	    cp->cpu_id, erratum);
5803446Smrj }
5813446Smrj 
5823446Smrj static void
5833446Smrj workaround_applied(uint_t erratum)
5843446Smrj {
5853446Smrj 	if (erratum > 1000000)
5863446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
5873446Smrj 		    erratum);
5883446Smrj 	else
5893446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
5903446Smrj 		    erratum);
5913446Smrj }
5923446Smrj 
5933446Smrj static void
5943446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
5953446Smrj {
5963446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
5973446Smrj 	    cp->cpu_id, rw, msr, error);
5983446Smrj }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate uint_t
6010Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate 	uint_t missing = 0;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	ASSERT(cpu == CPU);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*LINTED*/
6080Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
6110Sstevel@tonic-gate 		 */
6120Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
6130Sstevel@tonic-gate 		/*
6140Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
6150Sstevel@tonic-gate 		 */
6163446Smrj 		opteron_erratum_88++;
6170Sstevel@tonic-gate #else
6183446Smrj 		workaround_warning(cpu, 88);
6190Sstevel@tonic-gate 		missing++;
6200Sstevel@tonic-gate #endif
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
6240Sstevel@tonic-gate 		/*
6250Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
6260Sstevel@tonic-gate 		 */
6270Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6280Sstevel@tonic-gate 		/*
6290Sstevel@tonic-gate 		 * fix is in trap.c
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		opteron_erratum_91++;
6320Sstevel@tonic-gate #else
6333446Smrj 		workaround_warning(cpu, 91);
6340Sstevel@tonic-gate 		missing++;
6350Sstevel@tonic-gate #endif
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * fix is in trap.c
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate 		opteron_erratum_93++;
6470Sstevel@tonic-gate #else
6483446Smrj 		workaround_warning(cpu, 93);
6490Sstevel@tonic-gate 		missing++;
6500Sstevel@tonic-gate #endif
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*LINTED*/
6540Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
6550Sstevel@tonic-gate 		/*
6560Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
6570Sstevel@tonic-gate 		 */
6580Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
6590Sstevel@tonic-gate #if defined(_LP64)
6600Sstevel@tonic-gate 		/*
6610Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
6620Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
6630Sstevel@tonic-gate 		 * range mod 4G.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
6660Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/*LINTED*/
6690Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
6703446Smrj 		opteron_erratum_95++;
6710Sstevel@tonic-gate #endif	/* _LP64 */
6720Sstevel@tonic-gate #else
6733446Smrj 		workaround_warning(cpu, 95);
6740Sstevel@tonic-gate 		missing++;
6753446Smrj #endif
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
6790Sstevel@tonic-gate 		/*
6800Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
6810Sstevel@tonic-gate 		 */
6820Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * fix is in trap.c
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		opteron_erratum_100++;
6870Sstevel@tonic-gate #else
6883446Smrj 		workaround_warning(cpu, 100);
6890Sstevel@tonic-gate 		missing++;
6900Sstevel@tonic-gate #endif
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/*LINTED*/
6940Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
6950Sstevel@tonic-gate 		/*
6960Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
6970Sstevel@tonic-gate 		 * Some Processors
6980Sstevel@tonic-gate 		 */
6990Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
7020Sstevel@tonic-gate 		 * those processors)
7030Sstevel@tonic-gate 		 */
7040Sstevel@tonic-gate #else
7053446Smrj 		workaround_warning(cpu, 108);
7060Sstevel@tonic-gate 		missing++;
7070Sstevel@tonic-gate #endif
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/*LINTED*/
7113446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
7163446Smrj 		/*
7173446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
7183446Smrj 		 */
7193446Smrj 		uint64_t value;
7203446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
7213446Smrj 		int err;
7220Sstevel@tonic-gate 
7233446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
7243446Smrj 			msr_warning(cpu, "rd", msr, err);
7253446Smrj 			workaround_warning(cpu, 109);
7263446Smrj 			missing++;
7273446Smrj 		}
7283446Smrj 		if (value == 0)
7290Sstevel@tonic-gate 			opteron_erratum_109++;
7300Sstevel@tonic-gate #else
7313446Smrj 		workaround_warning(cpu, 109);
7320Sstevel@tonic-gate 		missing++;
7330Sstevel@tonic-gate #endif
7343446Smrj 	/*CONSTANTCONDITION*/
7353446Smrj 	} while (0);
7363446Smrj 
7370Sstevel@tonic-gate 	/*LINTED*/
7380Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
7390Sstevel@tonic-gate 		/*
7400Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
7410Sstevel@tonic-gate 		 * Processor Hang
7420Sstevel@tonic-gate 		 */
7430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
7443446Smrj #if defined(_LP64)
7450Sstevel@tonic-gate 		/*
7460Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
7470Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
7480Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
7490Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
7500Sstevel@tonic-gate 		 */
7513446Smrj 		if (opteron_erratum_121)
7523446Smrj 			opteron_erratum_121++;
7533446Smrj 		else {
7543446Smrj 			if (hole_start) {
7553446Smrj 				hole_start -= PAGESIZE;
7563446Smrj 			} else {
7573446Smrj 				/*
7583446Smrj 				 * hole_start not yet initialized by
7593446Smrj 				 * mmu_init. Initialize hole_start
7603446Smrj 				 * with value to be subtracted.
7613446Smrj 				 */
7623446Smrj 				hole_start = PAGESIZE;
7630Sstevel@tonic-gate 			}
7643446Smrj 			opteron_erratum_121++;
7650Sstevel@tonic-gate 		}
7663446Smrj #endif	/* _LP64 */
7670Sstevel@tonic-gate #else
7683446Smrj 		workaround_warning(cpu, 121);
7690Sstevel@tonic-gate 		missing++;
7700Sstevel@tonic-gate #endif
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/*LINTED*/
7743446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
7750Sstevel@tonic-gate 		/*
7763446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
7770Sstevel@tonic-gate 		 * Multiprocessor Systems
7780Sstevel@tonic-gate 		 */
7790Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
7803446Smrj 		uint64_t value;
7813446Smrj 		const uint_t msr = MSR_AMD_HWCR;
7823446Smrj 		int error;
7833446Smrj 
7840Sstevel@tonic-gate 		/*
7850Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
7860Sstevel@tonic-gate 		 * or multi-processor).
7870Sstevel@tonic-gate 		 */
7883446Smrj 		if (!opteron_erratum_122 && lgrp_plat_node_cnt == 1 &&
7893446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
7903446Smrj 			break;
7910Sstevel@tonic-gate 
7923446Smrj 		/* disable TLB Flush Filter */
7933446Smrj 
7943446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
7953446Smrj 			msr_warning(cpu, "rd", msr, error);
7963446Smrj 			workaround_warning(cpu, 122);
7973446Smrj 			missing++;
7983446Smrj 		} else {
7993446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
8003446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
8013446Smrj 				msr_warning(cpu, "wr", msr, error);
8023446Smrj 				workaround_warning(cpu, 122);
8033446Smrj 				missing++;
8043446Smrj 			}
8050Sstevel@tonic-gate 		}
8063446Smrj 		opteron_erratum_122++;
8070Sstevel@tonic-gate #else
8083446Smrj 		workaround_warning(cpu, 122);
8090Sstevel@tonic-gate 		missing++;
8100Sstevel@tonic-gate #endif
8113446Smrj 	/*CONSTANTCONDITION*/
8123446Smrj 	} while (0);
813302Skchow 
8140Sstevel@tonic-gate 	/*LINTED*/
8153446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
8160Sstevel@tonic-gate 		/*
8170Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
8180Sstevel@tonic-gate 		 * Dual Core Processors
8190Sstevel@tonic-gate 		 */
8203446Smrj #if defined(OPTERON_ERRATUM_123)
8213446Smrj 		uint64_t value;
8223446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8233446Smrj 		int err;
8243446Smrj 
8250Sstevel@tonic-gate 		/*
8260Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
8270Sstevel@tonic-gate 		 */
8283446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
8293446Smrj 			break;
8300Sstevel@tonic-gate 
8313446Smrj 		/*
8323446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8333446Smrj 		 */
8343446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8353446Smrj 			msr_warning(cpu, "rd", msr, err);
8363446Smrj 			workaround_warning(cpu, 123);
8373446Smrj 			missing++;
8380Sstevel@tonic-gate 		}
8393446Smrj 		if (value == 0)
8403446Smrj 			opteron_erratum_123++;
8413446Smrj #else
8423446Smrj 		workaround_warning(cpu, 123);
8433446Smrj 		missing++;
844359Skucharsk 
8453446Smrj #endif
8463446Smrj 	/*CONSTANTCONDITION*/
8473446Smrj 	} while (0);
8483446Smrj 
849359Skucharsk 	/*LINTED*/
8503446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
851359Skucharsk 		/*
852359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
853359Skucharsk 		 * Waiting for a Probe Response
854359Skucharsk 		 */
8553446Smrj #if defined(OPTERON_ERRATUM_131)
8563446Smrj 		uint64_t nbcfg;
8573446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
8583446Smrj 		const uint64_t wabits =
8593446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
8603446Smrj 		int error;
8613446Smrj 
862359Skucharsk 		/*
863359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
864359Skucharsk 		 */
8653446Smrj 		if (opteron_erratum_131)
8663446Smrj 			break;
8672519Skchow 
8683446Smrj 		if (lgrp_plat_node_cnt * cpuid_get_ncpu_per_chip(cpu) < 4)
8693446Smrj 			break;
8702519Skchow 
8713446Smrj 		/*
8723446Smrj 		 * Print a warning if neither of the workarounds for
8733446Smrj 		 * erratum 131 is present.
8743446Smrj 		 */
8753446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
8763446Smrj 			msr_warning(cpu, "rd", msr, error);
8773446Smrj 			workaround_warning(cpu, 131);
8783446Smrj 			missing++;
8793446Smrj 		} else if ((nbcfg & wabits) == 0) {
8803446Smrj 			opteron_erratum_131++;
8813446Smrj 		} else {
8823446Smrj 			/* cannot have both workarounds set */
8833446Smrj 			ASSERT((nbcfg & wabits) != wabits);
884359Skucharsk 		}
8853446Smrj #else
8863446Smrj 		workaround_warning(cpu, 131);
8873446Smrj 		missing++;
888359Skucharsk #endif
8893446Smrj 	/*CONSTANTCONDITION*/
8903446Smrj 	} while (0);
891938Sesaxe 
892938Sesaxe 	/*
8933446Smrj 	 * This isn't really an erratum, but for convenience the
894938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
895938Sesaxe 	 */
896938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
8973446Smrj #if defined(OPTERON_WORKAROUND_6336786)
898938Sesaxe 		/*
899938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
900938Sesaxe 		 * K8 platforms to guard against TSC drift.
901938Sesaxe 		 */
902938Sesaxe 		if (opteron_workaround_6336786) {
903938Sesaxe 			opteron_workaround_6336786++;
904938Sesaxe 		} else if ((lgrp_plat_node_cnt *
9053446Smrj 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
906938Sesaxe 		    opteron_workaround_6336786_UP) {
9073446Smrj 			int	node;
9083446Smrj 			uint8_t data;
9093446Smrj 
910938Sesaxe 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
911938Sesaxe 				/*
912938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
913938Sesaxe 				 * Northbridge device is the node id + 24.
914938Sesaxe 				 */
915938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
916938Sesaxe 				data &= 0xFC;
917938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
918938Sesaxe 			}
919938Sesaxe 			opteron_workaround_6336786++;
920938Sesaxe 		}
9213446Smrj #else
9223446Smrj 		workaround_warning(cpu, 6336786);
9233446Smrj 		missing++;
924938Sesaxe #endif
9253446Smrj 	}
9261582Skchow 
9271582Skchow 	/*LINTED*/
9281582Skchow 	/*
9291582Skchow 	 * Mutex primitives don't work as expected.
9301582Skchow 	 */
9311582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
9323446Smrj #if defined(OPTERON_WORKAROUND_6323525)
9331582Skchow 		/*
9343446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
9351582Skchow 		 * MSR_BU_CFG set, then not applicable. The workaround
9361582Skchow 		 * is to patch the semaphone routines with the lfence
9371582Skchow 		 * instruction to provide necessary load memory barrier with
9381582Skchow 		 * possible subsequent read-modify-write ops.
9391582Skchow 		 *
9401582Skchow 		 * It is too early in boot to call the patch routine so
9411582Skchow 		 * set erratum variable to be done in startup_end().
9421582Skchow 		 */
9431582Skchow 		if (opteron_workaround_6323525) {
9441582Skchow 			opteron_workaround_6323525++;
9451582Skchow 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
9463446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
9471582Skchow 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
9481582Skchow 				opteron_workaround_6323525++;
9491582Skchow 		}
9503446Smrj #else
9513446Smrj 		workaround_warning(cpu, 6323525);
9523446Smrj 		missing++;
9533446Smrj #endif
9541582Skchow 	}
9553446Smrj 
9560Sstevel@tonic-gate 	return (missing);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate void
9600Sstevel@tonic-gate workaround_errata_end()
9610Sstevel@tonic-gate {
9623446Smrj #if defined(OPTERON_ERRATUM_88)
9633446Smrj 	if (opteron_erratum_88)
9643446Smrj 		workaround_applied(88);
9653446Smrj #endif
9663446Smrj #if defined(OPTERON_ERRATUM_91)
9673446Smrj 	if (opteron_erratum_91)
9683446Smrj 		workaround_applied(91);
9693446Smrj #endif
9703446Smrj #if defined(OPTERON_ERRATUM_93)
9713446Smrj 	if (opteron_erratum_93)
9723446Smrj 		workaround_applied(93);
9733446Smrj #endif
9743446Smrj #if defined(OPTERON_ERRATUM_95)
9753446Smrj 	if (opteron_erratum_95)
9763446Smrj 		workaround_applied(95);
9773446Smrj #endif
9783446Smrj #if defined(OPTERON_ERRATUM_100)
9793446Smrj 	if (opteron_erratum_100)
9803446Smrj 		workaround_applied(100);
9813446Smrj #endif
9823446Smrj #if defined(OPTERON_ERRATUM_108)
9833446Smrj 	if (opteron_erratum_108)
9843446Smrj 		workaround_applied(108);
9853446Smrj #endif
9860Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
9870Sstevel@tonic-gate 	if (opteron_erratum_109) {
988359Skucharsk 		cmn_err(CE_WARN,
989359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
990359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
991359Skucharsk 		    " system's BIOS to a version\ncontaining this"
992359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
993359Skucharsk 		    " system\noperation may occur.\n");
9940Sstevel@tonic-gate 	}
9953446Smrj #endif
9963446Smrj #if defined(OPTERON_ERRATUM_121)
9973446Smrj 	if (opteron_erratum_121)
9983446Smrj 		workaround_applied(121);
9993446Smrj #endif
10003446Smrj #if defined(OPTERON_ERRATUM_122)
10013446Smrj 	if (opteron_erratum_122)
10023446Smrj 		workaround_applied(122);
10033446Smrj #endif
10040Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
10050Sstevel@tonic-gate 	if (opteron_erratum_123) {
1006359Skucharsk 		cmn_err(CE_WARN,
1007359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1008359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1009359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1010359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1011359Skucharsk 		    " system\noperation may occur.\n");
10120Sstevel@tonic-gate 	}
10133446Smrj #endif
1014359Skucharsk #if defined(OPTERON_ERRATUM_131)
1015359Skucharsk 	if (opteron_erratum_131) {
1016359Skucharsk 		cmn_err(CE_WARN,
1017359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1018359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1019359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1020359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1021359Skucharsk 		    " system\noperation may occur.\n");
1022359Skucharsk 	}
10233446Smrj #endif
10243446Smrj #if defined(OPTERON_WORKAROUND_6336786)
10253446Smrj 	if (opteron_workaround_6336786)
10263446Smrj 		workaround_applied(6336786);
10273446Smrj #endif
10283446Smrj #if defined(OPTERON_WORKAROUND_6323525)
10293446Smrj 	if (opteron_workaround_6323525)
10303446Smrj 		workaround_applied(6323525);
10313446Smrj #endif
10320Sstevel@tonic-gate }
10330Sstevel@tonic-gate 
10343446Smrj static cpuset_t procset;
10353446Smrj 
10363446Smrj /*
10373446Smrj  * Start a single cpu, assuming that the kernel context is available
10383446Smrj  * to successfully start another cpu.
10393446Smrj  *
10403446Smrj  * (For example, real mode code is mapped into the right place
10413446Smrj  * in memory and is ready to be run.)
10423446Smrj  */
10433446Smrj int
10443446Smrj start_cpu(processorid_t who)
10453446Smrj {
10463446Smrj 	void *ctx;
10473446Smrj 	cpu_t *cp;
10483446Smrj 	int delays;
10493446Smrj 	int error = 0;
10503446Smrj 
10513446Smrj 	ASSERT(who != 0);
10523446Smrj 
10533446Smrj 	/*
10543446Smrj 	 * Check if there's at least a Mbyte of kmem available
10553446Smrj 	 * before attempting to start the cpu.
10563446Smrj 	 */
10573446Smrj 	if (kmem_avail() < 1024 * 1024) {
10583446Smrj 		/*
10593446Smrj 		 * Kick off a reap in case that helps us with
10603446Smrj 		 * later attempts ..
10613446Smrj 		 */
10623446Smrj 		kmem_reap();
10633446Smrj 		return (ENOMEM);
10643446Smrj 	}
10653446Smrj 
10663446Smrj 	cp = mp_startup_init(who);
10673446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
10683446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
10693446Smrj 
10703446Smrj 		/*
10713446Smrj 		 * Something went wrong before we even started it
10723446Smrj 		 */
10733446Smrj 		if (ctx)
10743446Smrj 			cmn_err(CE_WARN,
10753446Smrj 			    "cpu%d: failed to start error %d",
10763446Smrj 			    cp->cpu_id, error);
10773446Smrj 		else
10783446Smrj 			cmn_err(CE_WARN,
10793446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
10800Sstevel@tonic-gate 
10813446Smrj 		if (ctx)
10823446Smrj 			mach_cpucontext_free(cp, ctx, error);
10833446Smrj 		else
10843446Smrj 			error = EAGAIN;		/* hmm. */
10853446Smrj 		mp_startup_fini(cp, error);
10863446Smrj 		return (error);
10873446Smrj 	}
10883446Smrj 
10893446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
10903446Smrj 		if (delays == 500) {
10913446Smrj 			/*
10923446Smrj 			 * After five seconds, things are probably looking
10933446Smrj 			 * a bit bleak - explain the hang.
10943446Smrj 			 */
10953446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
10963446Smrj 			    "but not running in the kernel yet", who);
10973446Smrj 		} else if (delays > 2000) {
10983446Smrj 			/*
10993446Smrj 			 * We waited at least 20 seconds, bail ..
11003446Smrj 			 */
11013446Smrj 			error = ETIMEDOUT;
11023446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
11033446Smrj 			mach_cpucontext_free(cp, ctx, error);
11043446Smrj 			mp_startup_fini(cp, error);
11053446Smrj 			return (error);
11063446Smrj 		}
11073446Smrj 
11083446Smrj 		/*
11093446Smrj 		 * wait at least 10ms, then check again..
11103446Smrj 		 */
11113446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
11123446Smrj 	}
11133446Smrj 
11143446Smrj 	mach_cpucontext_free(cp, ctx, 0);
11153446Smrj 
11163446Smrj 	if (tsc_gethrtime_enable)
11173446Smrj 		tsc_sync_master(who);
11183446Smrj 
11193446Smrj 	if (dtrace_cpu_init != NULL) {
11203446Smrj 		/*
11213446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
11223446Smrj 		 */
11233446Smrj 		mutex_enter(&cpu_lock);
11243446Smrj 		(*dtrace_cpu_init)(who);
11253446Smrj 		mutex_exit(&cpu_lock);
11263446Smrj 	}
11273446Smrj 
11283446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
11293446Smrj 		delay(1);
11303446Smrj 
11313446Smrj 	return (0);
11323446Smrj }
11333446Smrj 
11342006Sandrei 
11350Sstevel@tonic-gate /*ARGSUSED*/
11360Sstevel@tonic-gate void
11370Sstevel@tonic-gate start_other_cpus(int cprboot)
11380Sstevel@tonic-gate {
11393446Smrj 	uint_t who;
11403446Smrj 	uint_t skipped = 0;
11413446Smrj 	uint_t bootcpuid = 0;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/*
11440Sstevel@tonic-gate 	 * Initialize our own cpu_info.
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate 	init_cpu_info(CPU);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * Initialize our syscall handlers
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	init_cpu_syscall(CPU);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/*
11543446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
11553446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
11563446Smrj 	 * precisely the same reason.
11573446Smrj 	 */
11583446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
11593446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
11603446Smrj 
11613446Smrj 	/*
11620Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
11630Sstevel@tonic-gate 	 */
11643446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
11650Sstevel@tonic-gate 		if (use_mp == 0)
11660Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
11670Sstevel@tonic-gate 		goto done;
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	/*
11710Sstevel@tonic-gate 	 * perform such initialization as is needed
11720Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
11730Sstevel@tonic-gate 	 */
11740Sstevel@tonic-gate 	cpu_pause_init();
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
11770Sstevel@tonic-gate 
11783446Smrj 	if (mach_cpucontext_init() != 0)
11790Sstevel@tonic-gate 		goto done;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	flushes_require_xcalls = 1;
11820Sstevel@tonic-gate 
11832575Snf202958 	/*
11842575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
11852575Snf202958 	 * do their TSC syncs with the same CPU.
11862575Snf202958 	 */
11870Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
11902575Snf202958 
11912006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
11922006Sandrei 			continue;
11933446Smrj 		ASSERT(who != bootcpuid);
11942006Sandrei 		if (ncpus >= max_ncpus) {
11952006Sandrei 			skipped = who;
11960Sstevel@tonic-gate 			continue;
11972006Sandrei 		}
11983446Smrj 		if (start_cpu(who) != 0)
11993446Smrj 			CPUSET_DEL(mp_cpus, who);
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 
12024581Ssherrym 	/* Free the space allocated to hold the microcode file */
12034581Ssherrym 	ucode_free();
12044581Ssherrym 
12050Sstevel@tonic-gate 	affinity_clear();
12060Sstevel@tonic-gate 
12072006Sandrei 	if (skipped) {
12082006Sandrei 		cmn_err(CE_NOTE,
12093446Smrj 		    "System detected %d cpus, but "
12103446Smrj 		    "only %d cpu(s) were enabled during boot.",
12112006Sandrei 		    skipped + 1, ncpus);
12122006Sandrei 		cmn_err(CE_NOTE,
12132006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
12142006Sandrei 		    "See eeprom(1M).");
12152006Sandrei 	}
12162006Sandrei 
12170Sstevel@tonic-gate done:
12180Sstevel@tonic-gate 	workaround_errata_end();
12193446Smrj 	mach_cpucontext_fini();
12201642Sgavinm 
12211642Sgavinm 	cmi_post_mpstartup();
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate /*ARGSUSED*/
12280Sstevel@tonic-gate int
12290Sstevel@tonic-gate mp_cpu_configure(int cpuid)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate /*ARGSUSED*/
12350Sstevel@tonic-gate int
12360Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
12432985Sdmick  * Called from real_mode_start.
12441251Skchow  *
12451251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
12461251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
12471251Skchow  * hat_unload which requires CPU_READY to be set.
12480Sstevel@tonic-gate  */
12490Sstevel@tonic-gate void
12500Sstevel@tonic-gate mp_startup(void)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	struct cpu *cp = CPU;
12530Sstevel@tonic-gate 	uint_t new_x86_feature;
12540Sstevel@tonic-gate 
12552985Sdmick 	/*
12563021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
12573021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
12583021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
12593021Sdmick 	 * interrupts, cmn_err, etc.
12603021Sdmick 	 */
12613021Sdmick 
12623021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
12633021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
12643021Sdmick 
12653021Sdmick 	if (tsc_gethrtime_enable)
12663021Sdmick 		tsc_sync_slave();
12673021Sdmick 
12683021Sdmick 	/*
12692985Sdmick 	 * Once this was done from assembly, but it's safer here; if
12702985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
12712985Sdmick 	 * since we get here by calling t_pc, we need to do that call
12722985Sdmick 	 * before swtch() overwrites it.
12732985Sdmick 	 */
12742985Sdmick 
12752985Sdmick 	(void) (*ap_mlsetup)();
12762985Sdmick 
12770Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	/*
12800Sstevel@tonic-gate 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
12810Sstevel@tonic-gate 	 * this with interrupts disabled.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (x86_feature & X86_MTRR)
12840Sstevel@tonic-gate 		mtrr_sync();
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/*
12873446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
12883446Smrj 	 * for the rdtscp instruction.
12893446Smrj 	 */
12903446Smrj 	if (x86_feature & X86_TSCP)
12913446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
12923446Smrj 
12933446Smrj 	/*
12940Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
12950Sstevel@tonic-gate 	 */
12960Sstevel@tonic-gate 	init_cpu_syscall(cp);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	/*
12990Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
13000Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
13010Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
13020Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
13030Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
13040Sstevel@tonic-gate 	 * calls before CPU_READY is set.
13050Sstevel@tonic-gate 	 */
13063446Smrj 	splx(ipltospl(LOCK_LEVEL));
13073446Smrj 	sti();
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	/*
13100Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
13110Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
13120Sstevel@tonic-gate 	 *
13130Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
13140Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
13150Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
13160Sstevel@tonic-gate 	 * gets large enough.
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
13190Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
13200Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
13210Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13254481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
13264481Sbholler 	 * boot cpu supports monitor/mwait.
13274481Sbholler 	 */
13284481Sbholler 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
13294481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
13304481Sbholler 
13314481Sbholler 	/*
13320Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
13330Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
13340Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
13350Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
13380Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	cpuid_pass2(cp);
13410Sstevel@tonic-gate 	cpuid_pass3(cp);
13420Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	init_cpu_info(cp);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
13470Sstevel@tonic-gate 	/*
13483434Sesaxe 	 * Processor group initialization for this CPU is dependent on the
13493434Sesaxe 	 * cpuid probing, which must be done in the context of the current
13503434Sesaxe 	 * CPU.
13510Sstevel@tonic-gate 	 */
13523434Sesaxe 	pghw_physid_create(cp);
13533434Sesaxe 	pg_cpu_init(cp);
13543434Sesaxe 	pg_cmt_cpu_startup(cp);
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
13570Sstevel@tonic-gate 	cpu_add_active(cp);
13582575Snf202958 
13592575Snf202958 	if (dtrace_cpu_init != NULL) {
13602575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
13612575Snf202958 	}
13622575Snf202958 
13634581Ssherrym 	/*
13644581Ssherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
13654581Ssherrym 	 */
13664581Ssherrym 	ucode_check(cp);
13674581Ssherrym 
13680Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
13690Sstevel@tonic-gate 
13703029Ssethg 	/*
13713029Ssethg 	 * Enable preemption here so that contention for any locks acquired
13723029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
13733029Ssethg 	 * locks is continously executing on other CPUs (for example, this
13743029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
13753029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
13763029Ssethg 	 * CPU state is pretty-much fully constructed.
13773029Ssethg 	 */
13783029Ssethg 	curthread->t_preempt = 0;
13793029Ssethg 
13801251Skchow 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
13811251Skchow 
13821482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
13831482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
13841482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
13851482Ssethg 
13860Sstevel@tonic-gate 	(void) spl0();				/* enable interrupts */
13870Sstevel@tonic-gate 
13881414Scindi 	/*
13891414Scindi 	 * Set up the CPU module for this CPU.  This can't be done before
13901414Scindi 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
13911414Scindi 	 * need to go load another CPU module.  The act of attempting to load
13921414Scindi 	 * a module may trigger a cross-call, which will ASSERT unless this
13931414Scindi 	 * cpu is CPU_READY.
13941414Scindi 	 */
13951414Scindi 	cmi_init();
13961414Scindi 
13971414Scindi 	if (x86_feature & X86_MCA)
13981414Scindi 		cmi_mca_init();
13991414Scindi 
14000Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
14013446Smrj 		kdi_cpu_init();
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/*
14040Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
14050Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
14060Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
14070Sstevel@tonic-gate 	 */
14080Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	/*
14110Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
14120Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
14130Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
14140Sstevel@tonic-gate 	 */
14150Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
14160Sstevel@tonic-gate 	    cp->cpu_id);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	/*
14190Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
14200Sstevel@tonic-gate 	 */
14210Sstevel@tonic-gate 	thread_exit();
14220Sstevel@tonic-gate 	panic("mp_startup: cannot return");
14230Sstevel@tonic-gate 	/*NOTREACHED*/
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate /*
14280Sstevel@tonic-gate  * Start CPU on user request.
14290Sstevel@tonic-gate  */
14300Sstevel@tonic-gate /* ARGSUSED */
14310Sstevel@tonic-gate int
14320Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14350Sstevel@tonic-gate 	return (0);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate  * Stop CPU on user request.
14400Sstevel@tonic-gate  */
14410Sstevel@tonic-gate /* ARGSUSED */
14420Sstevel@tonic-gate int
14430Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
14440Sstevel@tonic-gate {
14451389Sdmick 	extern int cbe_psm_timer_mode;
14460Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14471389Sdmick 
14481389Sdmick 	/*
14491389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
14501389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
14511389Sdmick 	 */
14521389Sdmick 
14531389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
14541389Sdmick 		return (1);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	return (0);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
14610Sstevel@tonic-gate  */
14620Sstevel@tonic-gate int
14630Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
14660Sstevel@tonic-gate 		return (EBUSY);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
14690Sstevel@tonic-gate 	return (0);
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate /*
14730Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
14740Sstevel@tonic-gate  */
14750Sstevel@tonic-gate void
14760Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14790Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
14800Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate void
14860Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
14871414Scindi {
14881414Scindi 	cmi_faulted_enter(cp);
14891414Scindi }
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate void
14920Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
14931414Scindi {
14941414Scindi 	cmi_faulted_exit(cp);
14951414Scindi }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate /*
14980Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
14990Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
15000Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
15010Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
15020Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
15030Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
15040Sstevel@tonic-gate  * syscall feature flags.
15050Sstevel@tonic-gate  */
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate /*ARGSUSED*/
15080Sstevel@tonic-gate void
15090Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
15100Sstevel@tonic-gate {
15113446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
15120Sstevel@tonic-gate 		cpu_sep_disable();
15133446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
15140Sstevel@tonic-gate 		cpu_asysc_disable();
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate /*ARGSUSED*/
15180Sstevel@tonic-gate void
15190Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
15200Sstevel@tonic-gate {
15213446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
15220Sstevel@tonic-gate 		cpu_sep_enable();
15233446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
15240Sstevel@tonic-gate 		cpu_asysc_enable();
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate static void
15280Sstevel@tonic-gate cpu_sep_enable(void)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
15310Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15320Sstevel@tonic-gate 
1533770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
15340Sstevel@tonic-gate }
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate static void
15370Sstevel@tonic-gate cpu_sep_disable(void)
15380Sstevel@tonic-gate {
15390Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
15400Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
15440Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
15450Sstevel@tonic-gate 	 */
15463446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate static void
15500Sstevel@tonic-gate cpu_asysc_enable(void)
15510Sstevel@tonic-gate {
15520Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
15530Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15540Sstevel@tonic-gate 
1555770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1556770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate static void
15600Sstevel@tonic-gate cpu_asysc_disable(void)
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
15630Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/*
15660Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
15670Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
15680Sstevel@tonic-gate 	 */
1569770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1570770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
15710Sstevel@tonic-gate }
1572