xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 3446:5903aece022d)
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  */
21*3446Smrj 
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>
47*3446Smrj #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>
63*3446Smrj #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>
67*3446Smrj #include <sys/mach_mmu.h>
68*3446Smrj #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 /*
76*3446Smrj  * 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 /*
81*3446Smrj  * to be set by a PSM to indicate what cpus
82*3446Smrj  * are sitting around on the system.
832006Sandrei  */
84*3446Smrj 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  */
92*3446Smrj int flushes_require_xcalls;
93*3446Smrj 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 
1180Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1190Sstevel@tonic-gate 	if (fpu_exists)
1200Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1250Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1300Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1310Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Configure syscall support on this CPU.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate /*ARGSUSED*/
1400Sstevel@tonic-gate static void
1410Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	kpreempt_disable();
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #if defined(__amd64)
146*3446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate #if !defined(__lint)
1490Sstevel@tonic-gate 		/*
1500Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1510Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1520Sstevel@tonic-gate 		 * here.
1530Sstevel@tonic-gate 		 */
1540Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1550Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1560Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate 		/*
1590Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1600Sstevel@tonic-gate 		 */
1610Sstevel@tonic-gate 		cpu_asysc_enable();
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		/*
1640Sstevel@tonic-gate 		 * Program the magic registers ..
1650Sstevel@tonic-gate 		 */
166*3446Smrj 		wrmsr(MSR_AMD_STAR,
167*3446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
168770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
169770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		/*
1720Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1730Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1740Sstevel@tonic-gate 		 */
175770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate #endif
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1810Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1820Sstevel@tonic-gate 	 *
1830Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1840Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1850Sstevel@tonic-gate 	 */
186*3446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate #if !defined(__lint)
1890Sstevel@tonic-gate 		/*
1900Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1910Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1920Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
1930Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
1940Sstevel@tonic-gate 		 * Instruction Set Reference"
1950Sstevel@tonic-gate 		 */
1960Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
1990Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2020Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		cpu_sep_enable();
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2090Sstevel@tonic-gate 		 * via a context handler.
2100Sstevel@tonic-gate 		 */
211*3446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
212770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	kpreempt_enable();
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Multiprocessor initialization.
2200Sstevel@tonic-gate  *
2210Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2220Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2230Sstevel@tonic-gate  */
224*3446Smrj struct cpu *
2250Sstevel@tonic-gate mp_startup_init(int cpun)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	struct cpu *cp;
2280Sstevel@tonic-gate 	kthread_id_t tp;
2290Sstevel@tonic-gate 	caddr_t	sp;
2300Sstevel@tonic-gate 	proc_t *procp;
2310Sstevel@tonic-gate 	extern void idle();
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate #ifdef TRAPTRACE
2340Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2350Sstevel@tonic-gate #endif
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2380Sstevel@tonic-gate 
239*3446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2400Sstevel@tonic-gate 	procp = curthread->t_procp;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2430Sstevel@tonic-gate 	/*
2440Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	disp_cpu_init(cp);
2470Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2480Sstevel@tonic-gate 
249414Skchow 	cpu_vm_data_init(cp);
250414Skchow 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2530Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2540Sstevel@tonic-gate 	 * when the CPU starts running.
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2570Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2610Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2620Sstevel@tonic-gate 	 *
2630Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2640Sstevel@tonic-gate 	 * thread_create().
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2670Sstevel@tonic-gate 	tp->t_preempt = 1;
2680Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2690Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2700Sstevel@tonic-gate 	tp->t_cpu = cp;
2710Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	sp = tp->t_stk;
2770Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2780Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
279*3446Smrj #if defined(__amd64)
280*3446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
281*3446Smrj #endif
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	cp->cpu_id = cpun;
2840Sstevel@tonic-gate 	cp->cpu_self = cp;
2850Sstevel@tonic-gate 	cp->cpu_thread = tp;
2860Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
2870Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
2880Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2911482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
2921482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
2931482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
2941482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
2951482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
2961482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
2971482Ssethg 	 * proper value.
2981482Ssethg 	 */
2991482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3001482Ssethg 
3011482Ssethg 	/*
3020Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	tp->t_preempt = 1;
3090Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3100Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3110Sstevel@tonic-gate 	tp->t_cpu = cp;
3120Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3153434Sesaxe 	 * Bootstrap the CPU's PG data
31660Sesaxe 	 */
3173434Sesaxe 	pg_cpu_bootstrap(cp);
31860Sesaxe 
31960Sesaxe 	/*
320*3446Smrj 	 * Perform CPC initialization on the new CPU.
3210Sstevel@tonic-gate 	 */
3220Sstevel@tonic-gate 	kcpc_hw_init(cp);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3260Sstevel@tonic-gate 	 * for each CPU.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
331*3446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3320Sstevel@tonic-gate 	 */
333*3446Smrj #if !defined(__lint)
334*3446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
335*3446Smrj #endif
336*3446Smrj 	cp->cpu_m.mcpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
337*3446Smrj 	bcopy(CPU->cpu_m.mcpu_gdt, cp->cpu_m.mcpu_gdt,
338*3446Smrj 	    (sizeof (*cp->cpu_m.mcpu_gdt) * NGDT));
3391626Srab 
340*3446Smrj #if defined(__i386)
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * setup kernel %gs.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3450Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
346*3446Smrj #endif
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3500Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3510Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3520Sstevel@tonic-gate 	 * cmpxchgl register bug
3530Sstevel@tonic-gate 	 */
3540Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
355*3446Smrj 		struct machcpu *mcpu = &cp->cpu_m;
356*3446Smrj 
357*3446Smrj 		mcpu->mcpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
358*3446Smrj 		bcopy(idt0, mcpu->mcpu_idt, sizeof (idt0));
359*3446Smrj 	} else {
360*3446Smrj 		cp->cpu_m.mcpu_idt = CPU->cpu_m.mcpu_idt;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
364*3446Smrj 	 * Get interrupt priority data from cpu 0.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3670Sstevel@tonic-gate 
368*3446Smrj 	/*
369*3446Smrj 	 * alloc space for cpuid info
370*3446Smrj 	 */
371*3446Smrj 	cpuid_alloc_space(cp);
372*3446Smrj 
3730Sstevel@tonic-gate 	hat_cpu_online(cp);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate #ifdef TRAPTRACE
3760Sstevel@tonic-gate 	/*
377*3446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
3800Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
3810Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
3820Sstevel@tonic-gate #endif
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * Record that we have another CPU.
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
3890Sstevel@tonic-gate 	 */
3901455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
3930Sstevel@tonic-gate 	 * after mp_startup().
3940Sstevel@tonic-gate 	 */
3950Sstevel@tonic-gate 	cpu_add_unit(cp);
3960Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
397*3446Smrj 
398*3446Smrj 	return (cp);
399*3446Smrj }
400*3446Smrj 
401*3446Smrj /*
402*3446Smrj  * Undo what was done in mp_startup_init
403*3446Smrj  */
404*3446Smrj static void
405*3446Smrj mp_startup_fini(struct cpu *cp, int error)
406*3446Smrj {
407*3446Smrj 	mutex_enter(&cpu_lock);
408*3446Smrj 
409*3446Smrj 	/*
410*3446Smrj 	 * Remove the CPU from the list of available CPUs.
411*3446Smrj 	 */
412*3446Smrj 	cpu_del_unit(cp->cpu_id);
413*3446Smrj 
414*3446Smrj 	if (error == ETIMEDOUT) {
415*3446Smrj 		/*
416*3446Smrj 		 * The cpu was started, but never *seemed* to run any
417*3446Smrj 		 * code in the kernel; it's probably off spinning in its
418*3446Smrj 		 * own private world, though with potential references to
419*3446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
420*3446Smrj 		 *
421*3446Smrj 		 * Worse still, it may actually wake up some time later,
422*3446Smrj 		 * so rather than guess what it might or might not do, we
423*3446Smrj 		 * leave the fundamental data structures intact.
424*3446Smrj 		 */
425*3446Smrj 		cp->cpu_flags = 0;
426*3446Smrj 		mutex_exit(&cpu_lock);
427*3446Smrj 		return;
428*3446Smrj 	}
429*3446Smrj 
430*3446Smrj 	/*
431*3446Smrj 	 * At this point, the only threads bound to this CPU should
432*3446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
433*3446Smrj 	 * and it's interrupt threads.  Clean these up.
434*3446Smrj 	 */
435*3446Smrj 	cpu_destroy_bound_threads(cp);
436*3446Smrj 	cp->cpu_idle_thread = NULL;
437*3446Smrj 
438*3446Smrj 	/*
439*3446Smrj 	 * Free the interrupt stack.
440*3446Smrj 	 */
441*3446Smrj 	segkp_release(segkp,
442*3446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
443*3446Smrj 
444*3446Smrj 	mutex_exit(&cpu_lock);
445*3446Smrj 
446*3446Smrj #ifdef TRAPTRACE
447*3446Smrj 	/*
448*3446Smrj 	 * Discard the trap trace buffer
449*3446Smrj 	 */
450*3446Smrj 	{
451*3446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
452*3446Smrj 
453*3446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
454*3446Smrj 		ttc->ttc_first = NULL;
455*3446Smrj 	}
456*3446Smrj #endif
457*3446Smrj 
458*3446Smrj 	hat_cpu_offline(cp);
459*3446Smrj 
460*3446Smrj 	cpuid_free_space(cp);
461*3446Smrj 
462*3446Smrj 	if (cp->cpu_m.mcpu_idt != CPU->cpu_m.mcpu_idt)
463*3446Smrj 		kmem_free(cp->cpu_m.mcpu_idt, sizeof (idt0));
464*3446Smrj 	cp->cpu_m.mcpu_idt = NULL;
465*3446Smrj 
466*3446Smrj 	kmem_free(cp->cpu_m.mcpu_gdt, PAGESIZE);
467*3446Smrj 	cp->cpu_m.mcpu_gdt = NULL;
468*3446Smrj 
469*3446Smrj 	teardown_vaddr_for_ppcopy(cp);
470*3446Smrj 
471*3446Smrj 	kcpc_hw_fini(cp);
472*3446Smrj 
473*3446Smrj 	cp->cpu_dispthread = NULL;
474*3446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
475*3446Smrj 
476*3446Smrj 	cpu_vm_data_destroy(cp);
477*3446Smrj 
478*3446Smrj 	mutex_enter(&cpu_lock);
479*3446Smrj 	disp_cpu_fini(cp);
480*3446Smrj 	mutex_exit(&cpu_lock);
481*3446Smrj 
482*3446Smrj 	kmem_free(cp, sizeof (*cp));
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
4870Sstevel@tonic-gate  *
4880Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
4890Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
4900Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
4910Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
4920Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
4930Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
4940Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
4950Sstevel@tonic-gate  * system.
4960Sstevel@tonic-gate  *
497938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
498938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
499938Sesaxe  * to acknowledging their readiness to the master, so this routine will
500938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
501938Sesaxe  * global data safe.
502938Sesaxe  *
503359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
504359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate 
507*3446Smrj #if defined(OPTERON_ERRATUM_88)
508*3446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
509*3446Smrj #endif
510*3446Smrj 
5110Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5120Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5130Sstevel@tonic-gate #endif
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5160Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5170Sstevel@tonic-gate #endif
5180Sstevel@tonic-gate 
519*3446Smrj #if defined(OPTERON_ERRATUM_95)
520*3446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
521*3446Smrj #endif
522*3446Smrj 
5230Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5240Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate 
527*3446Smrj #if defined(OPTERON_ERRATUM_108)
528*3446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
529*3446Smrj #endif
530*3446Smrj 
5310Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5320Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5330Sstevel@tonic-gate #endif
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5360Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5370Sstevel@tonic-gate #endif
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5400Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5410Sstevel@tonic-gate #endif
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5440Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5450Sstevel@tonic-gate #endif
5460Sstevel@tonic-gate 
547359Skucharsk #if defined(OPTERON_ERRATUM_131)
548359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
549359Skucharsk #endif
5500Sstevel@tonic-gate 
551938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
552938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
553938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
554938Sesaxe #endif
555938Sesaxe 
5561582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5571582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5581582Skchow #endif
5591582Skchow 
560*3446Smrj static void
561*3446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
562*3446Smrj {
563*3446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
564*3446Smrj 	    cp->cpu_id, erratum);
565*3446Smrj }
566*3446Smrj 
567*3446Smrj static void
568*3446Smrj workaround_applied(uint_t erratum)
569*3446Smrj {
570*3446Smrj 	if (erratum > 1000000)
571*3446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
572*3446Smrj 		    erratum);
573*3446Smrj 	else
574*3446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
575*3446Smrj 		    erratum);
576*3446Smrj }
577*3446Smrj 
578*3446Smrj static void
579*3446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
580*3446Smrj {
581*3446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
582*3446Smrj 	    cp->cpu_id, rw, msr, error);
583*3446Smrj }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate uint_t
5860Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	uint_t missing = 0;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	ASSERT(cpu == CPU);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*LINTED*/
5930Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
5960Sstevel@tonic-gate 		 */
5970Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
6000Sstevel@tonic-gate 		 */
601*3446Smrj 		opteron_erratum_88++;
6020Sstevel@tonic-gate #else
603*3446Smrj 		workaround_warning(cpu, 88);
6040Sstevel@tonic-gate 		missing++;
6050Sstevel@tonic-gate #endif
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
6110Sstevel@tonic-gate 		 */
6120Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6130Sstevel@tonic-gate 		/*
6140Sstevel@tonic-gate 		 * fix is in trap.c
6150Sstevel@tonic-gate 		 */
6160Sstevel@tonic-gate 		opteron_erratum_91++;
6170Sstevel@tonic-gate #else
618*3446Smrj 		workaround_warning(cpu, 91);
6190Sstevel@tonic-gate 		missing++;
6200Sstevel@tonic-gate #endif
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
6240Sstevel@tonic-gate 		/*
6250Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
6260Sstevel@tonic-gate 		 */
6270Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6280Sstevel@tonic-gate 		/*
6290Sstevel@tonic-gate 		 * fix is in trap.c
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		opteron_erratum_93++;
6320Sstevel@tonic-gate #else
633*3446Smrj 		workaround_warning(cpu, 93);
6340Sstevel@tonic-gate 		missing++;
6350Sstevel@tonic-gate #endif
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*LINTED*/
6390Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
6420Sstevel@tonic-gate 		 */
6430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
6440Sstevel@tonic-gate #if defined(_LP64)
6450Sstevel@tonic-gate 		/*
6460Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
6470Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
6480Sstevel@tonic-gate 		 * range mod 4G.
6490Sstevel@tonic-gate 		 */
6500Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
6510Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		/*LINTED*/
6540Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
655*3446Smrj 		opteron_erratum_95++;
6560Sstevel@tonic-gate #endif	/* _LP64 */
6570Sstevel@tonic-gate #else
658*3446Smrj 		workaround_warning(cpu, 95);
6590Sstevel@tonic-gate 		missing++;
660*3446Smrj #endif
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
6640Sstevel@tonic-gate 		/*
6650Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
6660Sstevel@tonic-gate 		 */
6670Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6680Sstevel@tonic-gate 		/*
6690Sstevel@tonic-gate 		 * fix is in trap.c
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate 		opteron_erratum_100++;
6720Sstevel@tonic-gate #else
673*3446Smrj 		workaround_warning(cpu, 100);
6740Sstevel@tonic-gate 		missing++;
6750Sstevel@tonic-gate #endif
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*LINTED*/
6790Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
6800Sstevel@tonic-gate 		/*
6810Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
6820Sstevel@tonic-gate 		 * Some Processors
6830Sstevel@tonic-gate 		 */
6840Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
6870Sstevel@tonic-gate 		 * those processors)
6880Sstevel@tonic-gate 		 */
6890Sstevel@tonic-gate #else
690*3446Smrj 		workaround_warning(cpu, 108);
6910Sstevel@tonic-gate 		missing++;
6920Sstevel@tonic-gate #endif
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*LINTED*/
696*3446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
701*3446Smrj 		/*
702*3446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
703*3446Smrj 		 */
704*3446Smrj 		uint64_t value;
705*3446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
706*3446Smrj 		int err;
7070Sstevel@tonic-gate 
708*3446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
709*3446Smrj 			msr_warning(cpu, "rd", msr, err);
710*3446Smrj 			workaround_warning(cpu, 109);
711*3446Smrj 			missing++;
712*3446Smrj 		}
713*3446Smrj 		if (value == 0)
7140Sstevel@tonic-gate 			opteron_erratum_109++;
7150Sstevel@tonic-gate #else
716*3446Smrj 		workaround_warning(cpu, 109);
7170Sstevel@tonic-gate 		missing++;
7180Sstevel@tonic-gate #endif
719*3446Smrj 	/*CONSTANTCONDITION*/
720*3446Smrj 	} while (0);
721*3446Smrj 
7220Sstevel@tonic-gate 	/*LINTED*/
7230Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
7240Sstevel@tonic-gate 		/*
7250Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
7260Sstevel@tonic-gate 		 * Processor Hang
7270Sstevel@tonic-gate 		 */
7280Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
729*3446Smrj #if defined(_LP64)
7300Sstevel@tonic-gate 		/*
7310Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
7320Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
7330Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
7340Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
7350Sstevel@tonic-gate 		 */
736*3446Smrj 		if (opteron_erratum_121)
737*3446Smrj 			opteron_erratum_121++;
738*3446Smrj 		else {
739*3446Smrj 			if (hole_start) {
740*3446Smrj 				hole_start -= PAGESIZE;
741*3446Smrj 			} else {
742*3446Smrj 				/*
743*3446Smrj 				 * hole_start not yet initialized by
744*3446Smrj 				 * mmu_init. Initialize hole_start
745*3446Smrj 				 * with value to be subtracted.
746*3446Smrj 				 */
747*3446Smrj 				hole_start = PAGESIZE;
7480Sstevel@tonic-gate 			}
749*3446Smrj 			opteron_erratum_121++;
7500Sstevel@tonic-gate 		}
751*3446Smrj #endif	/* _LP64 */
7520Sstevel@tonic-gate #else
753*3446Smrj 		workaround_warning(cpu, 121);
7540Sstevel@tonic-gate 		missing++;
7550Sstevel@tonic-gate #endif
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*LINTED*/
759*3446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
7600Sstevel@tonic-gate 		/*
761*3446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
7620Sstevel@tonic-gate 		 * Multiprocessor Systems
7630Sstevel@tonic-gate 		 */
7640Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
765*3446Smrj 		uint64_t value;
766*3446Smrj 		const uint_t msr = MSR_AMD_HWCR;
767*3446Smrj 		int error;
768*3446Smrj 
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
7710Sstevel@tonic-gate 		 * or multi-processor).
7720Sstevel@tonic-gate 		 */
773*3446Smrj 		if (!opteron_erratum_122 && lgrp_plat_node_cnt == 1 &&
774*3446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
775*3446Smrj 			break;
7760Sstevel@tonic-gate 
777*3446Smrj 		/* disable TLB Flush Filter */
778*3446Smrj 
779*3446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
780*3446Smrj 			msr_warning(cpu, "rd", msr, error);
781*3446Smrj 			workaround_warning(cpu, 122);
782*3446Smrj 			missing++;
783*3446Smrj 		} else {
784*3446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
785*3446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
786*3446Smrj 				msr_warning(cpu, "wr", msr, error);
787*3446Smrj 				workaround_warning(cpu, 122);
788*3446Smrj 				missing++;
789*3446Smrj 			}
7900Sstevel@tonic-gate 		}
791*3446Smrj 		opteron_erratum_122++;
7920Sstevel@tonic-gate #else
793*3446Smrj 		workaround_warning(cpu, 122);
7940Sstevel@tonic-gate 		missing++;
7950Sstevel@tonic-gate #endif
796*3446Smrj 	/*CONSTANTCONDITION*/
797*3446Smrj 	} while (0);
798302Skchow 
7990Sstevel@tonic-gate 	/*LINTED*/
800*3446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
8010Sstevel@tonic-gate 		/*
8020Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
8030Sstevel@tonic-gate 		 * Dual Core Processors
8040Sstevel@tonic-gate 		 */
805*3446Smrj #if defined(OPTERON_ERRATUM_123)
806*3446Smrj 		uint64_t value;
807*3446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
808*3446Smrj 		int err;
809*3446Smrj 
8100Sstevel@tonic-gate 		/*
8110Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
8120Sstevel@tonic-gate 		 */
813*3446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
814*3446Smrj 			break;
8150Sstevel@tonic-gate 
816*3446Smrj 		/*
817*3446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
818*3446Smrj 		 */
819*3446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
820*3446Smrj 			msr_warning(cpu, "rd", msr, err);
821*3446Smrj 			workaround_warning(cpu, 123);
822*3446Smrj 			missing++;
8230Sstevel@tonic-gate 		}
824*3446Smrj 		if (value == 0)
825*3446Smrj 			opteron_erratum_123++;
826*3446Smrj #else
827*3446Smrj 		workaround_warning(cpu, 123);
828*3446Smrj 		missing++;
829359Skucharsk 
830*3446Smrj #endif
831*3446Smrj 	/*CONSTANTCONDITION*/
832*3446Smrj 	} while (0);
833*3446Smrj 
834359Skucharsk 	/*LINTED*/
835*3446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
836359Skucharsk 		/*
837359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
838359Skucharsk 		 * Waiting for a Probe Response
839359Skucharsk 		 */
840*3446Smrj #if defined(OPTERON_ERRATUM_131)
841*3446Smrj 		uint64_t nbcfg;
842*3446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
843*3446Smrj 		const uint64_t wabits =
844*3446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
845*3446Smrj 		int error;
846*3446Smrj 
847359Skucharsk 		/*
848359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
849359Skucharsk 		 */
850*3446Smrj 		if (opteron_erratum_131)
851*3446Smrj 			break;
8522519Skchow 
853*3446Smrj 		if (lgrp_plat_node_cnt * cpuid_get_ncpu_per_chip(cpu) < 4)
854*3446Smrj 			break;
8552519Skchow 
856*3446Smrj 		/*
857*3446Smrj 		 * Print a warning if neither of the workarounds for
858*3446Smrj 		 * erratum 131 is present.
859*3446Smrj 		 */
860*3446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
861*3446Smrj 			msr_warning(cpu, "rd", msr, error);
862*3446Smrj 			workaround_warning(cpu, 131);
863*3446Smrj 			missing++;
864*3446Smrj 		} else if ((nbcfg & wabits) == 0) {
865*3446Smrj 			opteron_erratum_131++;
866*3446Smrj 		} else {
867*3446Smrj 			/* cannot have both workarounds set */
868*3446Smrj 			ASSERT((nbcfg & wabits) != wabits);
869359Skucharsk 		}
870*3446Smrj #else
871*3446Smrj 		workaround_warning(cpu, 131);
872*3446Smrj 		missing++;
873359Skucharsk #endif
874*3446Smrj 	/*CONSTANTCONDITION*/
875*3446Smrj 	} while (0);
876938Sesaxe 
877938Sesaxe 	/*
878*3446Smrj 	 * This isn't really an erratum, but for convenience the
879938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
880938Sesaxe 	 */
881938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
882*3446Smrj #if defined(OPTERON_WORKAROUND_6336786)
883938Sesaxe 		/*
884938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
885938Sesaxe 		 * K8 platforms to guard against TSC drift.
886938Sesaxe 		 */
887938Sesaxe 		if (opteron_workaround_6336786) {
888938Sesaxe 			opteron_workaround_6336786++;
889938Sesaxe 		} else if ((lgrp_plat_node_cnt *
890*3446Smrj 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
891938Sesaxe 		    opteron_workaround_6336786_UP) {
892*3446Smrj 			int	node;
893*3446Smrj 			uint8_t data;
894*3446Smrj 
895938Sesaxe 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
896938Sesaxe 				/*
897938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
898938Sesaxe 				 * Northbridge device is the node id + 24.
899938Sesaxe 				 */
900938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
901938Sesaxe 				data &= 0xFC;
902938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
903938Sesaxe 			}
904938Sesaxe 			opteron_workaround_6336786++;
905938Sesaxe 		}
906*3446Smrj #else
907*3446Smrj 		workaround_warning(cpu, 6336786);
908*3446Smrj 		missing++;
909938Sesaxe #endif
910*3446Smrj 	}
9111582Skchow 
9121582Skchow 	/*LINTED*/
9131582Skchow 	/*
9141582Skchow 	 * Mutex primitives don't work as expected.
9151582Skchow 	 */
9161582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
917*3446Smrj #if defined(OPTERON_WORKAROUND_6323525)
9181582Skchow 		/*
919*3446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
9201582Skchow 		 * MSR_BU_CFG set, then not applicable. The workaround
9211582Skchow 		 * is to patch the semaphone routines with the lfence
9221582Skchow 		 * instruction to provide necessary load memory barrier with
9231582Skchow 		 * possible subsequent read-modify-write ops.
9241582Skchow 		 *
9251582Skchow 		 * It is too early in boot to call the patch routine so
9261582Skchow 		 * set erratum variable to be done in startup_end().
9271582Skchow 		 */
9281582Skchow 		if (opteron_workaround_6323525) {
9291582Skchow 			opteron_workaround_6323525++;
9301582Skchow 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
931*3446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
9321582Skchow 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
9331582Skchow 				opteron_workaround_6323525++;
9341582Skchow 		}
935*3446Smrj #else
936*3446Smrj 		workaround_warning(cpu, 6323525);
937*3446Smrj 		missing++;
938*3446Smrj #endif
9391582Skchow 	}
940*3446Smrj 
9410Sstevel@tonic-gate 	return (missing);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate void
9450Sstevel@tonic-gate workaround_errata_end()
9460Sstevel@tonic-gate {
947*3446Smrj #if defined(OPTERON_ERRATUM_88)
948*3446Smrj 	if (opteron_erratum_88)
949*3446Smrj 		workaround_applied(88);
950*3446Smrj #endif
951*3446Smrj #if defined(OPTERON_ERRATUM_91)
952*3446Smrj 	if (opteron_erratum_91)
953*3446Smrj 		workaround_applied(91);
954*3446Smrj #endif
955*3446Smrj #if defined(OPTERON_ERRATUM_93)
956*3446Smrj 	if (opteron_erratum_93)
957*3446Smrj 		workaround_applied(93);
958*3446Smrj #endif
959*3446Smrj #if defined(OPTERON_ERRATUM_95)
960*3446Smrj 	if (opteron_erratum_95)
961*3446Smrj 		workaround_applied(95);
962*3446Smrj #endif
963*3446Smrj #if defined(OPTERON_ERRATUM_100)
964*3446Smrj 	if (opteron_erratum_100)
965*3446Smrj 		workaround_applied(100);
966*3446Smrj #endif
967*3446Smrj #if defined(OPTERON_ERRATUM_108)
968*3446Smrj 	if (opteron_erratum_108)
969*3446Smrj 		workaround_applied(108);
970*3446Smrj #endif
9710Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
9720Sstevel@tonic-gate 	if (opteron_erratum_109) {
973359Skucharsk 		cmn_err(CE_WARN,
974359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
975359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
976359Skucharsk 		    " system's BIOS to a version\ncontaining this"
977359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
978359Skucharsk 		    " system\noperation may occur.\n");
9790Sstevel@tonic-gate 	}
980*3446Smrj #endif
981*3446Smrj #if defined(OPTERON_ERRATUM_121)
982*3446Smrj 	if (opteron_erratum_121)
983*3446Smrj 		workaround_applied(121);
984*3446Smrj #endif
985*3446Smrj #if defined(OPTERON_ERRATUM_122)
986*3446Smrj 	if (opteron_erratum_122)
987*3446Smrj 		workaround_applied(122);
988*3446Smrj #endif
9890Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
9900Sstevel@tonic-gate 	if (opteron_erratum_123) {
991359Skucharsk 		cmn_err(CE_WARN,
992359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
993359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
994359Skucharsk 		    " system's BIOS to a version\ncontaining this"
995359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
996359Skucharsk 		    " system\noperation may occur.\n");
9970Sstevel@tonic-gate 	}
998*3446Smrj #endif
999359Skucharsk #if defined(OPTERON_ERRATUM_131)
1000359Skucharsk 	if (opteron_erratum_131) {
1001359Skucharsk 		cmn_err(CE_WARN,
1002359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1003359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1004359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1005359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1006359Skucharsk 		    " system\noperation may occur.\n");
1007359Skucharsk 	}
1008*3446Smrj #endif
1009*3446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1010*3446Smrj 	if (opteron_workaround_6336786)
1011*3446Smrj 		workaround_applied(6336786);
1012*3446Smrj #endif
1013*3446Smrj #if defined(OPTERON_WORKAROUND_6323525)
1014*3446Smrj 	if (opteron_workaround_6323525)
1015*3446Smrj 		workaround_applied(6323525);
1016*3446Smrj #endif
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
1019*3446Smrj static cpuset_t procset;
1020*3446Smrj 
1021*3446Smrj /*
1022*3446Smrj  * Start a single cpu, assuming that the kernel context is available
1023*3446Smrj  * to successfully start another cpu.
1024*3446Smrj  *
1025*3446Smrj  * (For example, real mode code is mapped into the right place
1026*3446Smrj  * in memory and is ready to be run.)
1027*3446Smrj  */
1028*3446Smrj int
1029*3446Smrj start_cpu(processorid_t who)
1030*3446Smrj {
1031*3446Smrj 	void *ctx;
1032*3446Smrj 	cpu_t *cp;
1033*3446Smrj 	int delays;
1034*3446Smrj 	int error = 0;
1035*3446Smrj 
1036*3446Smrj 	ASSERT(who != 0);
1037*3446Smrj 
1038*3446Smrj 	/*
1039*3446Smrj 	 * Check if there's at least a Mbyte of kmem available
1040*3446Smrj 	 * before attempting to start the cpu.
1041*3446Smrj 	 */
1042*3446Smrj 	if (kmem_avail() < 1024 * 1024) {
1043*3446Smrj 		/*
1044*3446Smrj 		 * Kick off a reap in case that helps us with
1045*3446Smrj 		 * later attempts ..
1046*3446Smrj 		 */
1047*3446Smrj 		kmem_reap();
1048*3446Smrj 		return (ENOMEM);
1049*3446Smrj 	}
1050*3446Smrj 
1051*3446Smrj 	cp = mp_startup_init(who);
1052*3446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
1053*3446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
1054*3446Smrj 
1055*3446Smrj 		/*
1056*3446Smrj 		 * Something went wrong before we even started it
1057*3446Smrj 		 */
1058*3446Smrj 		if (ctx)
1059*3446Smrj 			cmn_err(CE_WARN,
1060*3446Smrj 			    "cpu%d: failed to start error %d",
1061*3446Smrj 			    cp->cpu_id, error);
1062*3446Smrj 		else
1063*3446Smrj 			cmn_err(CE_WARN,
1064*3446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
10650Sstevel@tonic-gate 
1066*3446Smrj 		if (ctx)
1067*3446Smrj 			mach_cpucontext_free(cp, ctx, error);
1068*3446Smrj 		else
1069*3446Smrj 			error = EAGAIN;		/* hmm. */
1070*3446Smrj 		mp_startup_fini(cp, error);
1071*3446Smrj 		return (error);
1072*3446Smrj 	}
1073*3446Smrj 
1074*3446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
1075*3446Smrj 		if (delays == 500) {
1076*3446Smrj 			/*
1077*3446Smrj 			 * After five seconds, things are probably looking
1078*3446Smrj 			 * a bit bleak - explain the hang.
1079*3446Smrj 			 */
1080*3446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
1081*3446Smrj 			    "but not running in the kernel yet", who);
1082*3446Smrj 		} else if (delays > 2000) {
1083*3446Smrj 			/*
1084*3446Smrj 			 * We waited at least 20 seconds, bail ..
1085*3446Smrj 			 */
1086*3446Smrj 			error = ETIMEDOUT;
1087*3446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
1088*3446Smrj 			mach_cpucontext_free(cp, ctx, error);
1089*3446Smrj 			mp_startup_fini(cp, error);
1090*3446Smrj 			return (error);
1091*3446Smrj 		}
1092*3446Smrj 
1093*3446Smrj 		/*
1094*3446Smrj 		 * wait at least 10ms, then check again..
1095*3446Smrj 		 */
1096*3446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
1097*3446Smrj 	}
1098*3446Smrj 
1099*3446Smrj 	mach_cpucontext_free(cp, ctx, 0);
1100*3446Smrj 
1101*3446Smrj 	if (tsc_gethrtime_enable)
1102*3446Smrj 		tsc_sync_master(who);
1103*3446Smrj 
1104*3446Smrj 	if (dtrace_cpu_init != NULL) {
1105*3446Smrj 		/*
1106*3446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
1107*3446Smrj 		 */
1108*3446Smrj 		mutex_enter(&cpu_lock);
1109*3446Smrj 		(*dtrace_cpu_init)(who);
1110*3446Smrj 		mutex_exit(&cpu_lock);
1111*3446Smrj 	}
1112*3446Smrj 
1113*3446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
1114*3446Smrj 		delay(1);
1115*3446Smrj 
1116*3446Smrj 	return (0);
1117*3446Smrj }
1118*3446Smrj 
11192006Sandrei 
11200Sstevel@tonic-gate /*ARGSUSED*/
11210Sstevel@tonic-gate void
11220Sstevel@tonic-gate start_other_cpus(int cprboot)
11230Sstevel@tonic-gate {
1124*3446Smrj 	uint_t who;
1125*3446Smrj 	uint_t skipped = 0;
1126*3446Smrj 	uint_t bootcpuid = 0;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Initialize our own cpu_info.
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	init_cpu_info(CPU);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	/*
11340Sstevel@tonic-gate 	 * Initialize our syscall handlers
11350Sstevel@tonic-gate 	 */
11360Sstevel@tonic-gate 	init_cpu_syscall(CPU);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/*
1139*3446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
1140*3446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
1141*3446Smrj 	 * precisely the same reason.
1142*3446Smrj 	 */
1143*3446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
1144*3446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
1145*3446Smrj 
1146*3446Smrj 	/*
11470Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
11480Sstevel@tonic-gate 	 */
1149*3446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
11500Sstevel@tonic-gate 		if (use_mp == 0)
11510Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
11520Sstevel@tonic-gate 		goto done;
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * perform such initialization as is needed
11570Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
11580Sstevel@tonic-gate 	 */
11590Sstevel@tonic-gate 	cpu_pause_init();
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
11620Sstevel@tonic-gate 
1163*3446Smrj 	if (mach_cpucontext_init() != 0)
11640Sstevel@tonic-gate 		goto done;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	flushes_require_xcalls = 1;
11670Sstevel@tonic-gate 
11682575Snf202958 	/*
11692575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
11702575Snf202958 	 * do their TSC syncs with the same CPU.
11712575Snf202958 	 */
11720Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
11752575Snf202958 
11762006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
11772006Sandrei 			continue;
1178*3446Smrj 		ASSERT(who != bootcpuid);
11792006Sandrei 		if (ncpus >= max_ncpus) {
11802006Sandrei 			skipped = who;
11810Sstevel@tonic-gate 			continue;
11822006Sandrei 		}
1183*3446Smrj 		if (start_cpu(who) != 0)
1184*3446Smrj 			CPUSET_DEL(mp_cpus, who);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	affinity_clear();
11880Sstevel@tonic-gate 
11892006Sandrei 	if (skipped) {
11902006Sandrei 		cmn_err(CE_NOTE,
1191*3446Smrj 		    "System detected %d cpus, but "
1192*3446Smrj 		    "only %d cpu(s) were enabled during boot.",
11932006Sandrei 		    skipped + 1, ncpus);
11942006Sandrei 		cmn_err(CE_NOTE,
11952006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
11962006Sandrei 		    "See eeprom(1M).");
11972006Sandrei 	}
11982006Sandrei 
11990Sstevel@tonic-gate done:
12000Sstevel@tonic-gate 	workaround_errata_end();
1201*3446Smrj 	mach_cpucontext_fini();
12021642Sgavinm 
12031642Sgavinm 	cmi_post_mpstartup();
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
12080Sstevel@tonic-gate  */
12090Sstevel@tonic-gate /*ARGSUSED*/
12100Sstevel@tonic-gate int
12110Sstevel@tonic-gate mp_cpu_configure(int cpuid)
12120Sstevel@tonic-gate {
12130Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate /*ARGSUSED*/
12170Sstevel@tonic-gate int
12180Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
12190Sstevel@tonic-gate {
12200Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate /*
12240Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
12252985Sdmick  * Called from real_mode_start.
12261251Skchow  *
12271251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
12281251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
12291251Skchow  * hat_unload which requires CPU_READY to be set.
12300Sstevel@tonic-gate  */
12310Sstevel@tonic-gate void
12320Sstevel@tonic-gate mp_startup(void)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate 	struct cpu *cp = CPU;
12350Sstevel@tonic-gate 	uint_t new_x86_feature;
12360Sstevel@tonic-gate 
12372985Sdmick 	/*
12383021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
12393021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
12403021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
12413021Sdmick 	 * interrupts, cmn_err, etc.
12423021Sdmick 	 */
12433021Sdmick 
12443021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
12453021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
12463021Sdmick 
12473021Sdmick 	if (tsc_gethrtime_enable)
12483021Sdmick 		tsc_sync_slave();
12493021Sdmick 
12503021Sdmick 	/*
12512985Sdmick 	 * Once this was done from assembly, but it's safer here; if
12522985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
12532985Sdmick 	 * since we get here by calling t_pc, we need to do that call
12542985Sdmick 	 * before swtch() overwrites it.
12552985Sdmick 	 */
12562985Sdmick 
12572985Sdmick 	(void) (*ap_mlsetup)();
12582985Sdmick 
12590Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/*
12620Sstevel@tonic-gate 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
12630Sstevel@tonic-gate 	 * this with interrupts disabled.
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	if (x86_feature & X86_MTRR)
12660Sstevel@tonic-gate 		mtrr_sync();
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	/*
1269*3446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
1270*3446Smrj 	 * for the rdtscp instruction.
1271*3446Smrj 	 */
1272*3446Smrj 	if (x86_feature & X86_TSCP)
1273*3446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
1274*3446Smrj 
1275*3446Smrj 	/*
12760Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
12770Sstevel@tonic-gate 	 */
12780Sstevel@tonic-gate 	init_cpu_syscall(cp);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/*
12810Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
12820Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
12830Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
12840Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
12850Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
12860Sstevel@tonic-gate 	 * calls before CPU_READY is set.
12870Sstevel@tonic-gate 	 */
1288*3446Smrj 	splx(ipltospl(LOCK_LEVEL));
1289*3446Smrj 	sti();
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/*
12920Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
12930Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
12940Sstevel@tonic-gate 	 *
12950Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
12960Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
12970Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
12980Sstevel@tonic-gate 	 * gets large enough.
12990Sstevel@tonic-gate 	 */
13000Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
13010Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
13020Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
13030Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
13080Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
13090Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
13100Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
13110Sstevel@tonic-gate 	 */
13120Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
13130Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	cpuid_pass2(cp);
13160Sstevel@tonic-gate 	cpuid_pass3(cp);
13170Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	init_cpu_info(cp);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
13220Sstevel@tonic-gate 	/*
13233434Sesaxe 	 * Processor group initialization for this CPU is dependent on the
13243434Sesaxe 	 * cpuid probing, which must be done in the context of the current
13253434Sesaxe 	 * CPU.
13260Sstevel@tonic-gate 	 */
13273434Sesaxe 	pghw_physid_create(cp);
13283434Sesaxe 	pg_cpu_init(cp);
13293434Sesaxe 	pg_cmt_cpu_startup(cp);
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
13320Sstevel@tonic-gate 	cpu_add_active(cp);
13332575Snf202958 
13342575Snf202958 	if (dtrace_cpu_init != NULL) {
13352575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
13362575Snf202958 	}
13372575Snf202958 
13380Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
13390Sstevel@tonic-gate 
13403029Ssethg 	/*
13413029Ssethg 	 * Enable preemption here so that contention for any locks acquired
13423029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
13433029Ssethg 	 * locks is continously executing on other CPUs (for example, this
13443029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
13453029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
13463029Ssethg 	 * CPU state is pretty-much fully constructed.
13473029Ssethg 	 */
13483029Ssethg 	curthread->t_preempt = 0;
13493029Ssethg 
13501251Skchow 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
13511251Skchow 
13521482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
13531482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
13541482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
13551482Ssethg 
13560Sstevel@tonic-gate 	(void) spl0();				/* enable interrupts */
13570Sstevel@tonic-gate 
13581414Scindi 	/*
13591414Scindi 	 * Set up the CPU module for this CPU.  This can't be done before
13601414Scindi 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
13611414Scindi 	 * need to go load another CPU module.  The act of attempting to load
13621414Scindi 	 * a module may trigger a cross-call, which will ASSERT unless this
13631414Scindi 	 * cpu is CPU_READY.
13641414Scindi 	 */
13651414Scindi 	cmi_init();
13661414Scindi 
13671414Scindi 	if (x86_feature & X86_MCA)
13681414Scindi 		cmi_mca_init();
13691414Scindi 
13700Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
1371*3446Smrj 		kdi_cpu_init();
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	/*
13740Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
13750Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
13760Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	/*
13810Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
13820Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
13830Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
13840Sstevel@tonic-gate 	 */
13850Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
13860Sstevel@tonic-gate 	    cp->cpu_id);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
13900Sstevel@tonic-gate 	 */
13910Sstevel@tonic-gate 	thread_exit();
13920Sstevel@tonic-gate 	panic("mp_startup: cannot return");
13930Sstevel@tonic-gate 	/*NOTREACHED*/
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate /*
13980Sstevel@tonic-gate  * Start CPU on user request.
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate /* ARGSUSED */
14010Sstevel@tonic-gate int
14020Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14050Sstevel@tonic-gate 	return (0);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * Stop CPU on user request.
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate /* ARGSUSED */
14120Sstevel@tonic-gate int
14130Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
14140Sstevel@tonic-gate {
14151389Sdmick 	extern int cbe_psm_timer_mode;
14160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14171389Sdmick 
14181389Sdmick 	/*
14191389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
14201389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
14211389Sdmick 	 */
14221389Sdmick 
14231389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
14241389Sdmick 		return (1);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	return (0);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate /*
14300Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
14310Sstevel@tonic-gate  */
14320Sstevel@tonic-gate int
14330Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
14340Sstevel@tonic-gate {
14350Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
14360Sstevel@tonic-gate 		return (EBUSY);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
14390Sstevel@tonic-gate 	return (0);
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
14440Sstevel@tonic-gate  */
14450Sstevel@tonic-gate void
14460Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
14490Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
14500Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate void
14560Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
14571414Scindi {
14581414Scindi 	cmi_faulted_enter(cp);
14591414Scindi }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate void
14620Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
14631414Scindi {
14641414Scindi 	cmi_faulted_exit(cp);
14651414Scindi }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate /*
14680Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
14690Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
14700Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
14710Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
14720Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
14730Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
14740Sstevel@tonic-gate  * syscall feature flags.
14750Sstevel@tonic-gate  */
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate /*ARGSUSED*/
14780Sstevel@tonic-gate void
14790Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
14800Sstevel@tonic-gate {
1481*3446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
14820Sstevel@tonic-gate 		cpu_sep_disable();
1483*3446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
14840Sstevel@tonic-gate 		cpu_asysc_disable();
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate /*ARGSUSED*/
14880Sstevel@tonic-gate void
14890Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
14900Sstevel@tonic-gate {
1491*3446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
14920Sstevel@tonic-gate 		cpu_sep_enable();
1493*3446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
14940Sstevel@tonic-gate 		cpu_asysc_enable();
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate static void
14980Sstevel@tonic-gate cpu_sep_enable(void)
14990Sstevel@tonic-gate {
15000Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
15010Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15020Sstevel@tonic-gate 
1503770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate static void
15070Sstevel@tonic-gate cpu_sep_disable(void)
15080Sstevel@tonic-gate {
15090Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
15100Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/*
15130Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
15140Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
15150Sstevel@tonic-gate 	 */
1516*3446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate static void
15200Sstevel@tonic-gate cpu_asysc_enable(void)
15210Sstevel@tonic-gate {
15220Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
15230Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15240Sstevel@tonic-gate 
1525770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1526770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
15270Sstevel@tonic-gate }
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate static void
15300Sstevel@tonic-gate cpu_asysc_disable(void)
15310Sstevel@tonic-gate {
15320Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
15330Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	/*
15360Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
15370Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
15380Sstevel@tonic-gate 	 */
1539770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1540770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
15410Sstevel@tonic-gate }
1542