xref: /onnv-gate/usr/src/uts/i86pc/os/mp_machdep.c (revision 4606:ffcd05844ec5)
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>
41*4606Sesaxe #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>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  *	Local function prototypes
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
580Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
590Sstevel@tonic-gate static void mach_init();
600Sstevel@tonic-gate static void mach_picinit();
610Sstevel@tonic-gate static uint64_t mach_calchz(uint32_t pit_counter, uint64_t *processor_clks);
620Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
630Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
640Sstevel@tonic-gate static void mach_fixcpufreq(void);
650Sstevel@tonic-gate static int mach_clkinit(int, int *);
660Sstevel@tonic-gate static void mach_smpinit(void);
67999Slq150181 static void mach_set_softintr(int ipl, struct av_softinfo *);
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;
109999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
110999Slq150181 	(void (*)(int, struct av_softinfo *))return_instr;
1110Sstevel@tonic-gate int (*slvltovect)(int)		= (int (*)(int))return_instr;
1120Sstevel@tonic-gate int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
1130Sstevel@tonic-gate void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
1140Sstevel@tonic-gate int (*psm_disable_intr)(int)	= mp_disable_intr;
1150Sstevel@tonic-gate void (*psm_enable_intr)(int)	= mp_enable_intr;
1160Sstevel@tonic-gate hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
1170Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
1180Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
1190Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1200Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1210Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1220Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1230Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1260Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1270Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1280Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1290Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1300Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1310Sstevel@tonic-gate     int *) = mach_intr_ops;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1340Sstevel@tonic-gate void (*hrtime_tick)(void)	= return_instr;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1370Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Local Static Data
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate static struct psm_ops mach_ops;
1430Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1440Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1473446Smrj  * If non-zero, idle cpus will become "halted" when there's
1480Sstevel@tonic-gate  * no work to do.
1490Sstevel@tonic-gate  */
1503446Smrj int	idle_cpu_use_hlt = 1;
1510Sstevel@tonic-gate 
1524481Sbholler /*
1534481Sbholler  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
1544481Sbholler  */
1554481Sbholler int	idle_cpu_prefer_mwait = 1;
1564481Sbholler 
1573434Sesaxe 
1583434Sesaxe /*ARGSUSED*/
1593434Sesaxe int
1603434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
1613434Sesaxe {
1623434Sesaxe 	switch (hw) {
1633434Sesaxe 	case PGHW_IPIPE:
1643434Sesaxe 		if (x86_feature & (X86_HTT)) {
1653434Sesaxe 			/*
1663434Sesaxe 			 * Hyper-threading is SMT
1673434Sesaxe 			 */
1683434Sesaxe 			return (1);
1693434Sesaxe 		} else {
1703434Sesaxe 			return (0);
1713434Sesaxe 		}
1723434Sesaxe 	case PGHW_CHIP:
1733434Sesaxe 		if (x86_feature & (X86_CMP|X86_HTT))
1743434Sesaxe 			return (1);
1753434Sesaxe 		else
1763434Sesaxe 			return (0);
177*4606Sesaxe 	case PGHW_CACHE:
178*4606Sesaxe 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
179*4606Sesaxe 			return (1);
180*4606Sesaxe 		else
181*4606Sesaxe 			return (0);
1823434Sesaxe 	default:
1833434Sesaxe 		return (0);
1843434Sesaxe 	}
1853434Sesaxe }
1863434Sesaxe 
1873434Sesaxe /*
1883434Sesaxe  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
1893434Sesaxe  * If pghw_type_t is an unsupported hardware type, then return -1
1903434Sesaxe  */
1913434Sesaxe int
1923434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
1930Sstevel@tonic-gate {
1943434Sesaxe 	id_t pgp_a, pgp_b;
1953434Sesaxe 
1963434Sesaxe 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
1973434Sesaxe 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
1983434Sesaxe 
1993434Sesaxe 	if (pgp_a == -1 || pgp_b == -1)
2003434Sesaxe 		return (-1);
2013434Sesaxe 
2023434Sesaxe 	return (pgp_a == pgp_b);
2033434Sesaxe }
2043434Sesaxe 
2053434Sesaxe /*
2063434Sesaxe  * Return a physical instance identifier for known hardware sharing
2073434Sesaxe  * relationships
2083434Sesaxe  */
2093434Sesaxe id_t
2103434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
2113434Sesaxe {
2123434Sesaxe 	switch (hw) {
2133434Sesaxe 	case PGHW_IPIPE:
2143434Sesaxe 		return (cpuid_get_coreid(cpu));
215*4606Sesaxe 	case PGHW_CACHE:
216*4606Sesaxe 		return (cpuid_get_last_lvl_cacheid(cpu));
2173434Sesaxe 	case PGHW_CHIP:
2183434Sesaxe 		return (cpuid_get_chipid(cpu));
2193434Sesaxe 	default:
2203434Sesaxe 		return (-1);
2211228Sandrei 	}
2223434Sesaxe }
2230Sstevel@tonic-gate 
2243434Sesaxe int
2253434Sesaxe pg_plat_hw_level(pghw_type_t hw)
2263434Sesaxe {
2273434Sesaxe 	int i;
2283434Sesaxe 	static pghw_type_t hw_hier[] = {
2293434Sesaxe 		PGHW_IPIPE,
230*4606Sesaxe 		PGHW_CACHE,
2313434Sesaxe 		PGHW_CHIP,
2323434Sesaxe 		PGHW_NUM_COMPONENTS
2333434Sesaxe 	};
2343434Sesaxe 
2353434Sesaxe 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
2363434Sesaxe 		if (hw_hier[i] == hw)
2373434Sesaxe 			return (i);
2383434Sesaxe 	}
2393434Sesaxe 	return (-1);
2403434Sesaxe }
2413434Sesaxe 
242*4606Sesaxe /*
243*4606Sesaxe  * Return 1 if CMT load balancing policies should be
244*4606Sesaxe  * implemented across instances of the specified hardware
245*4606Sesaxe  * sharing relationship.
246*4606Sesaxe  */
247*4606Sesaxe int
248*4606Sesaxe pg_plat_cmt_load_bal_hw(pghw_type_t hw)
249*4606Sesaxe {
250*4606Sesaxe 	if (hw == PGHW_IPIPE ||
251*4606Sesaxe 	    hw == PGHW_FPU ||
252*4606Sesaxe 	    hw == PGHW_CHIP ||
253*4606Sesaxe 	    hw == PGHW_CACHE)
254*4606Sesaxe 		return (1);
255*4606Sesaxe 	else
256*4606Sesaxe 		return (0);
257*4606Sesaxe }
258*4606Sesaxe 
259*4606Sesaxe 
260*4606Sesaxe /*
261*4606Sesaxe  * Return 1 if thread affinity polices should be implemented
262*4606Sesaxe  * for instances of the specifed hardware sharing relationship.
263*4606Sesaxe  */
264*4606Sesaxe int
265*4606Sesaxe pg_plat_cmt_affinity_hw(pghw_type_t hw)
266*4606Sesaxe {
267*4606Sesaxe 	if (hw == PGHW_CACHE)
268*4606Sesaxe 		return (1);
269*4606Sesaxe 	else
270*4606Sesaxe 		return (0);
271*4606Sesaxe }
272*4606Sesaxe 
2733434Sesaxe id_t
2743434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
2753434Sesaxe {
2763434Sesaxe 	return ((id_t)cpuid_get_coreid(cpu));
2773434Sesaxe }
2783434Sesaxe 
2793434Sesaxe void
2803434Sesaxe cmp_set_nosteal_interval(void)
2813434Sesaxe {
2823434Sesaxe 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
2833434Sesaxe 	nosteal_nsec = 100000UL;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * Routine to ensure initial callers to hrtime gets 0 as return
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate static hrtime_t
2900Sstevel@tonic-gate dummy_hrtime(void)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	return (0);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /* ARGSUSED */
2960Sstevel@tonic-gate static void
2970Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
2980Sstevel@tonic-gate {}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3013446Smrj  * Idle the present CPU until awoken via an interrupt
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate static void
3043446Smrj cpu_idle(void)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	cpu_t		*cpup = CPU;
3070Sstevel@tonic-gate 	processorid_t	cpun = cpup->cpu_id;
308711Sesaxe 	cpupart_t	*cp = cpup->cpu_part;
3090Sstevel@tonic-gate 	int		hset_update = 1;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * If this CPU is online, and there's multiple CPUs
3130Sstevel@tonic-gate 	 * in the system, then we should notate our halting
3140Sstevel@tonic-gate 	 * by adding ourselves to the partition's halted CPU
3150Sstevel@tonic-gate 	 * bitmap. This allows other CPUs to find/awaken us when
3160Sstevel@tonic-gate 	 * work becomes available.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
3190Sstevel@tonic-gate 		hset_update = 0;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Add ourselves to the partition's halted CPUs bitmask
3230Sstevel@tonic-gate 	 * and set our HALTED flag, if necessary.
3240Sstevel@tonic-gate 	 *
325711Sesaxe 	 * When a thread becomes runnable, it is placed on the queue
326711Sesaxe 	 * and then the halted cpuset is checked to determine who
327711Sesaxe 	 * (if anyone) should be awoken. We therefore need to first
328711Sesaxe 	 * add ourselves to the halted cpuset, and and then check if there
329711Sesaxe 	 * is any work available.
330711Sesaxe 	 *
3310Sstevel@tonic-gate 	 * Note that memory barriers after updating the HALTED flag
3320Sstevel@tonic-gate 	 * are not necessary since an atomic operation (updating the bitmap)
3330Sstevel@tonic-gate 	 * immediately follows. On x86 the atomic operation acts as a
3340Sstevel@tonic-gate 	 * memory barrier for the update of cpu_disp_flags.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (hset_update) {
3370Sstevel@tonic-gate 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
3382722Sjohnlev 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * Check to make sure there's really nothing to do.
343711Sesaxe 	 * Work destined for this CPU may become available after
344711Sesaxe 	 * this check. We'll be notified through the clearing of our
345711Sesaxe 	 * bit in the halted CPU bitmask, and a poke.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if (disp_anywork()) {
3480Sstevel@tonic-gate 		if (hset_update) {
3490Sstevel@tonic-gate 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
3502722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
3510Sstevel@tonic-gate 		}
352711Sesaxe 		return;
353711Sesaxe 	}
354711Sesaxe 
355711Sesaxe 	/*
356711Sesaxe 	 * We're on our way to being halted.
357711Sesaxe 	 *
358711Sesaxe 	 * Disable interrupts now, so that we'll awaken immediately
359711Sesaxe 	 * after halting if someone tries to poke us between now and
360711Sesaxe 	 * the time we actually halt.
361711Sesaxe 	 *
362711Sesaxe 	 * We check for the presence of our bit after disabling interrupts.
363711Sesaxe 	 * If it's cleared, we'll return. If the bit is cleared after
364711Sesaxe 	 * we check then the poke will pop us out of the halted state.
365711Sesaxe 	 *
366711Sesaxe 	 * This means that the ordering of the poke and the clearing
367711Sesaxe 	 * of the bit by cpu_wakeup is important.
368711Sesaxe 	 * cpu_wakeup() must clear, then poke.
3693446Smrj 	 * cpu_idle() must disable interrupts, then check for the bit.
370711Sesaxe 	 */
371711Sesaxe 	cli();
372711Sesaxe 
3732722Sjohnlev 	if (hset_update && !CPU_IN_SET(cp->cp_mach->mc_haltset, cpun)) {
374711Sesaxe 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
375711Sesaxe 		sti();
376711Sesaxe 		return;
377711Sesaxe 	}
378711Sesaxe 
379711Sesaxe 	/*
380711Sesaxe 	 * The check for anything locally runnable is here for performance
381711Sesaxe 	 * and isn't needed for correctness. disp_nrunnable ought to be
382711Sesaxe 	 * in our cache still, so it's inexpensive to check, and if there
383711Sesaxe 	 * is anything runnable we won't have to wait for the poke.
384711Sesaxe 	 */
385711Sesaxe 	if (cpup->cpu_disp->disp_nrunnable != 0) {
386711Sesaxe 		if (hset_update) {
387711Sesaxe 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
3882722Sjohnlev 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
389711Sesaxe 		}
3900Sstevel@tonic-gate 		sti();
3910Sstevel@tonic-gate 		return;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3943446Smrj 	mach_cpu_idle();
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * We're no longer halted
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (hset_update) {
4000Sstevel@tonic-gate 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4012722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
4080Sstevel@tonic-gate  * Otherwise, see if other CPUs in the cpu partition are halted and need to
4090Sstevel@tonic-gate  * be woken up so that they can steal the thread we placed on this CPU.
4100Sstevel@tonic-gate  * This function is only used on MP systems.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate static void
4130Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	uint_t		cpu_found;
4160Sstevel@tonic-gate 	int		result;
4170Sstevel@tonic-gate 	cpupart_t	*cp;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	cp = cpu->cpu_part;
4202722Sjohnlev 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
4210Sstevel@tonic-gate 		/*
4220Sstevel@tonic-gate 		 * Clear the halted bit for that CPU since it will be
4230Sstevel@tonic-gate 		 * poked in a moment.
4240Sstevel@tonic-gate 		 */
4252722Sjohnlev 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
4260Sstevel@tonic-gate 		/*
4270Sstevel@tonic-gate 		 * We may find the current CPU present in the halted cpuset
4280Sstevel@tonic-gate 		 * if we're in the context of an interrupt that occurred
4293446Smrj 		 * before we had a chance to clear our bit in cpu_idle().
4300Sstevel@tonic-gate 		 * Poking ourself is obviously unnecessary, since if
4310Sstevel@tonic-gate 		 * we're here, we're not halted.
4320Sstevel@tonic-gate 		 */
4330Sstevel@tonic-gate 		if (cpu != CPU)
4340Sstevel@tonic-gate 			poke_cpu(cpu->cpu_id);
4350Sstevel@tonic-gate 		return;
4360Sstevel@tonic-gate 	} else {
4370Sstevel@tonic-gate 		/*
4380Sstevel@tonic-gate 		 * This cpu isn't halted, but it's idle or undergoing a
4390Sstevel@tonic-gate 		 * context switch. No need to awaken anyone else.
4400Sstevel@tonic-gate 		 */
4410Sstevel@tonic-gate 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
4420Sstevel@tonic-gate 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
4430Sstevel@tonic-gate 			return;
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * No need to wake up other CPUs if the thread we just enqueued
4480Sstevel@tonic-gate 	 * is bound.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	if (bound)
4510Sstevel@tonic-gate 		return;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/*
4550Sstevel@tonic-gate 	 * See if there's any other halted CPUs. If there are, then
4560Sstevel@tonic-gate 	 * select one, and awaken it.
4570Sstevel@tonic-gate 	 * It's possible that after we find a CPU, somebody else
4580Sstevel@tonic-gate 	 * will awaken it before we get the chance.
4590Sstevel@tonic-gate 	 * In that case, look again.
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	do {
4622722Sjohnlev 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
4630Sstevel@tonic-gate 		if (cpu_found == CPUSET_NOTINSET)
4640Sstevel@tonic-gate 			return;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
4672722Sjohnlev 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
4680Sstevel@tonic-gate 	} while (result < 0);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (cpu_found != CPU->cpu_id)
4710Sstevel@tonic-gate 		poke_cpu(cpu_found);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4744481Sbholler /*
4754481Sbholler  * Idle the present CPU until awoken via touching its monitored line
4764481Sbholler  */
4774481Sbholler static void
4784481Sbholler cpu_idle_mwait(void)
4794481Sbholler {
4804481Sbholler 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
4814481Sbholler 	cpu_t			*cpup = CPU;
4824481Sbholler 	processorid_t		cpun = cpup->cpu_id;
4834481Sbholler 	cpupart_t		*cp = cpup->cpu_part;
4844481Sbholler 	int			hset_update = 1;
4854481Sbholler 
4864481Sbholler 	/*
4874481Sbholler 	 * Set our mcpu_mwait here, so we can tell if anyone trys to
4884481Sbholler 	 * wake us between now and when we call mwait.  No other cpu will
4894481Sbholler 	 * attempt to set our mcpu_mwait until we add ourself to the haltset.
4904481Sbholler 	 */
4914481Sbholler 	*mcpu_mwait = MWAIT_HALTED;
4924481Sbholler 
4934481Sbholler 	/*
4944481Sbholler 	 * If this CPU is online, and there's multiple CPUs
4954481Sbholler 	 * in the system, then we should notate our halting
4964481Sbholler 	 * by adding ourselves to the partition's halted CPU
4974481Sbholler 	 * bitmap. This allows other CPUs to find/awaken us when
4984481Sbholler 	 * work becomes available.
4994481Sbholler 	 */
5004481Sbholler 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
5014481Sbholler 		hset_update = 0;
5024481Sbholler 
5034481Sbholler 	/*
5044481Sbholler 	 * Add ourselves to the partition's halted CPUs bitmask
5054481Sbholler 	 * and set our HALTED flag, if necessary.
5064481Sbholler 	 *
5074481Sbholler 	 * When a thread becomes runnable, it is placed on the queue
5084481Sbholler 	 * and then the halted cpuset is checked to determine who
5094481Sbholler 	 * (if anyone) should be awoken. We therefore need to first
5104481Sbholler 	 * add ourselves to the halted cpuset, and and then check if there
5114481Sbholler 	 * is any work available.
5124481Sbholler 	 *
5134481Sbholler 	 * Note that memory barriers after updating the HALTED flag
5144481Sbholler 	 * are not necessary since an atomic operation (updating the bitmap)
5154481Sbholler 	 * immediately follows. On x86 the atomic operation acts as a
5164481Sbholler 	 * memory barrier for the update of cpu_disp_flags.
5174481Sbholler 	 */
5184481Sbholler 	if (hset_update) {
5194481Sbholler 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
5204481Sbholler 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
5214481Sbholler 	}
5224481Sbholler 
5234481Sbholler 	/*
5244481Sbholler 	 * Check to make sure there's really nothing to do.
5254481Sbholler 	 * Work destined for this CPU may become available after
5264481Sbholler 	 * this check. We'll be notified through the clearing of our
5274481Sbholler 	 * bit in the halted CPU bitmask, and a write to our mcpu_mwait.
5284481Sbholler 	 *
5294481Sbholler 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
5304481Sbholler 	 */
5314481Sbholler 	if (disp_anywork()) {
5324481Sbholler 		if (hset_update) {
5334481Sbholler 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5344481Sbholler 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5354481Sbholler 		}
5364481Sbholler 		return;
5374481Sbholler 	}
5384481Sbholler 
5394481Sbholler 	/*
5404481Sbholler 	 * We're on our way to being halted.
5414481Sbholler 	 * To avoid a lost wakeup, arm the monitor before checking if another
5424481Sbholler 	 * cpu wrote to mcpu_mwait to wake us up.
5434481Sbholler 	 */
5444481Sbholler 	i86_monitor(mcpu_mwait, 0, 0);
5454481Sbholler 	if (*mcpu_mwait == MWAIT_HALTED) {
5464481Sbholler 		tlb_going_idle();
5474481Sbholler 		i86_mwait(0, 0);
5484481Sbholler 		tlb_service();
5494481Sbholler 	}
5504481Sbholler 
5514481Sbholler 	/*
5524481Sbholler 	 * We're no longer halted
5534481Sbholler 	 */
5544481Sbholler 	if (hset_update) {
5554481Sbholler 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5564481Sbholler 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
5574481Sbholler 	}
5584481Sbholler }
5594481Sbholler 
5604481Sbholler /*
5614481Sbholler  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
5624481Sbholler  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
5634481Sbholler  * need to be woken up so that they can steal the thread we placed on this CPU.
5644481Sbholler  * This function is only used on MP systems.
5654481Sbholler  */
5664481Sbholler static void
5674481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
5684481Sbholler {
5694481Sbholler 	cpupart_t	*cpu_part;
5704481Sbholler 	uint_t		cpu_found;
5714481Sbholler 	int		result;
5724481Sbholler 
5734481Sbholler 	cpu_part = cp->cpu_part;
5744481Sbholler 
5754481Sbholler 	/*
5764481Sbholler 	 * Clear the halted bit for that CPU since it will be woken up
5774481Sbholler 	 * in a moment.
5784481Sbholler 	 */
5794481Sbholler 	if (CPU_IN_SET(cpu_part->cp_mach->mc_haltset, cp->cpu_id)) {
5804481Sbholler 		/*
5814481Sbholler 		 * Clear the halted bit for that CPU since it will be
5824481Sbholler 		 * poked in a moment.
5834481Sbholler 		 */
5844481Sbholler 		CPUSET_ATOMIC_DEL(cpu_part->cp_mach->mc_haltset, cp->cpu_id);
5854481Sbholler 		/*
5864481Sbholler 		 * We may find the current CPU present in the halted cpuset
5874481Sbholler 		 * if we're in the context of an interrupt that occurred
5884481Sbholler 		 * before we had a chance to clear our bit in cpu_idle().
5894481Sbholler 		 * Waking ourself is obviously unnecessary, since if
5904481Sbholler 		 * we're here, we're not halted.
5914481Sbholler 		 *
5924481Sbholler 		 * monitor/mwait wakeup via writing to our cache line is
5934481Sbholler 		 * harmless and less expensive than always checking if we
5944481Sbholler 		 * are waking ourself which is an uncommon case.
5954481Sbholler 		 */
5964481Sbholler 		MWAIT_WAKEUP(cp);	/* write to monitored line */
5974481Sbholler 		return;
5984481Sbholler 	} else {
5994481Sbholler 		/*
6004481Sbholler 		 * This cpu isn't halted, but it's idle or undergoing a
6014481Sbholler 		 * context switch. No need to awaken anyone else.
6024481Sbholler 		 */
6034481Sbholler 		if (cp->cpu_thread == cp->cpu_idle_thread ||
6044481Sbholler 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
6054481Sbholler 			return;
6064481Sbholler 	}
6074481Sbholler 
6084481Sbholler 	/*
6094481Sbholler 	 * No need to wake up other CPUs if the thread we just enqueued
6104481Sbholler 	 * is bound.
6114481Sbholler 	 */
6124481Sbholler 	if (bound)
6134481Sbholler 		return;
6144481Sbholler 
6154481Sbholler 
6164481Sbholler 	/*
6174481Sbholler 	 * See if there's any other halted CPUs. If there are, then
6184481Sbholler 	 * select one, and awaken it.
6194481Sbholler 	 * It's possible that after we find a CPU, somebody else
6204481Sbholler 	 * will awaken it before we get the chance.
6214481Sbholler 	 * In that case, look again.
6224481Sbholler 	 */
6234481Sbholler 	do {
6244481Sbholler 		CPUSET_FIND(cpu_part->cp_mach->mc_haltset, cpu_found);
6254481Sbholler 		if (cpu_found == CPUSET_NOTINSET)
6264481Sbholler 			return;
6274481Sbholler 
6284481Sbholler 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
6294481Sbholler 		CPUSET_ATOMIC_XDEL(cpu_part->cp_mach->mc_haltset, cpu_found,
6304481Sbholler 		    result);
6314481Sbholler 	} while (result < 0);
6324481Sbholler 
6334481Sbholler 	/*
6344481Sbholler 	 * Do not check if cpu_found is ourself as monitor/mwait wakeup is
6354481Sbholler 	 * cheap.
6364481Sbholler 	 */
6374481Sbholler 	MWAIT_WAKEUP(cpu[cpu_found]);	/* write to monitored line */
6384481Sbholler }
6394481Sbholler 
6403446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
6413446Smrj 
6420Sstevel@tonic-gate static int
6430Sstevel@tonic-gate mp_disable_intr(int cpun)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * switch to the offline cpu
6470Sstevel@tonic-gate 	 */
6480Sstevel@tonic-gate 	affinity_set(cpun);
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * raise ipl to just below cross call
6510Sstevel@tonic-gate 	 */
6520Sstevel@tonic-gate 	splx(XC_MED_PIL-1);
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 *	set base spl to prevent the next swtch to idle from
6550Sstevel@tonic-gate 	 *	lowering back to ipl 0
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1));
6580Sstevel@tonic-gate 	set_base_spl();
6590Sstevel@tonic-gate 	affinity_clear();
6600Sstevel@tonic-gate 	return (DDI_SUCCESS);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate static void
6640Sstevel@tonic-gate mp_enable_intr(int cpun)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	/*
6670Sstevel@tonic-gate 	 * switch to the online cpu
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	affinity_set(cpun);
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * clear the interrupt active mask
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1));
6740Sstevel@tonic-gate 	set_base_spl();
6750Sstevel@tonic-gate 	(void) spl0();
6760Sstevel@tonic-gate 	affinity_clear();
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate static void
6800Sstevel@tonic-gate mach_get_platform(int owner)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	void		**srv_opsp;
6830Sstevel@tonic-gate 	void		**clt_opsp;
6840Sstevel@tonic-gate 	int		i;
6850Sstevel@tonic-gate 	int		total_ops;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/* fix up psm ops */
6880Sstevel@tonic-gate 	srv_opsp = (void **)mach_set[0];
6890Sstevel@tonic-gate 	clt_opsp = (void **)mach_set[owner];
6900Sstevel@tonic-gate 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
6910Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops_ver01) /
6924481Sbholler 		    sizeof (void (*)(void));
6930Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
6940Sstevel@tonic-gate 		/* no psm_notify_func */
6950Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
6960Sstevel@tonic-gate 		    sizeof (void (*)(void));
6970Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
6980Sstevel@tonic-gate 		/* no psm_timer funcs */
6990Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
7000Sstevel@tonic-gate 		    sizeof (void (*)(void));
7010Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
7020Sstevel@tonic-gate 		/* no psm_preshutdown function */
7030Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
7040Sstevel@tonic-gate 		    sizeof (void (*)(void));
7050Sstevel@tonic-gate 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
7060Sstevel@tonic-gate 		/* no psm_preshutdown function */
7070Sstevel@tonic-gate 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
7080Sstevel@tonic-gate 		    sizeof (void (*)(void));
7090Sstevel@tonic-gate 	else
7100Sstevel@tonic-gate 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Save the version of the PSM module, in case we need to
7140Sstevel@tonic-gate 	 * bahave differently based on version.
7150Sstevel@tonic-gate 	 */
7160Sstevel@tonic-gate 	mach_ver[0] = mach_ver[owner];
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	for (i = 0; i < total_ops; i++)
7190Sstevel@tonic-gate 		if (clt_opsp[i] != NULL)
7200Sstevel@tonic-gate 			srv_opsp[i] = clt_opsp[i];
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate static void
7240Sstevel@tonic-gate mach_construct_info()
7250Sstevel@tonic-gate {
7263446Smrj 	struct psm_sw *swp;
7270Sstevel@tonic-gate 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
7280Sstevel@tonic-gate 	int	conflict_owner = 0;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (psmsw->psw_forw == psmsw)
7310Sstevel@tonic-gate 		panic("No valid PSM modules found");
7320Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
7330Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7340Sstevel@tonic-gate 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
7350Sstevel@tonic-gate 			continue;
7360Sstevel@tonic-gate 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
7370Sstevel@tonic-gate 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
7380Sstevel@tonic-gate 		mach_cnt[swp->psw_infop->p_owner]++;
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/* check to see are there any conflicts */
7450Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
7460Sstevel@tonic-gate 		conflict_owner = PSM_OWN_EXCLUSIVE;
7470Sstevel@tonic-gate 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
7480Sstevel@tonic-gate 		conflict_owner = PSM_OWN_OVERRIDE;
7490Sstevel@tonic-gate 	if (conflict_owner) {
7500Sstevel@tonic-gate 		/* remove all psm modules except uppc */
7510Sstevel@tonic-gate 		cmn_err(CE_WARN,
7524481Sbholler 		    "Conflicts detected on the following PSM modules:");
7530Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
7540Sstevel@tonic-gate 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
7550Sstevel@tonic-gate 			if (swp->psw_infop->p_owner == conflict_owner)
7560Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s ",
7574481Sbholler 				    swp->psw_infop->p_mach_idstring);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
7600Sstevel@tonic-gate 		cmn_err(CE_WARN,
7614481Sbholler 		    "Setting the system back to SINGLE processor mode!");
7620Sstevel@tonic-gate 		cmn_err(CE_WARN,
7630Sstevel@tonic-gate 		    "Please edit /etc/mach to remove the invalid PSM module.");
7640Sstevel@tonic-gate 		return;
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (mach_set[PSM_OWN_EXCLUSIVE])
7680Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_EXCLUSIVE);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if (mach_set[PSM_OWN_OVERRIDE])
7710Sstevel@tonic-gate 		mach_get_platform(PSM_OWN_OVERRIDE);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate static void
7750Sstevel@tonic-gate mach_init()
7760Sstevel@tonic-gate {
7773446Smrj 	struct psm_ops  *pops;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	mach_construct_info();
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	pops = mach_set[0];
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/* register the interrupt and clock initialization rotuines */
7840Sstevel@tonic-gate 	picinitf = mach_picinit;
7850Sstevel@tonic-gate 	clkinitf = mach_clkinit;
7860Sstevel@tonic-gate 	psm_get_clockirq = pops->psm_get_clockirq;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/* register the interrupt setup code */
7890Sstevel@tonic-gate 	slvltovect = mach_softlvl_to_vect;
7900Sstevel@tonic-gate 	addspl	= pops->psm_addspl;
7910Sstevel@tonic-gate 	delspl	= pops->psm_delspl;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (pops->psm_translate_irq)
7940Sstevel@tonic-gate 		psm_translate_irq = pops->psm_translate_irq;
7950Sstevel@tonic-gate 	if (pops->psm_intr_ops)
7960Sstevel@tonic-gate 		psm_intr_ops = pops->psm_intr_ops;
7973446Smrj 
7983446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
7993446Smrj 	/*
8003446Smrj 	 * Time-of-day functionality now handled in TOD modules.
8013446Smrj 	 * (Warn about PSM modules that think that we're going to use
8023446Smrj 	 * their ops vectors.)
8033446Smrj 	 */
8043446Smrj 	if (pops->psm_tod_get)
8053446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
8063446Smrj 		    (void *)pops->psm_tod_get);
8073446Smrj 
8083446Smrj 	if (pops->psm_tod_set)
8093446Smrj 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
8103446Smrj 		    (void *)pops->psm_tod_set);
8113446Smrj #endif
8123446Smrj 
8130Sstevel@tonic-gate 	if (pops->psm_notify_error) {
8140Sstevel@tonic-gate 		psm_notify_error = mach_notify_error;
8150Sstevel@tonic-gate 		notify_error = pops->psm_notify_error;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	(*pops->psm_softinit)();
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * Initialize the dispatcher's function hooks
8224481Sbholler 	 * to enable CPU halting when idle.
8234481Sbholler 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle).
8244481Sbholler 	 * Allocate monitor/mwait buffer for cpu0.
8250Sstevel@tonic-gate 	 */
8264481Sbholler 	if (idle_cpu_use_hlt) {
8274481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
8284481Sbholler 			CPU->cpu_m.mcpu_mwait = mach_alloc_mwait(CPU);
8294481Sbholler 			idle_cpu = cpu_idle_mwait;
8304481Sbholler 		} else {
8314481Sbholler 			idle_cpu = cpu_idle;
8324481Sbholler 		}
8334481Sbholler 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	mach_smpinit();
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8384481Sbholler /*
8394481Sbholler  * Return a pointer to memory suitable for monitor/mwait use.  Memory must be
8404481Sbholler  * aligned as specified by cpuid (a cache line size).
8414481Sbholler  */
8424481Sbholler uint32_t *
8434481Sbholler mach_alloc_mwait(cpu_t *cp)
8444481Sbholler {
8454481Sbholler 	size_t		mwait_size = cpuid_get_mwait_size(cp);
8464481Sbholler 	uint32_t	*ret;
8474481Sbholler 
8484481Sbholler 	if (mwait_size < sizeof (uint32_t) || !ISP2(mwait_size))
8494481Sbholler 		panic("Can't handle mwait size %ld", (long)mwait_size);
8504481Sbholler 
8514481Sbholler 	/*
8524481Sbholler 	 * kmem_alloc() returns cache line size aligned data for mwait_size
8534481Sbholler 	 * allocations.  mwait_size is currently cache line sized.  Neither
8544481Sbholler 	 * of these implementation details are guarantied to be true in the
8554481Sbholler 	 * future.
8564481Sbholler 	 *
8574481Sbholler 	 * First try allocating mwait_size as kmem_alloc() currently returns
8584481Sbholler 	 * correctly aligned memory.  If kmem_alloc() does not return
8594481Sbholler 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
8604481Sbholler 	 */
8614481Sbholler 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
8624481Sbholler 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
8634481Sbholler 		*ret = MWAIT_RUNNING;
8644481Sbholler 		return (ret);
8654481Sbholler 	} else {
8664481Sbholler 		kmem_free(ret, mwait_size);
8674481Sbholler 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
8684481Sbholler 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
8694481Sbholler 		*ret = MWAIT_RUNNING;
8704481Sbholler 		return (ret);
8714481Sbholler 	}
8724481Sbholler }
8734481Sbholler 
8740Sstevel@tonic-gate static void
8750Sstevel@tonic-gate mach_smpinit(void)
8760Sstevel@tonic-gate {
8772006Sandrei 	struct psm_ops  *pops;
8782006Sandrei 	processorid_t cpu_id;
8792006Sandrei 	int cnt;
8802006Sandrei 	cpuset_t cpumask;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	pops = mach_set[0];
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	cpu_id = -1;
8850Sstevel@tonic-gate 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
8862006Sandrei 	for (cnt = 0, CPUSET_ZERO(cpumask); cpu_id != -1; cnt++) {
8872006Sandrei 		CPUSET_ADD(cpumask, cpu_id);
8880Sstevel@tonic-gate 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	mp_cpus = cpumask;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/* MP related routines */
8940Sstevel@tonic-gate 	ap_mlsetup = pops->psm_post_cpu_start;
8950Sstevel@tonic-gate 	send_dirintf = pops->psm_send_ipi;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/* optional MP related routines */
8980Sstevel@tonic-gate 	if (pops->psm_shutdown)
8990Sstevel@tonic-gate 		psm_shutdownf = pops->psm_shutdown;
9000Sstevel@tonic-gate 	if (pops->psm_preshutdown)
9010Sstevel@tonic-gate 		psm_preshutdownf = pops->psm_preshutdown;
9020Sstevel@tonic-gate 	if (pops->psm_notify_func)
9030Sstevel@tonic-gate 		psm_notifyf = pops->psm_notify_func;
9040Sstevel@tonic-gate 	if (pops->psm_set_idlecpu)
9050Sstevel@tonic-gate 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
9060Sstevel@tonic-gate 	if (pops->psm_unset_idlecpu)
9070Sstevel@tonic-gate 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	psm_clkinit = pops->psm_clkinit;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	if (pops->psm_timer_reprogram)
9120Sstevel@tonic-gate 		psm_timer_reprogram = pops->psm_timer_reprogram;
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if (pops->psm_timer_enable)
9150Sstevel@tonic-gate 		psm_timer_enable = pops->psm_timer_enable;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (pops->psm_timer_disable)
9180Sstevel@tonic-gate 		psm_timer_disable = pops->psm_timer_disable;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	if (pops->psm_post_cyclic_setup)
9210Sstevel@tonic-gate 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/* check for multiple cpu's */
9240Sstevel@tonic-gate 	if (cnt < 2)
9250Sstevel@tonic-gate 		return;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/* check for MP platforms */
9280Sstevel@tonic-gate 	if (pops->psm_cpu_start == NULL)
9290Sstevel@tonic-gate 		return;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * Set the dispatcher hook to enable cpu "wake up"
9330Sstevel@tonic-gate 	 * when a thread becomes runnable.
9340Sstevel@tonic-gate 	 */
9353446Smrj 	if (idle_cpu_use_hlt)
9364481Sbholler 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
9374481Sbholler 			disp_enq_thread = cpu_wakeup_mwait;
9384481Sbholler 		else
9394481Sbholler 			disp_enq_thread = cpu_wakeup;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if (pops->psm_disable_intr)
9420Sstevel@tonic-gate 		psm_disable_intr = pops->psm_disable_intr;
9430Sstevel@tonic-gate 	if (pops->psm_enable_intr)
9440Sstevel@tonic-gate 		psm_enable_intr  = pops->psm_enable_intr;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	psm_get_ipivect = pops->psm_get_ipivect;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr",
9494481Sbholler 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
9504481Sbholler 	    (caddr_t)X_CALL_HIPRI, NULL, NULL, NULL);
9510Sstevel@tonic-gate 	(void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr",
9524481Sbholler 	    (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO),
9534481Sbholler 	    (caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate static void
9590Sstevel@tonic-gate mach_picinit()
9600Sstevel@tonic-gate {
9612006Sandrei 	struct psm_ops  *pops;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	pops = mach_set[0];
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* register the interrupt handlers */
9660Sstevel@tonic-gate 	setlvl = pops->psm_intr_enter;
9670Sstevel@tonic-gate 	setlvlx = pops->psm_intr_exit;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	/* initialize the interrupt hardware */
9700Sstevel@tonic-gate 	(*pops->psm_picinit)();
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/* set interrupt mask for current ipl */
9730Sstevel@tonic-gate 	setspl = pops->psm_setspl;
9743446Smrj 	cli();
9750Sstevel@tonic-gate 	setspl(CPU->cpu_pri);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate uint_t	cpu_freq;	/* MHz */
9790Sstevel@tonic-gate uint64_t cpu_freq_hz;	/* measured (in hertz) */
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate #define	MEGA_HZ		1000000
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate static uint64_t
9840Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate 	uint64_t cpu_hz;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	if ((pit_counter == 0) || (*processor_clks == 0) ||
9890Sstevel@tonic-gate 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
9900Sstevel@tonic-gate 		return (0);
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	return (cpu_hz);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate static uint64_t
9980Sstevel@tonic-gate mach_getcpufreq(void)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	uint32_t pit_counter;
10010Sstevel@tonic-gate 	uint64_t processor_clks;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (x86_feature & X86_TSC) {
10040Sstevel@tonic-gate 		/*
10050Sstevel@tonic-gate 		 * We have a TSC. freq_tsc() knows how to measure the number
10060Sstevel@tonic-gate 		 * of clock cycles sampled against the PIT.
10070Sstevel@tonic-gate 		 */
10083446Smrj 		ulong_t flags = clear_int_flag();
10090Sstevel@tonic-gate 		processor_clks = freq_tsc(&pit_counter);
10103446Smrj 		restore_int_flag(flags);
10110Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
10120Sstevel@tonic-gate 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
10130Sstevel@tonic-gate #if defined(__amd64)
10140Sstevel@tonic-gate 		panic("mach_getcpufreq: no TSC!");
10150Sstevel@tonic-gate #elif defined(__i386)
10160Sstevel@tonic-gate 		/*
10170Sstevel@tonic-gate 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
10180Sstevel@tonic-gate 		 * for which freq_notsc() knows how to measure the number of
10190Sstevel@tonic-gate 		 * elapsed clock cycles sampled against the PIT
10200Sstevel@tonic-gate 		 */
10213446Smrj 		ulong_t flags = clear_int_flag();
10220Sstevel@tonic-gate 		processor_clks = freq_notsc(&pit_counter);
10233446Smrj 		restore_int_flag(flags);
10240Sstevel@tonic-gate 		return (mach_calchz(pit_counter, &processor_clks));
10250Sstevel@tonic-gate #endif	/* __i386 */
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/* We do not know how to calculate cpu frequency for this cpu. */
10290Sstevel@tonic-gate 	return (0);
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate  * If the clock speed of a cpu is found to be reported incorrectly, do not add
10340Sstevel@tonic-gate  * to this array, instead improve the accuracy of the algorithm that determines
10350Sstevel@tonic-gate  * the clock speed of the processor or extend the implementation to support the
10360Sstevel@tonic-gate  * vendor as appropriate. This is here only to support adjusting the speed on
10370Sstevel@tonic-gate  * older slower processors that mach_fixcpufreq() would not be able to account
10380Sstevel@tonic-gate  * for otherwise.
10390Sstevel@tonic-gate  */
10400Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*
10430Sstevel@tonic-gate  * On fast processors the clock frequency that is measured may be off by
10440Sstevel@tonic-gate  * a few MHz from the value printed on the part. This is a combination of
10450Sstevel@tonic-gate  * the factors that for such fast parts being off by this much is within
10460Sstevel@tonic-gate  * the tolerances for manufacture and because of the difficulties in the
10470Sstevel@tonic-gate  * measurement that can lead to small error. This function uses some
10480Sstevel@tonic-gate  * heuristics in order to tweak the value that was measured to match what
10490Sstevel@tonic-gate  * is most likely printed on the part.
10500Sstevel@tonic-gate  *
10510Sstevel@tonic-gate  * Some examples:
10520Sstevel@tonic-gate  * 	AMD Athlon 1000 mhz measured as 998 mhz
10530Sstevel@tonic-gate  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
10540Sstevel@tonic-gate  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
10550Sstevel@tonic-gate  *
10560Sstevel@tonic-gate  * If in the future this function is no longer sufficient to correct
10570Sstevel@tonic-gate  * for the error in the measurement, then the algorithm used to perform
10580Sstevel@tonic-gate  * the measurement will have to be improved in order to increase accuracy
10590Sstevel@tonic-gate  * rather than adding horrible and questionable kludges here.
10600Sstevel@tonic-gate  *
10610Sstevel@tonic-gate  * This is called after the cyclics subsystem because of the potential
10620Sstevel@tonic-gate  * that the heuristics within may give a worse estimate of the clock
10630Sstevel@tonic-gate  * frequency than the value that was measured.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate static void
10660Sstevel@tonic-gate mach_fixcpufreq(void)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	freq = (uint32_t)cpu_freq;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/*
10730Sstevel@tonic-gate 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
10740Sstevel@tonic-gate 	 * measured speed taking into account that the 667 MHz parts were
10750Sstevel@tonic-gate 	 * the first to round-up.
10760Sstevel@tonic-gate 	 */
10770Sstevel@tonic-gate 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
10780Sstevel@tonic-gate 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
10790Sstevel@tonic-gate 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
10820Sstevel@tonic-gate 	mul = (freq + 25) / 50;
10830Sstevel@tonic-gate 	near50 = mul * 50;
10840Sstevel@tonic-gate 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* Find the closer of the two */
10870Sstevel@tonic-gate 	if (delta66 < delta50) {
10880Sstevel@tonic-gate 		fixed = near66;
10890Sstevel@tonic-gate 		delta = delta66;
10900Sstevel@tonic-gate 	} else {
10910Sstevel@tonic-gate 		fixed = near50;
10920Sstevel@tonic-gate 		delta = delta50;
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (fixed > INT_MAX)
10960Sstevel@tonic-gate 		return;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/*
10990Sstevel@tonic-gate 	 * Some older parts have a core clock frequency that is not an
11000Sstevel@tonic-gate 	 * integral multiple of 50 or 66 MHz. Check if one of the old
11010Sstevel@tonic-gate 	 * clock frequencies is closer to the measured value than any
11020Sstevel@tonic-gate 	 * of the integral multiples of 50 an 66, and if so set fixed
11030Sstevel@tonic-gate 	 * and delta appropriately to represent the closest value.
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 	i = sizeof (x86_cpu_freq) / sizeof (int);
11060Sstevel@tonic-gate 	while (i > 0) {
11070Sstevel@tonic-gate 		i--;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		if (x86_cpu_freq[i] <= freq) {
11100Sstevel@tonic-gate 			mul = freq - x86_cpu_freq[i];
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 			if (mul < delta) {
11130Sstevel@tonic-gate 				fixed = x86_cpu_freq[i];
11140Sstevel@tonic-gate 				delta = mul;
11150Sstevel@tonic-gate 			}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		mul = x86_cpu_freq[i] - freq;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		if (mul < delta) {
11230Sstevel@tonic-gate 			fixed = x86_cpu_freq[i];
11240Sstevel@tonic-gate 			delta = mul;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Set a reasonable maximum for how much to correct the measured
11300Sstevel@tonic-gate 	 * result by. This check is here to prevent the adjustment made
11310Sstevel@tonic-gate 	 * by this function from being more harm than good. It is entirely
11320Sstevel@tonic-gate 	 * possible that in the future parts will be made that are not
11330Sstevel@tonic-gate 	 * integral multiples of 66 or 50 in clock frequency or that
11340Sstevel@tonic-gate 	 * someone may overclock a part to some odd frequency. If the
11350Sstevel@tonic-gate 	 * measured value is farther from the corrected value than
11360Sstevel@tonic-gate 	 * allowed, then assume the corrected value is in error and use
11370Sstevel@tonic-gate 	 * the measured value.
11380Sstevel@tonic-gate 	 */
11390Sstevel@tonic-gate 	if (6 < delta)
11400Sstevel@tonic-gate 		return;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	cpu_freq = (int)fixed;
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate static int
11470Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
11480Sstevel@tonic-gate {
11490Sstevel@tonic-gate 	uint64_t cpu_mhz;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/* Round to nearest MHZ */
11520Sstevel@tonic-gate 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	if (cpu_mhz > INT_MAX)
11550Sstevel@tonic-gate 		return (0);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	return ((int)cpu_mhz);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate static int
11630Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
11640Sstevel@tonic-gate {
11653446Smrj 	struct psm_ops  *pops;
11660Sstevel@tonic-gate 	int resolution;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	pops = mach_set[0];
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	cpu_freq_hz = mach_getcpufreq();
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	cpu_freq = machhztomhz(cpu_freq_hz);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
11750Sstevel@tonic-gate 		tsc_gethrtime_enable = 0;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	if (tsc_gethrtime_enable) {
11780Sstevel@tonic-gate 		tsc_hrtimeinit(cpu_freq_hz);
11790Sstevel@tonic-gate 		gethrtimef = tsc_gethrtime;
11800Sstevel@tonic-gate 		gethrtimeunscaledf = tsc_gethrtimeunscaled;
11810Sstevel@tonic-gate 		scalehrtimef = tsc_scalehrtime;
11820Sstevel@tonic-gate 		hrtime_tick = tsc_tick;
11830Sstevel@tonic-gate 		tsc_gethrtime_initted = 1;
11840Sstevel@tonic-gate 	} else {
11850Sstevel@tonic-gate 		if (pops->psm_hrtimeinit)
11860Sstevel@tonic-gate 			(*pops->psm_hrtimeinit)();
11870Sstevel@tonic-gate 		gethrtimef = pops->psm_gethrtime;
11880Sstevel@tonic-gate 		gethrtimeunscaledf = gethrtimef;
11890Sstevel@tonic-gate 		/* scalehrtimef will remain dummy */
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	mach_fixcpufreq();
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
11950Sstevel@tonic-gate 		if ((preferred_mode == TIMER_ONESHOT) &&
11960Sstevel@tonic-gate 		    (tsc_gethrtime_enable)) {
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 			resolution = (*pops->psm_clkinit)(0);
11990Sstevel@tonic-gate 			if (resolution != 0)  {
12000Sstevel@tonic-gate 				*set_mode = TIMER_ONESHOT;
12010Sstevel@tonic-gate 				return (resolution);
12020Sstevel@tonic-gate 			}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		/*
12070Sstevel@tonic-gate 		 * either periodic mode was requested or could not set to
12080Sstevel@tonic-gate 		 * one-shot mode
12090Sstevel@tonic-gate 		 */
12100Sstevel@tonic-gate 		resolution = (*pops->psm_clkinit)(hz);
12110Sstevel@tonic-gate 		/*
12120Sstevel@tonic-gate 		 * psm should be able to do periodic, so we do not check
12130Sstevel@tonic-gate 		 * for return value of psm_clkinit here.
12140Sstevel@tonic-gate 		 */
12150Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
12160Sstevel@tonic-gate 		return (resolution);
12170Sstevel@tonic-gate 	} else {
12180Sstevel@tonic-gate 		/*
12190Sstevel@tonic-gate 		 * PSMI interface prior to PSMI_3 does not define a return
12200Sstevel@tonic-gate 		 * value for psm_clkinit, so the return value is ignored.
12210Sstevel@tonic-gate 		 */
12220Sstevel@tonic-gate 		(void) (*pops->psm_clkinit)(hz);
12230Sstevel@tonic-gate 		*set_mode = TIMER_PERIODIC;
12240Sstevel@tonic-gate 		return (nsec_per_tick);
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
1228999Slq150181 /*ARGSUSED*/
1229999Slq150181 static void
1230999Slq150181 mach_psm_set_softintr(int ipl, struct av_softinfo *pending)
1231999Slq150181 {
12323446Smrj 	struct psm_ops  *pops;
1233999Slq150181 
1234999Slq150181 	/* invoke hardware interrupt					*/
1235999Slq150181 	pops = mach_set[0];
1236999Slq150181 	(*pops->psm_set_softintr)(ipl);
1237999Slq150181 }
1238999Slq150181 
12390Sstevel@tonic-gate static int
12403446Smrj mach_softlvl_to_vect(int ipl)
12410Sstevel@tonic-gate {
12423446Smrj 	int softvect;
12433446Smrj 	struct psm_ops  *pops;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	pops = mach_set[0];
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/* check for null handler for set soft interrupt call		*/
12480Sstevel@tonic-gate 	if (pops->psm_set_softintr == NULL) {
1249999Slq150181 		setsoftint = av_set_softint_pending;
12500Sstevel@tonic-gate 		return (PSM_SV_SOFTWARE);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	softvect = (*pops->psm_softlvl_to_irq)(ipl);
12540Sstevel@tonic-gate 	/* check for hardware scheme					*/
12550Sstevel@tonic-gate 	if (softvect > PSM_SV_SOFTWARE) {
1256999Slq150181 		setsoftint = mach_psm_set_softintr;
12570Sstevel@tonic-gate 		return (softvect);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	if (softvect == PSM_SV_SOFTWARE)
1261999Slq150181 		setsoftint = av_set_softint_pending;
12620Sstevel@tonic-gate 	else	/* hardware and software mixed scheme			*/
12630Sstevel@tonic-gate 		setsoftint = mach_set_softintr;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	return (PSM_SV_SOFTWARE);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate static void
12693446Smrj mach_set_softintr(int ipl, struct av_softinfo *pending)
12700Sstevel@tonic-gate {
12713446Smrj 	struct psm_ops  *pops;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/* set software pending bits					*/
1274999Slq150181 	av_set_softint_pending(ipl, pending);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	/*	check if dosoftint will be called at the end of intr	*/
12770Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) || (curthread->t_intr))
12780Sstevel@tonic-gate 		return;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/* invoke hardware interrupt					*/
12810Sstevel@tonic-gate 	pops = mach_set[0];
12820Sstevel@tonic-gate 	(*pops->psm_set_softintr)(ipl);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate 
12853446Smrj #ifdef DEBUG
12863446Smrj /*
12873446Smrj  * This is here to allow us to simulate cpus that refuse to start.
12883446Smrj  */
12893446Smrj cpuset_t cpufailset;
12903446Smrj #endif
12913446Smrj 
12923446Smrj int
12933446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
12940Sstevel@tonic-gate {
12953446Smrj 	struct psm_ops *pops = mach_set[0];
12963446Smrj 	processorid_t id = cp->cpu_id;
12970Sstevel@tonic-gate 
12983446Smrj #ifdef DEBUG
12993446Smrj 	if (CPU_IN_SET(cpufailset, id))
13003446Smrj 		return (0);
13013446Smrj #endif
13023446Smrj 	return ((*pops->psm_cpu_start)(id, ctx));
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*ARGSUSED*/
13060Sstevel@tonic-gate static int
13070Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	return (irqno);	/* default to NO translation */
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate static void
13130Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
13140Sstevel@tonic-gate {
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * SL_FATAL is pass in once panicstr is set, deliver it
13170Sstevel@tonic-gate 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
13180Sstevel@tonic-gate 	 * codes for the psmi handler
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	if (level & SL_FATAL)
13210Sstevel@tonic-gate 		(*notify_error)(CE_PANIC, errmsg);
13220Sstevel@tonic-gate 	else if (level & SL_WARN)
13230Sstevel@tonic-gate 		(*notify_error)(CE_WARN, errmsg);
13240Sstevel@tonic-gate 	else if (level & SL_NOTE)
13250Sstevel@tonic-gate 		(*notify_error)(CE_NOTE, errmsg);
13260Sstevel@tonic-gate 	else if (level & SL_CONSOLE)
13270Sstevel@tonic-gate 		(*notify_error)(CE_CONT, errmsg);
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate /*
13310Sstevel@tonic-gate  * It provides the default basic intr_ops interface for the new DDI
13320Sstevel@tonic-gate  * interrupt framework if the PSM doesn't have one.
13330Sstevel@tonic-gate  *
13340Sstevel@tonic-gate  * Input:
13350Sstevel@tonic-gate  * dip     - pointer to the dev_info structure of the requested device
13360Sstevel@tonic-gate  * hdlp    - pointer to the internal interrupt handle structure for the
13370Sstevel@tonic-gate  *	     requested interrupt
13380Sstevel@tonic-gate  * intr_op - opcode for this call
13390Sstevel@tonic-gate  * result  - pointer to the integer that will hold the result to be
13400Sstevel@tonic-gate  *	     passed back if return value is PSM_SUCCESS
13410Sstevel@tonic-gate  *
13420Sstevel@tonic-gate  * Output:
13430Sstevel@tonic-gate  * return value is either PSM_SUCCESS or PSM_FAILURE
13440Sstevel@tonic-gate  */
13450Sstevel@tonic-gate static int
13460Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
13470Sstevel@tonic-gate     psm_intr_op_t intr_op, int *result)
13480Sstevel@tonic-gate {
13490Sstevel@tonic-gate 	struct intrspec *ispec;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	switch (intr_op) {
13520Sstevel@tonic-gate 	case PSM_INTR_OP_CHECK_MSI:
13530Sstevel@tonic-gate 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
13544481Sbholler 		    DDI_INTR_TYPE_MSIX);
13550Sstevel@tonic-gate 		break;
13560Sstevel@tonic-gate 	case PSM_INTR_OP_ALLOC_VECTORS:
13570Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
13580Sstevel@tonic-gate 			*result = 1;
13590Sstevel@tonic-gate 		else
13600Sstevel@tonic-gate 			*result = 0;
13610Sstevel@tonic-gate 		break;
13620Sstevel@tonic-gate 	case PSM_INTR_OP_FREE_VECTORS:
13630Sstevel@tonic-gate 		break;
13640Sstevel@tonic-gate 	case PSM_INTR_OP_NAVAIL_VECTORS:
13650Sstevel@tonic-gate 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
13660Sstevel@tonic-gate 			*result = 1;
13670Sstevel@tonic-gate 		else
13680Sstevel@tonic-gate 			*result = 0;
13690Sstevel@tonic-gate 		break;
13700Sstevel@tonic-gate 	case PSM_INTR_OP_XLATE_VECTOR:
1371916Sschwartz 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
13720Sstevel@tonic-gate 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
13730Sstevel@tonic-gate 		break;
13740Sstevel@tonic-gate 	case PSM_INTR_OP_GET_CAP:
13750Sstevel@tonic-gate 		*result = 0;
13760Sstevel@tonic-gate 		break;
13770Sstevel@tonic-gate 	case PSM_INTR_OP_GET_PENDING:
13780Sstevel@tonic-gate 	case PSM_INTR_OP_CLEAR_MASK:
13790Sstevel@tonic-gate 	case PSM_INTR_OP_SET_MASK:
13800Sstevel@tonic-gate 	case PSM_INTR_OP_GET_SHARED:
13810Sstevel@tonic-gate 	case PSM_INTR_OP_SET_PRI:
13820Sstevel@tonic-gate 	case PSM_INTR_OP_SET_CAP:
1383916Sschwartz 	case PSM_INTR_OP_SET_CPU:
1384916Sschwartz 	case PSM_INTR_OP_GET_INTR:
13850Sstevel@tonic-gate 	default:
13860Sstevel@tonic-gate 		return (PSM_FAILURE);
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 	return (PSM_SUCCESS);
13890Sstevel@tonic-gate }
1390