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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/systm.h> 300Sstevel@tonic-gate #include <sys/membar.h> 310Sstevel@tonic-gate #include <sys/machsystm.h> 320Sstevel@tonic-gate #include <sys/x_call.h> 330Sstevel@tonic-gate #include <sys/platform_module.h> 340Sstevel@tonic-gate #include <sys/cpuvar.h> 350Sstevel@tonic-gate #include <sys/cpu_module.h> 360Sstevel@tonic-gate #include <sys/cmp.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate static cpuset_t cpu_idle_set; 410Sstevel@tonic-gate static kmutex_t cpu_idle_lock; 420Sstevel@tonic-gate typedef const char *fn_t; 430Sstevel@tonic-gate 440Sstevel@tonic-gate /* 450Sstevel@tonic-gate * flags to determine if the PROM routines 460Sstevel@tonic-gate * should be used to idle/resume/stop cpus 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate static int kern_idle[NCPU]; /* kernel's idle loop */ 490Sstevel@tonic-gate static int cpu_are_paused; 500Sstevel@tonic-gate extern void debug_flush_windows(); 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Initialize the idlestop mutex 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate void 560Sstevel@tonic-gate idlestop_init(void) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate mutex_init(&cpu_idle_lock, NULL, MUTEX_SPIN, (void *)ipltospl(PIL_15)); 590Sstevel@tonic-gate } 600Sstevel@tonic-gate 610Sstevel@tonic-gate static void 620Sstevel@tonic-gate cpu_idle_self(void) 630Sstevel@tonic-gate { 640Sstevel@tonic-gate uint_t s; 650Sstevel@tonic-gate label_t save; 660Sstevel@tonic-gate 670Sstevel@tonic-gate s = spl8(); 680Sstevel@tonic-gate debug_flush_windows(); 690Sstevel@tonic-gate 700Sstevel@tonic-gate CPU->cpu_m.in_prom = 1; 710Sstevel@tonic-gate membar_stld(); 720Sstevel@tonic-gate 730Sstevel@tonic-gate save = curthread->t_pcb; 740Sstevel@tonic-gate (void) setjmp(&curthread->t_pcb); 750Sstevel@tonic-gate 760Sstevel@tonic-gate kern_idle[CPU->cpu_id] = 1; 770Sstevel@tonic-gate while (kern_idle[CPU->cpu_id]) 780Sstevel@tonic-gate /* SPIN */; 790Sstevel@tonic-gate 800Sstevel@tonic-gate CPU->cpu_m.in_prom = 0; 810Sstevel@tonic-gate membar_stld(); 820Sstevel@tonic-gate 830Sstevel@tonic-gate curthread->t_pcb = save; 840Sstevel@tonic-gate splx(s); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate void 880Sstevel@tonic-gate idle_other_cpus(void) 890Sstevel@tonic-gate { 900Sstevel@tonic-gate int i, cpuid, ntries; 910Sstevel@tonic-gate int failed = 0; 920Sstevel@tonic-gate 930Sstevel@tonic-gate if (ncpus == 1) 940Sstevel@tonic-gate return; 950Sstevel@tonic-gate 960Sstevel@tonic-gate mutex_enter(&cpu_idle_lock); 970Sstevel@tonic-gate 980Sstevel@tonic-gate cpuid = CPU->cpu_id; 990Sstevel@tonic-gate ASSERT(cpuid < NCPU); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate cpu_idle_set = cpu_ready_set; 1020Sstevel@tonic-gate CPUSET_DEL(cpu_idle_set, cpuid); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate if (CPUSET_ISNULL(cpu_idle_set)) 1050Sstevel@tonic-gate return; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate xt_some(cpu_idle_set, (xcfunc_t *)idle_stop_xcall, 1080Sstevel@tonic-gate (uint64_t)cpu_idle_self, NULL); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 1110Sstevel@tonic-gate if (!CPU_IN_SET(cpu_idle_set, i)) 1120Sstevel@tonic-gate continue; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate ntries = 0x10000; 1150Sstevel@tonic-gate while (!cpu[i]->cpu_m.in_prom && ntries) { 1160Sstevel@tonic-gate DELAY(50); 1170Sstevel@tonic-gate ntries--; 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * A cpu failing to idle is an error condition, since 1220Sstevel@tonic-gate * we can't be sure anymore of its state. 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate if (!cpu[i]->cpu_m.in_prom) { 1250Sstevel@tonic-gate cmn_err(CE_WARN, "cpuid 0x%x failed to idle", i); 1260Sstevel@tonic-gate failed++; 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate if (failed) { 1310Sstevel@tonic-gate mutex_exit(&cpu_idle_lock); 1320Sstevel@tonic-gate cmn_err(CE_PANIC, "idle_other_cpus: not all cpus idled"); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate void 1370Sstevel@tonic-gate resume_other_cpus(void) 1380Sstevel@tonic-gate { 1390Sstevel@tonic-gate int i, ntries; 1400Sstevel@tonic-gate int cpuid = CPU->cpu_id; 1410Sstevel@tonic-gate boolean_t failed = B_FALSE; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate if (ncpus == 1) 1440Sstevel@tonic-gate return; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate ASSERT(cpuid < NCPU); 1470Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_idle_lock)); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 1500Sstevel@tonic-gate if (!CPU_IN_SET(cpu_idle_set, i)) 1510Sstevel@tonic-gate continue; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate kern_idle[i] = 0; 1540Sstevel@tonic-gate membar_stld(); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 1580Sstevel@tonic-gate if (!CPU_IN_SET(cpu_idle_set, i)) 1590Sstevel@tonic-gate continue; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate ntries = 0x10000; 1620Sstevel@tonic-gate while (cpu[i]->cpu_m.in_prom && ntries) { 1630Sstevel@tonic-gate DELAY(50); 1640Sstevel@tonic-gate ntries--; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * A cpu failing to resume is an error condition, since 1690Sstevel@tonic-gate * intrs may have been directed there. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate if (cpu[i]->cpu_m.in_prom) { 1720Sstevel@tonic-gate cmn_err(CE_WARN, "cpuid 0x%x failed to resume", i); 1730Sstevel@tonic-gate continue; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate CPUSET_DEL(cpu_idle_set, i); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate failed = !CPUSET_ISNULL(cpu_idle_set); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate mutex_exit(&cpu_idle_lock); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * Non-zero if a cpu failed to resume 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate if (failed) 1860Sstevel@tonic-gate cmn_err(CE_PANIC, "resume_other_cpus: not all cpus resumed"); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Stop all other cpu's before halting or rebooting. We pause the cpu's 1920Sstevel@tonic-gate * instead of sending a cross call. 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate void 1950Sstevel@tonic-gate stop_other_cpus(void) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate mutex_enter(&cpu_lock); 1980Sstevel@tonic-gate if (cpu_are_paused) { 1990Sstevel@tonic-gate mutex_exit(&cpu_lock); 2000Sstevel@tonic-gate return; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (ncpus > 1) 2040Sstevel@tonic-gate intr_redist_all_cpus_shutdown(); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate pause_cpus(NULL); 2070Sstevel@tonic-gate cpu_are_paused = 1; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate mutex_exit(&cpu_lock); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate int cpu_quiesce_microsecond_sanity_limit = 60 * 1000000; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate void 2150Sstevel@tonic-gate mp_cpu_quiesce(cpu_t *cp0) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate volatile cpu_t *cp = (volatile cpu_t *) cp0; 2190Sstevel@tonic-gate int i, sanity_limit = cpu_quiesce_microsecond_sanity_limit; 2200Sstevel@tonic-gate int cpuid = cp->cpu_id; 2210Sstevel@tonic-gate int found_intr = 1; 2220Sstevel@tonic-gate static fn_t f = "mp_cpu_quiesce"; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate ASSERT(CPU->cpu_id != cpuid); 2250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2260Sstevel@tonic-gate ASSERT(cp->cpu_flags & CPU_QUIESCED); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Declare CPU as no longer being READY to process interrupts and 2310Sstevel@tonic-gate * wait for them to stop. A CPU that is not READY can no longer 2320Sstevel@tonic-gate * participate in x-calls or x-traps. 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate cp->cpu_flags &= ~CPU_READY; 2350Sstevel@tonic-gate CPUSET_DEL(cpu_ready_set, cpuid); 2360Sstevel@tonic-gate membar_sync(); 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate for (i = 0; i < sanity_limit; i++) { 2390Sstevel@tonic-gate if (cp->cpu_intr_actv == 0 && 240*834Sandrei (cp->cpu_thread == cp->cpu_idle_thread || 241*834Sandrei cp->cpu_thread == cp->cpu_startup_thread)) { 2420Sstevel@tonic-gate found_intr = 0; 2430Sstevel@tonic-gate break; 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate DELAY(1); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (found_intr) { 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate if (cp->cpu_intr_actv) { 2510Sstevel@tonic-gate cmn_err(CE_PANIC, "%s: cpu_intr_actv != 0", f); 252*834Sandrei } else if (cp->cpu_thread != cp->cpu_idle_thread && 253*834Sandrei cp->cpu_thread != cp->cpu_startup_thread) { 254*834Sandrei cmn_err(CE_PANIC, "%s: CPU %d is not quiesced", 255*834Sandrei f, cpuid); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* 2620Sstevel@tonic-gate * Start CPU on user request. 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate /* ARGSUSED */ 2650Sstevel@tonic-gate int 2660Sstevel@tonic-gate mp_cpu_start(struct cpu *cp) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * Platforms that use CPU signatures require the signature 2710Sstevel@tonic-gate * block update to indicate that this CPU is in the OS now. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cp->cpu_id); 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate cmp_error_resteer(cp->cpu_id); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate return (0); /* nothing special to do on this arch */ 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * Stop CPU on user request. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate /* ARGSUSED */ 2840Sstevel@tonic-gate int 2850Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp) 2860Sstevel@tonic-gate { 2870Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate cmp_error_resteer(cp->cpu_id); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * Platforms that use CPU signatures require the signature 2930Sstevel@tonic-gate * block update to indicate that this CPU is offlined now. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate CPU_SIGNATURE(OS_SIG, SIGST_OFFLINE, SIGSUBST_NULL, cp->cpu_id); 2960Sstevel@tonic-gate return (0); /* nothing special to do on this arch */ 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * Power on CPU. 3010Sstevel@tonic-gate */ 3020Sstevel@tonic-gate int 3030Sstevel@tonic-gate mp_cpu_poweron(struct cpu *cp) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 3060Sstevel@tonic-gate if (&plat_cpu_poweron) 3070Sstevel@tonic-gate return (plat_cpu_poweron(cp)); /* platform-dependent hook */ 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate return (ENOTSUP); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Power off CPU. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate int 3160Sstevel@tonic-gate mp_cpu_poweroff(struct cpu *cp) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 3190Sstevel@tonic-gate if (&plat_cpu_poweroff) 3200Sstevel@tonic-gate return (plat_cpu_poweroff(cp)); /* platform-dependent hook */ 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate return (ENOTSUP); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate void 3260Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate cpu_faulted_enter(cp); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate void 3320Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate cpu_faulted_exit(cp); 3350Sstevel@tonic-gate } 336