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 */ 211991Sheppo 220Sstevel@tonic-gate /* 23*8803SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/sysmacros.h> 280Sstevel@tonic-gate #include <sys/prom_plat.h> 290Sstevel@tonic-gate #include <sys/prom_debug.h> 300Sstevel@tonic-gate #include <vm/hat_sfmmu.h> 310Sstevel@tonic-gate #include <vm/seg_kp.h> 320Sstevel@tonic-gate #include <vm/seg_kmem.h> 330Sstevel@tonic-gate #include <sys/machsystm.h> 340Sstevel@tonic-gate #include <sys/callb.h> 350Sstevel@tonic-gate #include <sys/cpu_module.h> 363434Sesaxe #include <sys/pg.h> 373434Sesaxe #include <sys/cmt.h> 380Sstevel@tonic-gate #include <sys/dtrace.h> 390Sstevel@tonic-gate #include <sys/reboot.h> 400Sstevel@tonic-gate #include <sys/kdi.h> 411077Ssvemuri #include <sys/traptrace.h> 420Sstevel@tonic-gate #ifdef TRAPTRACE 430Sstevel@tonic-gate #include <sys/bootconf.h> 440Sstevel@tonic-gate #endif /* TRAPTRACE */ 450Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h> 460Sstevel@tonic-gate 474050Sjb145095 extern int cpu_intrq_setup(struct cpu *); 481991Sheppo extern void cpu_intrq_cleanup(struct cpu *); 490Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *); 500Sstevel@tonic-gate 510Sstevel@tonic-gate struct cpu *cpus; /* pointer to other cpus; dynamically allocate */ 520Sstevel@tonic-gate struct cpu *cpu[NCPU]; /* pointers to all CPUs */ 530Sstevel@tonic-gate uint64_t cpu_pa[NCPU]; /* pointers to all CPUs in PA */ 540Sstevel@tonic-gate cpu_core_t cpu_core[NCPU]; /* cpu_core structures */ 550Sstevel@tonic-gate 560Sstevel@tonic-gate #ifdef TRAPTRACE 575648Ssetje caddr_t ttrace_buf; /* kmem64 traptrace for all cpus except 0 */ 580Sstevel@tonic-gate #endif /* TRAPTRACE */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* bit mask of cpus ready for x-calls, protected by cpu_lock */ 610Sstevel@tonic-gate cpuset_t cpu_ready_set; 620Sstevel@tonic-gate 630Sstevel@tonic-gate /* bit mask used to communicate with cpus during bringup */ 640Sstevel@tonic-gate static cpuset_t proxy_ready_set; 650Sstevel@tonic-gate 660Sstevel@tonic-gate static void slave_startup(void); 670Sstevel@tonic-gate 680Sstevel@tonic-gate /* 690Sstevel@tonic-gate * Defined in $KARCH/os/mach_mp_startup.c 700Sstevel@tonic-gate */ 710Sstevel@tonic-gate #pragma weak init_cpu_info 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * Amount of time (in milliseconds) we should wait before giving up on CPU 750Sstevel@tonic-gate * initialization and assuming that the CPU we're trying to wake up is dead 760Sstevel@tonic-gate * or out of control. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate #define CPU_WAKEUP_GRACE_MSEC 1000 790Sstevel@tonic-gate 800Sstevel@tonic-gate #ifdef TRAPTRACE 810Sstevel@tonic-gate /* 823764Sdp78419 * This function sets traptrace buffers for all cpus 830Sstevel@tonic-gate * other than boot cpu. 840Sstevel@tonic-gate */ 855648Ssetje size_t 865648Ssetje calc_traptrace_sz(void) 870Sstevel@tonic-gate { 885648Ssetje return (TRAP_TSIZE * (max_ncpus - 1)); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate #endif /* TRAPTRACE */ 910Sstevel@tonic-gate 925648Ssetje 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * common slave cpu initialization code 950Sstevel@tonic-gate */ 960Sstevel@tonic-gate void 970Sstevel@tonic-gate common_startup_init(cpu_t *cp, int cpuid) 980Sstevel@tonic-gate { 990Sstevel@tonic-gate kthread_id_t tp; 1000Sstevel@tonic-gate sfmmu_t *sfmmup; 1010Sstevel@tonic-gate caddr_t sp; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * Allocate and initialize the startup thread for this CPU. 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate tp = thread_create(NULL, 0, slave_startup, NULL, 0, &p0, 1070Sstevel@tonic-gate TS_STOPPED, maxclsyspri); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * Set state to TS_ONPROC since this thread will start running 1110Sstevel@tonic-gate * as soon as the CPU comes online. 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * All the other fields of the thread structure are setup by 1140Sstevel@tonic-gate * thread_create(). 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate THREAD_ONPROC(tp, cp); 1170Sstevel@tonic-gate tp->t_preempt = 1; 1180Sstevel@tonic-gate tp->t_bound_cpu = cp; 1190Sstevel@tonic-gate tp->t_affinitycnt = 1; 1200Sstevel@tonic-gate tp->t_cpu = cp; 1210Sstevel@tonic-gate tp->t_disp_queue = cp->cpu_disp; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate sfmmup = astosfmmu(&kas); 1240Sstevel@tonic-gate CPUSET_ADD(sfmmup->sfmmu_cpusran, cpuid); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * Setup thread to start in slave_startup. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate sp = tp->t_stk; 1300Sstevel@tonic-gate tp->t_pc = (uintptr_t)slave_startup - 8; 1310Sstevel@tonic-gate tp->t_sp = (uintptr_t)((struct rwindow *)sp - 1) - STACK_BIAS; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate cp->cpu_id = cpuid; 1340Sstevel@tonic-gate cp->cpu_self = cp; 1350Sstevel@tonic-gate cp->cpu_thread = tp; 1360Sstevel@tonic-gate cp->cpu_lwp = NULL; 1370Sstevel@tonic-gate cp->cpu_dispthread = tp; 1380Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(tp); 139834Sandrei cp->cpu_startup_thread = tp; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * parametric flag setting functions. these routines set the cpu 1440Sstevel@tonic-gate * state just prior to releasing the slave cpu. 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate void 1470Sstevel@tonic-gate cold_flag_set(int cpuid) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate cpu_t *cp; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate cp = cpu[cpuid]; 1540Sstevel@tonic-gate cp->cpu_flags |= CPU_RUNNING | CPU_ENABLE | CPU_EXISTS; 1550Sstevel@tonic-gate cpu_add_active(cp); 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * Add CPU_READY after the cpu_add_active() call 1580Sstevel@tonic-gate * to avoid pausing cp. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate cp->cpu_flags |= CPU_READY; /* ready */ 1610Sstevel@tonic-gate cpu_set_state(cp); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static void 1650Sstevel@tonic-gate warm_flag_set(int cpuid) 1660Sstevel@tonic-gate { 1670Sstevel@tonic-gate cpu_t *cp; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * warm start activates cpus into the OFFLINE state 1730Sstevel@tonic-gate */ 1740Sstevel@tonic-gate cp = cpu[cpuid]; 1750Sstevel@tonic-gate cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS 1764681Sbholler | CPU_OFFLINE | CPU_QUIESCED; 1770Sstevel@tonic-gate cpu_set_state(cp); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Internal cpu startup sequencer 1820Sstevel@tonic-gate * The sequence is as follows: 1830Sstevel@tonic-gate * 1840Sstevel@tonic-gate * MASTER SLAVE 1850Sstevel@tonic-gate * ------- ---------- 1860Sstevel@tonic-gate * assume the kernel data is initialized 1870Sstevel@tonic-gate * clear the proxy bit 1880Sstevel@tonic-gate * start the slave cpu 1890Sstevel@tonic-gate * wait for the slave cpu to set the proxy 1900Sstevel@tonic-gate * 1910Sstevel@tonic-gate * the slave runs slave_startup and then sets the proxy 1920Sstevel@tonic-gate * the slave waits for the master to add slave to the ready set 1930Sstevel@tonic-gate * 1940Sstevel@tonic-gate * the master finishes the initialization and 1950Sstevel@tonic-gate * adds the slave to the ready set 1960Sstevel@tonic-gate * 1970Sstevel@tonic-gate * the slave exits the startup thread and is running 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate void 2000Sstevel@tonic-gate start_cpu(int cpuid, void(*flag_func)(int)) 2010Sstevel@tonic-gate { 2021640Spetede extern void cpu_startup(int); 2030Sstevel@tonic-gate int timout; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * Before we begin the dance, tell DTrace that we're about to start 2090Sstevel@tonic-gate * a CPU. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate if (dtrace_cpustart_init != NULL) 2120Sstevel@tonic-gate (*dtrace_cpustart_init)(); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* start the slave cpu */ 2150Sstevel@tonic-gate CPUSET_DEL(proxy_ready_set, cpuid); 2160Sstevel@tonic-gate if (prom_test("SUNW,start-cpu-by-cpuid") == 0) { 2170Sstevel@tonic-gate (void) prom_startcpu_bycpuid(cpuid, (caddr_t)&cpu_startup, 2180Sstevel@tonic-gate cpuid); 2190Sstevel@tonic-gate } else { 2200Sstevel@tonic-gate /* "by-cpuid" interface didn't exist. Do it the old way */ 221789Sahrens pnode_t nodeid = cpunodes[cpuid].nodeid; 2220Sstevel@tonic-gate 223789Sahrens ASSERT(nodeid != (pnode_t)0); 2240Sstevel@tonic-gate (void) prom_startcpu(nodeid, (caddr_t)&cpu_startup, cpuid); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* wait for the slave cpu to check in. */ 2280Sstevel@tonic-gate for (timout = CPU_WAKEUP_GRACE_MSEC; timout; timout--) { 2290Sstevel@tonic-gate if (CPU_IN_SET(proxy_ready_set, cpuid)) 2300Sstevel@tonic-gate break; 2310Sstevel@tonic-gate DELAY(1000); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate if (timout == 0) { 2340Sstevel@tonic-gate panic("cpu%d failed to start (2)", cpuid); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * The slave has started; we can tell DTrace that it's safe again. 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate if (dtrace_cpustart_fini != NULL) 2410Sstevel@tonic-gate (*dtrace_cpustart_fini)(); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* run the master side of stick synchronization for the slave cpu */ 2440Sstevel@tonic-gate sticksync_master(); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * deal with the cpu flags in a phase-specific manner 2480Sstevel@tonic-gate * for various reasons, this needs to run after the slave 2490Sstevel@tonic-gate * is checked in but before the slave is released. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate (*flag_func)(cpuid); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* release the slave */ 2540Sstevel@tonic-gate CPUSET_ADD(cpu_ready_set, cpuid); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate #ifdef TRAPTRACE 2580Sstevel@tonic-gate int trap_tr0_inuse = 1; /* it is always used on the boot cpu */ 2590Sstevel@tonic-gate int trap_trace_inuse[NCPU]; 2600Sstevel@tonic-gate #endif /* TRAPTRACE */ 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate #define cpu_next_free cpu_prev 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * Routine to set up a CPU to prepare for starting it up. 2660Sstevel@tonic-gate */ 2674050Sjb145095 int 2680Sstevel@tonic-gate setup_cpu_common(int cpuid) 2690Sstevel@tonic-gate { 2700Sstevel@tonic-gate struct cpu *cp = NULL; 2710Sstevel@tonic-gate kthread_id_t tp; 2720Sstevel@tonic-gate #ifdef TRAPTRACE 2730Sstevel@tonic-gate int tt_index; 2740Sstevel@tonic-gate TRAP_TRACE_CTL *ctlp; 2750Sstevel@tonic-gate caddr_t newbuf; 2760Sstevel@tonic-gate #endif /* TRAPTRACE */ 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate extern void idle(); 2794050Sjb145095 int rval; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2820Sstevel@tonic-gate ASSERT(cpu[cpuid] == NULL); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate ASSERT(ncpus <= max_ncpus); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate #ifdef TRAPTRACE 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * allocate a traptrace buffer for this CPU. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate ctlp = &trap_trace_ctl[cpuid]; 2910Sstevel@tonic-gate if (!trap_tr0_inuse) { 2920Sstevel@tonic-gate trap_tr0_inuse = 1; 2930Sstevel@tonic-gate newbuf = trap_tr0; 2940Sstevel@tonic-gate tt_index = -1; 2950Sstevel@tonic-gate } else { 2960Sstevel@tonic-gate for (tt_index = 0; tt_index < (max_ncpus-1); tt_index++) 2970Sstevel@tonic-gate if (!trap_trace_inuse[tt_index]) 2984681Sbholler break; 2990Sstevel@tonic-gate ASSERT(tt_index < max_ncpus - 1); 3000Sstevel@tonic-gate trap_trace_inuse[tt_index] = 1; 3011077Ssvemuri newbuf = (caddr_t)(ttrace_buf + (tt_index * TRAP_TSIZE)); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate ctlp->d.vaddr_base = newbuf; 3040Sstevel@tonic-gate ctlp->d.offset = ctlp->d.last_offset = 0; 3050Sstevel@tonic-gate ctlp->d.limit = trap_trace_bufsize; 3060Sstevel@tonic-gate ctlp->d.paddr_base = va_to_pa(newbuf); 3070Sstevel@tonic-gate ASSERT(ctlp->d.paddr_base != (uint64_t)-1); 3081077Ssvemuri #endif /* TRAPTRACE */ 3090Sstevel@tonic-gate /* 3101077Ssvemuri * initialize hv traptrace buffer for this CPU 3110Sstevel@tonic-gate */ 3121077Ssvemuri mach_htraptrace_setup(cpuid); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * Obtain pointer to the appropriate cpu structure. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate if (cpu0.cpu_flags == 0) { 3180Sstevel@tonic-gate cp = &cpu0; 3190Sstevel@tonic-gate } else { 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * When dynamically allocating cpu structs, 3220Sstevel@tonic-gate * cpus is used as a pointer to a list of freed 3230Sstevel@tonic-gate * cpu structs. 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate if (cpus) { 3260Sstevel@tonic-gate /* grab the first cpu struct on the free list */ 3270Sstevel@tonic-gate cp = cpus; 3280Sstevel@tonic-gate if (cp->cpu_next_free) 3290Sstevel@tonic-gate cpus = cp->cpu_next_free; 3300Sstevel@tonic-gate else 3310Sstevel@tonic-gate cpus = NULL; 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate if (cp == NULL) 3360Sstevel@tonic-gate cp = vmem_xalloc(static_alloc_arena, CPU_ALLOC_SIZE, 3370Sstevel@tonic-gate CPU_ALLOC_SIZE, 0, 0, NULL, NULL, VM_SLEEP); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate bzero(cp, sizeof (*cp)); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate cp->cpu_id = cpuid; 3420Sstevel@tonic-gate cp->cpu_self = cp; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * Initialize ptl1_panic stack 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate ptl1_init_cpu(cp); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Initialize the dispatcher for this CPU. 3510Sstevel@tonic-gate */ 3520Sstevel@tonic-gate disp_cpu_init(cp); 3530Sstevel@tonic-gate 354414Skchow cpu_vm_data_init(cp); 355414Skchow 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * Now, initialize per-CPU idle thread for this CPU. 3580Sstevel@tonic-gate */ 3590Sstevel@tonic-gate tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_ONPROC, -1); 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate cp->cpu_idle_thread = tp; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate tp->t_preempt = 1; 3640Sstevel@tonic-gate tp->t_bound_cpu = cp; 3650Sstevel@tonic-gate tp->t_affinitycnt = 1; 3660Sstevel@tonic-gate tp->t_cpu = cp; 3670Sstevel@tonic-gate tp->t_disp_queue = cp->cpu_disp; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * Registering a thread in the callback table is usually 3710Sstevel@tonic-gate * done in the initialization code of the thread. In this 3720Sstevel@tonic-gate * case, we do it right after thread creation to avoid 3730Sstevel@tonic-gate * blocking idle thread while registering itself. It also 3740Sstevel@tonic-gate * avoids the possibility of reregistration in case a CPU 3750Sstevel@tonic-gate * restarts its idle thread. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate CALLB_CPR_INIT_SAFE(tp, "idle"); 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate init_cpu_info(cp); 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Initialize the interrupt threads for this CPU 3830Sstevel@tonic-gate */ 3841455Sandrei cpu_intr_alloc(cp, NINTR_THREADS); 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* 387220Sesaxe * Add CPU to list of available CPUs. 388220Sesaxe * It'll be on the active list after it is started. 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate cpu_add_unit(cp); 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Allocate and init cpu module private data structures, 3940Sstevel@tonic-gate * including scrubber. 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate cpu_init_private(cp); 3977718SJason.Beloro@Sun.COM populate_idstr(cp); 3980Sstevel@tonic-gate 399220Sesaxe /* 4003434Sesaxe * Initialize the CPUs physical ID cache, and processor groups 401220Sesaxe */ 4023434Sesaxe pghw_physid_create(cp); 4033434Sesaxe pg_cpu_init(cp); 4043434Sesaxe 4054050Sjb145095 if ((rval = cpu_intrq_setup(cp)) != 0) { 4064050Sjb145095 return (rval); 4074050Sjb145095 } 4082241Shuah 4092241Shuah /* 4102241Shuah * Initialize MMU context domain information. 4112241Shuah */ 4122241Shuah sfmmu_cpu_init(cp); 4132241Shuah 4144050Sjb145095 return (0); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Routine to clean up a CPU after shutting it down. 4190Sstevel@tonic-gate */ 4200Sstevel@tonic-gate int 4210Sstevel@tonic-gate cleanup_cpu_common(int cpuid) 4220Sstevel@tonic-gate { 4230Sstevel@tonic-gate struct cpu *cp; 4240Sstevel@tonic-gate #ifdef TRAPTRACE 4250Sstevel@tonic-gate int i; 4260Sstevel@tonic-gate TRAP_TRACE_CTL *ctlp; 4270Sstevel@tonic-gate caddr_t newbuf; 4280Sstevel@tonic-gate #endif /* TRAPTRACE */ 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 4310Sstevel@tonic-gate ASSERT(cpu[cpuid] != NULL); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate cp = cpu[cpuid]; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* Free cpu module private data structures, including scrubber. */ 4360Sstevel@tonic-gate cpu_uninit_private(cp); 4370Sstevel@tonic-gate 4381670Srab /* Free cpu ID string and brand string. */ 4394050Sjb145095 if (cp->cpu_idstr) 4404050Sjb145095 kmem_free(cp->cpu_idstr, strlen(cp->cpu_idstr) + 1); 4414050Sjb145095 if (cp->cpu_brandstr) 4424050Sjb145095 kmem_free(cp->cpu_brandstr, strlen(cp->cpu_brandstr) + 1); 4431670Srab 444414Skchow cpu_vm_data_destroy(cp); 445414Skchow 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Remove CPU from list of available CPUs. 4480Sstevel@tonic-gate */ 4490Sstevel@tonic-gate cpu_del_unit(cpuid); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 4521991Sheppo * Clean any machine specific interrupt states. 4531991Sheppo */ 4541991Sheppo cpu_intrq_cleanup(cp); 4551991Sheppo 4561991Sheppo /* 4570Sstevel@tonic-gate * At this point, the only threads bound to this CPU should be 4580Sstevel@tonic-gate * special per-cpu threads: it's idle thread, it's pause thread, 4590Sstevel@tonic-gate * and it's interrupt threads. Clean these up. 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate cpu_destroy_bound_threads(cp); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * Free the interrupt stack. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate segkp_release(segkp, cp->cpu_intr_stack); 4670Sstevel@tonic-gate 4681077Ssvemuri /* 4691077Ssvemuri * Free hv traptrace buffer for this CPU. 4701077Ssvemuri */ 4711077Ssvemuri mach_htraptrace_cleanup(cpuid); 4720Sstevel@tonic-gate #ifdef TRAPTRACE 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * Free the traptrace buffer for this CPU. 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate ctlp = &trap_trace_ctl[cpuid]; 4770Sstevel@tonic-gate newbuf = ctlp->d.vaddr_base; 4781077Ssvemuri i = (newbuf - ttrace_buf) / (TRAP_TSIZE); 4791077Ssvemuri if (((newbuf - ttrace_buf) % (TRAP_TSIZE) == 0) && 4800Sstevel@tonic-gate ((i >= 0) && (i < (max_ncpus-1)))) { 4810Sstevel@tonic-gate /* 4820Sstevel@tonic-gate * This CPU got it's trap trace buffer from the 4830Sstevel@tonic-gate * boot-alloc'd bunch of them. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate trap_trace_inuse[i] = 0; 4861077Ssvemuri bzero(newbuf, (TRAP_TSIZE)); 4870Sstevel@tonic-gate } else if (newbuf == trap_tr0) { 4880Sstevel@tonic-gate trap_tr0_inuse = 0; 4891077Ssvemuri bzero(trap_tr0, (TRAP_TSIZE)); 4900Sstevel@tonic-gate } else { 4910Sstevel@tonic-gate cmn_err(CE_WARN, "failed to free trap trace buffer from cpu%d", 4920Sstevel@tonic-gate cpuid); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate bzero(ctlp, sizeof (*ctlp)); 4950Sstevel@tonic-gate #endif /* TRAPTRACE */ 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * There is a race condition with mutex_vector_enter() which 4990Sstevel@tonic-gate * caches a cpu pointer. The race is detected by checking cpu_next. 5000Sstevel@tonic-gate */ 5010Sstevel@tonic-gate disp_cpu_fini(cp); 5020Sstevel@tonic-gate cpu_pa[cpuid] = 0; 5034050Sjb145095 if (CPU_MMU_CTXP(cp)) 5044050Sjb145095 sfmmu_cpu_cleanup(cp); 5050Sstevel@tonic-gate bzero(cp, sizeof (*cp)); 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Place the freed cpu structure on the list of freed cpus. 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate if (cp != &cpu0) { 5110Sstevel@tonic-gate if (cpus) { 5120Sstevel@tonic-gate cp->cpu_next_free = cpus; 5130Sstevel@tonic-gate cpus = cp; 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate else 5160Sstevel@tonic-gate cpus = cp; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate return (0); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * This routine is used to start a previously powered off processor. 5240Sstevel@tonic-gate * Note that restarted cpus are initialized into the offline state. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate void 5270Sstevel@tonic-gate restart_other_cpu(int cpuid) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate struct cpu *cp; 5300Sstevel@tonic-gate kthread_id_t tp; 5310Sstevel@tonic-gate caddr_t sp; 5320Sstevel@tonic-gate extern void idle(); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 5350Sstevel@tonic-gate ASSERT(cpuid < NCPU && cpu[cpuid] != NULL); 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * Obtain pointer to the appropriate cpu structure. 5390Sstevel@tonic-gate */ 5400Sstevel@tonic-gate cp = cpu[cpuid]; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate common_startup_init(cp, cpuid); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * idle thread t_lock is held when the idle thread is suspended. 5460Sstevel@tonic-gate * Manually unlock the t_lock of idle loop so that we can resume 5470Sstevel@tonic-gate * the suspended idle thread. 5480Sstevel@tonic-gate * Also adjust the PC of idle thread for re-retry. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate cp->cpu_intr_actv = 0; /* clear the value from previous life */ 5510Sstevel@tonic-gate cp->cpu_m.mutex_ready = 0; /* we are not ready yet */ 5520Sstevel@tonic-gate lock_clear(&cp->cpu_idle_thread->t_lock); 5530Sstevel@tonic-gate tp = cp->cpu_idle_thread; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate sp = tp->t_stk; 5560Sstevel@tonic-gate tp->t_sp = (uintptr_t)((struct rwindow *)sp - 1) - STACK_BIAS; 5570Sstevel@tonic-gate tp->t_pc = (uintptr_t)idle - 8; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * restart the cpu now 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate promsafe_pause_cpus(); 5630Sstevel@tonic-gate start_cpu(cpuid, warm_flag_set); 5640Sstevel@tonic-gate start_cpus(); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate /* call cmn_err outside pause_cpus/start_cpus to avoid deadlock */ 5670Sstevel@tonic-gate cmn_err(CE_CONT, "!cpu%d initialization complete - restarted\n", 5680Sstevel@tonic-gate cpuid); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5720Sstevel@tonic-gate * Startup function executed on 'other' CPUs. This is the first 5730Sstevel@tonic-gate * C function after cpu_start sets up the cpu registers. 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate static void 5760Sstevel@tonic-gate slave_startup(void) 5770Sstevel@tonic-gate { 5780Sstevel@tonic-gate struct cpu *cp = CPU; 5790Sstevel@tonic-gate ushort_t original_flags = cp->cpu_flags; 5800Sstevel@tonic-gate 5811077Ssvemuri mach_htraptrace_configure(cp->cpu_id); 5820Sstevel@tonic-gate cpu_intrq_register(CPU); 5830Sstevel@tonic-gate cp->cpu_m.mutex_ready = 1; 5840Sstevel@tonic-gate cp->cpu_m.poke_cpu_outstanding = B_FALSE; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* acknowledge that we are done with initialization */ 5870Sstevel@tonic-gate CPUSET_ADD(proxy_ready_set, cp->cpu_id); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* synchronize STICK */ 5900Sstevel@tonic-gate sticksync_slave(); 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate if (boothowto & RB_DEBUG) 5930Sstevel@tonic-gate kdi_dvec_cpu_init(cp); 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate /* 5960Sstevel@tonic-gate * the slave will wait here forever -- assuming that the master 5970Sstevel@tonic-gate * will get back to us. if it doesn't we've got bigger problems 5980Sstevel@tonic-gate * than a master not replying to this slave. 5990Sstevel@tonic-gate * the small delay improves the slave's responsiveness to the 6000Sstevel@tonic-gate * master's ack and decreases the time window between master and 6010Sstevel@tonic-gate * slave operations. 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate while (!CPU_IN_SET(cpu_ready_set, cp->cpu_id)) 6040Sstevel@tonic-gate DELAY(1); 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate /* enable interrupts */ 6070Sstevel@tonic-gate (void) spl0(); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * Signature block update to indicate that this CPU is in OS now. 6110Sstevel@tonic-gate * This needs to be done after the PIL is lowered since on 6120Sstevel@tonic-gate * some platforms the update code may block. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cp->cpu_id); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * park the slave thread in a safe/quiet state and wait for the master 6180Sstevel@tonic-gate * to finish configuring this CPU before proceeding to thread_exit(). 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate while (((volatile ushort_t)cp->cpu_flags) & CPU_QUIESCED) 6210Sstevel@tonic-gate DELAY(1); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate /* 6240Sstevel@tonic-gate * Initialize CPC CPU state. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate kcpc_hw_startup_cpu(original_flags); 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate /* 6293434Sesaxe * Notify the PG subsystem that the CPU has started 6300Sstevel@tonic-gate */ 6313434Sesaxe pg_cmt_cpu_startup(CPU); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * Now we are done with the startup thread, so free it up. 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate thread_exit(); 6370Sstevel@tonic-gate cmn_err(CE_PANIC, "slave_startup: cannot return"); 6380Sstevel@tonic-gate /*NOTREACHED*/ 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate extern struct cpu *cpu[NCPU]; /* pointers to all CPUs */ 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate /* 6440Sstevel@tonic-gate * cpu_bringup_set is a tunable (via /etc/system, debugger, etc.) that 6450Sstevel@tonic-gate * can be used during debugging to control which processors are brought 6460Sstevel@tonic-gate * online at boot time. The variable represents a bitmap of the id's 6470Sstevel@tonic-gate * of the processors that will be brought online. The initialization 6480Sstevel@tonic-gate * of this variable depends on the type of cpuset_t, which varies 6490Sstevel@tonic-gate * depending on the number of processors supported (see cpuvar.h). 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate cpuset_t cpu_bringup_set; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /* 6550Sstevel@tonic-gate * Generic start-all cpus entry. Typically used during cold initialization. 6560Sstevel@tonic-gate * Note that cold start cpus are initialized into the online state. 6570Sstevel@tonic-gate */ 6580Sstevel@tonic-gate /*ARGSUSED*/ 6590Sstevel@tonic-gate void 6600Sstevel@tonic-gate start_other_cpus(int flag) 6610Sstevel@tonic-gate { 6620Sstevel@tonic-gate int cpuid; 6630Sstevel@tonic-gate extern void idlestop_init(void); 6640Sstevel@tonic-gate int bootcpu; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Check if cpu_bringup_set has been explicitly set before 6680Sstevel@tonic-gate * initializing it. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate if (CPUSET_ISNULL(cpu_bringup_set)) { 6710Sstevel@tonic-gate #ifdef MPSAS 6720Sstevel@tonic-gate /* just CPU 0 */ 6730Sstevel@tonic-gate CPUSET_ADD(cpu_bringup_set, 0); 6740Sstevel@tonic-gate #else 6750Sstevel@tonic-gate CPUSET_ALL(cpu_bringup_set); 6760Sstevel@tonic-gate #endif 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate if (&cpu_feature_init) 6800Sstevel@tonic-gate cpu_feature_init(); 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Initialize CPC. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate kcpc_hw_init(); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate mutex_enter(&cpu_lock); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Initialize our own cpu_info. 6910Sstevel@tonic-gate */ 6920Sstevel@tonic-gate init_cpu_info(CPU); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Initialize CPU 0 cpu module private data area, including scrubber. 6960Sstevel@tonic-gate */ 6970Sstevel@tonic-gate cpu_init_private(CPU); 6987718SJason.Beloro@Sun.COM populate_idstr(CPU); 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * perform such initialization as is needed 7020Sstevel@tonic-gate * to be able to take CPUs on- and off-line. 7030Sstevel@tonic-gate */ 7040Sstevel@tonic-gate cpu_pause_init(); 7050Sstevel@tonic-gate xc_init(); /* initialize processor crosscalls */ 7060Sstevel@tonic-gate idlestop_init(); 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate if (!use_mp) { 7090Sstevel@tonic-gate mutex_exit(&cpu_lock); 7100Sstevel@tonic-gate cmn_err(CE_CONT, "?***** Not in MP mode\n"); 7110Sstevel@tonic-gate return; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * should we be initializing this cpu? 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate bootcpu = getprocessorid(); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate /* 7190Sstevel@tonic-gate * launch all the slave cpus now 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate for (cpuid = 0; cpuid < NCPU; cpuid++) { 722789Sahrens pnode_t nodeid = cpunodes[cpuid].nodeid; 7230Sstevel@tonic-gate 724789Sahrens if (nodeid == (pnode_t)0) 7250Sstevel@tonic-gate continue; 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate if (cpuid == bootcpu) { 7280Sstevel@tonic-gate if (!CPU_IN_SET(cpu_bringup_set, cpuid)) { 7290Sstevel@tonic-gate cmn_err(CE_WARN, "boot cpu not a member " 7300Sstevel@tonic-gate "of cpu_bringup_set, adding it"); 7310Sstevel@tonic-gate CPUSET_ADD(cpu_bringup_set, cpuid); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate continue; 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate if (!CPU_IN_SET(cpu_bringup_set, cpuid)) 7360Sstevel@tonic-gate continue; 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate ASSERT(cpu[cpuid] == NULL); 7390Sstevel@tonic-gate 7404050Sjb145095 if (setup_cpu_common(cpuid)) { 7414050Sjb145095 cmn_err(CE_PANIC, "cpu%d: setup failed", cpuid); 7424050Sjb145095 } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate common_startup_init(cpu[cpuid], cpuid); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate start_cpu(cpuid, cold_flag_set); 7470Sstevel@tonic-gate /* 7480Sstevel@tonic-gate * Because slave_startup() gets fired off after init() 7490Sstevel@tonic-gate * starts, we can't use the '?' trick to do 'boot -v' 7500Sstevel@tonic-gate * printing - so we always direct the 'cpu .. online' 7510Sstevel@tonic-gate * messages to the log. 7520Sstevel@tonic-gate */ 7530Sstevel@tonic-gate cmn_err(CE_CONT, "!cpu%d initialization complete - online\n", 7540Sstevel@tonic-gate cpuid); 7550Sstevel@tonic-gate 756*8803SJonathan.Haslam@Sun.COM cpu_state_change_notify(cpuid, CPU_SETUP); 757*8803SJonathan.Haslam@Sun.COM 7580Sstevel@tonic-gate if (dtrace_cpu_init != NULL) 7590Sstevel@tonic-gate (*dtrace_cpu_init)(cpuid); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * since all the cpus are online now, redistribute interrupts to them. 7640Sstevel@tonic-gate */ 7650Sstevel@tonic-gate intr_redist_all_cpus(); 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate mutex_exit(&cpu_lock); 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate /* 7700Sstevel@tonic-gate * Start the Ecache scrubber. Must be done after all calls to 7710Sstevel@tonic-gate * cpu_init_private for every cpu (including CPU 0). 7720Sstevel@tonic-gate */ 7730Sstevel@tonic-gate cpu_init_cache_scrub(); 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate if (&cpu_mp_init) 7760Sstevel@tonic-gate cpu_mp_init(); 7770Sstevel@tonic-gate } 778