xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 1455:b43f098fa50c)
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
5*1455Sandrei  * Common Development and Distribution License (the "License").
6*1455Sandrei  * 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  */
210Sstevel@tonic-gate /*
221251Skchow  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/thread.h>
300Sstevel@tonic-gate #include <sys/cpuvar.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/disp.h>
350Sstevel@tonic-gate #include <sys/mmu.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 <sys/mmu.h>
460Sstevel@tonic-gate #include <vm/as.h>
470Sstevel@tonic-gate #include <vm/seg_kmem.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>
570Sstevel@tonic-gate #include <sys/chip.h>
580Sstevel@tonic-gate #include <sys/dtrace.h>
590Sstevel@tonic-gate #include <sys/archsystm.h>
600Sstevel@tonic-gate #include <sys/fp.h>
610Sstevel@tonic-gate #include <sys/reboot.h>
620Sstevel@tonic-gate #include <sys/kdi.h>
630Sstevel@tonic-gate #include <vm/hat_i86.h>
640Sstevel@tonic-gate #include <sys/memnode.h>
65938Sesaxe #include <sys/pci_cfgspace.h>
661414Scindi #include <sys/cpu_module.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
690Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
700Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Useful for disabling MP bring-up for an MP capable kernel
740Sstevel@tonic-gate  * (a kernel that was built with MP defined)
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate int use_mp = 1;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate int mp_cpus = 0x1;	/* to be set by platform specific module	*/
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
820Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
830Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
840Sstevel@tonic-gate  * order to allow cross calls.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate int flushes_require_xcalls = 0;
870Sstevel@tonic-gate ulong_t	cpu_ready_set = 1;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate extern	void	real_mode_start(void);
900Sstevel@tonic-gate extern	void	real_mode_end(void);
910Sstevel@tonic-gate static 	void	mp_startup(void);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static void cpu_sep_enable(void);
940Sstevel@tonic-gate static void cpu_sep_disable(void);
950Sstevel@tonic-gate static void cpu_asysc_enable(void);
960Sstevel@tonic-gate static void cpu_asysc_disable(void);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate extern int tsc_gethrtime_enable;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate void
1040Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1070Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1150Sstevel@tonic-gate 	if (fpu_exists)
1160Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1210Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1260Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1270Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Configure syscall support on this CPU.
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate /*ARGSUSED*/
1360Sstevel@tonic-gate static void
1370Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	kpreempt_disable();
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #if defined(__amd64)
1420Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC) {
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #if !defined(__lint)
1450Sstevel@tonic-gate 		/*
1460Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1470Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1480Sstevel@tonic-gate 		 * here.
1490Sstevel@tonic-gate 		 */
1500Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1510Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1520Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate 		/*
1550Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1560Sstevel@tonic-gate 		 */
1570Sstevel@tonic-gate 		cpu_asysc_enable();
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		/*
1600Sstevel@tonic-gate 		 * Program the magic registers ..
1610Sstevel@tonic-gate 		 */
162770Skucharsk 		wrmsr(MSR_AMD_STAR, ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) <<
163770Skucharsk 		    32);
164770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
165770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1690Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1700Sstevel@tonic-gate 		 */
171770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1770Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1780Sstevel@tonic-gate 	 *
1790Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1800Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	if (x86_feature & X86_SEP) {
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #if !defined(__lint)
1850Sstevel@tonic-gate 		/*
1860Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1870Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1880Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
1890Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
1900Sstevel@tonic-gate 		 * Instruction Set Reference"
1910Sstevel@tonic-gate 		 */
1920Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
1950Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
1980Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		cpu_sep_enable();
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2050Sstevel@tonic-gate 		 * via a context handler.
2060Sstevel@tonic-gate 		 */
207770Skucharsk 		wrmsr(MSR_INTC_SEP_ESP, 0ULL);
208770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	kpreempt_enable();
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Multiprocessor initialization.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2180Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate static void
2210Sstevel@tonic-gate mp_startup_init(int cpun)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate #if defined(__amd64)
2240Sstevel@tonic-gate extern void *long_mode_64(void);
2250Sstevel@tonic-gate #endif	/* __amd64 */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	struct cpu *cp;
2280Sstevel@tonic-gate 	struct tss *ntss;
2290Sstevel@tonic-gate 	kthread_id_t tp;
2300Sstevel@tonic-gate 	caddr_t	sp;
2310Sstevel@tonic-gate 	int size;
2320Sstevel@tonic-gate 	proc_t *procp;
2330Sstevel@tonic-gate 	extern void idle();
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	struct cpu_tables *tablesp;
2360Sstevel@tonic-gate 	rm_platter_t *real_mode_platter = (rm_platter_t *)rm_platter_va;
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 
2440Sstevel@tonic-gate 	if ((cp = kmem_zalloc(sizeof (*cp), KM_NOSLEEP)) == NULL) {
2450Sstevel@tonic-gate 		panic("mp_startup_init: cpu%d: "
2460Sstevel@tonic-gate 		    "no memory for cpu structure", cpun);
2470Sstevel@tonic-gate 		/*NOTREACHED*/
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	procp = curthread->t_procp;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	disp_cpu_init(cp);
2560Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2570Sstevel@tonic-gate 
258414Skchow 	cpu_vm_data_init(cp);
259414Skchow 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2620Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2630Sstevel@tonic-gate 	 * when the CPU starts running.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2660Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2700Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2710Sstevel@tonic-gate 	 *
2720Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2730Sstevel@tonic-gate 	 * thread_create().
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2760Sstevel@tonic-gate 	tp->t_preempt = 1;
2770Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2780Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2790Sstevel@tonic-gate 	tp->t_cpu = cp;
2800Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	sp = tp->t_stk;
2860Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2870Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	cp->cpu_id = cpun;
2900Sstevel@tonic-gate 	cp->cpu_self = cp;
2910Sstevel@tonic-gate 	cp->cpu_mask = 1 << cpun;
2920Sstevel@tonic-gate 	cp->cpu_thread = tp;
2930Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
2940Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
2950Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	tp->t_preempt = 1;
3050Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3060Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3070Sstevel@tonic-gate 	tp->t_cpu = cp;
3080Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/*
31160Sesaxe 	 * Bootstrap the CPU for CMT aware scheduling
31260Sesaxe 	 * The rest of the initialization will happen from
31360Sesaxe 	 * mp_startup()
31460Sesaxe 	 */
31560Sesaxe 	chip_bootstrap_cpu(cp);
31660Sesaxe 
31760Sesaxe 	/*
3180Sstevel@tonic-gate 	 * Perform CPC intialization on the new CPU.
3190Sstevel@tonic-gate 	 */
3200Sstevel@tonic-gate 	kcpc_hw_init(cp);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3240Sstevel@tonic-gate 	 * for each CPU.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Allocate space for page directory, stack, tss, gdt and idt.
3310Sstevel@tonic-gate 	 * This assumes that kmem_alloc will return memory which is aligned
3320Sstevel@tonic-gate 	 * to the next higher power of 2 or a page(if size > MAXABIG)
3330Sstevel@tonic-gate 	 * If this assumption goes wrong at any time due to change in
3340Sstevel@tonic-gate 	 * kmem alloc, things may not work as the page directory has to be
3350Sstevel@tonic-gate 	 * page aligned
3360Sstevel@tonic-gate 	 */
3370Sstevel@tonic-gate 	if ((tablesp = kmem_zalloc(sizeof (*tablesp), KM_NOSLEEP)) == NULL)
3380Sstevel@tonic-gate 		panic("mp_startup_init: cpu%d cannot allocate tables", cpun);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if ((uintptr_t)tablesp & ~MMU_STD_PAGEMASK) {
3410Sstevel@tonic-gate 		kmem_free(tablesp, sizeof (struct cpu_tables));
3420Sstevel@tonic-gate 		size = sizeof (struct cpu_tables) + MMU_STD_PAGESIZE;
3430Sstevel@tonic-gate 		tablesp = kmem_zalloc(size, KM_NOSLEEP);
3440Sstevel@tonic-gate 		tablesp = (struct cpu_tables *)
3450Sstevel@tonic-gate 		    (((uintptr_t)tablesp + MMU_STD_PAGESIZE) &
3460Sstevel@tonic-gate 		    MMU_STD_PAGEMASK);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	ntss = cp->cpu_tss = &tablesp->ct_tss;
3500Sstevel@tonic-gate 	cp->cpu_gdt = tablesp->ct_gdt;
3510Sstevel@tonic-gate 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, NGDT * (sizeof (user_desc_t)));
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate #if defined(__amd64)
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * #DF (double fault).
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	ntss->tss_ist1 =
3590Sstevel@tonic-gate 	    (uint64_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate #elif defined(__i386)
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	ntss->tss_esp0 = ntss->tss_esp1 = ntss->tss_esp2 = ntss->tss_esp =
3640Sstevel@tonic-gate 	    (uint32_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	ntss->tss_ss0 = ntss->tss_ss1 = ntss->tss_ss2 = ntss->tss_ss = KDS_SEL;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	ntss->tss_eip = (uint32_t)mp_startup;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	ntss->tss_cs = KCS_SEL;
3710Sstevel@tonic-gate 	ntss->tss_fs = KFS_SEL;
3720Sstevel@tonic-gate 	ntss->tss_gs = KGS_SEL;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * setup kernel %gs.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3780Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate #endif	/* __i386 */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * Set I/O bit map offset equal to size of TSS segment limit
3840Sstevel@tonic-gate 	 * for no I/O permission map. This will cause all user I/O
3850Sstevel@tonic-gate 	 * instructions to generate #gp fault.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 	ntss->tss_bitmapbase = sizeof (*ntss);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	/*
3900Sstevel@tonic-gate 	 * setup kernel tss.
3910Sstevel@tonic-gate 	 */
3920Sstevel@tonic-gate 	set_syssegd((system_desc_t *)&cp->cpu_gdt[GDT_KTSS], cp->cpu_tss,
3930Sstevel@tonic-gate 	    sizeof (*cp->cpu_tss) -1, SDT_SYSTSS, SEL_KPL);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3970Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3980Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3990Sstevel@tonic-gate 	 * cmpxchgl register bug
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	cp->cpu_idt = CPU->cpu_idt;
4020Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
4030Sstevel@tonic-gate 		cp->cpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
4040Sstevel@tonic-gate 		bcopy(idt0, cp->cpu_idt, sizeof (idt0));
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Get interrupt priority data from cpu 0
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	hat_cpu_online(cp);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/* Should remove all entries for the current process/thread here */
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Fill up the real mode platter to make it easy for real mode code to
4180Sstevel@tonic-gate 	 * kick it off. This area should really be one passed by boot to kernel
4190Sstevel@tonic-gate 	 * and guaranteed to be below 1MB and aligned to 16 bytes. Should also
4200Sstevel@tonic-gate 	 * have identical physical and virtual address in paged mode.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	real_mode_platter->rm_idt_base = cp->cpu_idt;
4230Sstevel@tonic-gate 	real_mode_platter->rm_idt_lim = sizeof (idt0) - 1;
4240Sstevel@tonic-gate 	real_mode_platter->rm_gdt_base = cp->cpu_gdt;
4250Sstevel@tonic-gate 	real_mode_platter->rm_gdt_lim = sizeof (gdt0) -1;
4260Sstevel@tonic-gate 	real_mode_platter->rm_pdbr = getcr3();
4270Sstevel@tonic-gate 	real_mode_platter->rm_cpu = cpun;
4280Sstevel@tonic-gate 	real_mode_platter->rm_x86feature = x86_feature;
4290Sstevel@tonic-gate 	real_mode_platter->rm_cr4 = cr4_value;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate #if defined(__amd64)
4320Sstevel@tonic-gate 	if (getcr3() > 0xffffffffUL)
4330Sstevel@tonic-gate 		panic("Cannot initialize CPUs; kernel's 64-bit page tables\n"
4340Sstevel@tonic-gate 			"located above 4G in physical memory (@ 0x%llx).",
4350Sstevel@tonic-gate 			(unsigned long long)getcr3());
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * Setup pseudo-descriptors for temporary GDT and IDT for use ONLY
4390Sstevel@tonic-gate 	 * by code in real_mode_start():
4400Sstevel@tonic-gate 	 *
4410Sstevel@tonic-gate 	 * GDT[0]:  NULL selector
4420Sstevel@tonic-gate 	 * GDT[1]:  64-bit CS: Long = 1, Present = 1, bits 12, 11 = 1
4430Sstevel@tonic-gate 	 *
4440Sstevel@tonic-gate 	 * Clear the IDT as interrupts will be off and a limit of 0 will cause
4450Sstevel@tonic-gate 	 * the CPU to triple fault and reset on an NMI, seemingly as reasonable
4460Sstevel@tonic-gate 	 * a course of action as any other, though it may cause the entire
4470Sstevel@tonic-gate 	 * platform to reset in some cases...
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt[0] = 0ULL;
4500Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt[TEMPGDT_KCODE64] = 0x20980000000000ULL;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt_lim = (ushort_t)
4530Sstevel@tonic-gate 	    (sizeof (real_mode_platter->rm_temp_gdt) - 1);
4540Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt_base = rm_platter_pa +
4550Sstevel@tonic-gate 	    (uint32_t)(&((rm_platter_t *)0)->rm_temp_gdt);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	real_mode_platter->rm_temp_idt_lim = 0;
4580Sstevel@tonic-gate 	real_mode_platter->rm_temp_idt_base = 0;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * Since the CPU needs to jump to protected mode using an identity
4620Sstevel@tonic-gate 	 * mapped address, we need to calculate it here.
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	real_mode_platter->rm_longmode64_addr = rm_platter_pa +
4650Sstevel@tonic-gate 	    ((uint32_t)long_mode_64 - (uint32_t)real_mode_start);
4660Sstevel@tonic-gate #endif	/* __amd64 */
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate #ifdef TRAPTRACE
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers for this
4710Sstevel@tonic-gate 	 * CPU.
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4740Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4750Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4760Sstevel@tonic-gate #endif
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * Record that we have another CPU.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4840Sstevel@tonic-gate 	 */
485*1455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4880Sstevel@tonic-gate 	 * after mp_startup().
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	cpu_add_unit(cp);
4910Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
4980Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
4990Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5000Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5010Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5020Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5030Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5040Sstevel@tonic-gate  * system.
5050Sstevel@tonic-gate  *
506938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
507938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
508938Sesaxe  * to acknowledging their readiness to the master, so this routine will
509938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
510938Sesaxe  * global data safe.
511938Sesaxe  *
512359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
513359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5170Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5180Sstevel@tonic-gate #endif
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5210Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5220Sstevel@tonic-gate #endif
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5250Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5260Sstevel@tonic-gate #endif
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5290Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5300Sstevel@tonic-gate #endif
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5330Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5340Sstevel@tonic-gate #endif
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5370Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5410Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate 
544359Skucharsk #if defined(OPTERON_ERRATUM_131)
545359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
546359Skucharsk #endif
5470Sstevel@tonic-gate 
548938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
549938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
550938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
551938Sesaxe #endif
552938Sesaxe 
5530Sstevel@tonic-gate #define	WARNING(cpu, n)						\
5540Sstevel@tonic-gate 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %d",	\
5550Sstevel@tonic-gate 	    (cpu)->cpu_id, (n))
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate uint_t
5580Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	uint_t missing = 0;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	ASSERT(cpu == CPU);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*LINTED*/
5650Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
5680Sstevel@tonic-gate 		 */
5690Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
5700Sstevel@tonic-gate 		/*
5710Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
5720Sstevel@tonic-gate 		 */
5730Sstevel@tonic-gate #else
5740Sstevel@tonic-gate 		WARNING(cpu, 88);
5750Sstevel@tonic-gate 		missing++;
5760Sstevel@tonic-gate #endif
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
5800Sstevel@tonic-gate 		/*
5810Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
5820Sstevel@tonic-gate 		 */
5830Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5840Sstevel@tonic-gate 		/*
5850Sstevel@tonic-gate 		 * fix is in trap.c
5860Sstevel@tonic-gate 		 */
5870Sstevel@tonic-gate 		opteron_erratum_91++;
5880Sstevel@tonic-gate #else
5890Sstevel@tonic-gate 		WARNING(cpu, 91);
5900Sstevel@tonic-gate 		missing++;
5910Sstevel@tonic-gate #endif
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
5950Sstevel@tonic-gate 		/*
5960Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
5970Sstevel@tonic-gate 		 */
5980Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5990Sstevel@tonic-gate 		/*
6000Sstevel@tonic-gate 		 * fix is in trap.c
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		opteron_erratum_93++;
6030Sstevel@tonic-gate #else
6040Sstevel@tonic-gate 		WARNING(cpu, 93);
6050Sstevel@tonic-gate 		missing++;
6060Sstevel@tonic-gate #endif
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*LINTED*/
6100Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
6110Sstevel@tonic-gate 		/*
6120Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
6130Sstevel@tonic-gate 		 */
6140Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
6150Sstevel@tonic-gate #if defined(_LP64)
6160Sstevel@tonic-gate 		/*
6170Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
6180Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
6190Sstevel@tonic-gate 		 * range mod 4G.
6200Sstevel@tonic-gate 		 */
6210Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
6220Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		/*LINTED*/
6250Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
6260Sstevel@tonic-gate #endif	/* _LP64 */
6270Sstevel@tonic-gate #else
6280Sstevel@tonic-gate 		WARNING(cpu, 95);
6290Sstevel@tonic-gate 		missing++;
6300Sstevel@tonic-gate #endif	/* OPTERON_ERRATUM_95 */
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
6340Sstevel@tonic-gate 		/*
6350Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
6360Sstevel@tonic-gate 		 */
6370Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * fix is in trap.c
6400Sstevel@tonic-gate 		 */
6410Sstevel@tonic-gate 		opteron_erratum_100++;
6420Sstevel@tonic-gate #else
6430Sstevel@tonic-gate 		WARNING(cpu, 100);
6440Sstevel@tonic-gate 		missing++;
6450Sstevel@tonic-gate #endif
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*LINTED*/
6490Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
6520Sstevel@tonic-gate 		 * Some Processors
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
6550Sstevel@tonic-gate 		/*
6560Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
6570Sstevel@tonic-gate 		 * those processors)
6580Sstevel@tonic-gate 		 */
6590Sstevel@tonic-gate #else
6600Sstevel@tonic-gate 		WARNING(cpu, 108);
6610Sstevel@tonic-gate 		missing++;
6620Sstevel@tonic-gate #endif
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*LINTED*/
6660Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 109) > 0) {
6670Sstevel@tonic-gate 		/*
6680Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
6690Sstevel@tonic-gate 		 */
6700Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		/* workaround is to print a warning to upgrade BIOS */
673770Skucharsk 		if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
6740Sstevel@tonic-gate 			opteron_erratum_109++;
6750Sstevel@tonic-gate #else
6760Sstevel@tonic-gate 		WARNING(cpu, 109);
6770Sstevel@tonic-gate 		missing++;
6780Sstevel@tonic-gate #endif
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 	/*LINTED*/
6810Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
6840Sstevel@tonic-gate 		 * Processor Hang
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
6870Sstevel@tonic-gate 		static int	lma;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		if (opteron_erratum_121)
6900Sstevel@tonic-gate 			opteron_erratum_121++;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		/*
6930Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
6940Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
6950Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
6960Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
6970Sstevel@tonic-gate 		 */
6980Sstevel@tonic-gate 		if (lma == 0) {
6990Sstevel@tonic-gate 			/*
7000Sstevel@tonic-gate 			 * check LMA once: assume all cpus are in long mode
7010Sstevel@tonic-gate 			 * or not.
7020Sstevel@tonic-gate 			 */
7030Sstevel@tonic-gate 			lma = 1;
7040Sstevel@tonic-gate 
705770Skucharsk 			if (rdmsr(MSR_AMD_EFER) & AMD_EFER_LMA) {
7060Sstevel@tonic-gate 				if (hole_start) {
7070Sstevel@tonic-gate 					hole_start -= PAGESIZE;
7080Sstevel@tonic-gate 				} else {
7090Sstevel@tonic-gate 					/*
7100Sstevel@tonic-gate 					 * hole_start not yet initialized by
7110Sstevel@tonic-gate 					 * mmu_init. Initialize hole_start
7120Sstevel@tonic-gate 					 * with value to be subtracted.
7130Sstevel@tonic-gate 					 */
7140Sstevel@tonic-gate 					hole_start = PAGESIZE;
7150Sstevel@tonic-gate 				}
7160Sstevel@tonic-gate 				opteron_erratum_121++;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate #else
7200Sstevel@tonic-gate 		WARNING(cpu, 121);
7210Sstevel@tonic-gate 		missing++;
7220Sstevel@tonic-gate #endif
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*LINTED*/
7260Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 122) > 0) {
7270Sstevel@tonic-gate 		/*
7280Sstevel@tonic-gate 		 * TLB Flush Filter May Cause Cohenrency Problem in
7290Sstevel@tonic-gate 		 * Multiprocessor Systems
7300Sstevel@tonic-gate 		 */
7310Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
7340Sstevel@tonic-gate 		 * or multi-processor).
7350Sstevel@tonic-gate 		 */
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		if (opteron_erratum_122 || lgrp_plat_node_cnt > 1 ||
7380Sstevel@tonic-gate 		    cpuid_get_ncpu_per_chip(cpu) > 1) {
7390Sstevel@tonic-gate 			/* disable TLB Flush Filter */
740770Skucharsk 			wrmsr(MSR_AMD_HWCR, rdmsr(MSR_AMD_HWCR) |
741770Skucharsk 			    (uint64_t)(uintptr_t)AMD_HWCR_FFDIS);
7420Sstevel@tonic-gate 			opteron_erratum_122++;
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate #else
7460Sstevel@tonic-gate 		WARNING(cpu, 122);
7470Sstevel@tonic-gate 		missing++;
7480Sstevel@tonic-gate #endif
7490Sstevel@tonic-gate 	}
750302Skchow 
751302Skchow #if defined(OPTERON_ERRATUM_123)
7520Sstevel@tonic-gate 	/*LINTED*/
7530Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 123) > 0) {
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
7560Sstevel@tonic-gate 		 * Dual Core Processors
7570Sstevel@tonic-gate 		 */
7580Sstevel@tonic-gate 		/*
7590Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		if (cpuid_get_ncpu_per_chip(cpu) > 1) {
7630Sstevel@tonic-gate 			/* workaround is to print a warning to upgrade BIOS */
764770Skucharsk 			if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
7650Sstevel@tonic-gate 				opteron_erratum_123++;
7660Sstevel@tonic-gate 		}
767302Skchow 	}
7680Sstevel@tonic-gate #endif
769359Skucharsk 
770359Skucharsk #if defined(OPTERON_ERRATUM_131)
771359Skucharsk 	/*LINTED*/
772359Skucharsk 	if (cpuid_opteron_erratum(cpu, 131) > 0) {
773359Skucharsk 		/*
774359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
775359Skucharsk 		 * Waiting for a Probe Response
776359Skucharsk 		 */
777359Skucharsk 		/*
778359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
779359Skucharsk 		 */
780359Skucharsk 		if ((opteron_erratum_131 == 0) && ((lgrp_plat_node_cnt *
781359Skucharsk 		    cpuid_get_ncpu_per_chip(cpu)) >= 4)) {
782359Skucharsk 			/*
783359Skucharsk 			 * Workaround is to print a warning to upgrade
784359Skucharsk 			 * the BIOS
785359Skucharsk 			 */
786770Skucharsk 			if (!(rdmsr(MSR_AMD_NB_CFG) & AMD_NB_CFG_SRQ_HEARTBEAT))
787359Skucharsk 				opteron_erratum_131++;
788359Skucharsk 		}
789938Sesaxe 	}
790359Skucharsk #endif
791938Sesaxe 
792938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
793938Sesaxe 	/*
794938Sesaxe 	 * This isn't really erratum, but for convenience the
795938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
796938Sesaxe 	 */
797938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
798938Sesaxe 		int	node;
799938Sesaxe 		uint8_t data;
800938Sesaxe 
801938Sesaxe 		/*
802938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
803938Sesaxe 		 * K8 platforms to guard against TSC drift.
804938Sesaxe 		 */
805938Sesaxe 		if (opteron_workaround_6336786) {
806938Sesaxe 			opteron_workaround_6336786++;
807938Sesaxe 		} else if ((lgrp_plat_node_cnt *
808938Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) >= 2) ||
809938Sesaxe 		    opteron_workaround_6336786_UP) {
810938Sesaxe 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
811938Sesaxe 				/*
812938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
813938Sesaxe 				 * Northbridge device is the node id + 24.
814938Sesaxe 				 */
815938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
816938Sesaxe 				data &= 0xFC;
817938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
818938Sesaxe 			}
819938Sesaxe 			opteron_workaround_6336786++;
820938Sesaxe 		}
821359Skucharsk 	}
822938Sesaxe #endif
8230Sstevel@tonic-gate 	return (missing);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate void
8270Sstevel@tonic-gate workaround_errata_end()
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8300Sstevel@tonic-gate 	if (opteron_erratum_109) {
831359Skucharsk 		cmn_err(CE_WARN,
832359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
833359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
834359Skucharsk 		    " system's BIOS to a version\ncontaining this"
835359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
836359Skucharsk 		    " system\noperation may occur.\n");
8370Sstevel@tonic-gate 	}
838359Skucharsk #endif	/* OPTERON_ERRATUM_109 */
8390Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
8400Sstevel@tonic-gate 	if (opteron_erratum_123) {
841359Skucharsk 		cmn_err(CE_WARN,
842359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
843359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
844359Skucharsk 		    " system's BIOS to a version\ncontaining this"
845359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
846359Skucharsk 		    " system\noperation may occur.\n");
8470Sstevel@tonic-gate 	}
848359Skucharsk #endif	/* OPTERON_ERRATUM_123 */
849359Skucharsk #if defined(OPTERON_ERRATUM_131)
850359Skucharsk 	if (opteron_erratum_131) {
851359Skucharsk 		cmn_err(CE_WARN,
852359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
853359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
854359Skucharsk 		    " system's BIOS to a version\ncontaining this"
855359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
856359Skucharsk 		    " system\noperation may occur.\n");
857359Skucharsk 	}
858359Skucharsk #endif	/* OPTERON_ERRATUM_131 */
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate static ushort_t *mp_map_warm_reset_vector();
8620Sstevel@tonic-gate static void mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate void
8660Sstevel@tonic-gate start_other_cpus(int cprboot)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	unsigned who;
8691389Sdmick 	int cpuid = 0;
8700Sstevel@tonic-gate 	int delays = 0;
8710Sstevel@tonic-gate 	int started_cpu;
8720Sstevel@tonic-gate 	ushort_t *warm_reset_vector = NULL;
8730Sstevel@tonic-gate 	extern int procset;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * Initialize our own cpu_info.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 	init_cpu_info(CPU);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Initialize our syscall handlers
8820Sstevel@tonic-gate 	 */
8830Sstevel@tonic-gate 	init_cpu_syscall(CPU);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/*
8860Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
8870Sstevel@tonic-gate 	 */
8880Sstevel@tonic-gate 	if (!(mp_cpus & ~(1 << cpuid)) || use_mp == 0) {
8890Sstevel@tonic-gate 		if (use_mp == 0)
8900Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
8910Sstevel@tonic-gate 		goto done;
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/*
8950Sstevel@tonic-gate 	 * perform such initialization as is needed
8960Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	cpu_pause_init();
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	/*
9030Sstevel@tonic-gate 	 * Copy the real mode code at "real_mode_start" to the
9040Sstevel@tonic-gate 	 * page at rm_platter_va.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	warm_reset_vector = mp_map_warm_reset_vector();
9070Sstevel@tonic-gate 	if (warm_reset_vector == NULL)
9080Sstevel@tonic-gate 		goto done;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	bcopy((caddr_t)real_mode_start,
9110Sstevel@tonic-gate 	    (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code,
9120Sstevel@tonic-gate 	    (size_t)real_mode_end - (size_t)real_mode_start);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	flushes_require_xcalls = 1;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
9190Sstevel@tonic-gate 		if (who == cpuid)
9200Sstevel@tonic-gate 			continue;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		if ((mp_cpus & (1 << who)) == 0)
9230Sstevel@tonic-gate 			continue;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		mp_startup_init(who);
9260Sstevel@tonic-gate 		started_cpu = 1;
9270Sstevel@tonic-gate 		(*cpu_startf)(who, rm_platter_pa);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		while ((procset & (1 << who)) == 0) {
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 			delay(1);
9320Sstevel@tonic-gate 			if (++delays > (20 * hz)) {
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 				cmn_err(CE_WARN,
9350Sstevel@tonic-gate 				    "cpu%d failed to start", who);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 				mutex_enter(&cpu_lock);
9380Sstevel@tonic-gate 				cpu[who]->cpu_flags = 0;
939414Skchow 				cpu_vm_data_destroy(cpu[who]);
9400Sstevel@tonic-gate 				cpu_del_unit(who);
9410Sstevel@tonic-gate 				mutex_exit(&cpu_lock);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 				started_cpu = 0;
9440Sstevel@tonic-gate 				break;
9450Sstevel@tonic-gate 			}
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		if (!started_cpu)
9480Sstevel@tonic-gate 			continue;
9490Sstevel@tonic-gate 		if (tsc_gethrtime_enable)
9500Sstevel@tonic-gate 			tsc_sync_master(who);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 		if (dtrace_cpu_init != NULL) {
9540Sstevel@tonic-gate 			/*
9550Sstevel@tonic-gate 			 * DTrace CPU initialization expects cpu_lock
9560Sstevel@tonic-gate 			 * to be held.
9570Sstevel@tonic-gate 			 */
9580Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
9590Sstevel@tonic-gate 			(*dtrace_cpu_init)(who);
9600Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	affinity_clear();
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
9670Sstevel@tonic-gate 		if (who == cpuid)
9680Sstevel@tonic-gate 			continue;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		if (!(procset & (1 << who)))
9710Sstevel@tonic-gate 			continue;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 		while (!(cpu_ready_set & (1 << who)))
9740Sstevel@tonic-gate 			delay(1);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate done:
9780Sstevel@tonic-gate 	workaround_errata_end();
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (warm_reset_vector != NULL)
9810Sstevel@tonic-gate 		mp_unmap_warm_reset_vector(warm_reset_vector);
9820Sstevel@tonic-gate 	hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
9830Sstevel@tonic-gate 	    HAT_UNLOAD);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate /*
9870Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
9880Sstevel@tonic-gate  */
9890Sstevel@tonic-gate /*ARGSUSED*/
9900Sstevel@tonic-gate int
9910Sstevel@tonic-gate mp_cpu_configure(int cpuid)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /*ARGSUSED*/
9970Sstevel@tonic-gate int
9980Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
10050Sstevel@tonic-gate  * Resumed from cpu_startup.
10061251Skchow  *
10071251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
10081251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
10091251Skchow  * hat_unload which requires CPU_READY to be set.
10100Sstevel@tonic-gate  */
10110Sstevel@tonic-gate void
10120Sstevel@tonic-gate mp_startup(void)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	struct cpu *cp = CPU;
10150Sstevel@tonic-gate 	extern int procset;
10160Sstevel@tonic-gate 	uint_t new_x86_feature;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
10220Sstevel@tonic-gate 	 * this with interrupts disabled.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	if (x86_feature & X86_MTRR)
10250Sstevel@tonic-gate 		mtrr_sync();
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	/*
10280Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
10290Sstevel@tonic-gate 	 */
10300Sstevel@tonic-gate 	init_cpu_syscall(cp);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/*
10330Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
10340Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
10350Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
10360Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
10370Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
10380Sstevel@tonic-gate 	 * calls before CPU_READY is set.
10390Sstevel@tonic-gate 	 */
10400Sstevel@tonic-gate 	(void) splx(ipltospl(LOCK_LEVEL));
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/*
10430Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
10440Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
10450Sstevel@tonic-gate 	 *
10460Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
10470Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
10480Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
10490Sstevel@tonic-gate 	 * gets large enough.
10500Sstevel@tonic-gate 	 */
10510Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
10520Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
10530Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
10540Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
10590Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
10600Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
10610Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
10620Sstevel@tonic-gate 	 */
10630Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
10640Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	cpuid_pass2(cp);
10670Sstevel@tonic-gate 	cpuid_pass3(cp);
10680Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	init_cpu_info(cp);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
10730Sstevel@tonic-gate 	procset |= 1 << cp->cpu_id;
10740Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (tsc_gethrtime_enable)
10770Sstevel@tonic-gate 		tsc_sync_slave();
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 * It's unfortunate that chip_cpu_init() has to be called here.
10820Sstevel@tonic-gate 	 * It really belongs in cpu_add_unit(), but unfortunately it is
10830Sstevel@tonic-gate 	 * dependent on the cpuid probing, which must be done in the
10840Sstevel@tonic-gate 	 * context of the current CPU. Care must be taken on x86 to ensure
10850Sstevel@tonic-gate 	 * that mp_startup can safely block even though chip_cpu_init() and
10860Sstevel@tonic-gate 	 * cpu_add_active() have not yet been called.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	chip_cpu_init(cp);
10890Sstevel@tonic-gate 	chip_cpu_startup(cp);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
10920Sstevel@tonic-gate 	cpu_add_active(cp);
10930Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
10940Sstevel@tonic-gate 
10951251Skchow 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
10961251Skchow 
10970Sstevel@tonic-gate 	(void) spl0();				/* enable interrupts */
10980Sstevel@tonic-gate 
10991414Scindi 	/*
11001414Scindi 	 * Set up the CPU module for this CPU.  This can't be done before
11011414Scindi 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
11021414Scindi 	 * need to go load another CPU module.  The act of attempting to load
11031414Scindi 	 * a module may trigger a cross-call, which will ASSERT unless this
11041414Scindi 	 * cpu is CPU_READY.
11051414Scindi 	 */
11061414Scindi 	cmi_init();
11071414Scindi 
11081414Scindi 	if (x86_feature & X86_MCA)
11091414Scindi 		cmi_mca_init();
11101414Scindi 
11110Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
11120Sstevel@tonic-gate 		kdi_dvec_cpu_init(cp);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
11160Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
11170Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	/*
11220Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
11230Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
11240Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
11270Sstevel@tonic-gate 	    cp->cpu_id);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/*
11300Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	thread_exit();
11330Sstevel@tonic-gate 	panic("mp_startup: cannot return");
11340Sstevel@tonic-gate 	/*NOTREACHED*/
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate /*
11390Sstevel@tonic-gate  * Start CPU on user request.
11400Sstevel@tonic-gate  */
11410Sstevel@tonic-gate /* ARGSUSED */
11420Sstevel@tonic-gate int
11430Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11460Sstevel@tonic-gate 	return (0);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Stop CPU on user request.
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate /* ARGSUSED */
11530Sstevel@tonic-gate int
11540Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
11550Sstevel@tonic-gate {
11561389Sdmick 	extern int cbe_psm_timer_mode;
11570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11581389Sdmick 
11591389Sdmick 	/*
11601389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
11611389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
11621389Sdmick 	 */
11631389Sdmick 
11641389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
11651389Sdmick 		return (1);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	return (0);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate  * Power on CPU.
11720Sstevel@tonic-gate  */
11730Sstevel@tonic-gate /* ARGSUSED */
11740Sstevel@tonic-gate int
11750Sstevel@tonic-gate mp_cpu_poweron(struct cpu *cp)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11780Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * Power off CPU.
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate /* ARGSUSED */
11850Sstevel@tonic-gate int
11860Sstevel@tonic-gate mp_cpu_poweroff(struct cpu *cp)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11890Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate /*
11940Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
11950Sstevel@tonic-gate  */
11960Sstevel@tonic-gate int
11970Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
12000Sstevel@tonic-gate 		return (EBUSY);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
12030Sstevel@tonic-gate 	return (0);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
12080Sstevel@tonic-gate  */
12090Sstevel@tonic-gate void
12100Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
12110Sstevel@tonic-gate {
12120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12130Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
12140Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate static ushort_t *
12200Sstevel@tonic-gate mp_map_warm_reset_vector()
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	ushort_t *warm_reset_vector;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (!(warm_reset_vector = (ushort_t *)psm_map_phys(WARM_RESET_VECTOR,
12250Sstevel@tonic-gate 	    sizeof (ushort_t *), PROT_READ|PROT_WRITE)))
12260Sstevel@tonic-gate 		return (NULL);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/*
12290Sstevel@tonic-gate 	 * setup secondary cpu bios boot up vector
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	*warm_reset_vector = (ushort_t)((caddr_t)
12320Sstevel@tonic-gate 		((struct rm_platter *)rm_platter_va)->rm_code - rm_platter_va
12330Sstevel@tonic-gate 		+ ((ulong_t)rm_platter_va & 0xf));
12340Sstevel@tonic-gate 	warm_reset_vector++;
12350Sstevel@tonic-gate 	*warm_reset_vector = (ushort_t)(rm_platter_pa >> 4);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	--warm_reset_vector;
12380Sstevel@tonic-gate 	return (warm_reset_vector);
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate static void
12420Sstevel@tonic-gate mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	psm_unmap_phys((caddr_t)warm_reset_vector, sizeof (ushort_t *));
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate void
12480Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
12491414Scindi {
12501414Scindi 	cmi_faulted_enter(cp);
12511414Scindi }
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate void
12540Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
12551414Scindi {
12561414Scindi 	cmi_faulted_exit(cp);
12571414Scindi }
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate /*
12600Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
12610Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
12620Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
12630Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
12640Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
12650Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
12660Sstevel@tonic-gate  * syscall feature flags.
12670Sstevel@tonic-gate  */
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate /*ARGSUSED*/
12700Sstevel@tonic-gate void
12710Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	if (x86_feature & X86_SEP)
12740Sstevel@tonic-gate 		cpu_sep_disable();
12750Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC)
12760Sstevel@tonic-gate 		cpu_asysc_disable();
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate /*ARGSUSED*/
12800Sstevel@tonic-gate void
12810Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	if (x86_feature & X86_SEP)
12840Sstevel@tonic-gate 		cpu_sep_enable();
12850Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC)
12860Sstevel@tonic-gate 		cpu_asysc_enable();
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate static void
12900Sstevel@tonic-gate cpu_sep_enable(void)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
12930Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
12940Sstevel@tonic-gate 
1295770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate static void
12990Sstevel@tonic-gate cpu_sep_disable(void)
13000Sstevel@tonic-gate {
13010Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
13020Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/*
13050Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
13060Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
13070Sstevel@tonic-gate 	 */
1308770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, 0ULL);
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate static void
13120Sstevel@tonic-gate cpu_asysc_enable(void)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
13150Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
13160Sstevel@tonic-gate 
1317770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1318770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate static void
13220Sstevel@tonic-gate cpu_asysc_disable(void)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
13250Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
13290Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
13300Sstevel@tonic-gate 	 */
1331770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1332770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
13330Sstevel@tonic-gate }
1334