xref: /onnv-gate/usr/src/uts/i86pc/os/mp_machdep.c (revision 5045:75a798a98460)
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 /*
223434Sesaxe  * Copyright 2007 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 
280Sstevel@tonic-gate #define	PSMI_1_5
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>
473446Smrj #include <sys/param.h>
483446Smrj #include <sys/promif.h>
49916Sschwartz #include <sys/mach_intr.h>
504481Sbholler #include <vm/hat_i86.h>
514652Scwb #include <sys/kdi_machimpl.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  *	Local function prototypes
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
590Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
600Sstevel@tonic-gate static void mach_init();
610Sstevel@tonic-gate static void mach_picinit();
620Sstevel@tonic-gate static uint64_t mach_calchz(uint32_t pit_counter, uint64_t *processor_clks);
630Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
640Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
650Sstevel@tonic-gate static void mach_fixcpufreq(void);
660Sstevel@tonic-gate static int mach_clkinit(int, int *);
670Sstevel@tonic-gate static void mach_smpinit(void);
680Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl);
690Sstevel@tonic-gate static void mach_get_platform(int owner);
700Sstevel@tonic-gate static void mach_construct_info();
710Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno);
720Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
730Sstevel@tonic-gate     psm_intr_op_t, int *);
740Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg);
750Sstevel@tonic-gate static hrtime_t dummy_hrtime(void);
760Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *);
773446Smrj static void cpu_idle(void);
780Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int);
794481Sbholler static void cpu_idle_mwait(void);
804481Sbholler static void cpu_wakeup_mwait(cpu_t *, int);
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  *	External reference functions
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate extern void return_instr();
850Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *);
860Sstevel@tonic-gate #if defined(__i386)
870Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *);
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *);
903434Sesaxe extern int cpuid_get_coreid(cpu_t *);
913434Sesaxe extern int cpuid_get_chipid(cpu_t *);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  *	PSM functions initialization
950Sstevel@tonic-gate  */
963446Smrj void (*psm_shutdownf)(int, int)	= (void (*)(int, int))return_instr;
973446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
983446Smrj void (*psm_notifyf)(int)	= (void (*)(int))return_instr;
993446Smrj void (*psm_set_idle_cpuf)(int)	= (void (*)(int))return_instr;
1003446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
1010Sstevel@tonic-gate void (*psminitf)()		= mach_init;
1020Sstevel@tonic-gate void (*picinitf)() 		= return_instr;
1030Sstevel@tonic-gate int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
1040Sstevel@tonic-gate int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
1050Sstevel@tonic-gate void (*send_dirintf)() 		= return_instr;
1063446Smrj void (*setspl)(int)		= (void (*)(int))return_instr;
1070Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1080Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1094652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)=
1104652Scwb 	(void (*)(int, struct av_softinfo *))return_instr;
111999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
112999Slq150181 	(void (*)(int, struct av_softinfo *))return_instr;
1130Sstevel@tonic-gate int (*slvltovect)(int)		= (int (*)(int))return_instr;
1140Sstevel@tonic-gate int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
1150Sstevel@tonic-gate void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
1160Sstevel@tonic-gate int (*psm_disable_intr)(int)	= mp_disable_intr;
1170Sstevel@tonic-gate void (*psm_enable_intr)(int)	= mp_enable_intr;
1180Sstevel@tonic-gate hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
1190Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
1200Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
1210Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1220Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1230Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1240Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1250Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1280Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1290Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1300Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1310Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1320Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1330Sstevel@tonic-gate     int *) = mach_intr_ops;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1360Sstevel@tonic-gate void (*hrtime_tick)(void)	= return_instr;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1390Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Local Static Data
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static struct psm_ops mach_ops;
1450Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1460Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1493446Smrj  * If non-zero, idle cpus will become "halted" when there's
1500Sstevel@tonic-gate  * no work to do.
1510Sstevel@tonic-gate  */
1523446Smrj int	idle_cpu_use_hlt = 1;
1530Sstevel@tonic-gate 
1544481Sbholler /*
1554481Sbholler  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
1564481Sbholler  */
1574481Sbholler int	idle_cpu_prefer_mwait = 1;
1584481Sbholler 
1593434Sesaxe 
1603434Sesaxe /*ARGSUSED*/
1613434Sesaxe int
1623434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
1633434Sesaxe {
1643434Sesaxe 	switch (hw) {
1653434Sesaxe 	case PGHW_IPIPE:
1663434Sesaxe 		if (x86_feature & (X86_HTT)) {
1673434Sesaxe 			/*
1683434Sesaxe 			 * Hyper-threading is SMT
1693434Sesaxe 			 */
1703434Sesaxe 			return (1);
1713434Sesaxe 		} else {
1723434Sesaxe 			return (0);
1733434Sesaxe 		}
1743434Sesaxe 	case PGHW_CHIP:
1753434Sesaxe 		if (x86_feature & (X86_CMP|X86_HTT))
1763434Sesaxe 			return (1);
1773434Sesaxe 		else
1783434Sesaxe 			return (0);
1794606Sesaxe 	case PGHW_CACHE:
1804606Sesaxe 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
1814606Sesaxe 			return (1);
1824606Sesaxe 		else
1834606Sesaxe 			return (0);
1843434Sesaxe 	default:
1853434Sesaxe 		return (0);
1863434Sesaxe 	}
1873434Sesaxe }
1883434Sesaxe 
1893434Sesaxe /*
1903434Sesaxe  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
1913434Sesaxe  * If pghw_type_t is an unsupported hardware type, then return -1
1923434Sesaxe  */
1933434Sesaxe int
1943434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
1950Sstevel@tonic-gate {
1963434Sesaxe 	id_t pgp_a, pgp_b;
1973434Sesaxe 
1983434Sesaxe 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
1993434Sesaxe 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
2003434Sesaxe 
2013434Sesaxe 	if (pgp_a == -1 || pgp_b == -1)
2023434Sesaxe 		return (-1);
2033434Sesaxe 
2043434Sesaxe 	return (pgp_a == pgp_b);
2053434Sesaxe }
2063434Sesaxe 
2073434Sesaxe /*
2083434Sesaxe  * Return a physical instance identifier for known hardware sharing
2093434Sesaxe  * relationships
2103434Sesaxe  */
2113434Sesaxe id_t
2123434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
2133434Sesaxe {
2143434Sesaxe 	switch (hw) {
2153434Sesaxe 	case PGHW_IPIPE:
2163434Sesaxe 		return (cpuid_get_coreid(cpu));
2174606Sesaxe 	case PGHW_CACHE:
2184606Sesaxe 		return (cpuid_get_last_lvl_cacheid(cpu));
2193434Sesaxe 	case PGHW_CHIP:
2203434Sesaxe 		return (cpuid_get_chipid(cpu));
2213434Sesaxe 	default:
2223434Sesaxe 		return (-1);
2231228Sandrei 	}
2243434Sesaxe }
2250Sstevel@tonic-gate 
2263434Sesaxe int
2273434Sesaxe pg_plat_hw_level(pghw_type_t hw)
2283434Sesaxe {
2293434Sesaxe 	int i;
2303434Sesaxe 	static pghw_type_t hw_hier[] = {
2313434Sesaxe 		PGHW_IPIPE,
2324606Sesaxe 		PGHW_CACHE,
2333434Sesaxe 		PGHW_CHIP,
2343434Sesaxe 		PGHW_NUM_COMPONENTS
2353434Sesaxe 	};
2363434Sesaxe 
2373434Sesaxe 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
2383434Sesaxe 		if (hw_hier[i] == hw)
2393434Sesaxe 			return (i);
2403434Sesaxe 	}
2413434Sesaxe 	return (-1);
2423434Sesaxe }
2433434Sesaxe 
2444606Sesaxe /*
2454606Sesaxe  * Return 1 if CMT load balancing policies should be
2464606Sesaxe  * implemented across instances of the specified hardware
2474606Sesaxe  * sharing relationship.
2484606Sesaxe  */
2494606Sesaxe int
2504606Sesaxe pg_plat_cmt_load_bal_hw(pghw_type_t hw)
2514606Sesaxe {
2524606Sesaxe 	if (hw == PGHW_IPIPE ||
2534606Sesaxe 	    hw == PGHW_FPU ||
2544606Sesaxe 	    hw == PGHW_CHIP ||
2554606Sesaxe 	    hw == PGHW_CACHE)
2564606Sesaxe 		return (1);
2574606Sesaxe 	else
2584606Sesaxe 		return (0);
2594606Sesaxe }
2604606Sesaxe 
2614606Sesaxe 
2624606Sesaxe /*
2634606Sesaxe  * Return 1 if thread affinity polices should be implemented
2644606Sesaxe  * for instances of the specifed hardware sharing relationship.
2654606Sesaxe  */
2664606Sesaxe int
2674606Sesaxe pg_plat_cmt_affinity_hw(pghw_type_t hw)
2684606Sesaxe {
2694606Sesaxe 	if (hw == PGHW_CACHE)
2704606Sesaxe 		return (1);
2714606Sesaxe 	else
2724606Sesaxe 		return (0);
2734606Sesaxe }
2744606Sesaxe 
2753434Sesaxe id_t
2763434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
2773434Sesaxe {
2783434Sesaxe 	return ((id_t)cpuid_get_coreid(cpu));
2793434Sesaxe }
2803434Sesaxe 
2813434Sesaxe void
2823434Sesaxe cmp_set_nosteal_interval(void)
2833434Sesaxe {
2843434Sesaxe 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
2853434Sesaxe 	nosteal_nsec = 100000UL;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Routine to ensure initial callers to hrtime gets 0 as return
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate static hrtime_t
2920Sstevel@tonic-gate dummy_hrtime(void)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	return (0);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /* ARGSUSED */
2980Sstevel@tonic-gate static void
2990Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
3000Sstevel@tonic-gate {}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3033446Smrj  * Idle the present CPU until awoken via an interrupt
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate static void
3063446Smrj cpu_idle(void)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	cpu_t		*cpup = CPU;
3090Sstevel@tonic-gate 	processorid_t	cpun = cpup->cpu_id;
310711Sesaxe 	cpupart_t	*cp = cpup->cpu_part;
3110Sstevel@tonic-gate 	int		hset_update = 1;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * If this CPU is online, and there's multiple CPUs
3150Sstevel@tonic-gate 	 * in the system, then we should notate our halting
3160Sstevel@tonic-gate 	 * by adding ourselves to the partition's halted CPU
3170Sstevel@tonic-gate 	 * bitmap. This allows other CPUs to find/awaken us when
3180Sstevel@tonic-gate 	 * work becomes available.
3190Sstevel@tonic-gate 	 */
3200Sstevel@tonic-gate 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
3210Sstevel@tonic-gate 		hset_update = 0;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Add ourselves to the partition's halted CPUs bitmask
3250Sstevel@tonic-gate 	 * and set our HALTED flag, if necessary.
3260Sstevel@tonic-gate 	 *
327711Sesaxe 	 * When a thread becomes runnable, it is placed on the queue
328711Sesaxe 	 * and then the halted cpuset is checked to determine who
329711Sesaxe 	 * (if anyone) should be awoken. We therefore need to first
330711Sesaxe 	 * add ourselves to the halted cpuset, and and then check if there
331711Sesaxe 	 * is any work available.
332711Sesaxe 	 *
3330Sstevel@tonic-gate 	 * Note that memory barriers after updating the HALTED flag
3340Sstevel@tonic-gate 	 * are not necessary since an atomic operation (updating the bitmap)
3350Sstevel@tonic-gate 	 * immediately follows. On x86 the atomic operation acts as a
3360Sstevel@tonic-gate 	 * memory barrier for the update of cpu_disp_flags.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if (hset_update) {
3390Sstevel@tonic-gate 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
3402722Sjohnlev 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Check to make sure there's really nothing to do.
345711Sesaxe 	 * Work destined for this CPU may become available after
346711Sesaxe 	 * this check. We'll be notified through the clearing of our
347711Sesaxe 	 * bit in the halted CPU bitmask, and a poke.
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	if (disp_anywork()) {
3500Sstevel@tonic-gate 		if (hset_update) {
3510Sstevel@tonic-gate 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
3522722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
3530Sstevel@tonic-gate 		}
354711Sesaxe 		return;
355711Sesaxe 	}
356711Sesaxe 
357711Sesaxe 	/*
358711Sesaxe 	 * We're on our way to being halted.
359711Sesaxe 	 *
360711Sesaxe 	 * Disable interrupts now, so that we'll awaken immediately
361711Sesaxe 	 * after halting if someone tries to poke us between now and
362711Sesaxe 	 * the time we actually halt.
363711Sesaxe 	 *
364711Sesaxe 	 * We check for the presence of our bit after disabling interrupts.
365711Sesaxe 	 * If it's cleared, we'll return. If the bit is cleared after
366711Sesaxe 	 * we check then the poke will pop us out of the halted state.
367711Sesaxe 	 *
368711Sesaxe 	 * This means that the ordering of the poke and the clearing
369711Sesaxe 	 * of the bit by cpu_wakeup is important.
370711Sesaxe 	 * cpu_wakeup() must clear, then poke.
3713446Smrj 	 * cpu_idle() must disable interrupts, then check for the bit.
372711Sesaxe 	 */
373711Sesaxe 	cli();
374711Sesaxe 
3752722Sjohnlev 	if (hset_update && !CPU_IN_SET(cp->cp_mach->mc_haltset, cpun)) {
376711Sesaxe 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
377711Sesaxe 		sti();
378711Sesaxe 		return;
379711Sesaxe 	}
380711Sesaxe 
381711Sesaxe 	/*
382711Sesaxe 	 * The check for anything locally runnable is here for performance
383711Sesaxe 	 * and isn't needed for correctness. disp_nrunnable ought to be
384711Sesaxe 	 * in our cache still, so it's inexpensive to check, and if there
385711Sesaxe 	 * is anything runnable we won't have to wait for the poke.
386711Sesaxe 	 */
387711Sesaxe 	if (cpup->cpu_disp->disp_nrunnable != 0) {
388711Sesaxe 		if (hset_update) {
389711Sesaxe 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
3902722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
391711Sesaxe 		}
3920Sstevel@tonic-gate 		sti();
3930Sstevel@tonic-gate 		return;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3963446Smrj 	mach_cpu_idle();
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * We're no longer halted
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (hset_update) {
4020Sstevel@tonic-gate 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4032722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
4100Sstevel@tonic-gate  * Otherwise, see if other CPUs in the cpu partition are halted and need to
4110Sstevel@tonic-gate  * be woken up so that they can steal the thread we placed on this CPU.
4120Sstevel@tonic-gate  * This function is only used on MP systems.
4130Sstevel@tonic-gate  */
4140Sstevel@tonic-gate static void
4150Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	uint_t		cpu_found;
4180Sstevel@tonic-gate 	int		result;
4190Sstevel@tonic-gate 	cpupart_t	*cp;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	cp = cpu->cpu_part;
4222722Sjohnlev 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
4230Sstevel@tonic-gate 		/*
4240Sstevel@tonic-gate 		 * Clear the halted bit for that CPU since it will be
4250Sstevel@tonic-gate 		 * poked in a moment.
4260Sstevel@tonic-gate 		 */
4272722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
4280Sstevel@tonic-gate 		/*
4290Sstevel@tonic-gate 		 * We may find the current CPU present in the halted cpuset
4300Sstevel@tonic-gate 		 * if we're in the context of an interrupt that occurred
4313446Smrj 		 * before we had a chance to clear our bit in cpu_idle().
4320Sstevel@tonic-gate 		 * Poking ourself is obviously unnecessary, since if
4330Sstevel@tonic-gate 		 * we're here, we're not halted.
4340Sstevel@tonic-gate 		 */
4350Sstevel@tonic-gate 		if (cpu != CPU)
4360Sstevel@tonic-gate 			poke_cpu(cpu->cpu_id);
4370Sstevel@tonic-gate 		return;
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 		/*
4400Sstevel@tonic-gate 		 * This cpu isn't halted, but it's idle or undergoing a
4410Sstevel@tonic-gate 		 * context switch. No need to awaken anyone else.
4420Sstevel@tonic-gate 		 */
4430Sstevel@tonic-gate 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
4440Sstevel@tonic-gate 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
4450Sstevel@tonic-gate 			return;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * No need to wake up other CPUs if the thread we just enqueued
4500Sstevel@tonic-gate 	 * is bound.
4510Sstevel@tonic-gate 	 */
4520Sstevel@tonic-gate 	if (bound)
4530Sstevel@tonic-gate 		return;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * See if there's any other halted CPUs. If there are, then
4580Sstevel@tonic-gate 	 * select one, and awaken it.
4590Sstevel@tonic-gate 	 * It's possible that after we find a CPU, somebody else
4600Sstevel@tonic-gate 	 * will awaken it before we get the chance.
4610Sstevel@tonic-gate 	 * In that case, look again.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	do {
4642722Sjohnlev 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
4650Sstevel@tonic-gate 		if (cpu_found == CPUSET_NOTINSET)
4660Sstevel@tonic-gate 			return;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
4692722Sjohnlev 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
4700Sstevel@tonic-gate 	} while (result < 0);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (cpu_found != CPU->cpu_id)
4730Sstevel@tonic-gate 		poke_cpu(cpu_found);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4764481Sbholler /*
4774481Sbholler  * Idle the present CPU until awoken via touching its monitored line
4784481Sbholler  */
4794481Sbholler static void
4804481Sbholler cpu_idle_mwait(void)
4814481Sbholler {
4824481Sbholler 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
4834481Sbholler 	cpu_t			*cpup = CPU;
4844481Sbholler 	processorid_t		cpun = cpup->cpu_id;
4854481Sbholler 	cpupart_t		*cp = cpup->cpu_part;
4864481Sbholler 	int			hset_update = 1;
4874481Sbholler 
4884481Sbholler 	/*
4894481Sbholler 	 * Set our mcpu_mwait here, so we can tell if anyone trys to
4904481Sbholler 	 * wake us between now and when we call mwait.  No other cpu will
4914481Sbholler 	 * attempt to set our mcpu_mwait until we add ourself to the haltset.
4924481Sbholler 	 */
4934481Sbholler 	*mcpu_mwait = MWAIT_HALTED;
4944481Sbholler 
4954481Sbholler 	/*
4964481Sbholler 	 * If this CPU is online, and there's multiple CPUs
4974481Sbholler 	 * in the system, then we should notate our halting
4984481Sbholler 	 * by adding ourselves to the partition's halted CPU
4994481Sbholler 	 * bitmap. This allows other CPUs to find/awaken us when
5004481Sbholler 	 * work becomes available.
5014481Sbholler 	 */
5024481Sbholler 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
5034481Sbholler 		hset_update = 0;
5044481Sbholler 
5054481Sbholler 	/*
5064481Sbholler 	 * Add ourselves to the partition's halted CPUs bitmask
5074481Sbholler 	 * and set our HALTED flag, if necessary.
5084481Sbholler 	 *
5094481Sbholler 	 * When a thread becomes runnable, it is placed on the queue
5104481Sbholler 	 * and then the halted cpuset is checked to determine who
5114481Sbholler 	 * (if anyone) should be awoken. We therefore need to first
5124481Sbholler 	 * add ourselves to the halted cpuset, and and then check if there
5134481Sbholler 	 * is any work available.
5144481Sbholler 	 *
5154481Sbholler 	 * Note that memory barriers after updating the HALTED flag
5164481Sbholler 	 * are not necessary since an atomic operation (updating the bitmap)
5174481Sbholler 	 * immediately follows. On x86 the atomic operation acts as a
5184481Sbholler 	 * memory barrier for the update of cpu_disp_flags.
5194481Sbholler 	 */
5204481Sbholler 	if (hset_update) {
5214481Sbholler 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
5224481Sbholler 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
5234481Sbholler 	}
5244481Sbholler 
5254481Sbholler 	/*
5264481Sbholler 	 * Check to make sure there's really nothing to do.
5274481Sbholler 	 * Work destined for this CPU may become available after
5284481Sbholler 	 * this check. We'll be notified through the clearing of our
5294481Sbholler 	 * bit in the halted CPU bitmask, and a write to our mcpu_mwait.
5304481Sbholler 	 *
5314481Sbholler 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
5324481Sbholler 	 */
5334481Sbholler 	if (disp_anywork()) {
5344481Sbholler 		if (hset_update) {
5354481Sbholler 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5364481Sbholler 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5374481Sbholler 		}
5384481Sbholler 		return;
5394481Sbholler 	}
5404481Sbholler 
5414481Sbholler 	/*
5424481Sbholler 	 * We're on our way to being halted.
5434481Sbholler 	 * To avoid a lost wakeup, arm the monitor before checking if another
5444481Sbholler 	 * cpu wrote to mcpu_mwait to wake us up.
5454481Sbholler 	 */
5464481Sbholler 	i86_monitor(mcpu_mwait, 0, 0);
5474481Sbholler 	if (*mcpu_mwait == MWAIT_HALTED) {
5484481Sbholler 		tlb_going_idle();
5494481Sbholler 		i86_mwait(0, 0);
5504481Sbholler 		tlb_service();
5514481Sbholler 	}
5524481Sbholler 
5534481Sbholler 	/*
5544481Sbholler 	 * We're no longer halted
5554481Sbholler 	 */
5564481Sbholler 	if (hset_update) {
5574481Sbholler 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5584481Sbholler 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5594481Sbholler 	}
5604481Sbholler }
5614481Sbholler 
5624481Sbholler /*
5634481Sbholler  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
5644481Sbholler  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
5654481Sbholler  * need to be woken up so that they can steal the thread we placed on this CPU.
5664481Sbholler  * This function is only used on MP systems.
5674481Sbholler  */
5684481Sbholler static void
5694481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
5704481Sbholler {
5714481Sbholler 	cpupart_t	*cpu_part;
5724481Sbholler 	uint_t		cpu_found;
5734481Sbholler 	int		result;
5744481Sbholler 
5754481Sbholler 	cpu_part = cp->cpu_part;
5764481Sbholler 
5774481Sbholler 	/*
5784481Sbholler 	 * Clear the halted bit for that CPU since it will be woken up
5794481Sbholler 	 * in a moment.
5804481Sbholler 	 */
5814481Sbholler 	if (CPU_IN_SET(cpu_part->cp_mach->mc_haltset, cp->cpu_id)) {
5824481Sbholler 		/*
5834481Sbholler 		 * Clear the halted bit for that CPU since it will be
5844481Sbholler 		 * poked in a moment.
5854481Sbholler 		 */
5864481Sbholler 		CPUSET_ATOMIC_DEL(cpu_part->cp_mach->mc_haltset, cp->cpu_id);
5874481Sbholler 		/*
5884481Sbholler 		 * We may find the current CPU present in the halted cpuset
5894481Sbholler 		 * if we're in the context of an interrupt that occurred
5904481Sbholler 		 * before we had a chance to clear our bit in cpu_idle().
5914481Sbholler 		 * Waking ourself is obviously unnecessary, since if
5924481Sbholler 		 * we're here, we're not halted.
5934481Sbholler 		 *
5944481Sbholler 		 * monitor/mwait wakeup via writing to our cache line is
5954481Sbholler 		 * harmless and less expensive than always checking if we
5964481Sbholler 		 * are waking ourself which is an uncommon case.
5974481Sbholler 		 */
5984481Sbholler 		MWAIT_WAKEUP(cp);	/* write to monitored line */
5994481Sbholler 		return;
6004481Sbholler 	} else {
6014481Sbholler 		/*
6024481Sbholler 		 * This cpu isn't halted, but it's idle or undergoing a
6034481Sbholler 		 * context switch. No need to awaken anyone else.
6044481Sbholler 		 */
6054481Sbholler 		if (cp->cpu_thread == cp->cpu_idle_thread ||
6064481Sbholler 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
6074481Sbholler 			return;
6084481Sbholler 	}
6094481Sbholler 
6104481Sbholler 	/*
6114481Sbholler 	 * No need to wake up other CPUs if the thread we just enqueued
6124481Sbholler 	 * is bound.
6134481Sbholler 	 */
6144481Sbholler 	if (bound)
6154481Sbholler 		return;
6164481Sbholler 
6174481Sbholler 
6184481Sbholler 	/*
6194481Sbholler 	 * See if there's any other halted CPUs. If there are, then
6204481Sbholler 	 * select one, and awaken it.
6214481Sbholler 	 * It's possible that after we find a CPU, somebody else
6224481Sbholler 	 * will awaken it before we get the chance.
6234481Sbholler 	 * In that case, look again.
6244481Sbholler 	 */
6254481Sbholler 	do {
6264481Sbholler 		CPUSET_FIND(cpu_part->cp_mach->mc_haltset, cpu_found);
6274481Sbholler 		if (cpu_found == CPUSET_NOTINSET)
6284481Sbholler 			return;
6294481Sbholler 
6304481Sbholler 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
6314481Sbholler 		CPUSET_ATOMIC_XDEL(cpu_part->cp_mach->mc_haltset, cpu_found,
6324481Sbholler 		    result);
6334481Sbholler 	} while (result < 0);
6344481Sbholler 
6354481Sbholler 	/*
6364481Sbholler 	 * Do not check if cpu_found is ourself as monitor/mwait wakeup is
6374481Sbholler 	 * cheap.
6384481Sbholler 	 */
6394481Sbholler 	MWAIT_WAKEUP(cpu[cpu_found]);	/* write to monitored line */
6404481Sbholler }
6414481Sbholler 
6423446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
6433446Smrj 
6440Sstevel@tonic-gate static int
6450Sstevel@tonic-gate mp_disable_intr(int cpun)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	/*
6480Sstevel@tonic-gate 	 * switch to the offline cpu
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	affinity_set(cpun);
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * raise ipl to just below cross call
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	splx(XC_MED_PIL-1);
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 *	set base spl to prevent the next swtch to idle from
6570Sstevel@tonic-gate 	 *	lowering back to ipl 0
6580Sstevel@tonic-gate 	 */
6590Sstevel@tonic-gate 	CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1));
6600Sstevel@tonic-gate 	set_base_spl();
6610Sstevel@tonic-gate 	affinity_clear();
6620Sstevel@tonic-gate 	return (DDI_SUCCESS);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate static void
6660Sstevel@tonic-gate mp_enable_intr(int cpun)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * switch to the online cpu
6700Sstevel@tonic-gate 	 */
6710Sstevel@tonic-gate 	affinity_set(cpun);
6720Sstevel@tonic-gate 	/*
6730Sstevel@tonic-gate 	 * clear the interrupt active mask
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1));
6760Sstevel@tonic-gate 	set_base_spl();
6770Sstevel@tonic-gate 	(void) spl0();
6780Sstevel@tonic-gate 	affinity_clear();
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate static void
6820Sstevel@tonic-gate mach_get_platform(int owner)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	void		**srv_opsp;
6850Sstevel@tonic-gate 	void		**clt_opsp;
6860Sstevel@tonic-gate 	int		i;
6870Sstevel@tonic-gate 	int		total_ops;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/* fix up psm ops */
6900Sstevel@tonic-gate 	srv_opsp = (void **)mach_set[0];
6910Sstevel@tonic-gate 	clt_opsp = (void **)mach_set[owner];
6920Sstevel@tonic-gate 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
6930Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops_ver01) /
6944481Sbholler 		    sizeof (void (*)(void));
6950Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
6960Sstevel@tonic-gate 		/* no psm_notify_func */
6970Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
6980Sstevel@tonic-gate 		    sizeof (void (*)(void));
6990Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
7000Sstevel@tonic-gate 		/* no psm_timer funcs */
7010Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
7020Sstevel@tonic-gate 		    sizeof (void (*)(void));
7030Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
7040Sstevel@tonic-gate 		/* no psm_preshutdown function */
7050Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
7060Sstevel@tonic-gate 		    sizeof (void (*)(void));
7070Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
7080Sstevel@tonic-gate 		/* no psm_preshutdown function */
7090Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
7100Sstevel@tonic-gate 		    sizeof (void (*)(void));
7110Sstevel@tonic-gate 	else
7120Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/*
7150Sstevel@tonic-gate 	 * Save the version of the PSM module, in case we need to
7160Sstevel@tonic-gate 	 * bahave differently based on version.
7170Sstevel@tonic-gate 	 */
7180Sstevel@tonic-gate 	mach_ver[0] = mach_ver[owner];
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	for (i = 0; i < total_ops; i++)
7210Sstevel@tonic-gate 		if (clt_opsp[i] != NULL)
7220Sstevel@tonic-gate 			srv_opsp[i] = clt_opsp[i];
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate static void
7260Sstevel@tonic-gate mach_construct_info()
7270Sstevel@tonic-gate {
7283446Smrj 	struct psm_sw *swp;
7290Sstevel@tonic-gate 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
7300Sstevel@tonic-gate 	int	conflict_owner = 0;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if (psmsw->psw_forw == psmsw)
7330Sstevel@tonic-gate 		panic("No valid PSM modules found");
7340Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
7350Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7360Sstevel@tonic-gate 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
7370Sstevel@tonic-gate 			continue;
7380Sstevel@tonic-gate 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
7390Sstevel@tonic-gate 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
7400Sstevel@tonic-gate 		mach_cnt[swp->psw_infop->p_owner]++;
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/* check to see are there any conflicts */
7470Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
7480Sstevel@tonic-gate 		conflict_owner = PSM_OWN_EXCLUSIVE;
7490Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
7500Sstevel@tonic-gate 		conflict_owner = PSM_OWN_OVERRIDE;
7510Sstevel@tonic-gate 	if (conflict_owner) {
7520Sstevel@tonic-gate 		/* remove all psm modules except uppc */
7530Sstevel@tonic-gate 		cmn_err(CE_WARN,
7544481Sbholler 		    "Conflicts detected on the following PSM modules:");
7550Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
7560Sstevel@tonic-gate 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7570Sstevel@tonic-gate 			if (swp->psw_infop->p_owner == conflict_owner)
7580Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s ",
7594481Sbholler 				    swp->psw_infop->p_mach_idstring);
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
7620Sstevel@tonic-gate 		cmn_err(CE_WARN,
7634481Sbholler 		    "Setting the system back to SINGLE processor mode!");
7640Sstevel@tonic-gate 		cmn_err(CE_WARN,
7650Sstevel@tonic-gate 		    "Please edit /etc/mach to remove the invalid PSM module.");
7660Sstevel@tonic-gate 		return;
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if (mach_set[PSM_OWN_EXCLUSIVE])
7700Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_EXCLUSIVE);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (mach_set[PSM_OWN_OVERRIDE])
7730Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_OVERRIDE);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate static void
7770Sstevel@tonic-gate mach_init()
7780Sstevel@tonic-gate {
7793446Smrj 	struct psm_ops  *pops;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	mach_construct_info();
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	pops = mach_set[0];
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/* register the interrupt and clock initialization rotuines */
7860Sstevel@tonic-gate 	picinitf = mach_picinit;
7870Sstevel@tonic-gate 	clkinitf = mach_clkinit;
7880Sstevel@tonic-gate 	psm_get_clockirq = pops->psm_get_clockirq;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/* register the interrupt setup code */
7910Sstevel@tonic-gate 	slvltovect = mach_softlvl_to_vect;
7920Sstevel@tonic-gate 	addspl	= pops->psm_addspl;
7930Sstevel@tonic-gate 	delspl	= pops->psm_delspl;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (pops->psm_translate_irq)
7960Sstevel@tonic-gate 		psm_translate_irq = pops->psm_translate_irq;
7970Sstevel@tonic-gate 	if (pops->psm_intr_ops)
7980Sstevel@tonic-gate 		psm_intr_ops = pops->psm_intr_ops;
7993446Smrj 
8003446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
8013446Smrj 	/*
8023446Smrj 	 * Time-of-day functionality now handled in TOD modules.
8033446Smrj 	 * (Warn about PSM modules that think that we're going to use
8043446Smrj 	 * their ops vectors.)
8053446Smrj 	 */
8063446Smrj 	if (pops->psm_tod_get)
8073446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
8083446Smrj 		    (void *)pops->psm_tod_get);
8093446Smrj 
8103446Smrj 	if (pops->psm_tod_set)
8113446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
8123446Smrj 		    (void *)pops->psm_tod_set);
8133446Smrj #endif
8143446Smrj 
8150Sstevel@tonic-gate 	if (pops->psm_notify_error) {
8160Sstevel@tonic-gate 		psm_notify_error = mach_notify_error;
8170Sstevel@tonic-gate 		notify_error = pops->psm_notify_error;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	(*pops->psm_softinit)();
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * Initialize the dispatcher's function hooks
8244481Sbholler 	 * to enable CPU halting when idle.
825*5045Sbholler 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
826*5045Sbholler 	 * or idle_cpu_prefer_mwait is not set.
8274481Sbholler 	 * Allocate monitor/mwait buffer for cpu0.
8280Sstevel@tonic-gate 	 */
8294481Sbholler 	if (idle_cpu_use_hlt) {
8304481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
831*5045Sbholler 			CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
832*5045Sbholler 			/*
833*5045Sbholler 			 * Protect ourself from insane mwait size.
834*5045Sbholler 			 */
835*5045Sbholler 			if (CPU->cpu_m.mcpu_mwait == NULL) {
836*5045Sbholler #ifdef DEBUG
837*5045Sbholler 				cmn_err(CE_NOTE, "Using hlt idle.  Cannot "
838*5045Sbholler 				    "handle cpu 0 mwait size.");
839*5045Sbholler #endif
840*5045Sbholler 				idle_cpu_prefer_mwait = 0;
841*5045Sbholler 				idle_cpu = cpu_idle;
842*5045Sbholler 			} else {
843*5045Sbholler 				idle_cpu = cpu_idle_mwait;
844*5045Sbholler 			}
8454481Sbholler 		} else {
8464481Sbholler 			idle_cpu = cpu_idle;
8474481Sbholler 		}
8484481Sbholler 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	mach_smpinit();
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate static void
8540Sstevel@tonic-gate mach_smpinit(void)
8550Sstevel@tonic-gate {
8562006Sandrei 	struct psm_ops  *pops;
8572006Sandrei 	processorid_t cpu_id;
8582006Sandrei 	int cnt;
8592006Sandrei 	cpuset_t cpumask;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	pops = mach_set[0];
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	cpu_id = -1;
8640Sstevel@tonic-gate 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
8652006Sandrei 	for (cnt = 0, CPUSET_ZERO(cpumask); cpu_id != -1; cnt++) {
8662006Sandrei 		CPUSET_ADD(cpumask, cpu_id);
8670Sstevel@tonic-gate 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	mp_cpus = cpumask;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/* MP related routines */
8730Sstevel@tonic-gate 	ap_mlsetup = pops->psm_post_cpu_start;
8740Sstevel@tonic-gate 	send_dirintf = pops->psm_send_ipi;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	/* optional MP related routines */
8770Sstevel@tonic-gate 	if (pops->psm_shutdown)
8780Sstevel@tonic-gate 		psm_shutdownf = pops->psm_shutdown;
8790Sstevel@tonic-gate 	if (pops->psm_preshutdown)
8800Sstevel@tonic-gate 		psm_preshutdownf = pops->psm_preshutdown;
8810Sstevel@tonic-gate 	if (pops->psm_notify_func)
8820Sstevel@tonic-gate 		psm_notifyf = pops->psm_notify_func;
8830Sstevel@tonic-gate 	if (pops->psm_set_idlecpu)
8840Sstevel@tonic-gate 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
8850Sstevel@tonic-gate 	if (pops->psm_unset_idlecpu)
8860Sstevel@tonic-gate 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	psm_clkinit = pops->psm_clkinit;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	if (pops->psm_timer_reprogram)
8910Sstevel@tonic-gate 		psm_timer_reprogram = pops->psm_timer_reprogram;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	if (pops->psm_timer_enable)
8940Sstevel@tonic-gate 		psm_timer_enable = pops->psm_timer_enable;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	if (pops->psm_timer_disable)
8970Sstevel@tonic-gate 		psm_timer_disable = pops->psm_timer_disable;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	if (pops->psm_post_cyclic_setup)
9000Sstevel@tonic-gate 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	/* check for multiple cpu's */
9030Sstevel@tonic-gate 	if (cnt < 2)
9040Sstevel@tonic-gate 		return;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	/* check for MP platforms */
9070Sstevel@tonic-gate 	if (pops->psm_cpu_start == NULL)
9080Sstevel@tonic-gate 		return;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * Set the dispatcher hook to enable cpu "wake up"
9120Sstevel@tonic-gate 	 * when a thread becomes runnable.
9130Sstevel@tonic-gate 	 */
9143446Smrj 	if (idle_cpu_use_hlt)
9154481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
9164481Sbholler 			disp_enq_thread = cpu_wakeup_mwait;
9174481Sbholler 		else
9184481Sbholler 			disp_enq_thread = cpu_wakeup;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	if (pops->psm_disable_intr)
9210Sstevel@tonic-gate 		psm_disable_intr = pops->psm_disable_intr;
9220Sstevel@tonic-gate 	if (pops->psm_enable_intr)
9230Sstevel@tonic-gate 		psm_enable_intr  = pops->psm_enable_intr;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	psm_get_ipivect = pops->psm_get_ipivect;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr",
9284481Sbholler 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
9294481Sbholler 	    (caddr_t)X_CALL_HIPRI, NULL, NULL, NULL);
9300Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr",
9314481Sbholler 	    (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO),
9324481Sbholler 	    (caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate static void
9380Sstevel@tonic-gate mach_picinit()
9390Sstevel@tonic-gate {
9402006Sandrei 	struct psm_ops  *pops;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	pops = mach_set[0];
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/* register the interrupt handlers */
9450Sstevel@tonic-gate 	setlvl = pops->psm_intr_enter;
9460Sstevel@tonic-gate 	setlvlx = pops->psm_intr_exit;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/* initialize the interrupt hardware */
9490Sstevel@tonic-gate 	(*pops->psm_picinit)();
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/* set interrupt mask for current ipl */
9520Sstevel@tonic-gate 	setspl = pops->psm_setspl;
9533446Smrj 	cli();
9540Sstevel@tonic-gate 	setspl(CPU->cpu_pri);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate uint_t	cpu_freq;	/* MHz */
9580Sstevel@tonic-gate uint64_t cpu_freq_hz;	/* measured (in hertz) */
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate #define	MEGA_HZ		1000000
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate static uint64_t
9630Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
9640Sstevel@tonic-gate {
9650Sstevel@tonic-gate 	uint64_t cpu_hz;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if ((pit_counter == 0) || (*processor_clks == 0) ||
9680Sstevel@tonic-gate 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
9690Sstevel@tonic-gate 		return (0);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	return (cpu_hz);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate static uint64_t
9770Sstevel@tonic-gate mach_getcpufreq(void)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	uint32_t pit_counter;
9800Sstevel@tonic-gate 	uint64_t processor_clks;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	if (x86_feature & X86_TSC) {
9830Sstevel@tonic-gate 		/*
9840Sstevel@tonic-gate 		 * We have a TSC. freq_tsc() knows how to measure the number
9850Sstevel@tonic-gate 		 * of clock cycles sampled against the PIT.
9860Sstevel@tonic-gate 		 */
9873446Smrj 		ulong_t flags = clear_int_flag();
9880Sstevel@tonic-gate 		processor_clks = freq_tsc(&pit_counter);
9893446Smrj 		restore_int_flag(flags);
9900Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
9910Sstevel@tonic-gate 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
9920Sstevel@tonic-gate #if defined(__amd64)
9930Sstevel@tonic-gate 		panic("mach_getcpufreq: no TSC!");
9940Sstevel@tonic-gate #elif defined(__i386)
9950Sstevel@tonic-gate 		/*
9960Sstevel@tonic-gate 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
9970Sstevel@tonic-gate 		 * for which freq_notsc() knows how to measure the number of
9980Sstevel@tonic-gate 		 * elapsed clock cycles sampled against the PIT
9990Sstevel@tonic-gate 		 */
10003446Smrj 		ulong_t flags = clear_int_flag();
10010Sstevel@tonic-gate 		processor_clks = freq_notsc(&pit_counter);
10023446Smrj 		restore_int_flag(flags);
10030Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
10040Sstevel@tonic-gate #endif	/* __i386 */
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	/* We do not know how to calculate cpu frequency for this cpu. */
10080Sstevel@tonic-gate 	return (0);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate  * If the clock speed of a cpu is found to be reported incorrectly, do not add
10130Sstevel@tonic-gate  * to this array, instead improve the accuracy of the algorithm that determines
10140Sstevel@tonic-gate  * the clock speed of the processor or extend the implementation to support the
10150Sstevel@tonic-gate  * vendor as appropriate. This is here only to support adjusting the speed on
10160Sstevel@tonic-gate  * older slower processors that mach_fixcpufreq() would not be able to account
10170Sstevel@tonic-gate  * for otherwise.
10180Sstevel@tonic-gate  */
10190Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate  * On fast processors the clock frequency that is measured may be off by
10230Sstevel@tonic-gate  * a few MHz from the value printed on the part. This is a combination of
10240Sstevel@tonic-gate  * the factors that for such fast parts being off by this much is within
10250Sstevel@tonic-gate  * the tolerances for manufacture and because of the difficulties in the
10260Sstevel@tonic-gate  * measurement that can lead to small error. This function uses some
10270Sstevel@tonic-gate  * heuristics in order to tweak the value that was measured to match what
10280Sstevel@tonic-gate  * is most likely printed on the part.
10290Sstevel@tonic-gate  *
10300Sstevel@tonic-gate  * Some examples:
10310Sstevel@tonic-gate  * 	AMD Athlon 1000 mhz measured as 998 mhz
10320Sstevel@tonic-gate  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
10330Sstevel@tonic-gate  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
10340Sstevel@tonic-gate  *
10350Sstevel@tonic-gate  * If in the future this function is no longer sufficient to correct
10360Sstevel@tonic-gate  * for the error in the measurement, then the algorithm used to perform
10370Sstevel@tonic-gate  * the measurement will have to be improved in order to increase accuracy
10380Sstevel@tonic-gate  * rather than adding horrible and questionable kludges here.
10390Sstevel@tonic-gate  *
10400Sstevel@tonic-gate  * This is called after the cyclics subsystem because of the potential
10410Sstevel@tonic-gate  * that the heuristics within may give a worse estimate of the clock
10420Sstevel@tonic-gate  * frequency than the value that was measured.
10430Sstevel@tonic-gate  */
10440Sstevel@tonic-gate static void
10450Sstevel@tonic-gate mach_fixcpufreq(void)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	freq = (uint32_t)cpu_freq;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	/*
10520Sstevel@tonic-gate 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
10530Sstevel@tonic-gate 	 * measured speed taking into account that the 667 MHz parts were
10540Sstevel@tonic-gate 	 * the first to round-up.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
10570Sstevel@tonic-gate 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
10580Sstevel@tonic-gate 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
10610Sstevel@tonic-gate 	mul = (freq + 25) / 50;
10620Sstevel@tonic-gate 	near50 = mul * 50;
10630Sstevel@tonic-gate 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/* Find the closer of the two */
10660Sstevel@tonic-gate 	if (delta66 < delta50) {
10670Sstevel@tonic-gate 		fixed = near66;
10680Sstevel@tonic-gate 		delta = delta66;
10690Sstevel@tonic-gate 	} else {
10700Sstevel@tonic-gate 		fixed = near50;
10710Sstevel@tonic-gate 		delta = delta50;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (fixed > INT_MAX)
10750Sstevel@tonic-gate 		return;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/*
10780Sstevel@tonic-gate 	 * Some older parts have a core clock frequency that is not an
10790Sstevel@tonic-gate 	 * integral multiple of 50 or 66 MHz. Check if one of the old
10800Sstevel@tonic-gate 	 * clock frequencies is closer to the measured value than any
10810Sstevel@tonic-gate 	 * of the integral multiples of 50 an 66, and if so set fixed
10820Sstevel@tonic-gate 	 * and delta appropriately to represent the closest value.
10830Sstevel@tonic-gate 	 */
10840Sstevel@tonic-gate 	i = sizeof (x86_cpu_freq) / sizeof (int);
10850Sstevel@tonic-gate 	while (i > 0) {
10860Sstevel@tonic-gate 		i--;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		if (x86_cpu_freq[i] <= freq) {
10890Sstevel@tonic-gate 			mul = freq - x86_cpu_freq[i];
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 			if (mul < delta) {
10920Sstevel@tonic-gate 				fixed = x86_cpu_freq[i];
10930Sstevel@tonic-gate 				delta = mul;
10940Sstevel@tonic-gate 			}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 			break;
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		mul = x86_cpu_freq[i] - freq;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		if (mul < delta) {
11020Sstevel@tonic-gate 			fixed = x86_cpu_freq[i];
11030Sstevel@tonic-gate 			delta = mul;
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	/*
11080Sstevel@tonic-gate 	 * Set a reasonable maximum for how much to correct the measured
11090Sstevel@tonic-gate 	 * result by. This check is here to prevent the adjustment made
11100Sstevel@tonic-gate 	 * by this function from being more harm than good. It is entirely
11110Sstevel@tonic-gate 	 * possible that in the future parts will be made that are not
11120Sstevel@tonic-gate 	 * integral multiples of 66 or 50 in clock frequency or that
11130Sstevel@tonic-gate 	 * someone may overclock a part to some odd frequency. If the
11140Sstevel@tonic-gate 	 * measured value is farther from the corrected value than
11150Sstevel@tonic-gate 	 * allowed, then assume the corrected value is in error and use
11160Sstevel@tonic-gate 	 * the measured value.
11170Sstevel@tonic-gate 	 */
11180Sstevel@tonic-gate 	if (6 < delta)
11190Sstevel@tonic-gate 		return;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	cpu_freq = (int)fixed;
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate static int
11260Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate 	uint64_t cpu_mhz;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	/* Round to nearest MHZ */
11310Sstevel@tonic-gate 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (cpu_mhz > INT_MAX)
11340Sstevel@tonic-gate 		return (0);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	return ((int)cpu_mhz);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate static int
11420Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
11430Sstevel@tonic-gate {
11443446Smrj 	struct psm_ops  *pops;
11450Sstevel@tonic-gate 	int resolution;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	pops = mach_set[0];
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	cpu_freq_hz = mach_getcpufreq();
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	cpu_freq = machhztomhz(cpu_freq_hz);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
11540Sstevel@tonic-gate 		tsc_gethrtime_enable = 0;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	if (tsc_gethrtime_enable) {
11570Sstevel@tonic-gate 		tsc_hrtimeinit(cpu_freq_hz);
11580Sstevel@tonic-gate 		gethrtimef = tsc_gethrtime;
11590Sstevel@tonic-gate 		gethrtimeunscaledf = tsc_gethrtimeunscaled;
11600Sstevel@tonic-gate 		scalehrtimef = tsc_scalehrtime;
11610Sstevel@tonic-gate 		hrtime_tick = tsc_tick;
11620Sstevel@tonic-gate 		tsc_gethrtime_initted = 1;
11630Sstevel@tonic-gate 	} else {
11640Sstevel@tonic-gate 		if (pops->psm_hrtimeinit)
11650Sstevel@tonic-gate 			(*pops->psm_hrtimeinit)();
11660Sstevel@tonic-gate 		gethrtimef = pops->psm_gethrtime;
11670Sstevel@tonic-gate 		gethrtimeunscaledf = gethrtimef;
11680Sstevel@tonic-gate 		/* scalehrtimef will remain dummy */
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	mach_fixcpufreq();
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
11740Sstevel@tonic-gate 		if ((preferred_mode == TIMER_ONESHOT) &&
11750Sstevel@tonic-gate 		    (tsc_gethrtime_enable)) {
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 			resolution = (*pops->psm_clkinit)(0);
11780Sstevel@tonic-gate 			if (resolution != 0)  {
11790Sstevel@tonic-gate 				*set_mode = TIMER_ONESHOT;
11800Sstevel@tonic-gate 				return (resolution);
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		/*
11860Sstevel@tonic-gate 		 * either periodic mode was requested or could not set to
11870Sstevel@tonic-gate 		 * one-shot mode
11880Sstevel@tonic-gate 		 */
11890Sstevel@tonic-gate 		resolution = (*pops->psm_clkinit)(hz);
11900Sstevel@tonic-gate 		/*
11910Sstevel@tonic-gate 		 * psm should be able to do periodic, so we do not check
11920Sstevel@tonic-gate 		 * for return value of psm_clkinit here.
11930Sstevel@tonic-gate 		 */
11940Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
11950Sstevel@tonic-gate 		return (resolution);
11960Sstevel@tonic-gate 	} else {
11970Sstevel@tonic-gate 		/*
11980Sstevel@tonic-gate 		 * PSMI interface prior to PSMI_3 does not define a return
11990Sstevel@tonic-gate 		 * value for psm_clkinit, so the return value is ignored.
12000Sstevel@tonic-gate 		 */
12010Sstevel@tonic-gate 		(void) (*pops->psm_clkinit)(hz);
12020Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
12030Sstevel@tonic-gate 		return (nsec_per_tick);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12074652Scwb 
1208999Slq150181 /*ARGSUSED*/
12090Sstevel@tonic-gate static int
12103446Smrj mach_softlvl_to_vect(int ipl)
12110Sstevel@tonic-gate {
12124652Scwb 	setsoftint = av_set_softint_pending;
12134652Scwb 	kdisetsoftint = kdi_av_set_softint_pending;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	return (PSM_SV_SOFTWARE);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12183446Smrj #ifdef DEBUG
12193446Smrj /*
12203446Smrj  * This is here to allow us to simulate cpus that refuse to start.
12213446Smrj  */
12223446Smrj cpuset_t cpufailset;
12233446Smrj #endif
12243446Smrj 
12253446Smrj int
12263446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
12270Sstevel@tonic-gate {
12283446Smrj 	struct psm_ops *pops = mach_set[0];
12293446Smrj 	processorid_t id = cp->cpu_id;
12300Sstevel@tonic-gate 
12313446Smrj #ifdef DEBUG
12323446Smrj 	if (CPU_IN_SET(cpufailset, id))
12333446Smrj 		return (0);
12343446Smrj #endif
12353446Smrj 	return ((*pops->psm_cpu_start)(id, ctx));
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate /*ARGSUSED*/
12390Sstevel@tonic-gate static int
12400Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	return (irqno);	/* default to NO translation */
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate static void
12460Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	/*
12490Sstevel@tonic-gate 	 * SL_FATAL is pass in once panicstr is set, deliver it
12500Sstevel@tonic-gate 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
12510Sstevel@tonic-gate 	 * codes for the psmi handler
12520Sstevel@tonic-gate 	 */
12530Sstevel@tonic-gate 	if (level & SL_FATAL)
12540Sstevel@tonic-gate 		(*notify_error)(CE_PANIC, errmsg);
12550Sstevel@tonic-gate 	else if (level & SL_WARN)
12560Sstevel@tonic-gate 		(*notify_error)(CE_WARN, errmsg);
12570Sstevel@tonic-gate 	else if (level & SL_NOTE)
12580Sstevel@tonic-gate 		(*notify_error)(CE_NOTE, errmsg);
12590Sstevel@tonic-gate 	else if (level & SL_CONSOLE)
12600Sstevel@tonic-gate 		(*notify_error)(CE_CONT, errmsg);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate  * It provides the default basic intr_ops interface for the new DDI
12650Sstevel@tonic-gate  * interrupt framework if the PSM doesn't have one.
12660Sstevel@tonic-gate  *
12670Sstevel@tonic-gate  * Input:
12680Sstevel@tonic-gate  * dip     - pointer to the dev_info structure of the requested device
12690Sstevel@tonic-gate  * hdlp    - pointer to the internal interrupt handle structure for the
12700Sstevel@tonic-gate  *	     requested interrupt
12710Sstevel@tonic-gate  * intr_op - opcode for this call
12720Sstevel@tonic-gate  * result  - pointer to the integer that will hold the result to be
12730Sstevel@tonic-gate  *	     passed back if return value is PSM_SUCCESS
12740Sstevel@tonic-gate  *
12750Sstevel@tonic-gate  * Output:
12760Sstevel@tonic-gate  * return value is either PSM_SUCCESS or PSM_FAILURE
12770Sstevel@tonic-gate  */
12780Sstevel@tonic-gate static int
12790Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
12800Sstevel@tonic-gate     psm_intr_op_t intr_op, int *result)
12810Sstevel@tonic-gate {
12820Sstevel@tonic-gate 	struct intrspec *ispec;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	switch (intr_op) {
12850Sstevel@tonic-gate 	case PSM_INTR_OP_CHECK_MSI:
12860Sstevel@tonic-gate 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
12874481Sbholler 		    DDI_INTR_TYPE_MSIX);
12880Sstevel@tonic-gate 		break;
12890Sstevel@tonic-gate 	case PSM_INTR_OP_ALLOC_VECTORS:
12900Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
12910Sstevel@tonic-gate 			*result = 1;
12920Sstevel@tonic-gate 		else
12930Sstevel@tonic-gate 			*result = 0;
12940Sstevel@tonic-gate 		break;
12950Sstevel@tonic-gate 	case PSM_INTR_OP_FREE_VECTORS:
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 	case PSM_INTR_OP_NAVAIL_VECTORS:
12980Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
12990Sstevel@tonic-gate 			*result = 1;
13000Sstevel@tonic-gate 		else
13010Sstevel@tonic-gate 			*result = 0;
13020Sstevel@tonic-gate 		break;
13030Sstevel@tonic-gate 	case PSM_INTR_OP_XLATE_VECTOR:
1304916Sschwartz 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
13050Sstevel@tonic-gate 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
13060Sstevel@tonic-gate 		break;
13070Sstevel@tonic-gate 	case PSM_INTR_OP_GET_CAP:
13080Sstevel@tonic-gate 		*result = 0;
13090Sstevel@tonic-gate 		break;
13100Sstevel@tonic-gate 	case PSM_INTR_OP_GET_PENDING:
13110Sstevel@tonic-gate 	case PSM_INTR_OP_CLEAR_MASK:
13120Sstevel@tonic-gate 	case PSM_INTR_OP_SET_MASK:
13130Sstevel@tonic-gate 	case PSM_INTR_OP_GET_SHARED:
13140Sstevel@tonic-gate 	case PSM_INTR_OP_SET_PRI:
13150Sstevel@tonic-gate 	case PSM_INTR_OP_SET_CAP:
1316916Sschwartz 	case PSM_INTR_OP_SET_CPU:
1317916Sschwartz 	case PSM_INTR_OP_GET_INTR:
13180Sstevel@tonic-gate 	default:
13190Sstevel@tonic-gate 		return (PSM_FAILURE);
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 	return (PSM_SUCCESS);
13220Sstevel@tonic-gate }
13234769Sdp78419 /*
13244769Sdp78419  * Return 1 if CMT load balancing policies should be
13254769Sdp78419  * implemented across instances of the specified hardware
13264769Sdp78419  * sharing relationship.
13274769Sdp78419  */
13284769Sdp78419 int
13294769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw)
13304769Sdp78419 {
13314769Sdp78419 	if (hw == PGHW_IPIPE ||
13324769Sdp78419 	    hw == PGHW_FPU ||
13334769Sdp78419 	    hw == PGHW_CHIP)
13344769Sdp78419 		return (1);
13354769Sdp78419 	else
13364769Sdp78419 		return (0);
13374769Sdp78419 }
13384769Sdp78419 /*
13394769Sdp78419  * Return 1 if thread affinity polices should be implemented
13404769Sdp78419  * for instances of the specifed hardware sharing relationship.
13414769Sdp78419  */
13424769Sdp78419 int
13434769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw)
13444769Sdp78419 {
13454769Sdp78419 	if (hw == PGHW_CACHE)
13464769Sdp78419 		return (1);
13474769Sdp78419 	else
13484769Sdp78419 		return (0);
13494769Sdp78419 }
1350