xref: /onnv-gate/usr/src/uts/i86pc/os/mp_machdep.c (revision 8408:7b4e48a75d0c)
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 
265295Srandyf #define	PSMI_1_6
270Sstevel@tonic-gate #include <sys/smp_impldefs.h>
280Sstevel@tonic-gate #include <sys/psm.h>
290Sstevel@tonic-gate #include <sys/psm_modctl.h>
300Sstevel@tonic-gate #include <sys/pit.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/strlog.h>
330Sstevel@tonic-gate #include <sys/clock.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/rtc.h>
360Sstevel@tonic-gate #include <sys/x86_archext.h>
370Sstevel@tonic-gate #include <sys/cpupart.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
394606Sesaxe #include <sys/cmt.h>
404481Sbholler #include <sys/cpu.h>
410Sstevel@tonic-gate #include <sys/disp.h>
420Sstevel@tonic-gate #include <sys/archsystm.h>
433446Smrj #include <sys/machsystm.h>
444481Sbholler #include <sys/sysmacros.h>
455084Sjohnlev #include <sys/memlist.h>
463446Smrj #include <sys/param.h>
473446Smrj #include <sys/promif.h>
485084Sjohnlev #if defined(__xpv)
495084Sjohnlev #include <sys/hypervisor.h>
505084Sjohnlev #endif
51916Sschwartz #include <sys/mach_intr.h>
524481Sbholler #include <vm/hat_i86.h>
534652Scwb #include <sys/kdi_machimpl.h>
545864Sesaxe #include <sys/sdt.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  *	Local function prototypes
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
620Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
630Sstevel@tonic-gate static void mach_init();
640Sstevel@tonic-gate static void mach_picinit();
650Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
660Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
670Sstevel@tonic-gate static void mach_fixcpufreq(void);
680Sstevel@tonic-gate static int mach_clkinit(int, int *);
690Sstevel@tonic-gate static void mach_smpinit(void);
700Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl);
710Sstevel@tonic-gate static void mach_get_platform(int owner);
720Sstevel@tonic-gate static void mach_construct_info();
730Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno);
740Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
750Sstevel@tonic-gate     psm_intr_op_t, int *);
760Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg);
770Sstevel@tonic-gate static hrtime_t dummy_hrtime(void);
780Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *);
793446Smrj static void cpu_idle(void);
800Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int);
815084Sjohnlev #ifndef __xpv
824481Sbholler static void cpu_idle_mwait(void);
834481Sbholler static void cpu_wakeup_mwait(cpu_t *, int);
845084Sjohnlev #endif
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  *	External reference functions
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate extern void return_instr();
890Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *);
900Sstevel@tonic-gate #if defined(__i386)
910Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *);
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *);
943434Sesaxe extern int cpuid_get_coreid(cpu_t *);
953434Sesaxe extern int cpuid_get_chipid(cpu_t *);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  *	PSM functions initialization
990Sstevel@tonic-gate  */
1003446Smrj void (*psm_shutdownf)(int, int)	= (void (*)(int, int))return_instr;
1013446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
1023446Smrj void (*psm_notifyf)(int)	= (void (*)(int))return_instr;
1033446Smrj void (*psm_set_idle_cpuf)(int)	= (void (*)(int))return_instr;
1043446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
1050Sstevel@tonic-gate void (*psminitf)()		= mach_init;
1060Sstevel@tonic-gate void (*picinitf)() 		= return_instr;
1070Sstevel@tonic-gate int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
1080Sstevel@tonic-gate int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
1090Sstevel@tonic-gate void (*send_dirintf)() 		= return_instr;
1103446Smrj void (*setspl)(int)		= (void (*)(int))return_instr;
1110Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1120Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1134652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)=
1144652Scwb 	(void (*)(int, struct av_softinfo *))return_instr;
115999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
116999Slq150181 	(void (*)(int, struct av_softinfo *))return_instr;
1170Sstevel@tonic-gate int (*slvltovect)(int)		= (int (*)(int))return_instr;
1180Sstevel@tonic-gate int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
1190Sstevel@tonic-gate void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
1200Sstevel@tonic-gate int (*psm_disable_intr)(int)	= mp_disable_intr;
1210Sstevel@tonic-gate void (*psm_enable_intr)(int)	= mp_enable_intr;
1220Sstevel@tonic-gate hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
1230Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
1240Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
1250Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1260Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1270Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1280Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1290Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1320Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1330Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1340Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1350Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1360Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1370Sstevel@tonic-gate     int *) = mach_intr_ops;
1385295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
1395295Srandyf     return_instr;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1420Sstevel@tonic-gate void (*hrtime_tick)(void)	= return_instr;
1430Sstevel@tonic-gate 
1445084Sjohnlev /*
1455084Sjohnlev  * True if the generic TSC code is our source of hrtime, rather than whatever
1465084Sjohnlev  * the PSM can provide.
1475084Sjohnlev  */
1485084Sjohnlev #ifdef __xpv
1495084Sjohnlev int tsc_gethrtime_enable = 0;
1505084Sjohnlev #else
1510Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1525084Sjohnlev #endif
1530Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1565084Sjohnlev  * True if the hrtime implementation is "hires"; namely, better than microdata.
1575084Sjohnlev  */
1585084Sjohnlev int gethrtime_hires = 0;
1595084Sjohnlev 
1605084Sjohnlev /*
1610Sstevel@tonic-gate  * Local Static Data
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate static struct psm_ops mach_ops;
1640Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1650Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1683446Smrj  * If non-zero, idle cpus will become "halted" when there's
1690Sstevel@tonic-gate  * no work to do.
1700Sstevel@tonic-gate  */
1713446Smrj int	idle_cpu_use_hlt = 1;
1720Sstevel@tonic-gate 
1735084Sjohnlev #ifndef __xpv
1744481Sbholler /*
1754481Sbholler  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
1764481Sbholler  */
1774481Sbholler int	idle_cpu_prefer_mwait = 1;
1787716SBill.Holler@Sun.COM /*
1797716SBill.Holler@Sun.COM  * Set to 0 to avoid MONITOR+CLFLUSH assertion.
1807716SBill.Holler@Sun.COM  */
1817716SBill.Holler@Sun.COM int	idle_cpu_assert_cflush_monitor = 1;
1827716SBill.Holler@Sun.COM 
1835084Sjohnlev #endif
1843434Sesaxe 
1853434Sesaxe /*ARGSUSED*/
1863434Sesaxe int
1873434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
1883434Sesaxe {
1893434Sesaxe 	switch (hw) {
1903434Sesaxe 	case PGHW_IPIPE:
1913434Sesaxe 		if (x86_feature & (X86_HTT)) {
1923434Sesaxe 			/*
1933434Sesaxe 			 * Hyper-threading is SMT
1943434Sesaxe 			 */
1953434Sesaxe 			return (1);
1963434Sesaxe 		} else {
1973434Sesaxe 			return (0);
1983434Sesaxe 		}
1993434Sesaxe 	case PGHW_CHIP:
2003434Sesaxe 		if (x86_feature & (X86_CMP|X86_HTT))
2013434Sesaxe 			return (1);
2023434Sesaxe 		else
2033434Sesaxe 			return (0);
2044606Sesaxe 	case PGHW_CACHE:
2054606Sesaxe 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
2064606Sesaxe 			return (1);
2074606Sesaxe 		else
2084606Sesaxe 			return (0);
2093434Sesaxe 	default:
2103434Sesaxe 		return (0);
2113434Sesaxe 	}
2123434Sesaxe }
2133434Sesaxe 
2143434Sesaxe /*
2153434Sesaxe  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
2163434Sesaxe  * If pghw_type_t is an unsupported hardware type, then return -1
2173434Sesaxe  */
2183434Sesaxe int
2193434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
2200Sstevel@tonic-gate {
2213434Sesaxe 	id_t pgp_a, pgp_b;
2223434Sesaxe 
2233434Sesaxe 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
2243434Sesaxe 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
2253434Sesaxe 
2263434Sesaxe 	if (pgp_a == -1 || pgp_b == -1)
2273434Sesaxe 		return (-1);
2283434Sesaxe 
2293434Sesaxe 	return (pgp_a == pgp_b);
2303434Sesaxe }
2313434Sesaxe 
2323434Sesaxe /*
2333434Sesaxe  * Return a physical instance identifier for known hardware sharing
2343434Sesaxe  * relationships
2353434Sesaxe  */
2363434Sesaxe id_t
2373434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
2383434Sesaxe {
2393434Sesaxe 	switch (hw) {
2403434Sesaxe 	case PGHW_IPIPE:
2413434Sesaxe 		return (cpuid_get_coreid(cpu));
2424606Sesaxe 	case PGHW_CACHE:
2434606Sesaxe 		return (cpuid_get_last_lvl_cacheid(cpu));
2443434Sesaxe 	case PGHW_CHIP:
2453434Sesaxe 		return (cpuid_get_chipid(cpu));
2463434Sesaxe 	default:
2473434Sesaxe 		return (-1);
2481228Sandrei 	}
2493434Sesaxe }
2500Sstevel@tonic-gate 
2513434Sesaxe int
2523434Sesaxe pg_plat_hw_level(pghw_type_t hw)
2533434Sesaxe {
2543434Sesaxe 	int i;
2553434Sesaxe 	static pghw_type_t hw_hier[] = {
2563434Sesaxe 		PGHW_IPIPE,
2574606Sesaxe 		PGHW_CACHE,
2583434Sesaxe 		PGHW_CHIP,
2593434Sesaxe 		PGHW_NUM_COMPONENTS
2603434Sesaxe 	};
2613434Sesaxe 
2623434Sesaxe 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
2633434Sesaxe 		if (hw_hier[i] == hw)
2643434Sesaxe 			return (i);
2653434Sesaxe 	}
2663434Sesaxe 	return (-1);
2673434Sesaxe }
2683434Sesaxe 
2694606Sesaxe /*
2704606Sesaxe  * Return 1 if CMT load balancing policies should be
2714606Sesaxe  * implemented across instances of the specified hardware
2724606Sesaxe  * sharing relationship.
2734606Sesaxe  */
2744606Sesaxe int
2754606Sesaxe pg_plat_cmt_load_bal_hw(pghw_type_t hw)
2764606Sesaxe {
2774606Sesaxe 	if (hw == PGHW_IPIPE ||
2784606Sesaxe 	    hw == PGHW_FPU ||
2794606Sesaxe 	    hw == PGHW_CHIP ||
2804606Sesaxe 	    hw == PGHW_CACHE)
2814606Sesaxe 		return (1);
2824606Sesaxe 	else
2834606Sesaxe 		return (0);
2844606Sesaxe }
2854606Sesaxe 
2864606Sesaxe 
2874606Sesaxe /*
2884606Sesaxe  * Return 1 if thread affinity polices should be implemented
2894606Sesaxe  * for instances of the specifed hardware sharing relationship.
2904606Sesaxe  */
2914606Sesaxe int
2924606Sesaxe pg_plat_cmt_affinity_hw(pghw_type_t hw)
2934606Sesaxe {
2944606Sesaxe 	if (hw == PGHW_CACHE)
2954606Sesaxe 		return (1);
2964606Sesaxe 	else
2974606Sesaxe 		return (0);
2984606Sesaxe }
2994606Sesaxe 
3003434Sesaxe id_t
3013434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
3023434Sesaxe {
3033434Sesaxe 	return ((id_t)cpuid_get_coreid(cpu));
3043434Sesaxe }
3053434Sesaxe 
3063434Sesaxe void
3073434Sesaxe cmp_set_nosteal_interval(void)
3083434Sesaxe {
3093434Sesaxe 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
3103434Sesaxe 	nosteal_nsec = 100000UL;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * Routine to ensure initial callers to hrtime gets 0 as return
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate static hrtime_t
3170Sstevel@tonic-gate dummy_hrtime(void)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	return (0);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /* ARGSUSED */
3230Sstevel@tonic-gate static void
3240Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
3250Sstevel@tonic-gate {}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3283446Smrj  * Idle the present CPU until awoken via an interrupt
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate static void
3313446Smrj cpu_idle(void)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	cpu_t		*cpup = CPU;
334*8408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid = cpup->cpu_seqid;
335711Sesaxe 	cpupart_t	*cp = cpup->cpu_part;
3360Sstevel@tonic-gate 	int		hset_update = 1;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * If this CPU is online, and there's multiple CPUs
3400Sstevel@tonic-gate 	 * in the system, then we should notate our halting
3410Sstevel@tonic-gate 	 * by adding ourselves to the partition's halted CPU
3420Sstevel@tonic-gate 	 * bitmap. This allows other CPUs to find/awaken us when
3430Sstevel@tonic-gate 	 * work becomes available.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
3460Sstevel@tonic-gate 		hset_update = 0;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
349*8408SEric.Saxe@Sun.COM 	 * Add ourselves to the partition's halted CPUs bitmap
3500Sstevel@tonic-gate 	 * and set our HALTED flag, if necessary.
3510Sstevel@tonic-gate 	 *
352711Sesaxe 	 * When a thread becomes runnable, it is placed on the queue
353*8408SEric.Saxe@Sun.COM 	 * and then the halted CPU bitmap is checked to determine who
354711Sesaxe 	 * (if anyone) should be awoken. We therefore need to first
355*8408SEric.Saxe@Sun.COM 	 * add ourselves to the bitmap, and and then check if there
356*8408SEric.Saxe@Sun.COM 	 * is any work available. The order is important to prevent a race
357*8408SEric.Saxe@Sun.COM 	 * that can lead to work languishing on a run queue somewhere while
358*8408SEric.Saxe@Sun.COM 	 * this CPU remains halted.
359*8408SEric.Saxe@Sun.COM 	 *
360*8408SEric.Saxe@Sun.COM 	 * Either the producing CPU will see we're halted and will awaken us,
361*8408SEric.Saxe@Sun.COM 	 * or this CPU will see the work available in disp_anywork().
362711Sesaxe 	 *
3630Sstevel@tonic-gate 	 * Note that memory barriers after updating the HALTED flag
364*8408SEric.Saxe@Sun.COM 	 * are not necessary since an atomic operation (updating the bitset)
3650Sstevel@tonic-gate 	 * immediately follows. On x86 the atomic operation acts as a
3660Sstevel@tonic-gate 	 * memory barrier for the update of cpu_disp_flags.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if (hset_update) {
3690Sstevel@tonic-gate 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
370*8408SEric.Saxe@Sun.COM 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * Check to make sure there's really nothing to do.
375711Sesaxe 	 * Work destined for this CPU may become available after
376711Sesaxe 	 * this check. We'll be notified through the clearing of our
377*8408SEric.Saxe@Sun.COM 	 * bit in the halted CPU bitmap, and a poke.
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	if (disp_anywork()) {
3800Sstevel@tonic-gate 		if (hset_update) {
3810Sstevel@tonic-gate 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
382*8408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
3830Sstevel@tonic-gate 		}
384711Sesaxe 		return;
385711Sesaxe 	}
386711Sesaxe 
387711Sesaxe 	/*
388711Sesaxe 	 * We're on our way to being halted.
389711Sesaxe 	 *
390711Sesaxe 	 * Disable interrupts now, so that we'll awaken immediately
391711Sesaxe 	 * after halting if someone tries to poke us between now and
392711Sesaxe 	 * the time we actually halt.
393711Sesaxe 	 *
394711Sesaxe 	 * We check for the presence of our bit after disabling interrupts.
395711Sesaxe 	 * If it's cleared, we'll return. If the bit is cleared after
396711Sesaxe 	 * we check then the poke will pop us out of the halted state.
397711Sesaxe 	 *
398711Sesaxe 	 * This means that the ordering of the poke and the clearing
399711Sesaxe 	 * of the bit by cpu_wakeup is important.
400711Sesaxe 	 * cpu_wakeup() must clear, then poke.
4013446Smrj 	 * cpu_idle() must disable interrupts, then check for the bit.
402711Sesaxe 	 */
403711Sesaxe 	cli();
404711Sesaxe 
405*8408SEric.Saxe@Sun.COM 	if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) {
406711Sesaxe 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
407711Sesaxe 		sti();
408711Sesaxe 		return;
409711Sesaxe 	}
410711Sesaxe 
411711Sesaxe 	/*
412711Sesaxe 	 * The check for anything locally runnable is here for performance
413711Sesaxe 	 * and isn't needed for correctness. disp_nrunnable ought to be
414711Sesaxe 	 * in our cache still, so it's inexpensive to check, and if there
415711Sesaxe 	 * is anything runnable we won't have to wait for the poke.
416711Sesaxe 	 */
417711Sesaxe 	if (cpup->cpu_disp->disp_nrunnable != 0) {
418711Sesaxe 		if (hset_update) {
419711Sesaxe 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
420*8408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
421711Sesaxe 		}
4220Sstevel@tonic-gate 		sti();
4230Sstevel@tonic-gate 		return;
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4267006Srv207048 	DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C1);
4277006Srv207048 
4283446Smrj 	mach_cpu_idle();
4290Sstevel@tonic-gate 
4307006Srv207048 	DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C0);
4317006Srv207048 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * We're no longer halted
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (hset_update) {
4360Sstevel@tonic-gate 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
437*8408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
4440Sstevel@tonic-gate  * Otherwise, see if other CPUs in the cpu partition are halted and need to
4450Sstevel@tonic-gate  * be woken up so that they can steal the thread we placed on this CPU.
4460Sstevel@tonic-gate  * This function is only used on MP systems.
4470Sstevel@tonic-gate  */
4480Sstevel@tonic-gate static void
4490Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	uint_t		cpu_found;
452*8408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid;
4530Sstevel@tonic-gate 	cpupart_t	*cp;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	cp = cpu->cpu_part;
456*8408SEric.Saxe@Sun.COM 	cpu_sid = cpu->cpu_seqid;
457*8408SEric.Saxe@Sun.COM 	if (bitset_in_set(&cp->cp_haltset, cpu_sid)) {
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * Clear the halted bit for that CPU since it will be
4600Sstevel@tonic-gate 		 * poked in a moment.
4610Sstevel@tonic-gate 		 */
462*8408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
4630Sstevel@tonic-gate 		/*
4640Sstevel@tonic-gate 		 * We may find the current CPU present in the halted cpuset
4650Sstevel@tonic-gate 		 * if we're in the context of an interrupt that occurred
4663446Smrj 		 * before we had a chance to clear our bit in cpu_idle().
4670Sstevel@tonic-gate 		 * Poking ourself is obviously unnecessary, since if
4680Sstevel@tonic-gate 		 * we're here, we're not halted.
4690Sstevel@tonic-gate 		 */
4700Sstevel@tonic-gate 		if (cpu != CPU)
4710Sstevel@tonic-gate 			poke_cpu(cpu->cpu_id);
4720Sstevel@tonic-gate 		return;
4730Sstevel@tonic-gate 	} else {
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * This cpu isn't halted, but it's idle or undergoing a
4760Sstevel@tonic-gate 		 * context switch. No need to awaken anyone else.
4770Sstevel@tonic-gate 		 */
4780Sstevel@tonic-gate 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
4790Sstevel@tonic-gate 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
4800Sstevel@tonic-gate 			return;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/*
484*8408SEric.Saxe@Sun.COM 	 * No need to wake up other CPUs if this is for a bound thread.
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	if (bound)
4870Sstevel@tonic-gate 		return;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
490*8408SEric.Saxe@Sun.COM 	 * The CPU specified for wakeup isn't currently halted, so check
491*8408SEric.Saxe@Sun.COM 	 * to see if there are any other halted CPUs in the partition,
492*8408SEric.Saxe@Sun.COM 	 * and if there are then awaken one.
4930Sstevel@tonic-gate 	 */
4940Sstevel@tonic-gate 	do {
495*8408SEric.Saxe@Sun.COM 		cpu_found = bitset_find(&cp->cp_haltset);
496*8408SEric.Saxe@Sun.COM 		if (cpu_found == (uint_t)-1)
4970Sstevel@tonic-gate 			return;
498*8408SEric.Saxe@Sun.COM 	} while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0);
4990Sstevel@tonic-gate 
500*8408SEric.Saxe@Sun.COM 	if (cpu_found != CPU->cpu_seqid) {
501*8408SEric.Saxe@Sun.COM 		poke_cpu(cpu_seq[cpu_found]->cpu_id);
502*8408SEric.Saxe@Sun.COM 	}
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5055084Sjohnlev #ifndef __xpv
5064481Sbholler /*
5074481Sbholler  * Idle the present CPU until awoken via touching its monitored line
5084481Sbholler  */
5094481Sbholler static void
5104481Sbholler cpu_idle_mwait(void)
5114481Sbholler {
5124481Sbholler 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
5134481Sbholler 	cpu_t			*cpup = CPU;
514*8408SEric.Saxe@Sun.COM 	processorid_t		cpu_sid = cpup->cpu_seqid;
5154481Sbholler 	cpupart_t		*cp = cpup->cpu_part;
5164481Sbholler 	int			hset_update = 1;
5174481Sbholler 
5184481Sbholler 	/*
5194481Sbholler 	 * Set our mcpu_mwait here, so we can tell if anyone trys to
5204481Sbholler 	 * wake us between now and when we call mwait.  No other cpu will
521*8408SEric.Saxe@Sun.COM 	 * attempt to set our mcpu_mwait until we add ourself to the halted
522*8408SEric.Saxe@Sun.COM 	 * CPU bitmap.
5234481Sbholler 	 */
5244481Sbholler 	*mcpu_mwait = MWAIT_HALTED;
5254481Sbholler 
5264481Sbholler 	/*
5274481Sbholler 	 * If this CPU is online, and there's multiple CPUs
5284481Sbholler 	 * in the system, then we should notate our halting
5294481Sbholler 	 * by adding ourselves to the partition's halted CPU
5304481Sbholler 	 * bitmap. This allows other CPUs to find/awaken us when
5314481Sbholler 	 * work becomes available.
5324481Sbholler 	 */
5334481Sbholler 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
5344481Sbholler 		hset_update = 0;
5354481Sbholler 
5364481Sbholler 	/*
537*8408SEric.Saxe@Sun.COM 	 * Add ourselves to the partition's halted CPUs bitmap
5384481Sbholler 	 * and set our HALTED flag, if necessary.
5394481Sbholler 	 *
5404481Sbholler 	 * When a thread becomes runnable, it is placed on the queue
541*8408SEric.Saxe@Sun.COM 	 * and then the halted CPU bitmap is checked to determine who
5424481Sbholler 	 * (if anyone) should be awoken. We therefore need to first
543*8408SEric.Saxe@Sun.COM 	 * add ourselves to the bitmap, and and then check if there
5444481Sbholler 	 * is any work available.
5454481Sbholler 	 *
5464481Sbholler 	 * Note that memory barriers after updating the HALTED flag
5474481Sbholler 	 * are not necessary since an atomic operation (updating the bitmap)
5484481Sbholler 	 * immediately follows. On x86 the atomic operation acts as a
5494481Sbholler 	 * memory barrier for the update of cpu_disp_flags.
5504481Sbholler 	 */
5514481Sbholler 	if (hset_update) {
5524481Sbholler 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
553*8408SEric.Saxe@Sun.COM 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
5544481Sbholler 	}
5554481Sbholler 
5564481Sbholler 	/*
5574481Sbholler 	 * Check to make sure there's really nothing to do.
5584481Sbholler 	 * Work destined for this CPU may become available after
5594481Sbholler 	 * this check. We'll be notified through the clearing of our
560*8408SEric.Saxe@Sun.COM 	 * bit in the halted CPU bitmap, and a write to our mcpu_mwait.
5614481Sbholler 	 *
5624481Sbholler 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
5634481Sbholler 	 */
5644481Sbholler 	if (disp_anywork()) {
5654481Sbholler 		if (hset_update) {
5664481Sbholler 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
567*8408SEric.Saxe@Sun.COM 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5684481Sbholler 		}
5694481Sbholler 		return;
5704481Sbholler 	}
5714481Sbholler 
5724481Sbholler 	/*
5734481Sbholler 	 * We're on our way to being halted.
5744481Sbholler 	 * To avoid a lost wakeup, arm the monitor before checking if another
5754481Sbholler 	 * cpu wrote to mcpu_mwait to wake us up.
5764481Sbholler 	 */
5774481Sbholler 	i86_monitor(mcpu_mwait, 0, 0);
5784481Sbholler 	if (*mcpu_mwait == MWAIT_HALTED) {
5795864Sesaxe 		DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C1);
5805864Sesaxe 
5814481Sbholler 		tlb_going_idle();
5824481Sbholler 		i86_mwait(0, 0);
5834481Sbholler 		tlb_service();
5845864Sesaxe 
5855864Sesaxe 		DTRACE_PROBE1(idle__state__transition, uint_t, IDLE_STATE_C0);
5864481Sbholler 	}
5874481Sbholler 
5884481Sbholler 	/*
5894481Sbholler 	 * We're no longer halted
5904481Sbholler 	 */
5914481Sbholler 	if (hset_update) {
5924481Sbholler 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
593*8408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5944481Sbholler 	}
5954481Sbholler }
5964481Sbholler 
5974481Sbholler /*
5984481Sbholler  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
5994481Sbholler  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
6004481Sbholler  * need to be woken up so that they can steal the thread we placed on this CPU.
6014481Sbholler  * This function is only used on MP systems.
6024481Sbholler  */
6034481Sbholler static void
6044481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
6054481Sbholler {
6064481Sbholler 	cpupart_t	*cpu_part;
6074481Sbholler 	uint_t		cpu_found;
608*8408SEric.Saxe@Sun.COM 	processorid_t	cpu_sid;
6094481Sbholler 
6104481Sbholler 	cpu_part = cp->cpu_part;
611*8408SEric.Saxe@Sun.COM 	cpu_sid = cp->cpu_seqid;
6124481Sbholler 
6134481Sbholler 	/*
6144481Sbholler 	 * Clear the halted bit for that CPU since it will be woken up
6154481Sbholler 	 * in a moment.
6164481Sbholler 	 */
617*8408SEric.Saxe@Sun.COM 	if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) {
6184481Sbholler 		/*
6194481Sbholler 		 * Clear the halted bit for that CPU since it will be
6204481Sbholler 		 * poked in a moment.
6214481Sbholler 		 */
622*8408SEric.Saxe@Sun.COM 		bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid);
6234481Sbholler 		/*
6244481Sbholler 		 * We may find the current CPU present in the halted cpuset
6254481Sbholler 		 * if we're in the context of an interrupt that occurred
6264481Sbholler 		 * before we had a chance to clear our bit in cpu_idle().
6274481Sbholler 		 * Waking ourself is obviously unnecessary, since if
6284481Sbholler 		 * we're here, we're not halted.
6294481Sbholler 		 *
6304481Sbholler 		 * monitor/mwait wakeup via writing to our cache line is
6314481Sbholler 		 * harmless and less expensive than always checking if we
6324481Sbholler 		 * are waking ourself which is an uncommon case.
6334481Sbholler 		 */
6344481Sbholler 		MWAIT_WAKEUP(cp);	/* write to monitored line */
6354481Sbholler 		return;
6364481Sbholler 	} else {
6374481Sbholler 		/*
6384481Sbholler 		 * This cpu isn't halted, but it's idle or undergoing a
6394481Sbholler 		 * context switch. No need to awaken anyone else.
6404481Sbholler 		 */
6414481Sbholler 		if (cp->cpu_thread == cp->cpu_idle_thread ||
6424481Sbholler 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
6434481Sbholler 			return;
6444481Sbholler 	}
6454481Sbholler 
6464481Sbholler 	/*
6474481Sbholler 	 * No need to wake up other CPUs if the thread we just enqueued
6484481Sbholler 	 * is bound.
6494481Sbholler 	 */
650*8408SEric.Saxe@Sun.COM 	if (bound || ncpus == 1)
6514481Sbholler 		return;
6524481Sbholler 
6534481Sbholler 	/*
6544481Sbholler 	 * See if there's any other halted CPUs. If there are, then
6554481Sbholler 	 * select one, and awaken it.
6564481Sbholler 	 * It's possible that after we find a CPU, somebody else
6574481Sbholler 	 * will awaken it before we get the chance.
6584481Sbholler 	 * In that case, look again.
6594481Sbholler 	 */
6604481Sbholler 	do {
661*8408SEric.Saxe@Sun.COM 		cpu_found = bitset_find(&cpu_part->cp_haltset);
662*8408SEric.Saxe@Sun.COM 		if (cpu_found == (uint_t)-1)
6634481Sbholler 			return;
664*8408SEric.Saxe@Sun.COM 	} while (bitset_atomic_test_and_del(&cpu_part->cp_haltset,
665*8408SEric.Saxe@Sun.COM 	    cpu_found) < 0);
6664481Sbholler 
6674481Sbholler 	/*
668*8408SEric.Saxe@Sun.COM 	 * Do not check if cpu_found is ourself as monitor/mwait
669*8408SEric.Saxe@Sun.COM 	 * wakeup is cheap.
6704481Sbholler 	 */
671*8408SEric.Saxe@Sun.COM 	MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */
6724481Sbholler }
673*8408SEric.Saxe@Sun.COM 
6745084Sjohnlev #endif
6754481Sbholler 
6763446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
6773446Smrj 
6780Sstevel@tonic-gate static int
6790Sstevel@tonic-gate mp_disable_intr(int cpun)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * switch to the offline cpu
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	affinity_set(cpun);
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * raise ipl to just below cross call
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 	splx(XC_MED_PIL-1);
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 *	set base spl to prevent the next swtch to idle from
6910Sstevel@tonic-gate 	 *	lowering back to ipl 0
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1));
6940Sstevel@tonic-gate 	set_base_spl();
6950Sstevel@tonic-gate 	affinity_clear();
6960Sstevel@tonic-gate 	return (DDI_SUCCESS);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate static void
7000Sstevel@tonic-gate mp_enable_intr(int cpun)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * switch to the online cpu
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	affinity_set(cpun);
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * clear the interrupt active mask
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1));
7100Sstevel@tonic-gate 	set_base_spl();
7110Sstevel@tonic-gate 	(void) spl0();
7120Sstevel@tonic-gate 	affinity_clear();
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate static void
7160Sstevel@tonic-gate mach_get_platform(int owner)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	void		**srv_opsp;
7190Sstevel@tonic-gate 	void		**clt_opsp;
7200Sstevel@tonic-gate 	int		i;
7210Sstevel@tonic-gate 	int		total_ops;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/* fix up psm ops */
7240Sstevel@tonic-gate 	srv_opsp = (void **)mach_set[0];
7250Sstevel@tonic-gate 	clt_opsp = (void **)mach_set[owner];
7260Sstevel@tonic-gate 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
7270Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops_ver01) /
7284481Sbholler 		    sizeof (void (*)(void));
7290Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
7300Sstevel@tonic-gate 		/* no psm_notify_func */
7310Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
7320Sstevel@tonic-gate 		    sizeof (void (*)(void));
7330Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
7340Sstevel@tonic-gate 		/* no psm_timer funcs */
7350Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
7360Sstevel@tonic-gate 		    sizeof (void (*)(void));
7370Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
7380Sstevel@tonic-gate 		/* no psm_preshutdown function */
7390Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
7400Sstevel@tonic-gate 		    sizeof (void (*)(void));
7410Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
7420Sstevel@tonic-gate 		/* no psm_preshutdown function */
7430Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
7440Sstevel@tonic-gate 		    sizeof (void (*)(void));
7450Sstevel@tonic-gate 	else
7460Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * Save the version of the PSM module, in case we need to
7500Sstevel@tonic-gate 	 * bahave differently based on version.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 	mach_ver[0] = mach_ver[owner];
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	for (i = 0; i < total_ops; i++)
7550Sstevel@tonic-gate 		if (clt_opsp[i] != NULL)
7560Sstevel@tonic-gate 			srv_opsp[i] = clt_opsp[i];
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate static void
7600Sstevel@tonic-gate mach_construct_info()
7610Sstevel@tonic-gate {
7623446Smrj 	struct psm_sw *swp;
7630Sstevel@tonic-gate 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
7640Sstevel@tonic-gate 	int	conflict_owner = 0;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (psmsw->psw_forw == psmsw)
7670Sstevel@tonic-gate 		panic("No valid PSM modules found");
7680Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
7690Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7700Sstevel@tonic-gate 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
7710Sstevel@tonic-gate 			continue;
7720Sstevel@tonic-gate 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
7730Sstevel@tonic-gate 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
7740Sstevel@tonic-gate 		mach_cnt[swp->psw_infop->p_owner]++;
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/* check to see are there any conflicts */
7810Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
7820Sstevel@tonic-gate 		conflict_owner = PSM_OWN_EXCLUSIVE;
7830Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
7840Sstevel@tonic-gate 		conflict_owner = PSM_OWN_OVERRIDE;
7850Sstevel@tonic-gate 	if (conflict_owner) {
7860Sstevel@tonic-gate 		/* remove all psm modules except uppc */
7870Sstevel@tonic-gate 		cmn_err(CE_WARN,
7884481Sbholler 		    "Conflicts detected on the following PSM modules:");
7890Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
7900Sstevel@tonic-gate 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7910Sstevel@tonic-gate 			if (swp->psw_infop->p_owner == conflict_owner)
7920Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s ",
7934481Sbholler 				    swp->psw_infop->p_mach_idstring);
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
7960Sstevel@tonic-gate 		cmn_err(CE_WARN,
7974481Sbholler 		    "Setting the system back to SINGLE processor mode!");
7980Sstevel@tonic-gate 		cmn_err(CE_WARN,
7990Sstevel@tonic-gate 		    "Please edit /etc/mach to remove the invalid PSM module.");
8000Sstevel@tonic-gate 		return;
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (mach_set[PSM_OWN_EXCLUSIVE])
8040Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_EXCLUSIVE);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (mach_set[PSM_OWN_OVERRIDE])
8070Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_OVERRIDE);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate static void
8110Sstevel@tonic-gate mach_init()
8120Sstevel@tonic-gate {
8133446Smrj 	struct psm_ops  *pops;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	mach_construct_info();
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	pops = mach_set[0];
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/* register the interrupt and clock initialization rotuines */
8200Sstevel@tonic-gate 	picinitf = mach_picinit;
8210Sstevel@tonic-gate 	clkinitf = mach_clkinit;
8220Sstevel@tonic-gate 	psm_get_clockirq = pops->psm_get_clockirq;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/* register the interrupt setup code */
8250Sstevel@tonic-gate 	slvltovect = mach_softlvl_to_vect;
8260Sstevel@tonic-gate 	addspl	= pops->psm_addspl;
8270Sstevel@tonic-gate 	delspl	= pops->psm_delspl;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (pops->psm_translate_irq)
8300Sstevel@tonic-gate 		psm_translate_irq = pops->psm_translate_irq;
8310Sstevel@tonic-gate 	if (pops->psm_intr_ops)
8320Sstevel@tonic-gate 		psm_intr_ops = pops->psm_intr_ops;
8333446Smrj 
8343446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
8353446Smrj 	/*
8363446Smrj 	 * Time-of-day functionality now handled in TOD modules.
8373446Smrj 	 * (Warn about PSM modules that think that we're going to use
8383446Smrj 	 * their ops vectors.)
8393446Smrj 	 */
8403446Smrj 	if (pops->psm_tod_get)
8413446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
8423446Smrj 		    (void *)pops->psm_tod_get);
8433446Smrj 
8443446Smrj 	if (pops->psm_tod_set)
8453446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
8463446Smrj 		    (void *)pops->psm_tod_set);
8473446Smrj #endif
8483446Smrj 
8490Sstevel@tonic-gate 	if (pops->psm_notify_error) {
8500Sstevel@tonic-gate 		psm_notify_error = mach_notify_error;
8510Sstevel@tonic-gate 		notify_error = pops->psm_notify_error;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	(*pops->psm_softinit)();
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/*
8570Sstevel@tonic-gate 	 * Initialize the dispatcher's function hooks
8584481Sbholler 	 * to enable CPU halting when idle.
8595045Sbholler 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
8605045Sbholler 	 * or idle_cpu_prefer_mwait is not set.
8614481Sbholler 	 * Allocate monitor/mwait buffer for cpu0.
8620Sstevel@tonic-gate 	 */
8634481Sbholler 	if (idle_cpu_use_hlt) {
8645084Sjohnlev 		idle_cpu = cpu_idle;
8655084Sjohnlev #ifndef __xpv
8664481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
8675045Sbholler 			CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
8685045Sbholler 			/*
8695045Sbholler 			 * Protect ourself from insane mwait size.
8705045Sbholler 			 */
8715045Sbholler 			if (CPU->cpu_m.mcpu_mwait == NULL) {
8725045Sbholler #ifdef DEBUG
8735045Sbholler 				cmn_err(CE_NOTE, "Using hlt idle.  Cannot "
8745045Sbholler 				    "handle cpu 0 mwait size.");
8755045Sbholler #endif
8765045Sbholler 				idle_cpu_prefer_mwait = 0;
8775045Sbholler 				idle_cpu = cpu_idle;
8785045Sbholler 			} else {
8795045Sbholler 				idle_cpu = cpu_idle_mwait;
8805045Sbholler 			}
8814481Sbholler 		} else {
8824481Sbholler 			idle_cpu = cpu_idle;
8834481Sbholler 		}
8845084Sjohnlev #endif
8854481Sbholler 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	mach_smpinit();
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate static void
8910Sstevel@tonic-gate mach_smpinit(void)
8920Sstevel@tonic-gate {
8932006Sandrei 	struct psm_ops  *pops;
8942006Sandrei 	processorid_t cpu_id;
8952006Sandrei 	int cnt;
8962006Sandrei 	cpuset_t cpumask;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	pops = mach_set[0];
8996336Sbholler 	CPUSET_ZERO(cpumask);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	cpu_id = -1;
9020Sstevel@tonic-gate 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
9036336Sbholler 	for (cnt = 0; cpu_id != -1; cnt++) {
9042006Sandrei 		CPUSET_ADD(cpumask, cpu_id);
9050Sstevel@tonic-gate 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	mp_cpus = cpumask;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/* MP related routines */
9110Sstevel@tonic-gate 	ap_mlsetup = pops->psm_post_cpu_start;
9120Sstevel@tonic-gate 	send_dirintf = pops->psm_send_ipi;
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	/* optional MP related routines */
9150Sstevel@tonic-gate 	if (pops->psm_shutdown)
9160Sstevel@tonic-gate 		psm_shutdownf = pops->psm_shutdown;
9170Sstevel@tonic-gate 	if (pops->psm_preshutdown)
9180Sstevel@tonic-gate 		psm_preshutdownf = pops->psm_preshutdown;
9190Sstevel@tonic-gate 	if (pops->psm_notify_func)
9200Sstevel@tonic-gate 		psm_notifyf = pops->psm_notify_func;
9210Sstevel@tonic-gate 	if (pops->psm_set_idlecpu)
9220Sstevel@tonic-gate 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
9230Sstevel@tonic-gate 	if (pops->psm_unset_idlecpu)
9240Sstevel@tonic-gate 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	psm_clkinit = pops->psm_clkinit;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (pops->psm_timer_reprogram)
9290Sstevel@tonic-gate 		psm_timer_reprogram = pops->psm_timer_reprogram;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (pops->psm_timer_enable)
9320Sstevel@tonic-gate 		psm_timer_enable = pops->psm_timer_enable;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (pops->psm_timer_disable)
9350Sstevel@tonic-gate 		psm_timer_disable = pops->psm_timer_disable;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (pops->psm_post_cyclic_setup)
9380Sstevel@tonic-gate 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
9390Sstevel@tonic-gate 
9405295Srandyf 	if (pops->psm_state)
9415295Srandyf 		psm_state = pops->psm_state;
9425295Srandyf 
9437113Sbholler 	/*
9447113Sbholler 	 * Set these vectors here so they can be used by Suspend/Resume
9457113Sbholler 	 * on UP machines.
9467113Sbholler 	 */
9477113Sbholler 	if (pops->psm_disable_intr)
9487113Sbholler 		psm_disable_intr = pops->psm_disable_intr;
9497113Sbholler 	if (pops->psm_enable_intr)
9507113Sbholler 		psm_enable_intr  = pops->psm_enable_intr;
9517113Sbholler 
9527113Sbholler 	/* check for multiple CPUs */
9530Sstevel@tonic-gate 	if (cnt < 2)
9540Sstevel@tonic-gate 		return;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/* check for MP platforms */
9570Sstevel@tonic-gate 	if (pops->psm_cpu_start == NULL)
9580Sstevel@tonic-gate 		return;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/*
9610Sstevel@tonic-gate 	 * Set the dispatcher hook to enable cpu "wake up"
9620Sstevel@tonic-gate 	 * when a thread becomes runnable.
9630Sstevel@tonic-gate 	 */
9645084Sjohnlev 	if (idle_cpu_use_hlt) {
9655084Sjohnlev 		disp_enq_thread = cpu_wakeup;
9665084Sjohnlev #ifndef __xpv
9674481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
9684481Sbholler 			disp_enq_thread = cpu_wakeup_mwait;
9695084Sjohnlev #endif
9705084Sjohnlev 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	psm_get_ipivect = pops->psm_get_ipivect;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr",
9754481Sbholler 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
9764481Sbholler 	    (caddr_t)X_CALL_HIPRI, NULL, NULL, NULL);
9770Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr",
9784481Sbholler 	    (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO),
9794481Sbholler 	    (caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate static void
9850Sstevel@tonic-gate mach_picinit()
9860Sstevel@tonic-gate {
9872006Sandrei 	struct psm_ops  *pops;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	pops = mach_set[0];
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	/* register the interrupt handlers */
9920Sstevel@tonic-gate 	setlvl = pops->psm_intr_enter;
9930Sstevel@tonic-gate 	setlvlx = pops->psm_intr_exit;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/* initialize the interrupt hardware */
9960Sstevel@tonic-gate 	(*pops->psm_picinit)();
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/* set interrupt mask for current ipl */
9990Sstevel@tonic-gate 	setspl = pops->psm_setspl;
10003446Smrj 	cli();
10010Sstevel@tonic-gate 	setspl(CPU->cpu_pri);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate uint_t	cpu_freq;	/* MHz */
10050Sstevel@tonic-gate uint64_t cpu_freq_hz;	/* measured (in hertz) */
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate #define	MEGA_HZ		1000000
10080Sstevel@tonic-gate 
10095084Sjohnlev #ifdef __xpv
10105084Sjohnlev 
10115084Sjohnlev int xpv_cpufreq_workaround = 1;
10125084Sjohnlev int xpv_cpufreq_verbose = 0;
10135084Sjohnlev 
10145084Sjohnlev #else	/* __xpv */
10155084Sjohnlev 
10160Sstevel@tonic-gate static uint64_t
10170Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	uint64_t cpu_hz;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	if ((pit_counter == 0) || (*processor_clks == 0) ||
10220Sstevel@tonic-gate 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
10230Sstevel@tonic-gate 		return (0);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	return (cpu_hz);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10305084Sjohnlev #endif	/* __xpv */
10315084Sjohnlev 
10320Sstevel@tonic-gate static uint64_t
10330Sstevel@tonic-gate mach_getcpufreq(void)
10340Sstevel@tonic-gate {
10355084Sjohnlev #if defined(__xpv)
10365084Sjohnlev 	vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
10375084Sjohnlev 	uint64_t cpu_hz;
10385084Sjohnlev 
10395084Sjohnlev 	/*
10405084Sjohnlev 	 * During dom0 bringup, it was noted that on at least one older
10415084Sjohnlev 	 * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul
10425084Sjohnlev 	 * value that is quite wrong (the 3.06GHz clock was reported
10435084Sjohnlev 	 * as 4.77GHz)
10445084Sjohnlev 	 *
10455084Sjohnlev 	 * The curious thing is, that if you stop the kernel at entry,
10465084Sjohnlev 	 * breakpoint here and inspect the value with kmdb, the value
10475084Sjohnlev 	 * is correct - but if you don't stop and simply enable the
10485084Sjohnlev 	 * printf statement (below), you can see the bad value printed
10495084Sjohnlev 	 * here.  Almost as if something kmdb did caused the hypervisor to
10505084Sjohnlev 	 * figure it out correctly.  And, note that the hypervisor
10515084Sjohnlev 	 * eventually -does- figure it out correctly ... if you look at
10525084Sjohnlev 	 * the field later in the life of dom0, it is correct.
10535084Sjohnlev 	 *
10545084Sjohnlev 	 * For now, on dom0, we employ a slightly cheesy workaround of
10555084Sjohnlev 	 * using the DOM0_PHYSINFO hypercall.
10565084Sjohnlev 	 */
10575084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
10585084Sjohnlev 		xen_sysctl_t op0, *op = &op0;
10595084Sjohnlev 
10605084Sjohnlev 		op->cmd = XEN_SYSCTL_physinfo;
10615084Sjohnlev 		op->interface_version = XEN_SYSCTL_INTERFACE_VERSION;
10625084Sjohnlev 		if (HYPERVISOR_sysctl(op) != 0)
10635084Sjohnlev 			panic("physinfo op refused");
10645084Sjohnlev 
10655084Sjohnlev 		cpu_hz = 1000 * (uint64_t)op->u.physinfo.cpu_khz;
10665084Sjohnlev 	} else {
10675084Sjohnlev 		cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
10685084Sjohnlev 
10695084Sjohnlev 		if (vti->tsc_shift < 0)
10705084Sjohnlev 			cpu_hz <<= -vti->tsc_shift;
10715084Sjohnlev 		else
10725084Sjohnlev 			cpu_hz >>= vti->tsc_shift;
10735084Sjohnlev 	}
10745084Sjohnlev 
10755084Sjohnlev 	if (xpv_cpufreq_verbose)
10765084Sjohnlev 		printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
10775084Sjohnlev 		    "cpu_hz %" PRId64 "Hz\n",
10785084Sjohnlev 		    vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
10795084Sjohnlev 
10805084Sjohnlev 	return (cpu_hz);
10815084Sjohnlev #else	/* __xpv */
10820Sstevel@tonic-gate 	uint32_t pit_counter;
10830Sstevel@tonic-gate 	uint64_t processor_clks;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (x86_feature & X86_TSC) {
10860Sstevel@tonic-gate 		/*
10870Sstevel@tonic-gate 		 * We have a TSC. freq_tsc() knows how to measure the number
10880Sstevel@tonic-gate 		 * of clock cycles sampled against the PIT.
10890Sstevel@tonic-gate 		 */
10903446Smrj 		ulong_t flags = clear_int_flag();
10910Sstevel@tonic-gate 		processor_clks = freq_tsc(&pit_counter);
10923446Smrj 		restore_int_flag(flags);
10930Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
10940Sstevel@tonic-gate 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
10950Sstevel@tonic-gate #if defined(__amd64)
10960Sstevel@tonic-gate 		panic("mach_getcpufreq: no TSC!");
10970Sstevel@tonic-gate #elif defined(__i386)
10980Sstevel@tonic-gate 		/*
10990Sstevel@tonic-gate 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
11000Sstevel@tonic-gate 		 * for which freq_notsc() knows how to measure the number of
11010Sstevel@tonic-gate 		 * elapsed clock cycles sampled against the PIT
11020Sstevel@tonic-gate 		 */
11033446Smrj 		ulong_t flags = clear_int_flag();
11040Sstevel@tonic-gate 		processor_clks = freq_notsc(&pit_counter);
11053446Smrj 		restore_int_flag(flags);
11060Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
11070Sstevel@tonic-gate #endif	/* __i386 */
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/* We do not know how to calculate cpu frequency for this cpu. */
11110Sstevel@tonic-gate 	return (0);
11125084Sjohnlev #endif	/* __xpv */
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate  * If the clock speed of a cpu is found to be reported incorrectly, do not add
11170Sstevel@tonic-gate  * to this array, instead improve the accuracy of the algorithm that determines
11180Sstevel@tonic-gate  * the clock speed of the processor or extend the implementation to support the
11190Sstevel@tonic-gate  * vendor as appropriate. This is here only to support adjusting the speed on
11200Sstevel@tonic-gate  * older slower processors that mach_fixcpufreq() would not be able to account
11210Sstevel@tonic-gate  * for otherwise.
11220Sstevel@tonic-gate  */
11230Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * On fast processors the clock frequency that is measured may be off by
11270Sstevel@tonic-gate  * a few MHz from the value printed on the part. This is a combination of
11280Sstevel@tonic-gate  * the factors that for such fast parts being off by this much is within
11290Sstevel@tonic-gate  * the tolerances for manufacture and because of the difficulties in the
11300Sstevel@tonic-gate  * measurement that can lead to small error. This function uses some
11310Sstevel@tonic-gate  * heuristics in order to tweak the value that was measured to match what
11320Sstevel@tonic-gate  * is most likely printed on the part.
11330Sstevel@tonic-gate  *
11340Sstevel@tonic-gate  * Some examples:
11350Sstevel@tonic-gate  * 	AMD Athlon 1000 mhz measured as 998 mhz
11360Sstevel@tonic-gate  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
11370Sstevel@tonic-gate  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
11380Sstevel@tonic-gate  *
11390Sstevel@tonic-gate  * If in the future this function is no longer sufficient to correct
11400Sstevel@tonic-gate  * for the error in the measurement, then the algorithm used to perform
11410Sstevel@tonic-gate  * the measurement will have to be improved in order to increase accuracy
11420Sstevel@tonic-gate  * rather than adding horrible and questionable kludges here.
11430Sstevel@tonic-gate  *
11440Sstevel@tonic-gate  * This is called after the cyclics subsystem because of the potential
11450Sstevel@tonic-gate  * that the heuristics within may give a worse estimate of the clock
11460Sstevel@tonic-gate  * frequency than the value that was measured.
11470Sstevel@tonic-gate  */
11480Sstevel@tonic-gate static void
11490Sstevel@tonic-gate mach_fixcpufreq(void)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	freq = (uint32_t)cpu_freq;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
11570Sstevel@tonic-gate 	 * measured speed taking into account that the 667 MHz parts were
11580Sstevel@tonic-gate 	 * the first to round-up.
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
11610Sstevel@tonic-gate 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
11620Sstevel@tonic-gate 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
11650Sstevel@tonic-gate 	mul = (freq + 25) / 50;
11660Sstevel@tonic-gate 	near50 = mul * 50;
11670Sstevel@tonic-gate 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/* Find the closer of the two */
11700Sstevel@tonic-gate 	if (delta66 < delta50) {
11710Sstevel@tonic-gate 		fixed = near66;
11720Sstevel@tonic-gate 		delta = delta66;
11730Sstevel@tonic-gate 	} else {
11740Sstevel@tonic-gate 		fixed = near50;
11750Sstevel@tonic-gate 		delta = delta50;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	if (fixed > INT_MAX)
11790Sstevel@tonic-gate 		return;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	/*
11820Sstevel@tonic-gate 	 * Some older parts have a core clock frequency that is not an
11830Sstevel@tonic-gate 	 * integral multiple of 50 or 66 MHz. Check if one of the old
11840Sstevel@tonic-gate 	 * clock frequencies is closer to the measured value than any
11850Sstevel@tonic-gate 	 * of the integral multiples of 50 an 66, and if so set fixed
11860Sstevel@tonic-gate 	 * and delta appropriately to represent the closest value.
11870Sstevel@tonic-gate 	 */
11880Sstevel@tonic-gate 	i = sizeof (x86_cpu_freq) / sizeof (int);
11890Sstevel@tonic-gate 	while (i > 0) {
11900Sstevel@tonic-gate 		i--;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 		if (x86_cpu_freq[i] <= freq) {
11930Sstevel@tonic-gate 			mul = freq - x86_cpu_freq[i];
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 			if (mul < delta) {
11960Sstevel@tonic-gate 				fixed = x86_cpu_freq[i];
11970Sstevel@tonic-gate 				delta = mul;
11980Sstevel@tonic-gate 			}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 			break;
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		mul = x86_cpu_freq[i] - freq;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if (mul < delta) {
12060Sstevel@tonic-gate 			fixed = x86_cpu_freq[i];
12070Sstevel@tonic-gate 			delta = mul;
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/*
12120Sstevel@tonic-gate 	 * Set a reasonable maximum for how much to correct the measured
12130Sstevel@tonic-gate 	 * result by. This check is here to prevent the adjustment made
12140Sstevel@tonic-gate 	 * by this function from being more harm than good. It is entirely
12150Sstevel@tonic-gate 	 * possible that in the future parts will be made that are not
12160Sstevel@tonic-gate 	 * integral multiples of 66 or 50 in clock frequency or that
12170Sstevel@tonic-gate 	 * someone may overclock a part to some odd frequency. If the
12180Sstevel@tonic-gate 	 * measured value is farther from the corrected value than
12190Sstevel@tonic-gate 	 * allowed, then assume the corrected value is in error and use
12200Sstevel@tonic-gate 	 * the measured value.
12210Sstevel@tonic-gate 	 */
12220Sstevel@tonic-gate 	if (6 < delta)
12230Sstevel@tonic-gate 		return;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	cpu_freq = (int)fixed;
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate static int
12300Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	uint64_t cpu_mhz;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/* Round to nearest MHZ */
12350Sstevel@tonic-gate 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if (cpu_mhz > INT_MAX)
12380Sstevel@tonic-gate 		return (0);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	return ((int)cpu_mhz);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate static int
12460Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
12470Sstevel@tonic-gate {
12483446Smrj 	struct psm_ops  *pops;
12490Sstevel@tonic-gate 	int resolution;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	pops = mach_set[0];
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	cpu_freq_hz = mach_getcpufreq();
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	cpu_freq = machhztomhz(cpu_freq_hz);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
12580Sstevel@tonic-gate 		tsc_gethrtime_enable = 0;
12590Sstevel@tonic-gate 
12605084Sjohnlev #ifndef __xpv
12610Sstevel@tonic-gate 	if (tsc_gethrtime_enable) {
12620Sstevel@tonic-gate 		tsc_hrtimeinit(cpu_freq_hz);
12635084Sjohnlev 	} else
12645084Sjohnlev #endif
12655084Sjohnlev 	{
12660Sstevel@tonic-gate 		if (pops->psm_hrtimeinit)
12670Sstevel@tonic-gate 			(*pops->psm_hrtimeinit)();
12680Sstevel@tonic-gate 		gethrtimef = pops->psm_gethrtime;
12690Sstevel@tonic-gate 		gethrtimeunscaledf = gethrtimef;
12700Sstevel@tonic-gate 		/* scalehrtimef will remain dummy */
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	mach_fixcpufreq();
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
12765084Sjohnlev 		if (preferred_mode == TIMER_ONESHOT) {
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 			resolution = (*pops->psm_clkinit)(0);
12790Sstevel@tonic-gate 			if (resolution != 0)  {
12800Sstevel@tonic-gate 				*set_mode = TIMER_ONESHOT;
12810Sstevel@tonic-gate 				return (resolution);
12820Sstevel@tonic-gate 			}
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		/*
12860Sstevel@tonic-gate 		 * either periodic mode was requested or could not set to
12870Sstevel@tonic-gate 		 * one-shot mode
12880Sstevel@tonic-gate 		 */
12890Sstevel@tonic-gate 		resolution = (*pops->psm_clkinit)(hz);
12900Sstevel@tonic-gate 		/*
12910Sstevel@tonic-gate 		 * psm should be able to do periodic, so we do not check
12920Sstevel@tonic-gate 		 * for return value of psm_clkinit here.
12930Sstevel@tonic-gate 		 */
12940Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
12950Sstevel@tonic-gate 		return (resolution);
12960Sstevel@tonic-gate 	} else {
12970Sstevel@tonic-gate 		/*
12980Sstevel@tonic-gate 		 * PSMI interface prior to PSMI_3 does not define a return
12990Sstevel@tonic-gate 		 * value for psm_clkinit, so the return value is ignored.
13000Sstevel@tonic-gate 		 */
13010Sstevel@tonic-gate 		(void) (*pops->psm_clkinit)(hz);
13020Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
13030Sstevel@tonic-gate 		return (nsec_per_tick);
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13074652Scwb 
1308999Slq150181 /*ARGSUSED*/
13090Sstevel@tonic-gate static int
13103446Smrj mach_softlvl_to_vect(int ipl)
13110Sstevel@tonic-gate {
13124652Scwb 	setsoftint = av_set_softint_pending;
13134652Scwb 	kdisetsoftint = kdi_av_set_softint_pending;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	return (PSM_SV_SOFTWARE);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate 
13183446Smrj #ifdef DEBUG
13193446Smrj /*
13203446Smrj  * This is here to allow us to simulate cpus that refuse to start.
13213446Smrj  */
13223446Smrj cpuset_t cpufailset;
13233446Smrj #endif
13243446Smrj 
13253446Smrj int
13263446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
13270Sstevel@tonic-gate {
13283446Smrj 	struct psm_ops *pops = mach_set[0];
13293446Smrj 	processorid_t id = cp->cpu_id;
13300Sstevel@tonic-gate 
13313446Smrj #ifdef DEBUG
13323446Smrj 	if (CPU_IN_SET(cpufailset, id))
13333446Smrj 		return (0);
13343446Smrj #endif
13353446Smrj 	return ((*pops->psm_cpu_start)(id, ctx));
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13385295Srandyf int
13395295Srandyf mach_cpuid_start(processorid_t id, void *ctx)
13405295Srandyf {
13415295Srandyf 	struct psm_ops *pops = mach_set[0];
13425295Srandyf 
13435295Srandyf #ifdef DEBUG
13445295Srandyf 	if (CPU_IN_SET(cpufailset, id))
13455295Srandyf 		return (0);
13465295Srandyf #endif
13475295Srandyf 	return ((*pops->psm_cpu_start)(id, ctx));
13485295Srandyf }
13495295Srandyf 
13500Sstevel@tonic-gate /*ARGSUSED*/
13510Sstevel@tonic-gate static int
13520Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
13530Sstevel@tonic-gate {
13540Sstevel@tonic-gate 	return (irqno);	/* default to NO translation */
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate static void
13580Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	/*
13610Sstevel@tonic-gate 	 * SL_FATAL is pass in once panicstr is set, deliver it
13620Sstevel@tonic-gate 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
13630Sstevel@tonic-gate 	 * codes for the psmi handler
13640Sstevel@tonic-gate 	 */
13650Sstevel@tonic-gate 	if (level & SL_FATAL)
13660Sstevel@tonic-gate 		(*notify_error)(CE_PANIC, errmsg);
13670Sstevel@tonic-gate 	else if (level & SL_WARN)
13680Sstevel@tonic-gate 		(*notify_error)(CE_WARN, errmsg);
13690Sstevel@tonic-gate 	else if (level & SL_NOTE)
13700Sstevel@tonic-gate 		(*notify_error)(CE_NOTE, errmsg);
13710Sstevel@tonic-gate 	else if (level & SL_CONSOLE)
13720Sstevel@tonic-gate 		(*notify_error)(CE_CONT, errmsg);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate  * It provides the default basic intr_ops interface for the new DDI
13770Sstevel@tonic-gate  * interrupt framework if the PSM doesn't have one.
13780Sstevel@tonic-gate  *
13790Sstevel@tonic-gate  * Input:
13800Sstevel@tonic-gate  * dip     - pointer to the dev_info structure of the requested device
13810Sstevel@tonic-gate  * hdlp    - pointer to the internal interrupt handle structure for the
13820Sstevel@tonic-gate  *	     requested interrupt
13830Sstevel@tonic-gate  * intr_op - opcode for this call
13840Sstevel@tonic-gate  * result  - pointer to the integer that will hold the result to be
13850Sstevel@tonic-gate  *	     passed back if return value is PSM_SUCCESS
13860Sstevel@tonic-gate  *
13870Sstevel@tonic-gate  * Output:
13880Sstevel@tonic-gate  * return value is either PSM_SUCCESS or PSM_FAILURE
13890Sstevel@tonic-gate  */
13900Sstevel@tonic-gate static int
13910Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
13920Sstevel@tonic-gate     psm_intr_op_t intr_op, int *result)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	struct intrspec *ispec;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	switch (intr_op) {
13970Sstevel@tonic-gate 	case PSM_INTR_OP_CHECK_MSI:
13980Sstevel@tonic-gate 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
13994481Sbholler 		    DDI_INTR_TYPE_MSIX);
14000Sstevel@tonic-gate 		break;
14010Sstevel@tonic-gate 	case PSM_INTR_OP_ALLOC_VECTORS:
14020Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
14030Sstevel@tonic-gate 			*result = 1;
14040Sstevel@tonic-gate 		else
14050Sstevel@tonic-gate 			*result = 0;
14060Sstevel@tonic-gate 		break;
14070Sstevel@tonic-gate 	case PSM_INTR_OP_FREE_VECTORS:
14080Sstevel@tonic-gate 		break;
14090Sstevel@tonic-gate 	case PSM_INTR_OP_NAVAIL_VECTORS:
14100Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
14110Sstevel@tonic-gate 			*result = 1;
14120Sstevel@tonic-gate 		else
14130Sstevel@tonic-gate 			*result = 0;
14140Sstevel@tonic-gate 		break;
14150Sstevel@tonic-gate 	case PSM_INTR_OP_XLATE_VECTOR:
1416916Sschwartz 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
14170Sstevel@tonic-gate 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
14180Sstevel@tonic-gate 		break;
14190Sstevel@tonic-gate 	case PSM_INTR_OP_GET_CAP:
14200Sstevel@tonic-gate 		*result = 0;
14210Sstevel@tonic-gate 		break;
14220Sstevel@tonic-gate 	case PSM_INTR_OP_GET_PENDING:
14230Sstevel@tonic-gate 	case PSM_INTR_OP_CLEAR_MASK:
14240Sstevel@tonic-gate 	case PSM_INTR_OP_SET_MASK:
14250Sstevel@tonic-gate 	case PSM_INTR_OP_GET_SHARED:
14260Sstevel@tonic-gate 	case PSM_INTR_OP_SET_PRI:
14270Sstevel@tonic-gate 	case PSM_INTR_OP_SET_CAP:
1428916Sschwartz 	case PSM_INTR_OP_SET_CPU:
1429916Sschwartz 	case PSM_INTR_OP_GET_INTR:
14300Sstevel@tonic-gate 	default:
14310Sstevel@tonic-gate 		return (PSM_FAILURE);
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 	return (PSM_SUCCESS);
14340Sstevel@tonic-gate }
14354769Sdp78419 /*
14364769Sdp78419  * Return 1 if CMT load balancing policies should be
14374769Sdp78419  * implemented across instances of the specified hardware
14384769Sdp78419  * sharing relationship.
14394769Sdp78419  */
14404769Sdp78419 int
14414769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw)
14424769Sdp78419 {
14434769Sdp78419 	if (hw == PGHW_IPIPE ||
14444769Sdp78419 	    hw == PGHW_FPU ||
14454769Sdp78419 	    hw == PGHW_CHIP)
14464769Sdp78419 		return (1);
14474769Sdp78419 	else
14484769Sdp78419 		return (0);
14494769Sdp78419 }
14504769Sdp78419 /*
14514769Sdp78419  * Return 1 if thread affinity polices should be implemented
14524769Sdp78419  * for instances of the specifed hardware sharing relationship.
14534769Sdp78419  */
14544769Sdp78419 int
14554769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw)
14564769Sdp78419 {
14574769Sdp78419 	if (hw == PGHW_CACHE)
14584769Sdp78419 		return (1);
14594769Sdp78419 	else
14604769Sdp78419 		return (0);
14614769Sdp78419 }
1462