xref: /onnv-gate/usr/src/uts/i86pc/os/mp_machdep.c (revision 11389:dd00b884e84f)
1*11389SAlexander.Kolbasov@Sun.COM 
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate  * CDDL HEADER START
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
62006Sandrei  * Common Development and Distribution License (the "License").
72006Sandrei  * You may not use this file except in compliance 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 /*
238675SVikram.Hegde@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
269652SMichael.Corcoran@Sun.COM /*
279652SMichael.Corcoran@Sun.COM  * Copyright (c) 2009, Intel Corporation.
289652SMichael.Corcoran@Sun.COM  * All rights reserved.
299652SMichael.Corcoran@Sun.COM  */
300Sstevel@tonic-gate 
315295Srandyf #define	PSMI_1_6
320Sstevel@tonic-gate #include <sys/smp_impldefs.h>
330Sstevel@tonic-gate #include <sys/psm.h>
340Sstevel@tonic-gate #include <sys/psm_modctl.h>
350Sstevel@tonic-gate #include <sys/pit.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/strlog.h>
380Sstevel@tonic-gate #include <sys/clock.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/rtc.h>
410Sstevel@tonic-gate #include <sys/x86_archext.h>
420Sstevel@tonic-gate #include <sys/cpupart.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
449637SRandy.Fishel@Sun.COM #include <sys/cpu_event.h>
454606Sesaxe #include <sys/cmt.h>
464481Sbholler #include <sys/cpu.h>
470Sstevel@tonic-gate #include <sys/disp.h>
480Sstevel@tonic-gate #include <sys/archsystm.h>
493446Smrj #include <sys/machsystm.h>
504481Sbholler #include <sys/sysmacros.h>
515084Sjohnlev #include <sys/memlist.h>
523446Smrj #include <sys/param.h>
533446Smrj #include <sys/promif.h>
548906SEric.Saxe@Sun.COM #include <sys/cpu_pm.h>
555084Sjohnlev #if defined(__xpv)
565084Sjohnlev #include <sys/hypervisor.h>
575084Sjohnlev #endif
58916Sschwartz #include <sys/mach_intr.h>
594481Sbholler #include <vm/hat_i86.h>
604652Scwb #include <sys/kdi_machimpl.h>
615864Sesaxe #include <sys/sdt.h>
628906SEric.Saxe@Sun.COM #include <sys/hpet.h>
639652SMichael.Corcoran@Sun.COM #include <sys/sunddi.h>
649652SMichael.Corcoran@Sun.COM #include <sys/sunndi.h>
65*11389SAlexander.Kolbasov@Sun.COM #include <sys/cpc_pcbe.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  *	Local function prototypes
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
730Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
740Sstevel@tonic-gate static void mach_init();
750Sstevel@tonic-gate static void mach_picinit();
760Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
770Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
780Sstevel@tonic-gate static void mach_fixcpufreq(void);
790Sstevel@tonic-gate static int mach_clkinit(int, int *);
800Sstevel@tonic-gate static void mach_smpinit(void);
810Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl);
820Sstevel@tonic-gate static void mach_get_platform(int owner);
830Sstevel@tonic-gate static void mach_construct_info();
840Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno);
850Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
860Sstevel@tonic-gate     psm_intr_op_t, int *);
870Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg);
880Sstevel@tonic-gate static hrtime_t dummy_hrtime(void);
890Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *);
9010797SEric.Saxe@Sun.COM static uint64_t dummy_unscalehrtime(hrtime_t);
918906SEric.Saxe@Sun.COM void cpu_idle(void);
920Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int);
935084Sjohnlev #ifndef __xpv
948906SEric.Saxe@Sun.COM void cpu_idle_mwait(void);
954481Sbholler static void cpu_wakeup_mwait(cpu_t *, int);
965084Sjohnlev #endif
979652SMichael.Corcoran@Sun.COM static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp);
989652SMichael.Corcoran@Sun.COM 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  *	External reference functions
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate extern void return_instr();
1030Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *);
1040Sstevel@tonic-gate #if defined(__i386)
1050Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *);
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *);
1083434Sesaxe extern int cpuid_get_coreid(cpu_t *);
1093434Sesaxe extern int cpuid_get_chipid(cpu_t *);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  *	PSM functions initialization
1130Sstevel@tonic-gate  */
1143446Smrj void (*psm_shutdownf)(int, int)	= (void (*)(int, int))return_instr;
1153446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
1163446Smrj void (*psm_notifyf)(int)	= (void (*)(int))return_instr;
1173446Smrj void (*psm_set_idle_cpuf)(int)	= (void (*)(int))return_instr;
1183446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
1190Sstevel@tonic-gate void (*psminitf)()		= mach_init;
1200Sstevel@tonic-gate void (*picinitf)() 		= return_instr;
1210Sstevel@tonic-gate int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
1220Sstevel@tonic-gate int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
1230Sstevel@tonic-gate void (*send_dirintf)() 		= return_instr;
1243446Smrj void (*setspl)(int)		= (void (*)(int))return_instr;
1250Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1260Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1274652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)=
1284652Scwb 	(void (*)(int, struct av_softinfo *))return_instr;
129999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
130999Slq150181 	(void (*)(int, struct av_softinfo *))return_instr;
1310Sstevel@tonic-gate int (*slvltovect)(int)		= (int (*)(int))return_instr;
1320Sstevel@tonic-gate int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
1330Sstevel@tonic-gate void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
1340Sstevel@tonic-gate int (*psm_disable_intr)(int)	= mp_disable_intr;
1350Sstevel@tonic-gate void (*psm_enable_intr)(int)	= mp_enable_intr;
1360Sstevel@tonic-gate hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
1370Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
1380Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
13910797SEric.Saxe@Sun.COM uint64_t (*unscalehrtimef)(hrtime_t)	= dummy_unscalehrtime;
1400Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1410Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1420Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1430Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1440Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1470Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1480Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1490Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1500Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1510Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1520Sstevel@tonic-gate     int *) = mach_intr_ops;
1535295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
1545295Srandyf     return_instr;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1570Sstevel@tonic-gate void (*hrtime_tick)(void)	= return_instr;
1580Sstevel@tonic-gate 
1599652SMichael.Corcoran@Sun.COM int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo;
1609652SMichael.Corcoran@Sun.COM 
1615084Sjohnlev /*
1625084Sjohnlev  * True if the generic TSC code is our source of hrtime, rather than whatever
1635084Sjohnlev  * the PSM can provide.
1645084Sjohnlev  */
1655084Sjohnlev #ifdef __xpv
1665084Sjohnlev int tsc_gethrtime_enable = 0;
1675084Sjohnlev #else
1680Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1695084Sjohnlev #endif
1700Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1735084Sjohnlev  * True if the hrtime implementation is "hires"; namely, better than microdata.
1745084Sjohnlev  */
1755084Sjohnlev int gethrtime_hires = 0;
1765084Sjohnlev 
1775084Sjohnlev /*
1780Sstevel@tonic-gate  * Local Static Data
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate static struct psm_ops mach_ops;
1810Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1820Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1858675SVikram.Hegde@Sun.COM  * virtualization support for psm
1868675SVikram.Hegde@Sun.COM  */
1878675SVikram.Hegde@Sun.COM void *psm_vt_ops = NULL;
1888675SVikram.Hegde@Sun.COM /*
1893446Smrj  * If non-zero, idle cpus will become "halted" when there's
1900Sstevel@tonic-gate  * no work to do.
1910Sstevel@tonic-gate  */
1923446Smrj int	idle_cpu_use_hlt = 1;
1930Sstevel@tonic-gate 
1945084Sjohnlev #ifndef __xpv
1954481Sbholler /*
1964481Sbholler  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
1974481Sbholler  */
1984481Sbholler int	idle_cpu_prefer_mwait = 1;
1997716SBill.Holler@Sun.COM /*
2007716SBill.Holler@Sun.COM  * Set to 0 to avoid MONITOR+CLFLUSH assertion.
2017716SBill.Holler@Sun.COM  */
2027716SBill.Holler@Sun.COM int	idle_cpu_assert_cflush_monitor = 1;
2037716SBill.Holler@Sun.COM 
2048906SEric.Saxe@Sun.COM /*
2058906SEric.Saxe@Sun.COM  * If non-zero, idle cpus will not use power saving Deep C-States idle loop.
2068906SEric.Saxe@Sun.COM  */
2078906SEric.Saxe@Sun.COM int	idle_cpu_no_deep_c = 0;
2088906SEric.Saxe@Sun.COM /*
2098906SEric.Saxe@Sun.COM  * Non-power saving idle loop and wakeup pointers.
2108906SEric.Saxe@Sun.COM  * Allows user to toggle Deep Idle power saving feature on/off.
2118906SEric.Saxe@Sun.COM  */
2128906SEric.Saxe@Sun.COM void	(*non_deep_idle_cpu)() = cpu_idle;
2138906SEric.Saxe@Sun.COM void	(*non_deep_idle_disp_enq_thread)(cpu_t *, int);
2148906SEric.Saxe@Sun.COM 
2158906SEric.Saxe@Sun.COM /*
2168906SEric.Saxe@Sun.COM  * Object for the kernel to access the HPET.
2178906SEric.Saxe@Sun.COM  */
2188906SEric.Saxe@Sun.COM hpet_t hpet;
2198906SEric.Saxe@Sun.COM 
2208906SEric.Saxe@Sun.COM #endif	/* ifndef __xpv */
2213434Sesaxe 
2223434Sesaxe /*ARGSUSED*/
2233434Sesaxe int
2243434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
2253434Sesaxe {
2263434Sesaxe 	switch (hw) {
2273434Sesaxe 	case PGHW_IPIPE:
2283434Sesaxe 		if (x86_feature & (X86_HTT)) {
2293434Sesaxe 			/*
2303434Sesaxe 			 * Hyper-threading is SMT
2313434Sesaxe 			 */
2323434Sesaxe 			return (1);
2333434Sesaxe 		} else {
2343434Sesaxe 			return (0);
2353434Sesaxe 		}
23610947SSrihari.Venkatesan@Sun.COM 	case PGHW_PROCNODE:
23710947SSrihari.Venkatesan@Sun.COM 		if (cpuid_get_procnodes_per_pkg(cp) > 1)
23810947SSrihari.Venkatesan@Sun.COM 			return (1);
23910947SSrihari.Venkatesan@Sun.COM 		else
24010947SSrihari.Venkatesan@Sun.COM 			return (0);
2413434Sesaxe 	case PGHW_CHIP:
2423434Sesaxe 		if (x86_feature & (X86_CMP|X86_HTT))
2433434Sesaxe 			return (1);
2443434Sesaxe 		else
2453434Sesaxe 			return (0);
2464606Sesaxe 	case PGHW_CACHE:
2474606Sesaxe 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
2484606Sesaxe 			return (1);
2494606Sesaxe 		else
2504606Sesaxe 			return (0);
2518906SEric.Saxe@Sun.COM 	case PGHW_POW_ACTIVE:
2528906SEric.Saxe@Sun.COM 		if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1)
2538906SEric.Saxe@Sun.COM 			return (1);
2548906SEric.Saxe@Sun.COM 		else
2558906SEric.Saxe@Sun.COM 			return (0);
2568906SEric.Saxe@Sun.COM 	case PGHW_POW_IDLE:
2578906SEric.Saxe@Sun.COM 		if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1)
2588906SEric.Saxe@Sun.COM 			return (1);
2598906SEric.Saxe@Sun.COM 		else
2608906SEric.Saxe@Sun.COM 			return (0);
2613434Sesaxe 	default:
2623434Sesaxe 		return (0);
2633434Sesaxe 	}
2643434Sesaxe }
2653434Sesaxe 
2663434Sesaxe /*
2673434Sesaxe  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
2683434Sesaxe  * If pghw_type_t is an unsupported hardware type, then return -1
2693434Sesaxe  */
2703434Sesaxe int
2713434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
2720Sstevel@tonic-gate {
2733434Sesaxe 	id_t pgp_a, pgp_b;
2743434Sesaxe 
2753434Sesaxe 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
2763434Sesaxe 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
2773434Sesaxe 
2783434Sesaxe 	if (pgp_a == -1 || pgp_b == -1)
2793434Sesaxe 		return (-1);
2803434Sesaxe 
2813434Sesaxe 	return (pgp_a == pgp_b);
2823434Sesaxe }
2833434Sesaxe 
2843434Sesaxe /*
2853434Sesaxe  * Return a physical instance identifier for known hardware sharing
2863434Sesaxe  * relationships
2873434Sesaxe  */
2883434Sesaxe id_t
2893434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
2903434Sesaxe {
2913434Sesaxe 	switch (hw) {
2923434Sesaxe 	case PGHW_IPIPE:
2933434Sesaxe 		return (cpuid_get_coreid(cpu));
2944606Sesaxe 	case PGHW_CACHE:
2954606Sesaxe 		return (cpuid_get_last_lvl_cacheid(cpu));
29610947SSrihari.Venkatesan@Sun.COM 	case PGHW_PROCNODE:
29710947SSrihari.Venkatesan@Sun.COM 		return (cpuid_get_procnodeid(cpu));
2983434Sesaxe 	case PGHW_CHIP:
2993434Sesaxe 		return (cpuid_get_chipid(cpu));
3008906SEric.Saxe@Sun.COM 	case PGHW_POW_ACTIVE:
3018906SEric.Saxe@Sun.COM 		return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE));
3028906SEric.Saxe@Sun.COM 	case PGHW_POW_IDLE:
3038906SEric.Saxe@Sun.COM 		return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE));
3043434Sesaxe 	default:
3053434Sesaxe 		return (-1);
3061228Sandrei 	}
3073434Sesaxe }
3080Sstevel@tonic-gate 
3098906SEric.Saxe@Sun.COM /*
3108906SEric.Saxe@Sun.COM  * Express preference for optimizing for sharing relationship
3118906SEric.Saxe@Sun.COM  * hw1 vs hw2
3128906SEric.Saxe@Sun.COM  */
3138906SEric.Saxe@Sun.COM pghw_type_t
3148906SEric.Saxe@Sun.COM pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2)
3153434Sesaxe {
3168906SEric.Saxe@Sun.COM 	int i, rank1, rank2;
3178906SEric.Saxe@Sun.COM 
3183434Sesaxe 	static pghw_type_t hw_hier[] = {
3193434Sesaxe 		PGHW_IPIPE,
3204606Sesaxe 		PGHW_CACHE,
32110947SSrihari.Venkatesan@Sun.COM 		PGHW_PROCNODE,
3223434Sesaxe 		PGHW_CHIP,
3238906SEric.Saxe@Sun.COM 		PGHW_POW_IDLE,
3248906SEric.Saxe@Sun.COM 		PGHW_POW_ACTIVE,
3253434Sesaxe 		PGHW_NUM_COMPONENTS
3263434Sesaxe 	};
3273434Sesaxe 
3283434Sesaxe 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
3298906SEric.Saxe@Sun.COM 		if (hw_hier[i] == hw1)
3308906SEric.Saxe@Sun.COM 			rank1 = i;
3318906SEric.Saxe@Sun.COM 		if (hw_hier[i] == hw2)
3328906SEric.Saxe@Sun.COM 			rank2 = i;
3333434Sesaxe 	}
3348906SEric.Saxe@Sun.COM 
3358906SEric.Saxe@Sun.COM 	if (rank1 > rank2)
3368906SEric.Saxe@Sun.COM 		return (hw1);
3378906SEric.Saxe@Sun.COM 	else
3388906SEric.Saxe@Sun.COM 		return (hw2);
3393434Sesaxe }
3403434Sesaxe 
3414606Sesaxe /*
3428906SEric.Saxe@Sun.COM  * Override the default CMT dispatcher policy for the specified
3438906SEric.Saxe@Sun.COM  * hardware sharing relationship
3444606Sesaxe  */
3458906SEric.Saxe@Sun.COM pg_cmt_policy_t
3468906SEric.Saxe@Sun.COM pg_plat_cmt_policy(pghw_type_t hw)
3474606Sesaxe {
3488906SEric.Saxe@Sun.COM 	/*
3498906SEric.Saxe@Sun.COM 	 * For shared caches, also load balance across them to
3508906SEric.Saxe@Sun.COM 	 * maximize aggregate cache capacity
3518906SEric.Saxe@Sun.COM 	 */
3528906SEric.Saxe@Sun.COM 	switch (hw) {
3538906SEric.Saxe@Sun.COM 	case PGHW_CACHE:
3548906SEric.Saxe@Sun.COM 		return (CMT_BALANCE|CMT_AFFINITY);
3558906SEric.Saxe@Sun.COM 	default:
3568906SEric.Saxe@Sun.COM 		return (CMT_NO_POLICY);
3578906SEric.Saxe@Sun.COM 	}
3584606Sesaxe }
3594606Sesaxe 
3603434Sesaxe id_t
3613434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
3623434Sesaxe {
3633434Sesaxe 	return ((id_t)cpuid_get_coreid(cpu));
3643434Sesaxe }
3653434Sesaxe 
3663434Sesaxe void
3673434Sesaxe cmp_set_nosteal_interval(void)
3683434Sesaxe {
3693434Sesaxe 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
3703434Sesaxe 	nosteal_nsec = 100000UL;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*
3740Sstevel@tonic-gate  * Routine to ensure initial callers to hrtime gets 0 as return
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate static hrtime_t
3770Sstevel@tonic-gate dummy_hrtime(void)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /* ARGSUSED */
3830Sstevel@tonic-gate static void
3840Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
3850Sstevel@tonic-gate {}
3860Sstevel@tonic-gate 
38710797SEric.Saxe@Sun.COM static uint64_t
38810797SEric.Saxe@Sun.COM dummy_unscalehrtime(hrtime_t nsecs)
38910797SEric.Saxe@Sun.COM {
39010797SEric.Saxe@Sun.COM 	return ((uint64_t)nsecs);
39110797SEric.Saxe@Sun.COM }
39210797SEric.Saxe@Sun.COM 
3930Sstevel@tonic-gate /*
3948906SEric.Saxe@Sun.COM  * Supports Deep C-State power saving idle loop.
3958906SEric.Saxe@Sun.COM  */
3968906SEric.Saxe@Sun.COM void
3978906SEric.Saxe@Sun.COM cpu_idle_adaptive(void)
3988906SEric.Saxe@Sun.COM {
3998906SEric.Saxe@Sun.COM 	(*CPU->cpu_m.mcpu_idle_cpu)();
4008906SEric.Saxe@Sun.COM }
4018906SEric.Saxe@Sun.COM 
4029637SRandy.Fishel@Sun.COM /*
4039637SRandy.Fishel@Sun.COM  * Function called by CPU idle notification framework to check whether CPU
4049637SRandy.Fishel@Sun.COM  * has been awakened. It will be called with interrupt disabled.
4059637SRandy.Fishel@Sun.COM  * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
4069637SRandy.Fishel@Sun.COM  * notification framework.
4079637SRandy.Fishel@Sun.COM  */
4089637SRandy.Fishel@Sun.COM /*ARGSUSED*/
4099637SRandy.Fishel@Sun.COM static void
4109637SRandy.Fishel@Sun.COM cpu_idle_check_wakeup(void *arg)
4118906SEric.Saxe@Sun.COM {
4129637SRandy.Fishel@Sun.COM 	/*
4139637SRandy.Fishel@Sun.COM 	 * Toggle interrupt flag to detect pending interrupts.
4149637SRandy.Fishel@Sun.COM 	 * If interrupt happened, do_interrupt() will notify CPU idle
4159637SRandy.Fishel@Sun.COM 	 * notification framework so no need to call cpu_idle_exit() here.
4169637SRandy.Fishel@Sun.COM 	 */
4179637SRandy.Fishel@Sun.COM 	sti();
4189637SRandy.Fishel@Sun.COM 	SMT_PAUSE();
4199637SRandy.Fishel@Sun.COM 	cli();
4208906SEric.Saxe@Sun.COM }
4218906SEric.Saxe@Sun.COM 
4228906SEric.Saxe@Sun.COM /*
4239637SRandy.Fishel@Sun.COM  * Idle the present CPU until wakened via an interrupt
4240Sstevel@tonic-gate  */
4258906SEric.Saxe@Sun.COM void
4263446Smrj cpu_idle(void)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	cpu_t		*cpup = CPU;
4298408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid = cpup->cpu_seqid;
430711Sesaxe 	cpupart_t	*cp = cpup->cpu_part;
4310Sstevel@tonic-gate 	int		hset_update = 1;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/*
4340Sstevel@tonic-gate 	 * If this CPU is online, and there's multiple CPUs
4350Sstevel@tonic-gate 	 * in the system, then we should notate our halting
4360Sstevel@tonic-gate 	 * by adding ourselves to the partition's halted CPU
4370Sstevel@tonic-gate 	 * bitmap. This allows other CPUs to find/awaken us when
4380Sstevel@tonic-gate 	 * work becomes available.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
4410Sstevel@tonic-gate 		hset_update = 0;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4448408SEric.Saxe@Sun.COM 	 * Add ourselves to the partition's halted CPUs bitmap
4450Sstevel@tonic-gate 	 * and set our HALTED flag, if necessary.
4460Sstevel@tonic-gate 	 *
447711Sesaxe 	 * When a thread becomes runnable, it is placed on the queue
4488408SEric.Saxe@Sun.COM 	 * and then the halted CPU bitmap is checked to determine who
4499637SRandy.Fishel@Sun.COM 	 * (if anyone) should be awakened. We therefore need to first
4508408SEric.Saxe@Sun.COM 	 * add ourselves to the bitmap, and and then check if there
4518408SEric.Saxe@Sun.COM 	 * is any work available. The order is important to prevent a race
4528408SEric.Saxe@Sun.COM 	 * that can lead to work languishing on a run queue somewhere while
4538408SEric.Saxe@Sun.COM 	 * this CPU remains halted.
4548408SEric.Saxe@Sun.COM 	 *
4558408SEric.Saxe@Sun.COM 	 * Either the producing CPU will see we're halted and will awaken us,
4568408SEric.Saxe@Sun.COM 	 * or this CPU will see the work available in disp_anywork().
457711Sesaxe 	 *
4580Sstevel@tonic-gate 	 * Note that memory barriers after updating the HALTED flag
4598408SEric.Saxe@Sun.COM 	 * are not necessary since an atomic operation (updating the bitset)
4600Sstevel@tonic-gate 	 * immediately follows. On x86 the atomic operation acts as a
4610Sstevel@tonic-gate 	 * memory barrier for the update of cpu_disp_flags.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	if (hset_update) {
4640Sstevel@tonic-gate 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
4658408SEric.Saxe@Sun.COM 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	/*
4690Sstevel@tonic-gate 	 * Check to make sure there's really nothing to do.
470711Sesaxe 	 * Work destined for this CPU may become available after
471711Sesaxe 	 * this check. We'll be notified through the clearing of our
4728408SEric.Saxe@Sun.COM 	 * bit in the halted CPU bitmap, and a poke.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	if (disp_anywork()) {
4750Sstevel@tonic-gate 		if (hset_update) {
4760Sstevel@tonic-gate 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4778408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
4780Sstevel@tonic-gate 		}
479711Sesaxe 		return;
480711Sesaxe 	}
481711Sesaxe 
482711Sesaxe 	/*
483711Sesaxe 	 * We're on our way to being halted.
484711Sesaxe 	 *
485711Sesaxe 	 * Disable interrupts now, so that we'll awaken immediately
486711Sesaxe 	 * after halting if someone tries to poke us between now and
487711Sesaxe 	 * the time we actually halt.
488711Sesaxe 	 *
489711Sesaxe 	 * We check for the presence of our bit after disabling interrupts.
490711Sesaxe 	 * If it's cleared, we'll return. If the bit is cleared after
491711Sesaxe 	 * we check then the poke will pop us out of the halted state.
492711Sesaxe 	 *
493711Sesaxe 	 * This means that the ordering of the poke and the clearing
494711Sesaxe 	 * of the bit by cpu_wakeup is important.
495711Sesaxe 	 * cpu_wakeup() must clear, then poke.
4963446Smrj 	 * cpu_idle() must disable interrupts, then check for the bit.
497711Sesaxe 	 */
498711Sesaxe 	cli();
499711Sesaxe 
5008408SEric.Saxe@Sun.COM 	if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) {
501711Sesaxe 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
502711Sesaxe 		sti();
503711Sesaxe 		return;
504711Sesaxe 	}
505711Sesaxe 
506711Sesaxe 	/*
507711Sesaxe 	 * The check for anything locally runnable is here for performance
508711Sesaxe 	 * and isn't needed for correctness. disp_nrunnable ought to be
509711Sesaxe 	 * in our cache still, so it's inexpensive to check, and if there
510711Sesaxe 	 * is anything runnable we won't have to wait for the poke.
511711Sesaxe 	 */
512711Sesaxe 	if (cpup->cpu_disp->disp_nrunnable != 0) {
513711Sesaxe 		if (hset_update) {
514711Sesaxe 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5158408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
516711Sesaxe 		}
5170Sstevel@tonic-gate 		sti();
5180Sstevel@tonic-gate 		return;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5219637SRandy.Fishel@Sun.COM 	if (cpu_idle_enter(IDLE_STATE_C1, 0,
5229637SRandy.Fishel@Sun.COM 	    cpu_idle_check_wakeup, NULL) == 0) {
5239637SRandy.Fishel@Sun.COM 		mach_cpu_idle();
5249637SRandy.Fishel@Sun.COM 		cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
5259637SRandy.Fishel@Sun.COM 	}
5267006Srv207048 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * We're no longer halted
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	if (hset_update) {
5310Sstevel@tonic-gate 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5328408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
5390Sstevel@tonic-gate  * Otherwise, see if other CPUs in the cpu partition are halted and need to
5400Sstevel@tonic-gate  * be woken up so that they can steal the thread we placed on this CPU.
5410Sstevel@tonic-gate  * This function is only used on MP systems.
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate static void
5440Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	uint_t		cpu_found;
5478408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid;
5480Sstevel@tonic-gate 	cpupart_t	*cp;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	cp = cpu->cpu_part;
5518408SEric.Saxe@Sun.COM 	cpu_sid = cpu->cpu_seqid;
5528408SEric.Saxe@Sun.COM 	if (bitset_in_set(&cp->cp_haltset, cpu_sid)) {
5530Sstevel@tonic-gate 		/*
5540Sstevel@tonic-gate 		 * Clear the halted bit for that CPU since it will be
5550Sstevel@tonic-gate 		 * poked in a moment.
5560Sstevel@tonic-gate 		 */
5578408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5580Sstevel@tonic-gate 		/*
5590Sstevel@tonic-gate 		 * We may find the current CPU present in the halted cpuset
5600Sstevel@tonic-gate 		 * if we're in the context of an interrupt that occurred
5613446Smrj 		 * before we had a chance to clear our bit in cpu_idle().
5620Sstevel@tonic-gate 		 * Poking ourself is obviously unnecessary, since if
5630Sstevel@tonic-gate 		 * we're here, we're not halted.
5640Sstevel@tonic-gate 		 */
5650Sstevel@tonic-gate 		if (cpu != CPU)
5660Sstevel@tonic-gate 			poke_cpu(cpu->cpu_id);
5670Sstevel@tonic-gate 		return;
5680Sstevel@tonic-gate 	} else {
5690Sstevel@tonic-gate 		/*
5700Sstevel@tonic-gate 		 * This cpu isn't halted, but it's idle or undergoing a
5710Sstevel@tonic-gate 		 * context switch. No need to awaken anyone else.
5720Sstevel@tonic-gate 		 */
5730Sstevel@tonic-gate 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
5740Sstevel@tonic-gate 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
5750Sstevel@tonic-gate 			return;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5798408SEric.Saxe@Sun.COM 	 * No need to wake up other CPUs if this is for a bound thread.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	if (bound)
5820Sstevel@tonic-gate 		return;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*
5858408SEric.Saxe@Sun.COM 	 * The CPU specified for wakeup isn't currently halted, so check
5868408SEric.Saxe@Sun.COM 	 * to see if there are any other halted CPUs in the partition,
5878408SEric.Saxe@Sun.COM 	 * and if there are then awaken one.
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	do {
5908408SEric.Saxe@Sun.COM 		cpu_found = bitset_find(&cp->cp_haltset);
5918408SEric.Saxe@Sun.COM 		if (cpu_found == (uint_t)-1)
5920Sstevel@tonic-gate 			return;
5938408SEric.Saxe@Sun.COM 	} while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0);
5940Sstevel@tonic-gate 
5958408SEric.Saxe@Sun.COM 	if (cpu_found != CPU->cpu_seqid) {
5968408SEric.Saxe@Sun.COM 		poke_cpu(cpu_seq[cpu_found]->cpu_id);
5978408SEric.Saxe@Sun.COM 	}
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6005084Sjohnlev #ifndef __xpv
6014481Sbholler /*
6029637SRandy.Fishel@Sun.COM  * Function called by CPU idle notification framework to check whether CPU
6039637SRandy.Fishel@Sun.COM  * has been awakened. It will be called with interrupt disabled.
6049637SRandy.Fishel@Sun.COM  * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
6059637SRandy.Fishel@Sun.COM  * notification framework.
6069637SRandy.Fishel@Sun.COM  */
6079637SRandy.Fishel@Sun.COM static void
6089637SRandy.Fishel@Sun.COM cpu_idle_mwait_check_wakeup(void *arg)
6099637SRandy.Fishel@Sun.COM {
6109637SRandy.Fishel@Sun.COM 	volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg;
6119637SRandy.Fishel@Sun.COM 
6129637SRandy.Fishel@Sun.COM 	ASSERT(arg != NULL);
6139637SRandy.Fishel@Sun.COM 	if (*mcpu_mwait != MWAIT_HALTED) {
6149637SRandy.Fishel@Sun.COM 		/*
6159637SRandy.Fishel@Sun.COM 		 * CPU has been awakened, notify CPU idle notification system.
6169637SRandy.Fishel@Sun.COM 		 */
6179637SRandy.Fishel@Sun.COM 		cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
6189637SRandy.Fishel@Sun.COM 	} else {
6199637SRandy.Fishel@Sun.COM 		/*
6209637SRandy.Fishel@Sun.COM 		 * Toggle interrupt flag to detect pending interrupts.
6219637SRandy.Fishel@Sun.COM 		 * If interrupt happened, do_interrupt() will notify CPU idle
6229637SRandy.Fishel@Sun.COM 		 * notification framework so no need to call cpu_idle_exit()
6239637SRandy.Fishel@Sun.COM 		 * here.
6249637SRandy.Fishel@Sun.COM 		 */
6259637SRandy.Fishel@Sun.COM 		sti();
6269637SRandy.Fishel@Sun.COM 		SMT_PAUSE();
6279637SRandy.Fishel@Sun.COM 		cli();
6289637SRandy.Fishel@Sun.COM 	}
6299637SRandy.Fishel@Sun.COM }
6309637SRandy.Fishel@Sun.COM 
6319637SRandy.Fishel@Sun.COM /*
6329637SRandy.Fishel@Sun.COM  * Idle the present CPU until awakened via touching its monitored line
6334481Sbholler  */
6348906SEric.Saxe@Sun.COM void
6354481Sbholler cpu_idle_mwait(void)
6364481Sbholler {
6374481Sbholler 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
6384481Sbholler 	cpu_t			*cpup = CPU;
6398408SEric.Saxe@Sun.COM 	processorid_t		cpu_sid = cpup->cpu_seqid;
6404481Sbholler 	cpupart_t		*cp = cpup->cpu_part;
6414481Sbholler 	int			hset_update = 1;
6424481Sbholler 
6434481Sbholler 	/*
6448906SEric.Saxe@Sun.COM 	 * Set our mcpu_mwait here, so we can tell if anyone tries to
6454481Sbholler 	 * wake us between now and when we call mwait.  No other cpu will
6468408SEric.Saxe@Sun.COM 	 * attempt to set our mcpu_mwait until we add ourself to the halted
6478408SEric.Saxe@Sun.COM 	 * CPU bitmap.
6484481Sbholler 	 */
6494481Sbholler 	*mcpu_mwait = MWAIT_HALTED;
6504481Sbholler 
6514481Sbholler 	/*
6524481Sbholler 	 * If this CPU is online, and there's multiple CPUs
6538906SEric.Saxe@Sun.COM 	 * in the system, then we should note our halting
6544481Sbholler 	 * by adding ourselves to the partition's halted CPU
6554481Sbholler 	 * bitmap. This allows other CPUs to find/awaken us when
6564481Sbholler 	 * work becomes available.
6574481Sbholler 	 */
6584481Sbholler 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
6594481Sbholler 		hset_update = 0;
6604481Sbholler 
6614481Sbholler 	/*
6628408SEric.Saxe@Sun.COM 	 * Add ourselves to the partition's halted CPUs bitmap
6634481Sbholler 	 * and set our HALTED flag, if necessary.
6644481Sbholler 	 *
6654481Sbholler 	 * When a thread becomes runnable, it is placed on the queue
6668408SEric.Saxe@Sun.COM 	 * and then the halted CPU bitmap is checked to determine who
6678906SEric.Saxe@Sun.COM 	 * (if anyone) should be awakened. We therefore need to first
6688408SEric.Saxe@Sun.COM 	 * add ourselves to the bitmap, and and then check if there
6694481Sbholler 	 * is any work available.
6704481Sbholler 	 *
6714481Sbholler 	 * Note that memory barriers after updating the HALTED flag
6724481Sbholler 	 * are not necessary since an atomic operation (updating the bitmap)
6734481Sbholler 	 * immediately follows. On x86 the atomic operation acts as a
6744481Sbholler 	 * memory barrier for the update of cpu_disp_flags.
6754481Sbholler 	 */
6764481Sbholler 	if (hset_update) {
6774481Sbholler 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
6788408SEric.Saxe@Sun.COM 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
6794481Sbholler 	}
6804481Sbholler 
6814481Sbholler 	/*
6824481Sbholler 	 * Check to make sure there's really nothing to do.
6834481Sbholler 	 * Work destined for this CPU may become available after
6844481Sbholler 	 * this check. We'll be notified through the clearing of our
6858408SEric.Saxe@Sun.COM 	 * bit in the halted CPU bitmap, and a write to our mcpu_mwait.
6864481Sbholler 	 *
6874481Sbholler 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
6884481Sbholler 	 */
6894481Sbholler 	if (disp_anywork()) {
6904481Sbholler 		if (hset_update) {
6914481Sbholler 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
6928408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
6934481Sbholler 		}
6944481Sbholler 		return;
6954481Sbholler 	}
6964481Sbholler 
6974481Sbholler 	/*
6984481Sbholler 	 * We're on our way to being halted.
6994481Sbholler 	 * To avoid a lost wakeup, arm the monitor before checking if another
7004481Sbholler 	 * cpu wrote to mcpu_mwait to wake us up.
7014481Sbholler 	 */
7024481Sbholler 	i86_monitor(mcpu_mwait, 0, 0);
7034481Sbholler 	if (*mcpu_mwait == MWAIT_HALTED) {
7049637SRandy.Fishel@Sun.COM 		if (cpu_idle_enter(IDLE_STATE_C1, 0,
7059637SRandy.Fishel@Sun.COM 		    cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) {
7069637SRandy.Fishel@Sun.COM 			if (*mcpu_mwait == MWAIT_HALTED) {
7079637SRandy.Fishel@Sun.COM 				i86_mwait(0, 0);
7089637SRandy.Fishel@Sun.COM 			}
7099637SRandy.Fishel@Sun.COM 			cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
7109637SRandy.Fishel@Sun.COM 		}
7114481Sbholler 	}
7124481Sbholler 
7134481Sbholler 	/*
7144481Sbholler 	 * We're no longer halted
7154481Sbholler 	 */
7164481Sbholler 	if (hset_update) {
7174481Sbholler 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
7188408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
7194481Sbholler 	}
7204481Sbholler }
7214481Sbholler 
7224481Sbholler /*
7234481Sbholler  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
7244481Sbholler  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
7254481Sbholler  * need to be woken up so that they can steal the thread we placed on this CPU.
7264481Sbholler  * This function is only used on MP systems.
7274481Sbholler  */
7284481Sbholler static void
7294481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
7304481Sbholler {
7314481Sbholler 	cpupart_t	*cpu_part;
7324481Sbholler 	uint_t		cpu_found;
7338408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid;
7344481Sbholler 
7354481Sbholler 	cpu_part = cp->cpu_part;
7368408SEric.Saxe@Sun.COM 	cpu_sid = cp->cpu_seqid;
7374481Sbholler 
7384481Sbholler 	/*
7394481Sbholler 	 * Clear the halted bit for that CPU since it will be woken up
7404481Sbholler 	 * in a moment.
7414481Sbholler 	 */
7428408SEric.Saxe@Sun.COM 	if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) {
7434481Sbholler 		/*
7444481Sbholler 		 * Clear the halted bit for that CPU since it will be
7454481Sbholler 		 * poked in a moment.
7464481Sbholler 		 */
7478408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid);
7484481Sbholler 		/*
7494481Sbholler 		 * We may find the current CPU present in the halted cpuset
7504481Sbholler 		 * if we're in the context of an interrupt that occurred
7514481Sbholler 		 * before we had a chance to clear our bit in cpu_idle().
7524481Sbholler 		 * Waking ourself is obviously unnecessary, since if
7534481Sbholler 		 * we're here, we're not halted.
7544481Sbholler 		 *
7554481Sbholler 		 * monitor/mwait wakeup via writing to our cache line is
7564481Sbholler 		 * harmless and less expensive than always checking if we
7574481Sbholler 		 * are waking ourself which is an uncommon case.
7584481Sbholler 		 */
7594481Sbholler 		MWAIT_WAKEUP(cp);	/* write to monitored line */
7604481Sbholler 		return;
7614481Sbholler 	} else {
7624481Sbholler 		/*
7634481Sbholler 		 * This cpu isn't halted, but it's idle or undergoing a
7644481Sbholler 		 * context switch. No need to awaken anyone else.
7654481Sbholler 		 */
7664481Sbholler 		if (cp->cpu_thread == cp->cpu_idle_thread ||
7674481Sbholler 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
7684481Sbholler 			return;
7694481Sbholler 	}
7704481Sbholler 
7714481Sbholler 	/*
7724481Sbholler 	 * No need to wake up other CPUs if the thread we just enqueued
7734481Sbholler 	 * is bound.
7744481Sbholler 	 */
7758408SEric.Saxe@Sun.COM 	if (bound || ncpus == 1)
7764481Sbholler 		return;
7774481Sbholler 
7784481Sbholler 	/*
7794481Sbholler 	 * See if there's any other halted CPUs. If there are, then
7804481Sbholler 	 * select one, and awaken it.
7814481Sbholler 	 * It's possible that after we find a CPU, somebody else
7824481Sbholler 	 * will awaken it before we get the chance.
7834481Sbholler 	 * In that case, look again.
7844481Sbholler 	 */
7854481Sbholler 	do {
7868408SEric.Saxe@Sun.COM 		cpu_found = bitset_find(&cpu_part->cp_haltset);
7878408SEric.Saxe@Sun.COM 		if (cpu_found == (uint_t)-1)
7884481Sbholler 			return;
7898408SEric.Saxe@Sun.COM 	} while (bitset_atomic_test_and_del(&cpu_part->cp_haltset,
7908408SEric.Saxe@Sun.COM 	    cpu_found) < 0);
7914481Sbholler 
7924481Sbholler 	/*
7938408SEric.Saxe@Sun.COM 	 * Do not check if cpu_found is ourself as monitor/mwait
7948408SEric.Saxe@Sun.COM 	 * wakeup is cheap.
7954481Sbholler 	 */
7968408SEric.Saxe@Sun.COM 	MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */
7974481Sbholler }
7988408SEric.Saxe@Sun.COM 
7995084Sjohnlev #endif
8004481Sbholler 
8013446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
8023446Smrj 
8030Sstevel@tonic-gate static int
8040Sstevel@tonic-gate mp_disable_intr(int cpun)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * switch to the offline cpu
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	affinity_set(cpun);
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * raise ipl to just below cross call
8120Sstevel@tonic-gate 	 */
8139489SJoe.Bonasera@sun.com 	splx(XC_SYS_PIL - 1);
8140Sstevel@tonic-gate 	/*
8150Sstevel@tonic-gate 	 *	set base spl to prevent the next swtch to idle from
8160Sstevel@tonic-gate 	 *	lowering back to ipl 0
8170Sstevel@tonic-gate 	 */
8189489SJoe.Bonasera@sun.com 	CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1));
8190Sstevel@tonic-gate 	set_base_spl();
8200Sstevel@tonic-gate 	affinity_clear();
8210Sstevel@tonic-gate 	return (DDI_SUCCESS);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate static void
8250Sstevel@tonic-gate mp_enable_intr(int cpun)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * switch to the online cpu
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	affinity_set(cpun);
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * clear the interrupt active mask
8330Sstevel@tonic-gate 	 */
8349489SJoe.Bonasera@sun.com 	CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1));
8350Sstevel@tonic-gate 	set_base_spl();
8360Sstevel@tonic-gate 	(void) spl0();
8370Sstevel@tonic-gate 	affinity_clear();
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate static void
8410Sstevel@tonic-gate mach_get_platform(int owner)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	void		**srv_opsp;
8440Sstevel@tonic-gate 	void		**clt_opsp;
8450Sstevel@tonic-gate 	int		i;
8460Sstevel@tonic-gate 	int		total_ops;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/* fix up psm ops */
8490Sstevel@tonic-gate 	srv_opsp = (void **)mach_set[0];
8500Sstevel@tonic-gate 	clt_opsp = (void **)mach_set[owner];
8510Sstevel@tonic-gate 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
8520Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops_ver01) /
8534481Sbholler 		    sizeof (void (*)(void));
8540Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
8550Sstevel@tonic-gate 		/* no psm_notify_func */
8560Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
8570Sstevel@tonic-gate 		    sizeof (void (*)(void));
8580Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
8590Sstevel@tonic-gate 		/* no psm_timer funcs */
8600Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
8610Sstevel@tonic-gate 		    sizeof (void (*)(void));
8620Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
8630Sstevel@tonic-gate 		/* no psm_preshutdown function */
8640Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
8650Sstevel@tonic-gate 		    sizeof (void (*)(void));
8660Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
8670Sstevel@tonic-gate 		/* no psm_preshutdown function */
8680Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
8690Sstevel@tonic-gate 		    sizeof (void (*)(void));
8700Sstevel@tonic-gate 	else
8710Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/*
8740Sstevel@tonic-gate 	 * Save the version of the PSM module, in case we need to
8759637SRandy.Fishel@Sun.COM 	 * behave differently based on version.
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	mach_ver[0] = mach_ver[owner];
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	for (i = 0; i < total_ops; i++)
8800Sstevel@tonic-gate 		if (clt_opsp[i] != NULL)
8810Sstevel@tonic-gate 			srv_opsp[i] = clt_opsp[i];
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate static void
8850Sstevel@tonic-gate mach_construct_info()
8860Sstevel@tonic-gate {
8873446Smrj 	struct psm_sw *swp;
8880Sstevel@tonic-gate 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
8890Sstevel@tonic-gate 	int	conflict_owner = 0;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (psmsw->psw_forw == psmsw)
8920Sstevel@tonic-gate 		panic("No valid PSM modules found");
8930Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
8940Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
8950Sstevel@tonic-gate 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
8960Sstevel@tonic-gate 			continue;
8970Sstevel@tonic-gate 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
8980Sstevel@tonic-gate 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
8990Sstevel@tonic-gate 		mach_cnt[swp->psw_infop->p_owner]++;
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/* check to see are there any conflicts */
9060Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
9070Sstevel@tonic-gate 		conflict_owner = PSM_OWN_EXCLUSIVE;
9080Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
9090Sstevel@tonic-gate 		conflict_owner = PSM_OWN_OVERRIDE;
9100Sstevel@tonic-gate 	if (conflict_owner) {
9110Sstevel@tonic-gate 		/* remove all psm modules except uppc */
9120Sstevel@tonic-gate 		cmn_err(CE_WARN,
9134481Sbholler 		    "Conflicts detected on the following PSM modules:");
9140Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
9150Sstevel@tonic-gate 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
9160Sstevel@tonic-gate 			if (swp->psw_infop->p_owner == conflict_owner)
9170Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s ",
9184481Sbholler 				    swp->psw_infop->p_mach_idstring);
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
9210Sstevel@tonic-gate 		cmn_err(CE_WARN,
9224481Sbholler 		    "Setting the system back to SINGLE processor mode!");
9230Sstevel@tonic-gate 		cmn_err(CE_WARN,
9240Sstevel@tonic-gate 		    "Please edit /etc/mach to remove the invalid PSM module.");
9250Sstevel@tonic-gate 		return;
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (mach_set[PSM_OWN_EXCLUSIVE])
9290Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_EXCLUSIVE);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (mach_set[PSM_OWN_OVERRIDE])
9320Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_OVERRIDE);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate static void
9360Sstevel@tonic-gate mach_init()
9370Sstevel@tonic-gate {
9383446Smrj 	struct psm_ops  *pops;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	mach_construct_info();
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	pops = mach_set[0];
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/* register the interrupt and clock initialization rotuines */
9450Sstevel@tonic-gate 	picinitf = mach_picinit;
9460Sstevel@tonic-gate 	clkinitf = mach_clkinit;
9470Sstevel@tonic-gate 	psm_get_clockirq = pops->psm_get_clockirq;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	/* register the interrupt setup code */
9500Sstevel@tonic-gate 	slvltovect = mach_softlvl_to_vect;
9510Sstevel@tonic-gate 	addspl	= pops->psm_addspl;
9520Sstevel@tonic-gate 	delspl	= pops->psm_delspl;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if (pops->psm_translate_irq)
9550Sstevel@tonic-gate 		psm_translate_irq = pops->psm_translate_irq;
9560Sstevel@tonic-gate 	if (pops->psm_intr_ops)
9570Sstevel@tonic-gate 		psm_intr_ops = pops->psm_intr_ops;
9583446Smrj 
9593446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
9603446Smrj 	/*
9613446Smrj 	 * Time-of-day functionality now handled in TOD modules.
9623446Smrj 	 * (Warn about PSM modules that think that we're going to use
9633446Smrj 	 * their ops vectors.)
9643446Smrj 	 */
9653446Smrj 	if (pops->psm_tod_get)
9663446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
9673446Smrj 		    (void *)pops->psm_tod_get);
9683446Smrj 
9693446Smrj 	if (pops->psm_tod_set)
9703446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
9713446Smrj 		    (void *)pops->psm_tod_set);
9723446Smrj #endif
9733446Smrj 
9740Sstevel@tonic-gate 	if (pops->psm_notify_error) {
9750Sstevel@tonic-gate 		psm_notify_error = mach_notify_error;
9760Sstevel@tonic-gate 		notify_error = pops->psm_notify_error;
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	(*pops->psm_softinit)();
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/*
9828906SEric.Saxe@Sun.COM 	 * Initialize the dispatcher's function hooks to enable CPU halting
9838906SEric.Saxe@Sun.COM 	 * when idle.  Set both the deep-idle and non-deep-idle hooks.
9848906SEric.Saxe@Sun.COM 	 *
9858906SEric.Saxe@Sun.COM 	 * Assume we can use power saving deep-idle loop cpu_idle_adaptive.
9868906SEric.Saxe@Sun.COM 	 * Platform deep-idle driver will reset our idle loop to
9878906SEric.Saxe@Sun.COM 	 * non_deep_idle_cpu if power saving deep-idle feature is not available.
9888906SEric.Saxe@Sun.COM 	 *
9895045Sbholler 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
9905045Sbholler 	 * or idle_cpu_prefer_mwait is not set.
9914481Sbholler 	 * Allocate monitor/mwait buffer for cpu0.
9920Sstevel@tonic-gate 	 */
9938906SEric.Saxe@Sun.COM #ifndef __xpv
9948906SEric.Saxe@Sun.COM 	non_deep_idle_disp_enq_thread = disp_enq_thread;
9958906SEric.Saxe@Sun.COM #endif
9964481Sbholler 	if (idle_cpu_use_hlt) {
9978906SEric.Saxe@Sun.COM 		idle_cpu = cpu_idle_adaptive;
9988906SEric.Saxe@Sun.COM 		CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
9995084Sjohnlev #ifndef __xpv
10004481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
10015045Sbholler 			CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
10025045Sbholler 			/*
10035045Sbholler 			 * Protect ourself from insane mwait size.
10045045Sbholler 			 */
10055045Sbholler 			if (CPU->cpu_m.mcpu_mwait == NULL) {
10065045Sbholler #ifdef DEBUG
10075045Sbholler 				cmn_err(CE_NOTE, "Using hlt idle.  Cannot "
10085045Sbholler 				    "handle cpu 0 mwait size.");
10095045Sbholler #endif
10105045Sbholler 				idle_cpu_prefer_mwait = 0;
10118906SEric.Saxe@Sun.COM 				CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
10125045Sbholler 			} else {
10138906SEric.Saxe@Sun.COM 				CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
10145045Sbholler 			}
10154481Sbholler 		} else {
10168906SEric.Saxe@Sun.COM 			CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
10178906SEric.Saxe@Sun.COM 		}
10188906SEric.Saxe@Sun.COM 		non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu;
10198906SEric.Saxe@Sun.COM 
10208906SEric.Saxe@Sun.COM 		/*
10218906SEric.Saxe@Sun.COM 		 * Disable power saving deep idle loop?
10228906SEric.Saxe@Sun.COM 		 */
10238906SEric.Saxe@Sun.COM 		if (idle_cpu_no_deep_c) {
10248906SEric.Saxe@Sun.COM 			idle_cpu = non_deep_idle_cpu;
10254481Sbholler 		}
10265084Sjohnlev #endif
10274481Sbholler 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	mach_smpinit();
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate static void
10330Sstevel@tonic-gate mach_smpinit(void)
10340Sstevel@tonic-gate {
10352006Sandrei 	struct psm_ops  *pops;
10362006Sandrei 	processorid_t cpu_id;
10372006Sandrei 	int cnt;
10382006Sandrei 	cpuset_t cpumask;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	pops = mach_set[0];
10416336Sbholler 	CPUSET_ZERO(cpumask);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	cpu_id = -1;
10440Sstevel@tonic-gate 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
10456336Sbholler 	for (cnt = 0; cpu_id != -1; cnt++) {
10462006Sandrei 		CPUSET_ADD(cpumask, cpu_id);
10470Sstevel@tonic-gate 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	mp_cpus = cpumask;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/* MP related routines */
10530Sstevel@tonic-gate 	ap_mlsetup = pops->psm_post_cpu_start;
10540Sstevel@tonic-gate 	send_dirintf = pops->psm_send_ipi;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/* optional MP related routines */
10570Sstevel@tonic-gate 	if (pops->psm_shutdown)
10580Sstevel@tonic-gate 		psm_shutdownf = pops->psm_shutdown;
10590Sstevel@tonic-gate 	if (pops->psm_preshutdown)
10600Sstevel@tonic-gate 		psm_preshutdownf = pops->psm_preshutdown;
10610Sstevel@tonic-gate 	if (pops->psm_notify_func)
10620Sstevel@tonic-gate 		psm_notifyf = pops->psm_notify_func;
10630Sstevel@tonic-gate 	if (pops->psm_set_idlecpu)
10640Sstevel@tonic-gate 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
10650Sstevel@tonic-gate 	if (pops->psm_unset_idlecpu)
10660Sstevel@tonic-gate 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	psm_clkinit = pops->psm_clkinit;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (pops->psm_timer_reprogram)
10710Sstevel@tonic-gate 		psm_timer_reprogram = pops->psm_timer_reprogram;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	if (pops->psm_timer_enable)
10740Sstevel@tonic-gate 		psm_timer_enable = pops->psm_timer_enable;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (pops->psm_timer_disable)
10770Sstevel@tonic-gate 		psm_timer_disable = pops->psm_timer_disable;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if (pops->psm_post_cyclic_setup)
10800Sstevel@tonic-gate 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
10810Sstevel@tonic-gate 
10825295Srandyf 	if (pops->psm_state)
10835295Srandyf 		psm_state = pops->psm_state;
10845295Srandyf 
10857113Sbholler 	/*
10867113Sbholler 	 * Set these vectors here so they can be used by Suspend/Resume
10877113Sbholler 	 * on UP machines.
10887113Sbholler 	 */
10897113Sbholler 	if (pops->psm_disable_intr)
10907113Sbholler 		psm_disable_intr = pops->psm_disable_intr;
10917113Sbholler 	if (pops->psm_enable_intr)
10927113Sbholler 		psm_enable_intr  = pops->psm_enable_intr;
10937113Sbholler 
10947113Sbholler 	/* check for multiple CPUs */
10950Sstevel@tonic-gate 	if (cnt < 2)
10960Sstevel@tonic-gate 		return;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/* check for MP platforms */
10990Sstevel@tonic-gate 	if (pops->psm_cpu_start == NULL)
11000Sstevel@tonic-gate 		return;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/*
11030Sstevel@tonic-gate 	 * Set the dispatcher hook to enable cpu "wake up"
11040Sstevel@tonic-gate 	 * when a thread becomes runnable.
11050Sstevel@tonic-gate 	 */
11065084Sjohnlev 	if (idle_cpu_use_hlt) {
11075084Sjohnlev 		disp_enq_thread = cpu_wakeup;
11085084Sjohnlev #ifndef __xpv
11094481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
11104481Sbholler 			disp_enq_thread = cpu_wakeup_mwait;
11118906SEric.Saxe@Sun.COM 		non_deep_idle_disp_enq_thread = disp_enq_thread;
11125084Sjohnlev #endif
11135084Sjohnlev 	}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	psm_get_ipivect = pops->psm_get_ipivect;
11160Sstevel@tonic-gate 
11179489SJoe.Bonasera@sun.com 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr",
11184481Sbholler 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
11199489SJoe.Bonasera@sun.com 	    NULL, NULL, NULL, NULL);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate static void
11250Sstevel@tonic-gate mach_picinit()
11260Sstevel@tonic-gate {
11272006Sandrei 	struct psm_ops  *pops;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	pops = mach_set[0];
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/* register the interrupt handlers */
11320Sstevel@tonic-gate 	setlvl = pops->psm_intr_enter;
11330Sstevel@tonic-gate 	setlvlx = pops->psm_intr_exit;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/* initialize the interrupt hardware */
11360Sstevel@tonic-gate 	(*pops->psm_picinit)();
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/* set interrupt mask for current ipl */
11390Sstevel@tonic-gate 	setspl = pops->psm_setspl;
11403446Smrj 	cli();
11410Sstevel@tonic-gate 	setspl(CPU->cpu_pri);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate uint_t	cpu_freq;	/* MHz */
11450Sstevel@tonic-gate uint64_t cpu_freq_hz;	/* measured (in hertz) */
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate #define	MEGA_HZ		1000000
11480Sstevel@tonic-gate 
11495084Sjohnlev #ifdef __xpv
11505084Sjohnlev 
11515084Sjohnlev int xpv_cpufreq_workaround = 1;
11525084Sjohnlev int xpv_cpufreq_verbose = 0;
11535084Sjohnlev 
11545084Sjohnlev #else	/* __xpv */
11555084Sjohnlev 
11560Sstevel@tonic-gate static uint64_t
11570Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	uint64_t cpu_hz;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if ((pit_counter == 0) || (*processor_clks == 0) ||
11620Sstevel@tonic-gate 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
11630Sstevel@tonic-gate 		return (0);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	return (cpu_hz);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11705084Sjohnlev #endif	/* __xpv */
11715084Sjohnlev 
11720Sstevel@tonic-gate static uint64_t
11730Sstevel@tonic-gate mach_getcpufreq(void)
11740Sstevel@tonic-gate {
11755084Sjohnlev #if defined(__xpv)
11765084Sjohnlev 	vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
11775084Sjohnlev 	uint64_t cpu_hz;
11785084Sjohnlev 
11795084Sjohnlev 	/*
11805084Sjohnlev 	 * During dom0 bringup, it was noted that on at least one older
11815084Sjohnlev 	 * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul
11825084Sjohnlev 	 * value that is quite wrong (the 3.06GHz clock was reported
11835084Sjohnlev 	 * as 4.77GHz)
11845084Sjohnlev 	 *
11855084Sjohnlev 	 * The curious thing is, that if you stop the kernel at entry,
11865084Sjohnlev 	 * breakpoint here and inspect the value with kmdb, the value
11875084Sjohnlev 	 * is correct - but if you don't stop and simply enable the
11885084Sjohnlev 	 * printf statement (below), you can see the bad value printed
11895084Sjohnlev 	 * here.  Almost as if something kmdb did caused the hypervisor to
11905084Sjohnlev 	 * figure it out correctly.  And, note that the hypervisor
11915084Sjohnlev 	 * eventually -does- figure it out correctly ... if you look at
11925084Sjohnlev 	 * the field later in the life of dom0, it is correct.
11935084Sjohnlev 	 *
11945084Sjohnlev 	 * For now, on dom0, we employ a slightly cheesy workaround of
11955084Sjohnlev 	 * using the DOM0_PHYSINFO hypercall.
11965084Sjohnlev 	 */
11975084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
119810175SStuart.Maybee@Sun.COM 		cpu_hz = 1000 * xpv_cpu_khz();
11995084Sjohnlev 	} else {
12005084Sjohnlev 		cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
12015084Sjohnlev 
12025084Sjohnlev 		if (vti->tsc_shift < 0)
12035084Sjohnlev 			cpu_hz <<= -vti->tsc_shift;
12045084Sjohnlev 		else
12055084Sjohnlev 			cpu_hz >>= vti->tsc_shift;
12065084Sjohnlev 	}
12075084Sjohnlev 
12085084Sjohnlev 	if (xpv_cpufreq_verbose)
12095084Sjohnlev 		printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
12105084Sjohnlev 		    "cpu_hz %" PRId64 "Hz\n",
12115084Sjohnlev 		    vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
12125084Sjohnlev 
12135084Sjohnlev 	return (cpu_hz);
12145084Sjohnlev #else	/* __xpv */
12150Sstevel@tonic-gate 	uint32_t pit_counter;
12160Sstevel@tonic-gate 	uint64_t processor_clks;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (x86_feature & X86_TSC) {
12190Sstevel@tonic-gate 		/*
12200Sstevel@tonic-gate 		 * We have a TSC. freq_tsc() knows how to measure the number
12210Sstevel@tonic-gate 		 * of clock cycles sampled against the PIT.
12220Sstevel@tonic-gate 		 */
12233446Smrj 		ulong_t flags = clear_int_flag();
12240Sstevel@tonic-gate 		processor_clks = freq_tsc(&pit_counter);
12253446Smrj 		restore_int_flag(flags);
12260Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
12270Sstevel@tonic-gate 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
12280Sstevel@tonic-gate #if defined(__amd64)
12290Sstevel@tonic-gate 		panic("mach_getcpufreq: no TSC!");
12300Sstevel@tonic-gate #elif defined(__i386)
12310Sstevel@tonic-gate 		/*
12320Sstevel@tonic-gate 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
12330Sstevel@tonic-gate 		 * for which freq_notsc() knows how to measure the number of
12340Sstevel@tonic-gate 		 * elapsed clock cycles sampled against the PIT
12350Sstevel@tonic-gate 		 */
12363446Smrj 		ulong_t flags = clear_int_flag();
12370Sstevel@tonic-gate 		processor_clks = freq_notsc(&pit_counter);
12383446Smrj 		restore_int_flag(flags);
12390Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
12400Sstevel@tonic-gate #endif	/* __i386 */
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* We do not know how to calculate cpu frequency for this cpu. */
12440Sstevel@tonic-gate 	return (0);
12455084Sjohnlev #endif	/* __xpv */
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * If the clock speed of a cpu is found to be reported incorrectly, do not add
12500Sstevel@tonic-gate  * to this array, instead improve the accuracy of the algorithm that determines
12510Sstevel@tonic-gate  * the clock speed of the processor or extend the implementation to support the
12520Sstevel@tonic-gate  * vendor as appropriate. This is here only to support adjusting the speed on
12530Sstevel@tonic-gate  * older slower processors that mach_fixcpufreq() would not be able to account
12540Sstevel@tonic-gate  * for otherwise.
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate /*
12590Sstevel@tonic-gate  * On fast processors the clock frequency that is measured may be off by
12600Sstevel@tonic-gate  * a few MHz from the value printed on the part. This is a combination of
12610Sstevel@tonic-gate  * the factors that for such fast parts being off by this much is within
12620Sstevel@tonic-gate  * the tolerances for manufacture and because of the difficulties in the
12630Sstevel@tonic-gate  * measurement that can lead to small error. This function uses some
12640Sstevel@tonic-gate  * heuristics in order to tweak the value that was measured to match what
12650Sstevel@tonic-gate  * is most likely printed on the part.
12660Sstevel@tonic-gate  *
12670Sstevel@tonic-gate  * Some examples:
12680Sstevel@tonic-gate  * 	AMD Athlon 1000 mhz measured as 998 mhz
12690Sstevel@tonic-gate  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
12700Sstevel@tonic-gate  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
12710Sstevel@tonic-gate  *
12720Sstevel@tonic-gate  * If in the future this function is no longer sufficient to correct
12730Sstevel@tonic-gate  * for the error in the measurement, then the algorithm used to perform
12740Sstevel@tonic-gate  * the measurement will have to be improved in order to increase accuracy
12750Sstevel@tonic-gate  * rather than adding horrible and questionable kludges here.
12760Sstevel@tonic-gate  *
12770Sstevel@tonic-gate  * This is called after the cyclics subsystem because of the potential
12780Sstevel@tonic-gate  * that the heuristics within may give a worse estimate of the clock
12790Sstevel@tonic-gate  * frequency than the value that was measured.
12800Sstevel@tonic-gate  */
12810Sstevel@tonic-gate static void
12820Sstevel@tonic-gate mach_fixcpufreq(void)
12830Sstevel@tonic-gate {
12840Sstevel@tonic-gate 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	freq = (uint32_t)cpu_freq;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/*
12890Sstevel@tonic-gate 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
12900Sstevel@tonic-gate 	 * measured speed taking into account that the 667 MHz parts were
12910Sstevel@tonic-gate 	 * the first to round-up.
12920Sstevel@tonic-gate 	 */
12930Sstevel@tonic-gate 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
12940Sstevel@tonic-gate 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
12950Sstevel@tonic-gate 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
12980Sstevel@tonic-gate 	mul = (freq + 25) / 50;
12990Sstevel@tonic-gate 	near50 = mul * 50;
13000Sstevel@tonic-gate 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/* Find the closer of the two */
13030Sstevel@tonic-gate 	if (delta66 < delta50) {
13040Sstevel@tonic-gate 		fixed = near66;
13050Sstevel@tonic-gate 		delta = delta66;
13060Sstevel@tonic-gate 	} else {
13070Sstevel@tonic-gate 		fixed = near50;
13080Sstevel@tonic-gate 		delta = delta50;
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	if (fixed > INT_MAX)
13120Sstevel@tonic-gate 		return;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	/*
13150Sstevel@tonic-gate 	 * Some older parts have a core clock frequency that is not an
13160Sstevel@tonic-gate 	 * integral multiple of 50 or 66 MHz. Check if one of the old
13170Sstevel@tonic-gate 	 * clock frequencies is closer to the measured value than any
13180Sstevel@tonic-gate 	 * of the integral multiples of 50 an 66, and if so set fixed
13190Sstevel@tonic-gate 	 * and delta appropriately to represent the closest value.
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	i = sizeof (x86_cpu_freq) / sizeof (int);
13220Sstevel@tonic-gate 	while (i > 0) {
13230Sstevel@tonic-gate 		i--;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		if (x86_cpu_freq[i] <= freq) {
13260Sstevel@tonic-gate 			mul = freq - x86_cpu_freq[i];
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 			if (mul < delta) {
13290Sstevel@tonic-gate 				fixed = x86_cpu_freq[i];
13300Sstevel@tonic-gate 				delta = mul;
13310Sstevel@tonic-gate 			}
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 			break;
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		mul = x86_cpu_freq[i] - freq;
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		if (mul < delta) {
13390Sstevel@tonic-gate 			fixed = x86_cpu_freq[i];
13400Sstevel@tonic-gate 			delta = mul;
13410Sstevel@tonic-gate 		}
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * Set a reasonable maximum for how much to correct the measured
13460Sstevel@tonic-gate 	 * result by. This check is here to prevent the adjustment made
13470Sstevel@tonic-gate 	 * by this function from being more harm than good. It is entirely
13480Sstevel@tonic-gate 	 * possible that in the future parts will be made that are not
13490Sstevel@tonic-gate 	 * integral multiples of 66 or 50 in clock frequency or that
13500Sstevel@tonic-gate 	 * someone may overclock a part to some odd frequency. If the
13510Sstevel@tonic-gate 	 * measured value is farther from the corrected value than
13520Sstevel@tonic-gate 	 * allowed, then assume the corrected value is in error and use
13530Sstevel@tonic-gate 	 * the measured value.
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 	if (6 < delta)
13560Sstevel@tonic-gate 		return;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	cpu_freq = (int)fixed;
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate static int
13630Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate 	uint64_t cpu_mhz;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/* Round to nearest MHZ */
13680Sstevel@tonic-gate 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	if (cpu_mhz > INT_MAX)
13710Sstevel@tonic-gate 		return (0);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	return ((int)cpu_mhz);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate static int
13790Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
13800Sstevel@tonic-gate {
13813446Smrj 	struct psm_ops  *pops;
13820Sstevel@tonic-gate 	int resolution;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	pops = mach_set[0];
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	cpu_freq_hz = mach_getcpufreq();
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	cpu_freq = machhztomhz(cpu_freq_hz);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
13910Sstevel@tonic-gate 		tsc_gethrtime_enable = 0;
13920Sstevel@tonic-gate 
13935084Sjohnlev #ifndef __xpv
13940Sstevel@tonic-gate 	if (tsc_gethrtime_enable) {
13950Sstevel@tonic-gate 		tsc_hrtimeinit(cpu_freq_hz);
13965084Sjohnlev 	} else
13975084Sjohnlev #endif
13985084Sjohnlev 	{
13990Sstevel@tonic-gate 		if (pops->psm_hrtimeinit)
14000Sstevel@tonic-gate 			(*pops->psm_hrtimeinit)();
14010Sstevel@tonic-gate 		gethrtimef = pops->psm_gethrtime;
14020Sstevel@tonic-gate 		gethrtimeunscaledf = gethrtimef;
14030Sstevel@tonic-gate 		/* scalehrtimef will remain dummy */
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	mach_fixcpufreq();
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
14095084Sjohnlev 		if (preferred_mode == TIMER_ONESHOT) {
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 			resolution = (*pops->psm_clkinit)(0);
14120Sstevel@tonic-gate 			if (resolution != 0)  {
14130Sstevel@tonic-gate 				*set_mode = TIMER_ONESHOT;
14140Sstevel@tonic-gate 				return (resolution);
14150Sstevel@tonic-gate 			}
14160Sstevel@tonic-gate 		}
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 		/*
14190Sstevel@tonic-gate 		 * either periodic mode was requested or could not set to
14200Sstevel@tonic-gate 		 * one-shot mode
14210Sstevel@tonic-gate 		 */
14220Sstevel@tonic-gate 		resolution = (*pops->psm_clkinit)(hz);
14230Sstevel@tonic-gate 		/*
14240Sstevel@tonic-gate 		 * psm should be able to do periodic, so we do not check
14250Sstevel@tonic-gate 		 * for return value of psm_clkinit here.
14260Sstevel@tonic-gate 		 */
14270Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
14280Sstevel@tonic-gate 		return (resolution);
14290Sstevel@tonic-gate 	} else {
14300Sstevel@tonic-gate 		/*
14310Sstevel@tonic-gate 		 * PSMI interface prior to PSMI_3 does not define a return
14320Sstevel@tonic-gate 		 * value for psm_clkinit, so the return value is ignored.
14330Sstevel@tonic-gate 		 */
14340Sstevel@tonic-gate 		(void) (*pops->psm_clkinit)(hz);
14350Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
14360Sstevel@tonic-gate 		return (nsec_per_tick);
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate 
14404652Scwb 
1441999Slq150181 /*ARGSUSED*/
14420Sstevel@tonic-gate static int
14433446Smrj mach_softlvl_to_vect(int ipl)
14440Sstevel@tonic-gate {
14454652Scwb 	setsoftint = av_set_softint_pending;
14464652Scwb 	kdisetsoftint = kdi_av_set_softint_pending;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	return (PSM_SV_SOFTWARE);
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate 
14513446Smrj #ifdef DEBUG
14523446Smrj /*
14533446Smrj  * This is here to allow us to simulate cpus that refuse to start.
14543446Smrj  */
14553446Smrj cpuset_t cpufailset;
14563446Smrj #endif
14573446Smrj 
14583446Smrj int
14593446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
14600Sstevel@tonic-gate {
14613446Smrj 	struct psm_ops *pops = mach_set[0];
14623446Smrj 	processorid_t id = cp->cpu_id;
14630Sstevel@tonic-gate 
14643446Smrj #ifdef DEBUG
14653446Smrj 	if (CPU_IN_SET(cpufailset, id))
14663446Smrj 		return (0);
14673446Smrj #endif
14683446Smrj 	return ((*pops->psm_cpu_start)(id, ctx));
14690Sstevel@tonic-gate }
14700Sstevel@tonic-gate 
14715295Srandyf int
14725295Srandyf mach_cpuid_start(processorid_t id, void *ctx)
14735295Srandyf {
14745295Srandyf 	struct psm_ops *pops = mach_set[0];
14755295Srandyf 
14765295Srandyf #ifdef DEBUG
14775295Srandyf 	if (CPU_IN_SET(cpufailset, id))
14785295Srandyf 		return (0);
14795295Srandyf #endif
14805295Srandyf 	return ((*pops->psm_cpu_start)(id, ctx));
14815295Srandyf }
14825295Srandyf 
14839652SMichael.Corcoran@Sun.COM /*
14849652SMichael.Corcoran@Sun.COM  * Default handler to create device node for CPU.
14859652SMichael.Corcoran@Sun.COM  * One reference count will be held on created device node.
14869652SMichael.Corcoran@Sun.COM  */
14879652SMichael.Corcoran@Sun.COM static int
14889652SMichael.Corcoran@Sun.COM mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp)
14899652SMichael.Corcoran@Sun.COM {
14909652SMichael.Corcoran@Sun.COM 	int rv, circ;
14919652SMichael.Corcoran@Sun.COM 	dev_info_t *dip;
14929652SMichael.Corcoran@Sun.COM 	static kmutex_t cpu_node_lock;
14939652SMichael.Corcoran@Sun.COM 	static dev_info_t *cpu_nex_devi = NULL;
14949652SMichael.Corcoran@Sun.COM 
14959652SMichael.Corcoran@Sun.COM 	ASSERT(cp != NULL);
14969652SMichael.Corcoran@Sun.COM 	ASSERT(dipp != NULL);
14979652SMichael.Corcoran@Sun.COM 	*dipp = NULL;
14989652SMichael.Corcoran@Sun.COM 
14999652SMichael.Corcoran@Sun.COM 	if (cpu_nex_devi == NULL) {
15009652SMichael.Corcoran@Sun.COM 		mutex_enter(&cpu_node_lock);
15019652SMichael.Corcoran@Sun.COM 		/* First check whether cpus exists. */
15029652SMichael.Corcoran@Sun.COM 		cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0);
15039652SMichael.Corcoran@Sun.COM 		/* Create cpus if it doesn't exist. */
15049652SMichael.Corcoran@Sun.COM 		if (cpu_nex_devi == NULL) {
15059652SMichael.Corcoran@Sun.COM 			ndi_devi_enter(ddi_root_node(), &circ);
15069652SMichael.Corcoran@Sun.COM 			rv = ndi_devi_alloc(ddi_root_node(), "cpus",
15079652SMichael.Corcoran@Sun.COM 			    (pnode_t)DEVI_SID_NODEID, &dip);
15089652SMichael.Corcoran@Sun.COM 			if (rv != NDI_SUCCESS) {
15099652SMichael.Corcoran@Sun.COM 				mutex_exit(&cpu_node_lock);
15109652SMichael.Corcoran@Sun.COM 				cmn_err(CE_CONT,
15119652SMichael.Corcoran@Sun.COM 				    "?failed to create cpu nexus device.\n");
15129652SMichael.Corcoran@Sun.COM 				return (PSM_FAILURE);
15139652SMichael.Corcoran@Sun.COM 			}
15149652SMichael.Corcoran@Sun.COM 			ASSERT(dip != NULL);
15159652SMichael.Corcoran@Sun.COM 			(void) ndi_devi_online(dip, 0);
15169652SMichael.Corcoran@Sun.COM 			ndi_devi_exit(ddi_root_node(), circ);
15179652SMichael.Corcoran@Sun.COM 			cpu_nex_devi = dip;
15189652SMichael.Corcoran@Sun.COM 		}
15199652SMichael.Corcoran@Sun.COM 		mutex_exit(&cpu_node_lock);
15209652SMichael.Corcoran@Sun.COM 	}
15219652SMichael.Corcoran@Sun.COM 
15229652SMichael.Corcoran@Sun.COM 	/*
15239652SMichael.Corcoran@Sun.COM 	 * create a child node for cpu identified as 'cpu_id'
15249652SMichael.Corcoran@Sun.COM 	 */
15259652SMichael.Corcoran@Sun.COM 	ndi_devi_enter(cpu_nex_devi, &circ);
152610537SMichael.Corcoran@Sun.COM 	dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1);
15279652SMichael.Corcoran@Sun.COM 	if (dip == NULL) {
15289652SMichael.Corcoran@Sun.COM 		cmn_err(CE_CONT,
15299652SMichael.Corcoran@Sun.COM 		    "?failed to create device node for cpu%d.\n", cp->cpu_id);
15309652SMichael.Corcoran@Sun.COM 		rv = PSM_FAILURE;
15319652SMichael.Corcoran@Sun.COM 	} else {
15329652SMichael.Corcoran@Sun.COM 		*dipp = dip;
15339652SMichael.Corcoran@Sun.COM 		(void) ndi_hold_devi(dip);
15349652SMichael.Corcoran@Sun.COM 		rv = PSM_SUCCESS;
15359652SMichael.Corcoran@Sun.COM 	}
15369652SMichael.Corcoran@Sun.COM 	ndi_devi_exit(cpu_nex_devi, circ);
15379652SMichael.Corcoran@Sun.COM 
15389652SMichael.Corcoran@Sun.COM 	return (rv);
15399652SMichael.Corcoran@Sun.COM }
15409652SMichael.Corcoran@Sun.COM 
15419652SMichael.Corcoran@Sun.COM /*
15429652SMichael.Corcoran@Sun.COM  * Create cpu device node in device tree and online it.
15439652SMichael.Corcoran@Sun.COM  * Return created dip with reference count held if requested.
15449652SMichael.Corcoran@Sun.COM  */
15459652SMichael.Corcoran@Sun.COM int
15469652SMichael.Corcoran@Sun.COM mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp)
15479652SMichael.Corcoran@Sun.COM {
15489652SMichael.Corcoran@Sun.COM 	int rv;
15499652SMichael.Corcoran@Sun.COM 	dev_info_t *dip = NULL;
15509652SMichael.Corcoran@Sun.COM 
15519652SMichael.Corcoran@Sun.COM 	ASSERT(psm_cpu_create_devinfo != NULL);
15529652SMichael.Corcoran@Sun.COM 	rv = psm_cpu_create_devinfo(cp, &dip);
15539652SMichael.Corcoran@Sun.COM 	if (rv == PSM_SUCCESS) {
15549652SMichael.Corcoran@Sun.COM 		cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi);
15559652SMichael.Corcoran@Sun.COM 		/* Recursively attach driver for parent nexus device. */
15569652SMichael.Corcoran@Sun.COM 		if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) ==
15579652SMichael.Corcoran@Sun.COM 		    DDI_SUCCESS) {
15589652SMichael.Corcoran@Sun.COM 			/* Configure cpu itself and descendants. */
15599652SMichael.Corcoran@Sun.COM 			(void) ndi_devi_online(dip,
15609652SMichael.Corcoran@Sun.COM 			    NDI_ONLINE_ATTACH | NDI_CONFIG);
15619652SMichael.Corcoran@Sun.COM 		}
15629652SMichael.Corcoran@Sun.COM 		if (dipp != NULL) {
15639652SMichael.Corcoran@Sun.COM 			*dipp = dip;
15649652SMichael.Corcoran@Sun.COM 		} else {
15659652SMichael.Corcoran@Sun.COM 			(void) ndi_rele_devi(dip);
15669652SMichael.Corcoran@Sun.COM 		}
15679652SMichael.Corcoran@Sun.COM 	}
15689652SMichael.Corcoran@Sun.COM 
15699652SMichael.Corcoran@Sun.COM 	return (rv);
15709652SMichael.Corcoran@Sun.COM }
15719652SMichael.Corcoran@Sun.COM 
15720Sstevel@tonic-gate /*ARGSUSED*/
15730Sstevel@tonic-gate static int
15740Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
15750Sstevel@tonic-gate {
15760Sstevel@tonic-gate 	return (irqno);	/* default to NO translation */
15770Sstevel@tonic-gate }
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate static void
15800Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	/*
15830Sstevel@tonic-gate 	 * SL_FATAL is pass in once panicstr is set, deliver it
15840Sstevel@tonic-gate 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
15850Sstevel@tonic-gate 	 * codes for the psmi handler
15860Sstevel@tonic-gate 	 */
15870Sstevel@tonic-gate 	if (level & SL_FATAL)
15880Sstevel@tonic-gate 		(*notify_error)(CE_PANIC, errmsg);
15890Sstevel@tonic-gate 	else if (level & SL_WARN)
15900Sstevel@tonic-gate 		(*notify_error)(CE_WARN, errmsg);
15910Sstevel@tonic-gate 	else if (level & SL_NOTE)
15920Sstevel@tonic-gate 		(*notify_error)(CE_NOTE, errmsg);
15930Sstevel@tonic-gate 	else if (level & SL_CONSOLE)
15940Sstevel@tonic-gate 		(*notify_error)(CE_CONT, errmsg);
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate /*
15980Sstevel@tonic-gate  * It provides the default basic intr_ops interface for the new DDI
15990Sstevel@tonic-gate  * interrupt framework if the PSM doesn't have one.
16000Sstevel@tonic-gate  *
16010Sstevel@tonic-gate  * Input:
16020Sstevel@tonic-gate  * dip     - pointer to the dev_info structure of the requested device
16030Sstevel@tonic-gate  * hdlp    - pointer to the internal interrupt handle structure for the
16040Sstevel@tonic-gate  *	     requested interrupt
16050Sstevel@tonic-gate  * intr_op - opcode for this call
16060Sstevel@tonic-gate  * result  - pointer to the integer that will hold the result to be
16070Sstevel@tonic-gate  *	     passed back if return value is PSM_SUCCESS
16080Sstevel@tonic-gate  *
16090Sstevel@tonic-gate  * Output:
16100Sstevel@tonic-gate  * return value is either PSM_SUCCESS or PSM_FAILURE
16110Sstevel@tonic-gate  */
16120Sstevel@tonic-gate static int
16130Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
16140Sstevel@tonic-gate     psm_intr_op_t intr_op, int *result)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	struct intrspec *ispec;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	switch (intr_op) {
16190Sstevel@tonic-gate 	case PSM_INTR_OP_CHECK_MSI:
16200Sstevel@tonic-gate 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
16214481Sbholler 		    DDI_INTR_TYPE_MSIX);
16220Sstevel@tonic-gate 		break;
16230Sstevel@tonic-gate 	case PSM_INTR_OP_ALLOC_VECTORS:
16240Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
16250Sstevel@tonic-gate 			*result = 1;
16260Sstevel@tonic-gate 		else
16270Sstevel@tonic-gate 			*result = 0;
16280Sstevel@tonic-gate 		break;
16290Sstevel@tonic-gate 	case PSM_INTR_OP_FREE_VECTORS:
16300Sstevel@tonic-gate 		break;
16310Sstevel@tonic-gate 	case PSM_INTR_OP_NAVAIL_VECTORS:
16320Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
16330Sstevel@tonic-gate 			*result = 1;
16340Sstevel@tonic-gate 		else
16350Sstevel@tonic-gate 			*result = 0;
16360Sstevel@tonic-gate 		break;
16370Sstevel@tonic-gate 	case PSM_INTR_OP_XLATE_VECTOR:
1638916Sschwartz 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
16390Sstevel@tonic-gate 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
16400Sstevel@tonic-gate 		break;
16410Sstevel@tonic-gate 	case PSM_INTR_OP_GET_CAP:
16420Sstevel@tonic-gate 		*result = 0;
16430Sstevel@tonic-gate 		break;
16440Sstevel@tonic-gate 	case PSM_INTR_OP_GET_PENDING:
16450Sstevel@tonic-gate 	case PSM_INTR_OP_CLEAR_MASK:
16460Sstevel@tonic-gate 	case PSM_INTR_OP_SET_MASK:
16470Sstevel@tonic-gate 	case PSM_INTR_OP_GET_SHARED:
16480Sstevel@tonic-gate 	case PSM_INTR_OP_SET_PRI:
16490Sstevel@tonic-gate 	case PSM_INTR_OP_SET_CAP:
1650916Sschwartz 	case PSM_INTR_OP_SET_CPU:
1651916Sschwartz 	case PSM_INTR_OP_GET_INTR:
16520Sstevel@tonic-gate 	default:
16530Sstevel@tonic-gate 		return (PSM_FAILURE);
16540Sstevel@tonic-gate 	}
16550Sstevel@tonic-gate 	return (PSM_SUCCESS);
16560Sstevel@tonic-gate }
16574769Sdp78419 /*
16584769Sdp78419  * Return 1 if CMT load balancing policies should be
16594769Sdp78419  * implemented across instances of the specified hardware
16604769Sdp78419  * sharing relationship.
16614769Sdp78419  */
16624769Sdp78419 int
16634769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw)
16644769Sdp78419 {
16654769Sdp78419 	if (hw == PGHW_IPIPE ||
16664769Sdp78419 	    hw == PGHW_FPU ||
166710947SSrihari.Venkatesan@Sun.COM 	    hw == PGHW_PROCNODE ||
16684769Sdp78419 	    hw == PGHW_CHIP)
16694769Sdp78419 		return (1);
16704769Sdp78419 	else
16714769Sdp78419 		return (0);
16724769Sdp78419 }
16734769Sdp78419 /*
16744769Sdp78419  * Return 1 if thread affinity polices should be implemented
16754769Sdp78419  * for instances of the specifed hardware sharing relationship.
16764769Sdp78419  */
16774769Sdp78419 int
16784769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw)
16794769Sdp78419 {
16804769Sdp78419 	if (hw == PGHW_CACHE)
16814769Sdp78419 		return (1);
16824769Sdp78419 	else
16834769Sdp78419 		return (0);
16844769Sdp78419 }
1685*11389SAlexander.Kolbasov@Sun.COM 
1686*11389SAlexander.Kolbasov@Sun.COM /*
1687*11389SAlexander.Kolbasov@Sun.COM  * Return number of counter events requested to measure hardware capacity and
1688*11389SAlexander.Kolbasov@Sun.COM  * utilization and setup CPC requests for specified CPU as needed
1689*11389SAlexander.Kolbasov@Sun.COM  *
1690*11389SAlexander.Kolbasov@Sun.COM  * May return 0 when platform or processor specific code knows that no CPC
1691*11389SAlexander.Kolbasov@Sun.COM  * events should be programmed on this CPU or -1 when platform or processor
1692*11389SAlexander.Kolbasov@Sun.COM  * specific code doesn't know which counter events are best to use and common
1693*11389SAlexander.Kolbasov@Sun.COM  * code should decide for itself
1694*11389SAlexander.Kolbasov@Sun.COM  */
1695*11389SAlexander.Kolbasov@Sun.COM int
1696*11389SAlexander.Kolbasov@Sun.COM /* LINTED E_FUNC_ARG_UNUSED */
1697*11389SAlexander.Kolbasov@Sun.COM cu_plat_cpc_init(cpu_t *cp, kcpc_request_list_t *reqs, int nreqs)
1698*11389SAlexander.Kolbasov@Sun.COM {
1699*11389SAlexander.Kolbasov@Sun.COM 	const char	*impl_name;
1700*11389SAlexander.Kolbasov@Sun.COM 
1701*11389SAlexander.Kolbasov@Sun.COM 	/*
1702*11389SAlexander.Kolbasov@Sun.COM 	 * Return error if pcbe_ops not set
1703*11389SAlexander.Kolbasov@Sun.COM 	 */
1704*11389SAlexander.Kolbasov@Sun.COM 	if (pcbe_ops == NULL)
1705*11389SAlexander.Kolbasov@Sun.COM 		return (-1);
1706*11389SAlexander.Kolbasov@Sun.COM 
1707*11389SAlexander.Kolbasov@Sun.COM 	/*
1708*11389SAlexander.Kolbasov@Sun.COM 	 * Return that no CPC events should be programmed on hyperthreaded
1709*11389SAlexander.Kolbasov@Sun.COM 	 * Pentium 4 and return error for all other x86 processors to tell
1710*11389SAlexander.Kolbasov@Sun.COM 	 * common code to decide what counter events to program on those CPUs
1711*11389SAlexander.Kolbasov@Sun.COM 	 * for measuring hardware capacity and utilization
1712*11389SAlexander.Kolbasov@Sun.COM 	 */
1713*11389SAlexander.Kolbasov@Sun.COM 	impl_name = pcbe_ops->pcbe_impl_name();
1714*11389SAlexander.Kolbasov@Sun.COM 	if (impl_name != NULL && strcmp(impl_name, PCBE_IMPL_NAME_P4HT) == 0)
1715*11389SAlexander.Kolbasov@Sun.COM 		return (0);
1716*11389SAlexander.Kolbasov@Sun.COM 	else
1717*11389SAlexander.Kolbasov@Sun.COM 		return (-1);
1718*11389SAlexander.Kolbasov@Sun.COM }
1719