xref: /onnv-gate/usr/src/uts/i86pc/os/mp_machdep.c (revision 7113:9b2fd7c2000b)
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
52006Sandrei  * Common Development and Distribution License (the "License").
62006Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
225864Sesaxe  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
285295Srandyf #define	PSMI_1_6
290Sstevel@tonic-gate #include <sys/smp_impldefs.h>
300Sstevel@tonic-gate #include <sys/psm.h>
310Sstevel@tonic-gate #include <sys/psm_modctl.h>
320Sstevel@tonic-gate #include <sys/pit.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/strlog.h>
350Sstevel@tonic-gate #include <sys/clock.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/rtc.h>
380Sstevel@tonic-gate #include <sys/x86_archext.h>
390Sstevel@tonic-gate #include <sys/cpupart.h>
400Sstevel@tonic-gate #include <sys/cpuvar.h>
414606Sesaxe #include <sys/cmt.h>
424481Sbholler #include <sys/cpu.h>
430Sstevel@tonic-gate #include <sys/disp.h>
440Sstevel@tonic-gate #include <sys/archsystm.h>
453446Smrj #include <sys/machsystm.h>
464481Sbholler #include <sys/sysmacros.h>
475084Sjohnlev #include <sys/memlist.h>
483446Smrj #include <sys/param.h>
493446Smrj #include <sys/promif.h>
505084Sjohnlev #if defined(__xpv)
515084Sjohnlev #include <sys/hypervisor.h>
525084Sjohnlev #endif
53916Sschwartz #include <sys/mach_intr.h>
544481Sbholler #include <vm/hat_i86.h>
554652Scwb #include <sys/kdi_machimpl.h>
565864Sesaxe #include <sys/sdt.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  *	Local function prototypes
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
640Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
650Sstevel@tonic-gate static void mach_init();
660Sstevel@tonic-gate static void mach_picinit();
670Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
680Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
690Sstevel@tonic-gate static void mach_fixcpufreq(void);
700Sstevel@tonic-gate static int mach_clkinit(int, int *);
710Sstevel@tonic-gate static void mach_smpinit(void);
720Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl);
730Sstevel@tonic-gate static void mach_get_platform(int owner);
740Sstevel@tonic-gate static void mach_construct_info();
750Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno);
760Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
770Sstevel@tonic-gate     psm_intr_op_t, int *);
780Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg);
790Sstevel@tonic-gate static hrtime_t dummy_hrtime(void);
800Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *);
813446Smrj static void cpu_idle(void);
820Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int);
835084Sjohnlev #ifndef __xpv
844481Sbholler static void cpu_idle_mwait(void);
854481Sbholler static void cpu_wakeup_mwait(cpu_t *, int);
865084Sjohnlev #endif
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  *	External reference functions
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate extern void return_instr();
910Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *);
920Sstevel@tonic-gate #if defined(__i386)
930Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *);
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *);
963434Sesaxe extern int cpuid_get_coreid(cpu_t *);
973434Sesaxe extern int cpuid_get_chipid(cpu_t *);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  *	PSM functions initialization
1010Sstevel@tonic-gate  */
1023446Smrj void (*psm_shutdownf)(int, int)	= (void (*)(int, int))return_instr;
1033446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
1043446Smrj void (*psm_notifyf)(int)	= (void (*)(int))return_instr;
1053446Smrj void (*psm_set_idle_cpuf)(int)	= (void (*)(int))return_instr;
1063446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
1070Sstevel@tonic-gate void (*psminitf)()		= mach_init;
1080Sstevel@tonic-gate void (*picinitf)() 		= return_instr;
1090Sstevel@tonic-gate int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
1100Sstevel@tonic-gate int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
1110Sstevel@tonic-gate void (*send_dirintf)() 		= return_instr;
1123446Smrj void (*setspl)(int)		= (void (*)(int))return_instr;
1130Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1140Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1154652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)=
1164652Scwb 	(void (*)(int, struct av_softinfo *))return_instr;
117999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
118999Slq150181 	(void (*)(int, struct av_softinfo *))return_instr;
1190Sstevel@tonic-gate int (*slvltovect)(int)		= (int (*)(int))return_instr;
1200Sstevel@tonic-gate int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
1210Sstevel@tonic-gate void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
1220Sstevel@tonic-gate int (*psm_disable_intr)(int)	= mp_disable_intr;
1230Sstevel@tonic-gate void (*psm_enable_intr)(int)	= mp_enable_intr;
1240Sstevel@tonic-gate hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
1250Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
1260Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
1270Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1280Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1290Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1300Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1310Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1340Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1350Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1360Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1370Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1380Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1390Sstevel@tonic-gate     int *) = mach_intr_ops;
1405295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
1415295Srandyf     return_instr;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1440Sstevel@tonic-gate void (*hrtime_tick)(void)	= return_instr;
1450Sstevel@tonic-gate 
1465084Sjohnlev /*
1475084Sjohnlev  * True if the generic TSC code is our source of hrtime, rather than whatever
1485084Sjohnlev  * the PSM can provide.
1495084Sjohnlev  */
1505084Sjohnlev #ifdef __xpv
1515084Sjohnlev int tsc_gethrtime_enable = 0;
1525084Sjohnlev #else
1530Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1545084Sjohnlev #endif
1550Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1585084Sjohnlev  * True if the hrtime implementation is "hires"; namely, better than microdata.
1595084Sjohnlev  */
1605084Sjohnlev int gethrtime_hires = 0;
1615084Sjohnlev 
1625084Sjohnlev /*
1630Sstevel@tonic-gate  * Local Static Data
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate static struct psm_ops mach_ops;
1660Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1670Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1703446Smrj  * If non-zero, idle cpus will become "halted" when there's
1710Sstevel@tonic-gate  * no work to do.
1720Sstevel@tonic-gate  */
1733446Smrj int	idle_cpu_use_hlt = 1;
1740Sstevel@tonic-gate 
1755084Sjohnlev #ifndef __xpv
1764481Sbholler /*
1774481Sbholler  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
1784481Sbholler  */
1794481Sbholler int	idle_cpu_prefer_mwait = 1;
1805084Sjohnlev #endif
1813434Sesaxe 
1823434Sesaxe /*ARGSUSED*/
1833434Sesaxe int
1843434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
1853434Sesaxe {
1863434Sesaxe 	switch (hw) {
1873434Sesaxe 	case PGHW_IPIPE:
1883434Sesaxe 		if (x86_feature & (X86_HTT)) {
1893434Sesaxe 			/*
1903434Sesaxe 			 * Hyper-threading is SMT
1913434Sesaxe 			 */
1923434Sesaxe 			return (1);
1933434Sesaxe 		} else {
1943434Sesaxe 			return (0);
1953434Sesaxe 		}
1963434Sesaxe 	case PGHW_CHIP:
1973434Sesaxe 		if (x86_feature & (X86_CMP|X86_HTT))
1983434Sesaxe 			return (1);
1993434Sesaxe 		else
2003434Sesaxe 			return (0);
2014606Sesaxe 	case PGHW_CACHE:
2024606Sesaxe 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
2034606Sesaxe 			return (1);
2044606Sesaxe 		else
2054606Sesaxe 			return (0);
2063434Sesaxe 	default:
2073434Sesaxe 		return (0);
2083434Sesaxe 	}
2093434Sesaxe }
2103434Sesaxe 
2113434Sesaxe /*
2123434Sesaxe  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
2133434Sesaxe  * If pghw_type_t is an unsupported hardware type, then return -1
2143434Sesaxe  */
2153434Sesaxe int
2163434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
2170Sstevel@tonic-gate {
2183434Sesaxe 	id_t pgp_a, pgp_b;
2193434Sesaxe 
2203434Sesaxe 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
2213434Sesaxe 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
2223434Sesaxe 
2233434Sesaxe 	if (pgp_a == -1 || pgp_b == -1)
2243434Sesaxe 		return (-1);
2253434Sesaxe 
2263434Sesaxe 	return (pgp_a == pgp_b);
2273434Sesaxe }
2283434Sesaxe 
2293434Sesaxe /*
2303434Sesaxe  * Return a physical instance identifier for known hardware sharing
2313434Sesaxe  * relationships
2323434Sesaxe  */
2333434Sesaxe id_t
2343434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
2353434Sesaxe {
2363434Sesaxe 	switch (hw) {
2373434Sesaxe 	case PGHW_IPIPE:
2383434Sesaxe 		return (cpuid_get_coreid(cpu));
2394606Sesaxe 	case PGHW_CACHE:
2404606Sesaxe 		return (cpuid_get_last_lvl_cacheid(cpu));
2413434Sesaxe 	case PGHW_CHIP:
2423434Sesaxe 		return (cpuid_get_chipid(cpu));
2433434Sesaxe 	default:
2443434Sesaxe 		return (-1);
2451228Sandrei 	}
2463434Sesaxe }
2470Sstevel@tonic-gate 
2483434Sesaxe int
2493434Sesaxe pg_plat_hw_level(pghw_type_t hw)
2503434Sesaxe {
2513434Sesaxe 	int i;
2523434Sesaxe 	static pghw_type_t hw_hier[] = {
2533434Sesaxe 		PGHW_IPIPE,
2544606Sesaxe 		PGHW_CACHE,
2553434Sesaxe 		PGHW_CHIP,
2563434Sesaxe 		PGHW_NUM_COMPONENTS
2573434Sesaxe 	};
2583434Sesaxe 
2593434Sesaxe 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
2603434Sesaxe 		if (hw_hier[i] == hw)
2613434Sesaxe 			return (i);
2623434Sesaxe 	}
2633434Sesaxe 	return (-1);
2643434Sesaxe }
2653434Sesaxe 
2664606Sesaxe /*
2674606Sesaxe  * Return 1 if CMT load balancing policies should be
2684606Sesaxe  * implemented across instances of the specified hardware
2694606Sesaxe  * sharing relationship.
2704606Sesaxe  */
2714606Sesaxe int
2724606Sesaxe pg_plat_cmt_load_bal_hw(pghw_type_t hw)
2734606Sesaxe {
2744606Sesaxe 	if (hw == PGHW_IPIPE ||
2754606Sesaxe 	    hw == PGHW_FPU ||
2764606Sesaxe 	    hw == PGHW_CHIP ||
2774606Sesaxe 	    hw == PGHW_CACHE)
2784606Sesaxe 		return (1);
2794606Sesaxe 	else
2804606Sesaxe 		return (0);
2814606Sesaxe }
2824606Sesaxe 
2834606Sesaxe 
2844606Sesaxe /*
2854606Sesaxe  * Return 1 if thread affinity polices should be implemented
2864606Sesaxe  * for instances of the specifed hardware sharing relationship.
2874606Sesaxe  */
2884606Sesaxe int
2894606Sesaxe pg_plat_cmt_affinity_hw(pghw_type_t hw)
2904606Sesaxe {
2914606Sesaxe 	if (hw == PGHW_CACHE)
2924606Sesaxe 		return (1);
2934606Sesaxe 	else
2944606Sesaxe 		return (0);
2954606Sesaxe }
2964606Sesaxe 
2973434Sesaxe id_t
2983434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
2993434Sesaxe {
3003434Sesaxe 	return ((id_t)cpuid_get_coreid(cpu));
3013434Sesaxe }
3023434Sesaxe 
3033434Sesaxe void
3043434Sesaxe cmp_set_nosteal_interval(void)
3053434Sesaxe {
3063434Sesaxe 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
3073434Sesaxe 	nosteal_nsec = 100000UL;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Routine to ensure initial callers to hrtime gets 0 as return
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate static hrtime_t
3140Sstevel@tonic-gate dummy_hrtime(void)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	return (0);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /* ARGSUSED */
3200Sstevel@tonic-gate static void
3210Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
3220Sstevel@tonic-gate {}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3253446Smrj  * Idle the present CPU until awoken via an interrupt
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate static void
3283446Smrj cpu_idle(void)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	cpu_t		*cpup = CPU;
3310Sstevel@tonic-gate 	processorid_t	cpun = cpup->cpu_id;
332711Sesaxe 	cpupart_t	*cp = cpup->cpu_part;
3330Sstevel@tonic-gate 	int		hset_update = 1;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * If this CPU is online, and there's multiple CPUs
3370Sstevel@tonic-gate 	 * in the system, then we should notate our halting
3380Sstevel@tonic-gate 	 * by adding ourselves to the partition's halted CPU
3390Sstevel@tonic-gate 	 * bitmap. This allows other CPUs to find/awaken us when
3400Sstevel@tonic-gate 	 * work becomes available.
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
3430Sstevel@tonic-gate 		hset_update = 0;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/*
3460Sstevel@tonic-gate 	 * Add ourselves to the partition's halted CPUs bitmask
3470Sstevel@tonic-gate 	 * and set our HALTED flag, if necessary.
3480Sstevel@tonic-gate 	 *
349711Sesaxe 	 * When a thread becomes runnable, it is placed on the queue
350711Sesaxe 	 * and then the halted cpuset is checked to determine who
351711Sesaxe 	 * (if anyone) should be awoken. We therefore need to first
352711Sesaxe 	 * add ourselves to the halted cpuset, and and then check if there
353711Sesaxe 	 * is any work available.
354711Sesaxe 	 *
3550Sstevel@tonic-gate 	 * Note that memory barriers after updating the HALTED flag
3560Sstevel@tonic-gate 	 * are not necessary since an atomic operation (updating the bitmap)
3570Sstevel@tonic-gate 	 * immediately follows. On x86 the atomic operation acts as a
3580Sstevel@tonic-gate 	 * memory barrier for the update of cpu_disp_flags.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	if (hset_update) {
3610Sstevel@tonic-gate 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
3622722Sjohnlev 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * Check to make sure there's really nothing to do.
367711Sesaxe 	 * Work destined for this CPU may become available after
368711Sesaxe 	 * this check. We'll be notified through the clearing of our
369711Sesaxe 	 * bit in the halted CPU bitmask, and a poke.
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	if (disp_anywork()) {
3720Sstevel@tonic-gate 		if (hset_update) {
3730Sstevel@tonic-gate 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
3742722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
3750Sstevel@tonic-gate 		}
376711Sesaxe 		return;
377711Sesaxe 	}
378711Sesaxe 
379711Sesaxe 	/*
380711Sesaxe 	 * We're on our way to being halted.
381711Sesaxe 	 *
382711Sesaxe 	 * Disable interrupts now, so that we'll awaken immediately
383711Sesaxe 	 * after halting if someone tries to poke us between now and
384711Sesaxe 	 * the time we actually halt.
385711Sesaxe 	 *
386711Sesaxe 	 * We check for the presence of our bit after disabling interrupts.
387711Sesaxe 	 * If it's cleared, we'll return. If the bit is cleared after
388711Sesaxe 	 * we check then the poke will pop us out of the halted state.
389711Sesaxe 	 *
390711Sesaxe 	 * This means that the ordering of the poke and the clearing
391711Sesaxe 	 * of the bit by cpu_wakeup is important.
392711Sesaxe 	 * cpu_wakeup() must clear, then poke.
3933446Smrj 	 * cpu_idle() must disable interrupts, then check for the bit.
394711Sesaxe 	 */
395711Sesaxe 	cli();
396711Sesaxe 
3972722Sjohnlev 	if (hset_update && !CPU_IN_SET(cp->cp_mach->mc_haltset, cpun)) {
398711Sesaxe 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
399711Sesaxe 		sti();
400711Sesaxe 		return;
401711Sesaxe 	}
402711Sesaxe 
403711Sesaxe 	/*
404711Sesaxe 	 * The check for anything locally runnable is here for performance
405711Sesaxe 	 * and isn't needed for correctness. disp_nrunnable ought to be
406711Sesaxe 	 * in our cache still, so it's inexpensive to check, and if there
407711Sesaxe 	 * is anything runnable we won't have to wait for the poke.
408711Sesaxe 	 */
409711Sesaxe 	if (cpup->cpu_disp->disp_nrunnable != 0) {
410711Sesaxe 		if (hset_update) {
411711Sesaxe 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4122722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
413711Sesaxe 		}
4140Sstevel@tonic-gate 		sti();
4150Sstevel@tonic-gate 		return;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4187006Srv207048 	DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C1);
4197006Srv207048 
4203446Smrj 	mach_cpu_idle();
4210Sstevel@tonic-gate 
4227006Srv207048 	DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C0);
4237006Srv207048 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * We're no longer halted
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (hset_update) {
4280Sstevel@tonic-gate 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4292722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
4360Sstevel@tonic-gate  * Otherwise, see if other CPUs in the cpu partition are halted and need to
4370Sstevel@tonic-gate  * be woken up so that they can steal the thread we placed on this CPU.
4380Sstevel@tonic-gate  * This function is only used on MP systems.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate static void
4410Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	uint_t		cpu_found;
4440Sstevel@tonic-gate 	int		result;
4450Sstevel@tonic-gate 	cpupart_t	*cp;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	cp = cpu->cpu_part;
4482722Sjohnlev 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
4490Sstevel@tonic-gate 		/*
4500Sstevel@tonic-gate 		 * Clear the halted bit for that CPU since it will be
4510Sstevel@tonic-gate 		 * poked in a moment.
4520Sstevel@tonic-gate 		 */
4532722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
4540Sstevel@tonic-gate 		/*
4550Sstevel@tonic-gate 		 * We may find the current CPU present in the halted cpuset
4560Sstevel@tonic-gate 		 * if we're in the context of an interrupt that occurred
4573446Smrj 		 * before we had a chance to clear our bit in cpu_idle().
4580Sstevel@tonic-gate 		 * Poking ourself is obviously unnecessary, since if
4590Sstevel@tonic-gate 		 * we're here, we're not halted.
4600Sstevel@tonic-gate 		 */
4610Sstevel@tonic-gate 		if (cpu != CPU)
4620Sstevel@tonic-gate 			poke_cpu(cpu->cpu_id);
4630Sstevel@tonic-gate 		return;
4640Sstevel@tonic-gate 	} else {
4650Sstevel@tonic-gate 		/*
4660Sstevel@tonic-gate 		 * This cpu isn't halted, but it's idle or undergoing a
4670Sstevel@tonic-gate 		 * context switch. No need to awaken anyone else.
4680Sstevel@tonic-gate 		 */
4690Sstevel@tonic-gate 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
4700Sstevel@tonic-gate 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
4710Sstevel@tonic-gate 			return;
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * No need to wake up other CPUs if the thread we just enqueued
4760Sstevel@tonic-gate 	 * is bound.
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	if (bound)
4790Sstevel@tonic-gate 		return;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * See if there's any other halted CPUs. If there are, then
4840Sstevel@tonic-gate 	 * select one, and awaken it.
4850Sstevel@tonic-gate 	 * It's possible that after we find a CPU, somebody else
4860Sstevel@tonic-gate 	 * will awaken it before we get the chance.
4870Sstevel@tonic-gate 	 * In that case, look again.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	do {
4902722Sjohnlev 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
4910Sstevel@tonic-gate 		if (cpu_found == CPUSET_NOTINSET)
4920Sstevel@tonic-gate 			return;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
4952722Sjohnlev 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
4960Sstevel@tonic-gate 	} while (result < 0);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (cpu_found != CPU->cpu_id)
4990Sstevel@tonic-gate 		poke_cpu(cpu_found);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5025084Sjohnlev #ifndef __xpv
5034481Sbholler /*
5044481Sbholler  * Idle the present CPU until awoken via touching its monitored line
5054481Sbholler  */
5064481Sbholler static void
5074481Sbholler cpu_idle_mwait(void)
5084481Sbholler {
5094481Sbholler 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
5104481Sbholler 	cpu_t			*cpup = CPU;
5114481Sbholler 	processorid_t		cpun = cpup->cpu_id;
5124481Sbholler 	cpupart_t		*cp = cpup->cpu_part;
5134481Sbholler 	int			hset_update = 1;
5144481Sbholler 
5154481Sbholler 	/*
5164481Sbholler 	 * Set our mcpu_mwait here, so we can tell if anyone trys to
5174481Sbholler 	 * wake us between now and when we call mwait.  No other cpu will
5184481Sbholler 	 * attempt to set our mcpu_mwait until we add ourself to the haltset.
5194481Sbholler 	 */
5204481Sbholler 	*mcpu_mwait = MWAIT_HALTED;
5214481Sbholler 
5224481Sbholler 	/*
5234481Sbholler 	 * If this CPU is online, and there's multiple CPUs
5244481Sbholler 	 * in the system, then we should notate our halting
5254481Sbholler 	 * by adding ourselves to the partition's halted CPU
5264481Sbholler 	 * bitmap. This allows other CPUs to find/awaken us when
5274481Sbholler 	 * work becomes available.
5284481Sbholler 	 */
5294481Sbholler 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
5304481Sbholler 		hset_update = 0;
5314481Sbholler 
5324481Sbholler 	/*
5334481Sbholler 	 * Add ourselves to the partition's halted CPUs bitmask
5344481Sbholler 	 * and set our HALTED flag, if necessary.
5354481Sbholler 	 *
5364481Sbholler 	 * When a thread becomes runnable, it is placed on the queue
5374481Sbholler 	 * and then the halted cpuset is checked to determine who
5384481Sbholler 	 * (if anyone) should be awoken. We therefore need to first
5394481Sbholler 	 * add ourselves to the halted cpuset, and and then check if there
5404481Sbholler 	 * is any work available.
5414481Sbholler 	 *
5424481Sbholler 	 * Note that memory barriers after updating the HALTED flag
5434481Sbholler 	 * are not necessary since an atomic operation (updating the bitmap)
5444481Sbholler 	 * immediately follows. On x86 the atomic operation acts as a
5454481Sbholler 	 * memory barrier for the update of cpu_disp_flags.
5464481Sbholler 	 */
5474481Sbholler 	if (hset_update) {
5484481Sbholler 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
5494481Sbholler 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
5504481Sbholler 	}
5514481Sbholler 
5524481Sbholler 	/*
5534481Sbholler 	 * Check to make sure there's really nothing to do.
5544481Sbholler 	 * Work destined for this CPU may become available after
5554481Sbholler 	 * this check. We'll be notified through the clearing of our
5564481Sbholler 	 * bit in the halted CPU bitmask, and a write to our mcpu_mwait.
5574481Sbholler 	 *
5584481Sbholler 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
5594481Sbholler 	 */
5604481Sbholler 	if (disp_anywork()) {
5614481Sbholler 		if (hset_update) {
5624481Sbholler 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5634481Sbholler 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5644481Sbholler 		}
5654481Sbholler 		return;
5664481Sbholler 	}
5674481Sbholler 
5684481Sbholler 	/*
5694481Sbholler 	 * We're on our way to being halted.
5704481Sbholler 	 * To avoid a lost wakeup, arm the monitor before checking if another
5714481Sbholler 	 * cpu wrote to mcpu_mwait to wake us up.
5724481Sbholler 	 */
5734481Sbholler 	i86_monitor(mcpu_mwait, 0, 0);
5744481Sbholler 	if (*mcpu_mwait == MWAIT_HALTED) {
5755864Sesaxe 		DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C1);
5765864Sesaxe 
5774481Sbholler 		tlb_going_idle();
5784481Sbholler 		i86_mwait(0, 0);
5794481Sbholler 		tlb_service();
5805864Sesaxe 
5815864Sesaxe 		DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C0);
5824481Sbholler 	}
5834481Sbholler 
5844481Sbholler 	/*
5854481Sbholler 	 * We're no longer halted
5864481Sbholler 	 */
5874481Sbholler 	if (hset_update) {
5884481Sbholler 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5894481Sbholler 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5904481Sbholler 	}
5914481Sbholler }
5924481Sbholler 
5934481Sbholler /*
5944481Sbholler  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
5954481Sbholler  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
5964481Sbholler  * need to be woken up so that they can steal the thread we placed on this CPU.
5974481Sbholler  * This function is only used on MP systems.
5984481Sbholler  */
5994481Sbholler static void
6004481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
6014481Sbholler {
6024481Sbholler 	cpupart_t	*cpu_part;
6034481Sbholler 	uint_t		cpu_found;
6044481Sbholler 	int		result;
6054481Sbholler 
6064481Sbholler 	cpu_part = cp->cpu_part;
6074481Sbholler 
6084481Sbholler 	/*
6094481Sbholler 	 * Clear the halted bit for that CPU since it will be woken up
6104481Sbholler 	 * in a moment.
6114481Sbholler 	 */
6124481Sbholler 	if (CPU_IN_SET(cpu_part->cp_mach->mc_haltset, cp->cpu_id)) {
6134481Sbholler 		/*
6144481Sbholler 		 * Clear the halted bit for that CPU since it will be
6154481Sbholler 		 * poked in a moment.
6164481Sbholler 		 */
6174481Sbholler 		CPUSET_ATOMIC_DEL(cpu_part->cp_mach->mc_haltset, cp->cpu_id);
6184481Sbholler 		/*
6194481Sbholler 		 * We may find the current CPU present in the halted cpuset
6204481Sbholler 		 * if we're in the context of an interrupt that occurred
6214481Sbholler 		 * before we had a chance to clear our bit in cpu_idle().
6224481Sbholler 		 * Waking ourself is obviously unnecessary, since if
6234481Sbholler 		 * we're here, we're not halted.
6244481Sbholler 		 *
6254481Sbholler 		 * monitor/mwait wakeup via writing to our cache line is
6264481Sbholler 		 * harmless and less expensive than always checking if we
6274481Sbholler 		 * are waking ourself which is an uncommon case.
6284481Sbholler 		 */
6294481Sbholler 		MWAIT_WAKEUP(cp);	/* write to monitored line */
6304481Sbholler 		return;
6314481Sbholler 	} else {
6324481Sbholler 		/*
6334481Sbholler 		 * This cpu isn't halted, but it's idle or undergoing a
6344481Sbholler 		 * context switch. No need to awaken anyone else.
6354481Sbholler 		 */
6364481Sbholler 		if (cp->cpu_thread == cp->cpu_idle_thread ||
6374481Sbholler 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
6384481Sbholler 			return;
6394481Sbholler 	}
6404481Sbholler 
6414481Sbholler 	/*
6424481Sbholler 	 * No need to wake up other CPUs if the thread we just enqueued
6434481Sbholler 	 * is bound.
6444481Sbholler 	 */
6454481Sbholler 	if (bound)
6464481Sbholler 		return;
6474481Sbholler 
6484481Sbholler 
6494481Sbholler 	/*
6504481Sbholler 	 * See if there's any other halted CPUs. If there are, then
6514481Sbholler 	 * select one, and awaken it.
6524481Sbholler 	 * It's possible that after we find a CPU, somebody else
6534481Sbholler 	 * will awaken it before we get the chance.
6544481Sbholler 	 * In that case, look again.
6554481Sbholler 	 */
6564481Sbholler 	do {
6574481Sbholler 		CPUSET_FIND(cpu_part->cp_mach->mc_haltset, cpu_found);
6584481Sbholler 		if (cpu_found == CPUSET_NOTINSET)
6594481Sbholler 			return;
6604481Sbholler 
6614481Sbholler 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
6624481Sbholler 		CPUSET_ATOMIC_XDEL(cpu_part->cp_mach->mc_haltset, cpu_found,
6634481Sbholler 		    result);
6644481Sbholler 	} while (result < 0);
6654481Sbholler 
6664481Sbholler 	/*
6674481Sbholler 	 * Do not check if cpu_found is ourself as monitor/mwait wakeup is
6684481Sbholler 	 * cheap.
6694481Sbholler 	 */
6704481Sbholler 	MWAIT_WAKEUP(cpu[cpu_found]);	/* write to monitored line */
6714481Sbholler }
6725084Sjohnlev #endif
6734481Sbholler 
6743446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
6753446Smrj 
6760Sstevel@tonic-gate static int
6770Sstevel@tonic-gate mp_disable_intr(int cpun)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * switch to the offline cpu
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	affinity_set(cpun);
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * raise ipl to just below cross call
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	splx(XC_MED_PIL-1);
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 *	set base spl to prevent the next swtch to idle from
6890Sstevel@tonic-gate 	 *	lowering back to ipl 0
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1));
6920Sstevel@tonic-gate 	set_base_spl();
6930Sstevel@tonic-gate 	affinity_clear();
6940Sstevel@tonic-gate 	return (DDI_SUCCESS);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate static void
6980Sstevel@tonic-gate mp_enable_intr(int cpun)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	/*
7010Sstevel@tonic-gate 	 * switch to the online cpu
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	affinity_set(cpun);
7040Sstevel@tonic-gate 	/*
7050Sstevel@tonic-gate 	 * clear the interrupt active mask
7060Sstevel@tonic-gate 	 */
7070Sstevel@tonic-gate 	CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1));
7080Sstevel@tonic-gate 	set_base_spl();
7090Sstevel@tonic-gate 	(void) spl0();
7100Sstevel@tonic-gate 	affinity_clear();
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate static void
7140Sstevel@tonic-gate mach_get_platform(int owner)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	void		**srv_opsp;
7170Sstevel@tonic-gate 	void		**clt_opsp;
7180Sstevel@tonic-gate 	int		i;
7190Sstevel@tonic-gate 	int		total_ops;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* fix up psm ops */
7220Sstevel@tonic-gate 	srv_opsp = (void **)mach_set[0];
7230Sstevel@tonic-gate 	clt_opsp = (void **)mach_set[owner];
7240Sstevel@tonic-gate 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
7250Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops_ver01) /
7264481Sbholler 		    sizeof (void (*)(void));
7270Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
7280Sstevel@tonic-gate 		/* no psm_notify_func */
7290Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
7300Sstevel@tonic-gate 		    sizeof (void (*)(void));
7310Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
7320Sstevel@tonic-gate 		/* no psm_timer funcs */
7330Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
7340Sstevel@tonic-gate 		    sizeof (void (*)(void));
7350Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
7360Sstevel@tonic-gate 		/* no psm_preshutdown function */
7370Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
7380Sstevel@tonic-gate 		    sizeof (void (*)(void));
7390Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
7400Sstevel@tonic-gate 		/* no psm_preshutdown function */
7410Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
7420Sstevel@tonic-gate 		    sizeof (void (*)(void));
7430Sstevel@tonic-gate 	else
7440Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/*
7470Sstevel@tonic-gate 	 * Save the version of the PSM module, in case we need to
7480Sstevel@tonic-gate 	 * bahave differently based on version.
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	mach_ver[0] = mach_ver[owner];
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	for (i = 0; i < total_ops; i++)
7530Sstevel@tonic-gate 		if (clt_opsp[i] != NULL)
7540Sstevel@tonic-gate 			srv_opsp[i] = clt_opsp[i];
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate static void
7580Sstevel@tonic-gate mach_construct_info()
7590Sstevel@tonic-gate {
7603446Smrj 	struct psm_sw *swp;
7610Sstevel@tonic-gate 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
7620Sstevel@tonic-gate 	int	conflict_owner = 0;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if (psmsw->psw_forw == psmsw)
7650Sstevel@tonic-gate 		panic("No valid PSM modules found");
7660Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
7670Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7680Sstevel@tonic-gate 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
7690Sstevel@tonic-gate 			continue;
7700Sstevel@tonic-gate 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
7710Sstevel@tonic-gate 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
7720Sstevel@tonic-gate 		mach_cnt[swp->psw_infop->p_owner]++;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/* check to see are there any conflicts */
7790Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
7800Sstevel@tonic-gate 		conflict_owner = PSM_OWN_EXCLUSIVE;
7810Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
7820Sstevel@tonic-gate 		conflict_owner = PSM_OWN_OVERRIDE;
7830Sstevel@tonic-gate 	if (conflict_owner) {
7840Sstevel@tonic-gate 		/* remove all psm modules except uppc */
7850Sstevel@tonic-gate 		cmn_err(CE_WARN,
7864481Sbholler 		    "Conflicts detected on the following PSM modules:");
7870Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
7880Sstevel@tonic-gate 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7890Sstevel@tonic-gate 			if (swp->psw_infop->p_owner == conflict_owner)
7900Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s ",
7914481Sbholler 				    swp->psw_infop->p_mach_idstring);
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
7940Sstevel@tonic-gate 		cmn_err(CE_WARN,
7954481Sbholler 		    "Setting the system back to SINGLE processor mode!");
7960Sstevel@tonic-gate 		cmn_err(CE_WARN,
7970Sstevel@tonic-gate 		    "Please edit /etc/mach to remove the invalid PSM module.");
7980Sstevel@tonic-gate 		return;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (mach_set[PSM_OWN_EXCLUSIVE])
8020Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_EXCLUSIVE);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (mach_set[PSM_OWN_OVERRIDE])
8050Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_OVERRIDE);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate static void
8090Sstevel@tonic-gate mach_init()
8100Sstevel@tonic-gate {
8113446Smrj 	struct psm_ops  *pops;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	mach_construct_info();
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	pops = mach_set[0];
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/* register the interrupt and clock initialization rotuines */
8180Sstevel@tonic-gate 	picinitf = mach_picinit;
8190Sstevel@tonic-gate 	clkinitf = mach_clkinit;
8200Sstevel@tonic-gate 	psm_get_clockirq = pops->psm_get_clockirq;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/* register the interrupt setup code */
8230Sstevel@tonic-gate 	slvltovect = mach_softlvl_to_vect;
8240Sstevel@tonic-gate 	addspl	= pops->psm_addspl;
8250Sstevel@tonic-gate 	delspl	= pops->psm_delspl;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (pops->psm_translate_irq)
8280Sstevel@tonic-gate 		psm_translate_irq = pops->psm_translate_irq;
8290Sstevel@tonic-gate 	if (pops->psm_intr_ops)
8300Sstevel@tonic-gate 		psm_intr_ops = pops->psm_intr_ops;
8313446Smrj 
8323446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
8333446Smrj 	/*
8343446Smrj 	 * Time-of-day functionality now handled in TOD modules.
8353446Smrj 	 * (Warn about PSM modules that think that we're going to use
8363446Smrj 	 * their ops vectors.)
8373446Smrj 	 */
8383446Smrj 	if (pops->psm_tod_get)
8393446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
8403446Smrj 		    (void *)pops->psm_tod_get);
8413446Smrj 
8423446Smrj 	if (pops->psm_tod_set)
8433446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
8443446Smrj 		    (void *)pops->psm_tod_set);
8453446Smrj #endif
8463446Smrj 
8470Sstevel@tonic-gate 	if (pops->psm_notify_error) {
8480Sstevel@tonic-gate 		psm_notify_error = mach_notify_error;
8490Sstevel@tonic-gate 		notify_error = pops->psm_notify_error;
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	(*pops->psm_softinit)();
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/*
8550Sstevel@tonic-gate 	 * Initialize the dispatcher's function hooks
8564481Sbholler 	 * to enable CPU halting when idle.
8575045Sbholler 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
8585045Sbholler 	 * or idle_cpu_prefer_mwait is not set.
8594481Sbholler 	 * Allocate monitor/mwait buffer for cpu0.
8600Sstevel@tonic-gate 	 */
8614481Sbholler 	if (idle_cpu_use_hlt) {
8625084Sjohnlev 		idle_cpu = cpu_idle;
8635084Sjohnlev #ifndef __xpv
8644481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
8655045Sbholler 			CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
8665045Sbholler 			/*
8675045Sbholler 			 * Protect ourself from insane mwait size.
8685045Sbholler 			 */
8695045Sbholler 			if (CPU->cpu_m.mcpu_mwait == NULL) {
8705045Sbholler #ifdef DEBUG
8715045Sbholler 				cmn_err(CE_NOTE, "Using hlt idle.  Cannot "
8725045Sbholler 				    "handle cpu 0 mwait size.");
8735045Sbholler #endif
8745045Sbholler 				idle_cpu_prefer_mwait = 0;
8755045Sbholler 				idle_cpu = cpu_idle;
8765045Sbholler 			} else {
8775045Sbholler 				idle_cpu = cpu_idle_mwait;
8785045Sbholler 			}
8794481Sbholler 		} else {
8804481Sbholler 			idle_cpu = cpu_idle;
8814481Sbholler 		}
8825084Sjohnlev #endif
8834481Sbholler 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	mach_smpinit();
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate static void
8890Sstevel@tonic-gate mach_smpinit(void)
8900Sstevel@tonic-gate {
8912006Sandrei 	struct psm_ops  *pops;
8922006Sandrei 	processorid_t cpu_id;
8932006Sandrei 	int cnt;
8942006Sandrei 	cpuset_t cpumask;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	pops = mach_set[0];
8976336Sbholler 	CPUSET_ZERO(cpumask);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	cpu_id = -1;
9000Sstevel@tonic-gate 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
9016336Sbholler 	for (cnt = 0; cpu_id != -1; cnt++) {
9022006Sandrei 		CPUSET_ADD(cpumask, cpu_id);
9030Sstevel@tonic-gate 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	mp_cpus = cpumask;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/* MP related routines */
9090Sstevel@tonic-gate 	ap_mlsetup = pops->psm_post_cpu_start;
9100Sstevel@tonic-gate 	send_dirintf = pops->psm_send_ipi;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/* optional MP related routines */
9130Sstevel@tonic-gate 	if (pops->psm_shutdown)
9140Sstevel@tonic-gate 		psm_shutdownf = pops->psm_shutdown;
9150Sstevel@tonic-gate 	if (pops->psm_preshutdown)
9160Sstevel@tonic-gate 		psm_preshutdownf = pops->psm_preshutdown;
9170Sstevel@tonic-gate 	if (pops->psm_notify_func)
9180Sstevel@tonic-gate 		psm_notifyf = pops->psm_notify_func;
9190Sstevel@tonic-gate 	if (pops->psm_set_idlecpu)
9200Sstevel@tonic-gate 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
9210Sstevel@tonic-gate 	if (pops->psm_unset_idlecpu)
9220Sstevel@tonic-gate 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	psm_clkinit = pops->psm_clkinit;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (pops->psm_timer_reprogram)
9270Sstevel@tonic-gate 		psm_timer_reprogram = pops->psm_timer_reprogram;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (pops->psm_timer_enable)
9300Sstevel@tonic-gate 		psm_timer_enable = pops->psm_timer_enable;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if (pops->psm_timer_disable)
9330Sstevel@tonic-gate 		psm_timer_disable = pops->psm_timer_disable;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (pops->psm_post_cyclic_setup)
9360Sstevel@tonic-gate 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
9370Sstevel@tonic-gate 
9385295Srandyf 	if (pops->psm_state)
9395295Srandyf 		psm_state = pops->psm_state;
9405295Srandyf 
941*7113Sbholler 	/*
942*7113Sbholler 	 * Set these vectors here so they can be used by Suspend/Resume
943*7113Sbholler 	 * on UP machines.
944*7113Sbholler 	 */
945*7113Sbholler 	if (pops->psm_disable_intr)
946*7113Sbholler 		psm_disable_intr = pops->psm_disable_intr;
947*7113Sbholler 	if (pops->psm_enable_intr)
948*7113Sbholler 		psm_enable_intr  = pops->psm_enable_intr;
949*7113Sbholler 
950*7113Sbholler 	/* check for multiple CPUs */
9510Sstevel@tonic-gate 	if (cnt < 2)
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/* check for MP platforms */
9550Sstevel@tonic-gate 	if (pops->psm_cpu_start == NULL)
9560Sstevel@tonic-gate 		return;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * Set the dispatcher hook to enable cpu "wake up"
9600Sstevel@tonic-gate 	 * when a thread becomes runnable.
9610Sstevel@tonic-gate 	 */
9625084Sjohnlev 	if (idle_cpu_use_hlt) {
9635084Sjohnlev 		disp_enq_thread = cpu_wakeup;
9645084Sjohnlev #ifndef __xpv
9654481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
9664481Sbholler 			disp_enq_thread = cpu_wakeup_mwait;
9675084Sjohnlev #endif
9685084Sjohnlev 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	psm_get_ipivect = pops->psm_get_ipivect;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr",
9734481Sbholler 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
9744481Sbholler 	    (caddr_t)X_CALL_HIPRI, NULL, NULL, NULL);
9750Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr",
9764481Sbholler 	    (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO),
9774481Sbholler 	    (caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate static void
9830Sstevel@tonic-gate mach_picinit()
9840Sstevel@tonic-gate {
9852006Sandrei 	struct psm_ops  *pops;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	pops = mach_set[0];
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/* register the interrupt handlers */
9900Sstevel@tonic-gate 	setlvl = pops->psm_intr_enter;
9910Sstevel@tonic-gate 	setlvlx = pops->psm_intr_exit;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/* initialize the interrupt hardware */
9940Sstevel@tonic-gate 	(*pops->psm_picinit)();
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/* set interrupt mask for current ipl */
9970Sstevel@tonic-gate 	setspl = pops->psm_setspl;
9983446Smrj 	cli();
9990Sstevel@tonic-gate 	setspl(CPU->cpu_pri);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate uint_t	cpu_freq;	/* MHz */
10030Sstevel@tonic-gate uint64_t cpu_freq_hz;	/* measured (in hertz) */
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate #define	MEGA_HZ		1000000
10060Sstevel@tonic-gate 
10075084Sjohnlev #ifdef __xpv
10085084Sjohnlev 
10095084Sjohnlev int xpv_cpufreq_workaround = 1;
10105084Sjohnlev int xpv_cpufreq_verbose = 0;
10115084Sjohnlev 
10125084Sjohnlev #else	/* __xpv */
10135084Sjohnlev 
10140Sstevel@tonic-gate static uint64_t
10150Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	uint64_t cpu_hz;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if ((pit_counter == 0) || (*processor_clks == 0) ||
10200Sstevel@tonic-gate 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
10210Sstevel@tonic-gate 		return (0);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	return (cpu_hz);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10285084Sjohnlev #endif	/* __xpv */
10295084Sjohnlev 
10300Sstevel@tonic-gate static uint64_t
10310Sstevel@tonic-gate mach_getcpufreq(void)
10320Sstevel@tonic-gate {
10335084Sjohnlev #if defined(__xpv)
10345084Sjohnlev 	vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
10355084Sjohnlev 	uint64_t cpu_hz;
10365084Sjohnlev 
10375084Sjohnlev 	/*
10385084Sjohnlev 	 * During dom0 bringup, it was noted that on at least one older
10395084Sjohnlev 	 * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul
10405084Sjohnlev 	 * value that is quite wrong (the 3.06GHz clock was reported
10415084Sjohnlev 	 * as 4.77GHz)
10425084Sjohnlev 	 *
10435084Sjohnlev 	 * The curious thing is, that if you stop the kernel at entry,
10445084Sjohnlev 	 * breakpoint here and inspect the value with kmdb, the value
10455084Sjohnlev 	 * is correct - but if you don't stop and simply enable the
10465084Sjohnlev 	 * printf statement (below), you can see the bad value printed
10475084Sjohnlev 	 * here.  Almost as if something kmdb did caused the hypervisor to
10485084Sjohnlev 	 * figure it out correctly.  And, note that the hypervisor
10495084Sjohnlev 	 * eventually -does- figure it out correctly ... if you look at
10505084Sjohnlev 	 * the field later in the life of dom0, it is correct.
10515084Sjohnlev 	 *
10525084Sjohnlev 	 * For now, on dom0, we employ a slightly cheesy workaround of
10535084Sjohnlev 	 * using the DOM0_PHYSINFO hypercall.
10545084Sjohnlev 	 */
10555084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
10565084Sjohnlev 		xen_sysctl_t op0, *op = &op0;
10575084Sjohnlev 
10585084Sjohnlev 		op->cmd = XEN_SYSCTL_physinfo;
10595084Sjohnlev 		op->interface_version = XEN_SYSCTL_INTERFACE_VERSION;
10605084Sjohnlev 		if (HYPERVISOR_sysctl(op) != 0)
10615084Sjohnlev 			panic("physinfo op refused");
10625084Sjohnlev 
10635084Sjohnlev 		cpu_hz = 1000 * (uint64_t)op->u.physinfo.cpu_khz;
10645084Sjohnlev 	} else {
10655084Sjohnlev 		cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
10665084Sjohnlev 
10675084Sjohnlev 		if (vti->tsc_shift < 0)
10685084Sjohnlev 			cpu_hz <<= -vti->tsc_shift;
10695084Sjohnlev 		else
10705084Sjohnlev 			cpu_hz >>= vti->tsc_shift;
10715084Sjohnlev 	}
10725084Sjohnlev 
10735084Sjohnlev 	if (xpv_cpufreq_verbose)
10745084Sjohnlev 		printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
10755084Sjohnlev 		    "cpu_hz %" PRId64 "Hz\n",
10765084Sjohnlev 		    vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
10775084Sjohnlev 
10785084Sjohnlev 	return (cpu_hz);
10795084Sjohnlev #else	/* __xpv */
10800Sstevel@tonic-gate 	uint32_t pit_counter;
10810Sstevel@tonic-gate 	uint64_t processor_clks;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (x86_feature & X86_TSC) {
10840Sstevel@tonic-gate 		/*
10850Sstevel@tonic-gate 		 * We have a TSC. freq_tsc() knows how to measure the number
10860Sstevel@tonic-gate 		 * of clock cycles sampled against the PIT.
10870Sstevel@tonic-gate 		 */
10883446Smrj 		ulong_t flags = clear_int_flag();
10890Sstevel@tonic-gate 		processor_clks = freq_tsc(&pit_counter);
10903446Smrj 		restore_int_flag(flags);
10910Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
10920Sstevel@tonic-gate 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
10930Sstevel@tonic-gate #if defined(__amd64)
10940Sstevel@tonic-gate 		panic("mach_getcpufreq: no TSC!");
10950Sstevel@tonic-gate #elif defined(__i386)
10960Sstevel@tonic-gate 		/*
10970Sstevel@tonic-gate 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
10980Sstevel@tonic-gate 		 * for which freq_notsc() knows how to measure the number of
10990Sstevel@tonic-gate 		 * elapsed clock cycles sampled against the PIT
11000Sstevel@tonic-gate 		 */
11013446Smrj 		ulong_t flags = clear_int_flag();
11020Sstevel@tonic-gate 		processor_clks = freq_notsc(&pit_counter);
11033446Smrj 		restore_int_flag(flags);
11040Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
11050Sstevel@tonic-gate #endif	/* __i386 */
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/* We do not know how to calculate cpu frequency for this cpu. */
11090Sstevel@tonic-gate 	return (0);
11105084Sjohnlev #endif	/* __xpv */
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /*
11140Sstevel@tonic-gate  * If the clock speed of a cpu is found to be reported incorrectly, do not add
11150Sstevel@tonic-gate  * to this array, instead improve the accuracy of the algorithm that determines
11160Sstevel@tonic-gate  * the clock speed of the processor or extend the implementation to support the
11170Sstevel@tonic-gate  * vendor as appropriate. This is here only to support adjusting the speed on
11180Sstevel@tonic-gate  * older slower processors that mach_fixcpufreq() would not be able to account
11190Sstevel@tonic-gate  * for otherwise.
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * On fast processors the clock frequency that is measured may be off by
11250Sstevel@tonic-gate  * a few MHz from the value printed on the part. This is a combination of
11260Sstevel@tonic-gate  * the factors that for such fast parts being off by this much is within
11270Sstevel@tonic-gate  * the tolerances for manufacture and because of the difficulties in the
11280Sstevel@tonic-gate  * measurement that can lead to small error. This function uses some
11290Sstevel@tonic-gate  * heuristics in order to tweak the value that was measured to match what
11300Sstevel@tonic-gate  * is most likely printed on the part.
11310Sstevel@tonic-gate  *
11320Sstevel@tonic-gate  * Some examples:
11330Sstevel@tonic-gate  * 	AMD Athlon 1000 mhz measured as 998 mhz
11340Sstevel@tonic-gate  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
11350Sstevel@tonic-gate  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
11360Sstevel@tonic-gate  *
11370Sstevel@tonic-gate  * If in the future this function is no longer sufficient to correct
11380Sstevel@tonic-gate  * for the error in the measurement, then the algorithm used to perform
11390Sstevel@tonic-gate  * the measurement will have to be improved in order to increase accuracy
11400Sstevel@tonic-gate  * rather than adding horrible and questionable kludges here.
11410Sstevel@tonic-gate  *
11420Sstevel@tonic-gate  * This is called after the cyclics subsystem because of the potential
11430Sstevel@tonic-gate  * that the heuristics within may give a worse estimate of the clock
11440Sstevel@tonic-gate  * frequency than the value that was measured.
11450Sstevel@tonic-gate  */
11460Sstevel@tonic-gate static void
11470Sstevel@tonic-gate mach_fixcpufreq(void)
11480Sstevel@tonic-gate {
11490Sstevel@tonic-gate 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	freq = (uint32_t)cpu_freq;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/*
11540Sstevel@tonic-gate 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
11550Sstevel@tonic-gate 	 * measured speed taking into account that the 667 MHz parts were
11560Sstevel@tonic-gate 	 * the first to round-up.
11570Sstevel@tonic-gate 	 */
11580Sstevel@tonic-gate 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
11590Sstevel@tonic-gate 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
11600Sstevel@tonic-gate 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
11630Sstevel@tonic-gate 	mul = (freq + 25) / 50;
11640Sstevel@tonic-gate 	near50 = mul * 50;
11650Sstevel@tonic-gate 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	/* Find the closer of the two */
11680Sstevel@tonic-gate 	if (delta66 < delta50) {
11690Sstevel@tonic-gate 		fixed = near66;
11700Sstevel@tonic-gate 		delta = delta66;
11710Sstevel@tonic-gate 	} else {
11720Sstevel@tonic-gate 		fixed = near50;
11730Sstevel@tonic-gate 		delta = delta50;
11740Sstevel@tonic-gate 	}
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	if (fixed > INT_MAX)
11770Sstevel@tonic-gate 		return;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/*
11800Sstevel@tonic-gate 	 * Some older parts have a core clock frequency that is not an
11810Sstevel@tonic-gate 	 * integral multiple of 50 or 66 MHz. Check if one of the old
11820Sstevel@tonic-gate 	 * clock frequencies is closer to the measured value than any
11830Sstevel@tonic-gate 	 * of the integral multiples of 50 an 66, and if so set fixed
11840Sstevel@tonic-gate 	 * and delta appropriately to represent the closest value.
11850Sstevel@tonic-gate 	 */
11860Sstevel@tonic-gate 	i = sizeof (x86_cpu_freq) / sizeof (int);
11870Sstevel@tonic-gate 	while (i > 0) {
11880Sstevel@tonic-gate 		i--;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		if (x86_cpu_freq[i] <= freq) {
11910Sstevel@tonic-gate 			mul = freq - x86_cpu_freq[i];
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 			if (mul < delta) {
11940Sstevel@tonic-gate 				fixed = x86_cpu_freq[i];
11950Sstevel@tonic-gate 				delta = mul;
11960Sstevel@tonic-gate 			}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 			break;
11990Sstevel@tonic-gate 		}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		mul = x86_cpu_freq[i] - freq;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		if (mul < delta) {
12040Sstevel@tonic-gate 			fixed = x86_cpu_freq[i];
12050Sstevel@tonic-gate 			delta = mul;
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	/*
12100Sstevel@tonic-gate 	 * Set a reasonable maximum for how much to correct the measured
12110Sstevel@tonic-gate 	 * result by. This check is here to prevent the adjustment made
12120Sstevel@tonic-gate 	 * by this function from being more harm than good. It is entirely
12130Sstevel@tonic-gate 	 * possible that in the future parts will be made that are not
12140Sstevel@tonic-gate 	 * integral multiples of 66 or 50 in clock frequency or that
12150Sstevel@tonic-gate 	 * someone may overclock a part to some odd frequency. If the
12160Sstevel@tonic-gate 	 * measured value is farther from the corrected value than
12170Sstevel@tonic-gate 	 * allowed, then assume the corrected value is in error and use
12180Sstevel@tonic-gate 	 * the measured value.
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	if (6 < delta)
12210Sstevel@tonic-gate 		return;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	cpu_freq = (int)fixed;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate static int
12280Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	uint64_t cpu_mhz;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/* Round to nearest MHZ */
12330Sstevel@tonic-gate 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if (cpu_mhz > INT_MAX)
12360Sstevel@tonic-gate 		return (0);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	return ((int)cpu_mhz);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate static int
12440Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
12450Sstevel@tonic-gate {
12463446Smrj 	struct psm_ops  *pops;
12470Sstevel@tonic-gate 	int resolution;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	pops = mach_set[0];
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	cpu_freq_hz = mach_getcpufreq();
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	cpu_freq = machhztomhz(cpu_freq_hz);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
12560Sstevel@tonic-gate 		tsc_gethrtime_enable = 0;
12570Sstevel@tonic-gate 
12585084Sjohnlev #ifndef __xpv
12590Sstevel@tonic-gate 	if (tsc_gethrtime_enable) {
12600Sstevel@tonic-gate 		tsc_hrtimeinit(cpu_freq_hz);
12615084Sjohnlev 	} else
12625084Sjohnlev #endif
12635084Sjohnlev 	{
12640Sstevel@tonic-gate 		if (pops->psm_hrtimeinit)
12650Sstevel@tonic-gate 			(*pops->psm_hrtimeinit)();
12660Sstevel@tonic-gate 		gethrtimef = pops->psm_gethrtime;
12670Sstevel@tonic-gate 		gethrtimeunscaledf = gethrtimef;
12680Sstevel@tonic-gate 		/* scalehrtimef will remain dummy */
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	mach_fixcpufreq();
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
12745084Sjohnlev 		if (preferred_mode == TIMER_ONESHOT) {
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 			resolution = (*pops->psm_clkinit)(0);
12770Sstevel@tonic-gate 			if (resolution != 0)  {
12780Sstevel@tonic-gate 				*set_mode = TIMER_ONESHOT;
12790Sstevel@tonic-gate 				return (resolution);
12800Sstevel@tonic-gate 			}
12810Sstevel@tonic-gate 		}
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 		/*
12840Sstevel@tonic-gate 		 * either periodic mode was requested or could not set to
12850Sstevel@tonic-gate 		 * one-shot mode
12860Sstevel@tonic-gate 		 */
12870Sstevel@tonic-gate 		resolution = (*pops->psm_clkinit)(hz);
12880Sstevel@tonic-gate 		/*
12890Sstevel@tonic-gate 		 * psm should be able to do periodic, so we do not check
12900Sstevel@tonic-gate 		 * for return value of psm_clkinit here.
12910Sstevel@tonic-gate 		 */
12920Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
12930Sstevel@tonic-gate 		return (resolution);
12940Sstevel@tonic-gate 	} else {
12950Sstevel@tonic-gate 		/*
12960Sstevel@tonic-gate 		 * PSMI interface prior to PSMI_3 does not define a return
12970Sstevel@tonic-gate 		 * value for psm_clkinit, so the return value is ignored.
12980Sstevel@tonic-gate 		 */
12990Sstevel@tonic-gate 		(void) (*pops->psm_clkinit)(hz);
13000Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
13010Sstevel@tonic-gate 		return (nsec_per_tick);
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13054652Scwb 
1306999Slq150181 /*ARGSUSED*/
13070Sstevel@tonic-gate static int
13083446Smrj mach_softlvl_to_vect(int ipl)
13090Sstevel@tonic-gate {
13104652Scwb 	setsoftint = av_set_softint_pending;
13114652Scwb 	kdisetsoftint = kdi_av_set_softint_pending;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	return (PSM_SV_SOFTWARE);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate 
13163446Smrj #ifdef DEBUG
13173446Smrj /*
13183446Smrj  * This is here to allow us to simulate cpus that refuse to start.
13193446Smrj  */
13203446Smrj cpuset_t cpufailset;
13213446Smrj #endif
13223446Smrj 
13233446Smrj int
13243446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
13250Sstevel@tonic-gate {
13263446Smrj 	struct psm_ops *pops = mach_set[0];
13273446Smrj 	processorid_t id = cp->cpu_id;
13280Sstevel@tonic-gate 
13293446Smrj #ifdef DEBUG
13303446Smrj 	if (CPU_IN_SET(cpufailset, id))
13313446Smrj 		return (0);
13323446Smrj #endif
13333446Smrj 	return ((*pops->psm_cpu_start)(id, ctx));
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13365295Srandyf int
13375295Srandyf mach_cpuid_start(processorid_t id, void *ctx)
13385295Srandyf {
13395295Srandyf 	struct psm_ops *pops = mach_set[0];
13405295Srandyf 
13415295Srandyf #ifdef DEBUG
13425295Srandyf 	if (CPU_IN_SET(cpufailset, id))
13435295Srandyf 		return (0);
13445295Srandyf #endif
13455295Srandyf 	return ((*pops->psm_cpu_start)(id, ctx));
13465295Srandyf }
13475295Srandyf 
13480Sstevel@tonic-gate /*ARGSUSED*/
13490Sstevel@tonic-gate static int
13500Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
13510Sstevel@tonic-gate {
13520Sstevel@tonic-gate 	return (irqno);	/* default to NO translation */
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate static void
13560Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
13570Sstevel@tonic-gate {
13580Sstevel@tonic-gate 	/*
13590Sstevel@tonic-gate 	 * SL_FATAL is pass in once panicstr is set, deliver it
13600Sstevel@tonic-gate 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
13610Sstevel@tonic-gate 	 * codes for the psmi handler
13620Sstevel@tonic-gate 	 */
13630Sstevel@tonic-gate 	if (level & SL_FATAL)
13640Sstevel@tonic-gate 		(*notify_error)(CE_PANIC, errmsg);
13650Sstevel@tonic-gate 	else if (level & SL_WARN)
13660Sstevel@tonic-gate 		(*notify_error)(CE_WARN, errmsg);
13670Sstevel@tonic-gate 	else if (level & SL_NOTE)
13680Sstevel@tonic-gate 		(*notify_error)(CE_NOTE, errmsg);
13690Sstevel@tonic-gate 	else if (level & SL_CONSOLE)
13700Sstevel@tonic-gate 		(*notify_error)(CE_CONT, errmsg);
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate /*
13740Sstevel@tonic-gate  * It provides the default basic intr_ops interface for the new DDI
13750Sstevel@tonic-gate  * interrupt framework if the PSM doesn't have one.
13760Sstevel@tonic-gate  *
13770Sstevel@tonic-gate  * Input:
13780Sstevel@tonic-gate  * dip     - pointer to the dev_info structure of the requested device
13790Sstevel@tonic-gate  * hdlp    - pointer to the internal interrupt handle structure for the
13800Sstevel@tonic-gate  *	     requested interrupt
13810Sstevel@tonic-gate  * intr_op - opcode for this call
13820Sstevel@tonic-gate  * result  - pointer to the integer that will hold the result to be
13830Sstevel@tonic-gate  *	     passed back if return value is PSM_SUCCESS
13840Sstevel@tonic-gate  *
13850Sstevel@tonic-gate  * Output:
13860Sstevel@tonic-gate  * return value is either PSM_SUCCESS or PSM_FAILURE
13870Sstevel@tonic-gate  */
13880Sstevel@tonic-gate static int
13890Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
13900Sstevel@tonic-gate     psm_intr_op_t intr_op, int *result)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	struct intrspec *ispec;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	switch (intr_op) {
13950Sstevel@tonic-gate 	case PSM_INTR_OP_CHECK_MSI:
13960Sstevel@tonic-gate 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
13974481Sbholler 		    DDI_INTR_TYPE_MSIX);
13980Sstevel@tonic-gate 		break;
13990Sstevel@tonic-gate 	case PSM_INTR_OP_ALLOC_VECTORS:
14000Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
14010Sstevel@tonic-gate 			*result = 1;
14020Sstevel@tonic-gate 		else
14030Sstevel@tonic-gate 			*result = 0;
14040Sstevel@tonic-gate 		break;
14050Sstevel@tonic-gate 	case PSM_INTR_OP_FREE_VECTORS:
14060Sstevel@tonic-gate 		break;
14070Sstevel@tonic-gate 	case PSM_INTR_OP_NAVAIL_VECTORS:
14080Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
14090Sstevel@tonic-gate 			*result = 1;
14100Sstevel@tonic-gate 		else
14110Sstevel@tonic-gate 			*result = 0;
14120Sstevel@tonic-gate 		break;
14130Sstevel@tonic-gate 	case PSM_INTR_OP_XLATE_VECTOR:
1414916Sschwartz 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
14150Sstevel@tonic-gate 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
14160Sstevel@tonic-gate 		break;
14170Sstevel@tonic-gate 	case PSM_INTR_OP_GET_CAP:
14180Sstevel@tonic-gate 		*result = 0;
14190Sstevel@tonic-gate 		break;
14200Sstevel@tonic-gate 	case PSM_INTR_OP_GET_PENDING:
14210Sstevel@tonic-gate 	case PSM_INTR_OP_CLEAR_MASK:
14220Sstevel@tonic-gate 	case PSM_INTR_OP_SET_MASK:
14230Sstevel@tonic-gate 	case PSM_INTR_OP_GET_SHARED:
14240Sstevel@tonic-gate 	case PSM_INTR_OP_SET_PRI:
14250Sstevel@tonic-gate 	case PSM_INTR_OP_SET_CAP:
1426916Sschwartz 	case PSM_INTR_OP_SET_CPU:
1427916Sschwartz 	case PSM_INTR_OP_GET_INTR:
14280Sstevel@tonic-gate 	default:
14290Sstevel@tonic-gate 		return (PSM_FAILURE);
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 	return (PSM_SUCCESS);
14320Sstevel@tonic-gate }
14334769Sdp78419 /*
14344769Sdp78419  * Return 1 if CMT load balancing policies should be
14354769Sdp78419  * implemented across instances of the specified hardware
14364769Sdp78419  * sharing relationship.
14374769Sdp78419  */
14384769Sdp78419 int
14394769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw)
14404769Sdp78419 {
14414769Sdp78419 	if (hw == PGHW_IPIPE ||
14424769Sdp78419 	    hw == PGHW_FPU ||
14434769Sdp78419 	    hw == PGHW_CHIP)
14444769Sdp78419 		return (1);
14454769Sdp78419 	else
14464769Sdp78419 		return (0);
14474769Sdp78419 }
14484769Sdp78419 /*
14494769Sdp78419  * Return 1 if thread affinity polices should be implemented
14504769Sdp78419  * for instances of the specifed hardware sharing relationship.
14514769Sdp78419  */
14524769Sdp78419 int
14534769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw)
14544769Sdp78419 {
14554769Sdp78419 	if (hw == PGHW_CACHE)
14564769Sdp78419 		return (1);
14574769Sdp78419 	else
14584769Sdp78419 		return (0);
14594769Sdp78419 }
1460