xref: /onnv-gate/usr/src/uts/i86pc/io/pcplusmp/apic.c (revision 7986:20935bdc00cc)
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
51456Sdmick  * Common Development and Distribution License (the "License").
61456Sdmick  * 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  */
213446Smrj 
220Sstevel@tonic-gate /*
236749Ssherrym  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * PSMI 1.1 extensions are supported only in 2.6 and later versions.
290Sstevel@tonic-gate  * PSMI 1.2 extensions are supported only in 2.7 and later versions.
300Sstevel@tonic-gate  * PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
310Sstevel@tonic-gate  * PSMI 1.5 extensions are supported in Solaris Nevada.
325295Srandyf  * PSMI 1.6 extensions are supported in Solaris Nevada.
330Sstevel@tonic-gate  */
345295Srandyf #define	PSMI_1_6
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/processor.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate #include <sys/psm.h>
390Sstevel@tonic-gate #include <sys/smp_impldefs.h>
400Sstevel@tonic-gate #include <sys/cram.h>
410Sstevel@tonic-gate #include <sys/acpi/acpi.h>
420Sstevel@tonic-gate #include <sys/acpica.h>
430Sstevel@tonic-gate #include <sys/psm_common.h>
443446Smrj #include <sys/apic.h>
450Sstevel@tonic-gate #include <sys/pit.h>
460Sstevel@tonic-gate #include <sys/ddi.h>
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
490Sstevel@tonic-gate #include <sys/pci.h>
500Sstevel@tonic-gate #include <sys/promif.h>
510Sstevel@tonic-gate #include <sys/x86_archext.h>
520Sstevel@tonic-gate #include <sys/cpc_impl.h>
530Sstevel@tonic-gate #include <sys/uadmin.h>
540Sstevel@tonic-gate #include <sys/panic.h>
550Sstevel@tonic-gate #include <sys/debug.h>
560Sstevel@tonic-gate #include <sys/archsystm.h>
570Sstevel@tonic-gate #include <sys/trap.h>
580Sstevel@tonic-gate #include <sys/machsystm.h>
593446Smrj #include <sys/sysmacros.h>
600Sstevel@tonic-gate #include <sys/cpuvar.h>
610Sstevel@tonic-gate #include <sys/rm_platter.h>
620Sstevel@tonic-gate #include <sys/privregs.h>
630Sstevel@tonic-gate #include <sys/note.h>
640Sstevel@tonic-gate #include <sys/pci_intr_lib.h>
653446Smrj #include <sys/spl.h>
665084Sjohnlev #include <sys/clock.h>
675107Seota #include <sys/dditypes.h>
685107Seota #include <sys/sunddi.h>
697349SAdrian.Frost@Sun.COM #include <sys/x_call.h>
70*7986SSaurabh.Mishra@Sun.COM #include <sys/reboot.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  *	Local Function Prototypes
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate static void apic_init_intr();
765084Sjohnlev static void apic_nmi_intr(caddr_t arg, struct regs *rp);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  *	standard MP entries
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static int	apic_probe();
820Sstevel@tonic-gate static int	apic_clkinit();
830Sstevel@tonic-gate static int	apic_getclkirq(int ipl);
840Sstevel@tonic-gate static uint_t	apic_calibrate(volatile uint32_t *addr,
850Sstevel@tonic-gate     uint16_t *pit_ticks_adj);
860Sstevel@tonic-gate static hrtime_t apic_gettime();
870Sstevel@tonic-gate static hrtime_t apic_gethrtime();
880Sstevel@tonic-gate static void	apic_init();
890Sstevel@tonic-gate static void	apic_picinit(void);
903446Smrj static int	apic_cpu_start(processorid_t, caddr_t);
910Sstevel@tonic-gate static int	apic_post_cpu_start(void);
920Sstevel@tonic-gate static void	apic_send_ipi(int cpun, int ipl);
930Sstevel@tonic-gate static void	apic_set_idlecpu(processorid_t cpun);
940Sstevel@tonic-gate static void	apic_unset_idlecpu(processorid_t cpun);
950Sstevel@tonic-gate static int	apic_intr_enter(int ipl, int *vect);
960Sstevel@tonic-gate static void	apic_setspl(int ipl);
977282Smishra static void	x2apic_setspl(int ipl);
980Sstevel@tonic-gate static int	apic_addspl(int ipl, int vector, int min_ipl, int max_ipl);
990Sstevel@tonic-gate static int	apic_delspl(int ipl, int vector, int min_ipl, int max_ipl);
1000Sstevel@tonic-gate static void	apic_shutdown(int cmd, int fcn);
1010Sstevel@tonic-gate static void	apic_preshutdown(int cmd, int fcn);
1020Sstevel@tonic-gate static int	apic_disable_intr(processorid_t cpun);
1030Sstevel@tonic-gate static void	apic_enable_intr(processorid_t cpun);
1040Sstevel@tonic-gate static processorid_t	apic_get_next_processorid(processorid_t cpun);
1050Sstevel@tonic-gate static int		apic_get_ipivect(int ipl, int type);
1060Sstevel@tonic-gate static void	apic_timer_reprogram(hrtime_t time);
1070Sstevel@tonic-gate static void	apic_timer_enable(void);
1080Sstevel@tonic-gate static void	apic_timer_disable(void);
1090Sstevel@tonic-gate static void	apic_post_cyclic_setup(void *arg);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static int	apic_oneshot = 0;
1120Sstevel@tonic-gate int	apic_oneshot_enable = 1; /* to allow disabling one-shot capability */
1130Sstevel@tonic-gate 
1143446Smrj /* Now the ones for Dynamic Interrupt distribution */
1153446Smrj int	apic_enable_dynamic_migration = 0;
1163446Smrj 
1173446Smrj 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * These variables are frequently accessed in apic_intr_enter(),
1200Sstevel@tonic-gate  * apic_intr_exit and apic_setspl, so group them together
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate volatile uint32_t *apicadr =  NULL;	/* virtual addr of local APIC	*/
1230Sstevel@tonic-gate int apic_setspl_delay = 1;		/* apic_setspl - delay enable	*/
1240Sstevel@tonic-gate int apic_clkvect;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* vector at which error interrupts come in */
1270Sstevel@tonic-gate int apic_errvect;
1280Sstevel@tonic-gate int apic_enable_error_intr = 1;
1290Sstevel@tonic-gate int apic_error_display_delay = 100;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /* vector at which performance counter overflow interrupts come in */
1320Sstevel@tonic-gate int apic_cpcovf_vect;
1330Sstevel@tonic-gate int apic_enable_cpcovf_intr = 1;
1340Sstevel@tonic-gate 
1357349SAdrian.Frost@Sun.COM /* vector at which CMCI interrupts come in */
1367349SAdrian.Frost@Sun.COM int apic_cmci_vect;
1377349SAdrian.Frost@Sun.COM extern int cmi_enable_cmci;
1387349SAdrian.Frost@Sun.COM extern void cmi_cmci_trap(void);
1397349SAdrian.Frost@Sun.COM 
1407349SAdrian.Frost@Sun.COM static kmutex_t cmci_cpu_setup_lock;	/* protects cmci_cpu_setup_registered */
1417349SAdrian.Frost@Sun.COM static int cmci_cpu_setup_registered;
1427349SAdrian.Frost@Sun.COM 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * The following vector assignments influence the value of ipltopri and
1450Sstevel@tonic-gate  * vectortoipl. Note that vectors 0 - 0x1f are not used. We can program
1463745Ssethg  * idle to 0 and IPL 0 to 0xf to differentiate idle in case
1470Sstevel@tonic-gate  * we care to do so in future. Note some IPLs which are rarely used
1480Sstevel@tonic-gate  * will share the vector ranges and heavily used IPLs (5 and 6) have
1490Sstevel@tonic-gate  * a wide range.
1503745Ssethg  *
1513745Ssethg  * This array is used to initialize apic_ipls[] (in apic_init()).
1523745Ssethg  *
1530Sstevel@tonic-gate  *	IPL		Vector range.		as passed to intr_enter
1540Sstevel@tonic-gate  *	0		none.
1550Sstevel@tonic-gate  *	1,2,3		0x20-0x2f		0x0-0xf
1560Sstevel@tonic-gate  *	4		0x30-0x3f		0x10-0x1f
1570Sstevel@tonic-gate  *	5		0x40-0x5f		0x20-0x3f
1580Sstevel@tonic-gate  *	6		0x60-0x7f		0x40-0x5f
1590Sstevel@tonic-gate  *	7,8,9		0x80-0x8f		0x60-0x6f
1600Sstevel@tonic-gate  *	10		0x90-0x9f		0x70-0x7f
1610Sstevel@tonic-gate  *	11		0xa0-0xaf		0x80-0x8f
1620Sstevel@tonic-gate  *	...		...
1633745Ssethg  *	15		0xe0-0xef		0xc0-0xcf
1643745Ssethg  *	15		0xf0-0xff		0xd0-0xdf
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate uchar_t apic_vectortoipl[APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL] = {
1673745Ssethg 	3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 15
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 	/*
1703745Ssethg 	 * The ipl of an ISR at vector X is apic_vectortoipl[X>>4]
1710Sstevel@tonic-gate 	 * NOTE that this is vector as passed into intr_enter which is
1720Sstevel@tonic-gate 	 * programmed vector - 0x20 (APIC_BASE_VECT)
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate uchar_t	apic_ipltopri[MAXIPL + 1];	/* unix ipl to apic pri	*/
1760Sstevel@tonic-gate 	/* The taskpri to be programmed into apic to mask given ipl */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #if defined(__amd64)
1790Sstevel@tonic-gate uchar_t	apic_cr8pri[MAXIPL + 1];	/* unix ipl to cr8 pri	*/
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1833745Ssethg  * Correlation of the hardware vector to the IPL in use, initialized
1843745Ssethg  * from apic_vectortoipl[] in apic_init().  The final IPLs may not correlate
1853745Ssethg  * to the IPLs in apic_vectortoipl on some systems that share interrupt lines
1863745Ssethg  * connected to errata-stricken IOAPICs
1873745Ssethg  */
1883745Ssethg uchar_t apic_ipls[APIC_AVAIL_VECTOR];
1893745Ssethg 
1903745Ssethg /*
1910Sstevel@tonic-gate  * Patchable global variables.
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate int	apic_forceload = 0;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate int	apic_coarse_hrtime = 1;		/* 0 - use accurate slow gethrtime() */
1960Sstevel@tonic-gate 					/* 1 - use gettime() for performance */
1970Sstevel@tonic-gate int	apic_flat_model = 0;		/* 0 - clustered. 1 - flat */
1980Sstevel@tonic-gate int	apic_enable_hwsoftint = 0;	/* 0 - disable, 1 - enable	*/
1990Sstevel@tonic-gate int	apic_enable_bind_log = 1;	/* 1 - display interrupt binding log */
2000Sstevel@tonic-gate int	apic_panic_on_nmi = 0;
2010Sstevel@tonic-gate int	apic_panic_on_apic_error = 0;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate int	apic_verbose = 0;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /* minimum number of timer ticks to program to */
2060Sstevel@tonic-gate int apic_min_timer_ticks = 1;
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  *	Local static data
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate static struct	psm_ops apic_ops = {
2110Sstevel@tonic-gate 	apic_probe,
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	apic_init,
2140Sstevel@tonic-gate 	apic_picinit,
2150Sstevel@tonic-gate 	apic_intr_enter,
2160Sstevel@tonic-gate 	apic_intr_exit,
2170Sstevel@tonic-gate 	apic_setspl,
2180Sstevel@tonic-gate 	apic_addspl,
2190Sstevel@tonic-gate 	apic_delspl,
2200Sstevel@tonic-gate 	apic_disable_intr,
2210Sstevel@tonic-gate 	apic_enable_intr,
2224652Scwb 	(int (*)(int))NULL,		/* psm_softlvl_to_irq */
2234652Scwb 	(void (*)(int))NULL,		/* psm_set_softintr */
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	apic_set_idlecpu,
2260Sstevel@tonic-gate 	apic_unset_idlecpu,
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	apic_clkinit,
2290Sstevel@tonic-gate 	apic_getclkirq,
2300Sstevel@tonic-gate 	(void (*)(void))NULL,		/* psm_hrtimeinit */
2310Sstevel@tonic-gate 	apic_gethrtime,
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	apic_get_next_processorid,
2340Sstevel@tonic-gate 	apic_cpu_start,
2350Sstevel@tonic-gate 	apic_post_cpu_start,
2360Sstevel@tonic-gate 	apic_shutdown,
2370Sstevel@tonic-gate 	apic_get_ipivect,
2380Sstevel@tonic-gate 	apic_send_ipi,
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	(int (*)(dev_info_t *, int))NULL,	/* psm_translate_irq */
2410Sstevel@tonic-gate 	(void (*)(int, char *))NULL,	/* psm_notify_error */
2420Sstevel@tonic-gate 	(void (*)(int))NULL,		/* psm_notify_func */
2430Sstevel@tonic-gate 	apic_timer_reprogram,
2440Sstevel@tonic-gate 	apic_timer_enable,
2450Sstevel@tonic-gate 	apic_timer_disable,
2460Sstevel@tonic-gate 	apic_post_cyclic_setup,
2470Sstevel@tonic-gate 	apic_preshutdown,
2485295Srandyf 	apic_intr_ops,			/* Advanced DDI Interrupt framework */
2495295Srandyf 	apic_state,			/* save, restore apic state for S3 */
2500Sstevel@tonic-gate };
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static struct	psm_info apic_psm_info = {
2545295Srandyf 	PSM_INFO_VER01_6,			/* version */
2550Sstevel@tonic-gate 	PSM_OWN_EXCLUSIVE,			/* ownership */
2560Sstevel@tonic-gate 	(struct psm_ops *)&apic_ops,		/* operation */
2574397Sschwartz 	APIC_PCPLUSMP_NAME,			/* machine name */
2586896Sdmick 	"pcplusmp v1.4 compatible",
2590Sstevel@tonic-gate };
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static void *apic_hdlp;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate #ifdef DEBUG
2640Sstevel@tonic-gate int	apic_debug = 0;
2650Sstevel@tonic-gate int	apic_restrict_vector = 0;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate int	apic_debug_msgbuf[APIC_DEBUG_MSGBUFSIZE];
2680Sstevel@tonic-gate int	apic_debug_msgbufindex = 0;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate #endif /* DEBUG */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate apic_cpus_info_t	*apic_cpus;
2730Sstevel@tonic-gate 
2743446Smrj cpuset_t	apic_cpumask;
2755084Sjohnlev uint_t	apic_picinit_called;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /* Flag to indicate that we need to shut down all processors */
2780Sstevel@tonic-gate static uint_t	apic_shutdown_processors;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate uint_t apic_nsec_per_intr = 0;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * apic_let_idle_redistribute can have the following values:
2840Sstevel@tonic-gate  * 0 - If clock decremented it from 1 to 0, clock has to call redistribute.
2850Sstevel@tonic-gate  * apic_redistribute_lock prevents multiple idle cpus from redistributing
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate int	apic_num_idle_redistributions = 0;
2880Sstevel@tonic-gate static	int apic_let_idle_redistribute = 0;
2890Sstevel@tonic-gate static	uint_t apic_nticks = 0;
2900Sstevel@tonic-gate static	uint_t apic_skipped_redistribute = 0;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /* to gather intr data and redistribute */
2930Sstevel@tonic-gate static void apic_redistribute_compute(void);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static	uint_t last_count_read = 0;
2960Sstevel@tonic-gate static	lock_t	apic_gethrtime_lock;
2970Sstevel@tonic-gate volatile int	apic_hrtime_stamp = 0;
2980Sstevel@tonic-gate volatile hrtime_t apic_nsec_since_boot = 0;
2992992Sdmick static uint_t apic_hertz_count;
3002992Sdmick 
3012992Sdmick uint64_t apic_ticks_per_SFnsecs;	/* # of ticks in SF nsecs */
3022992Sdmick 
3030Sstevel@tonic-gate static hrtime_t apic_nsec_max;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static	hrtime_t	apic_last_hrtime = 0;
3060Sstevel@tonic-gate int		apic_hrtime_error = 0;
3070Sstevel@tonic-gate int		apic_remote_hrterr = 0;
3080Sstevel@tonic-gate int		apic_num_nmis = 0;
3090Sstevel@tonic-gate int		apic_apic_error = 0;
3100Sstevel@tonic-gate int		apic_num_apic_errors = 0;
3110Sstevel@tonic-gate int		apic_num_cksum_errors = 0;
3120Sstevel@tonic-gate 
3133446Smrj int	apic_error = 0;
3140Sstevel@tonic-gate static	int	apic_cmos_ssb_set = 0;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /* use to make sure only one cpu handles the nmi */
3170Sstevel@tonic-gate static	lock_t	apic_nmi_lock;
3180Sstevel@tonic-gate /* use to make sure only one cpu handles the error interrupt */
3190Sstevel@tonic-gate static	lock_t	apic_error_lock;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static	struct {
3220Sstevel@tonic-gate 	uchar_t	cntl;
3230Sstevel@tonic-gate 	uchar_t	data;
3240Sstevel@tonic-gate } aspen_bmc[] = {
3250Sstevel@tonic-gate 	{ CC_SMS_WR_START,	0x18 },		/* NetFn/LUN */
3260Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x24 },		/* Cmd SET_WATCHDOG_TIMER */
3270Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x84 },		/* DataByte 1: SMS/OS no log */
3280Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x2 },		/* DataByte 2: Power Down */
3290Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x0 },		/* DataByte 3: no pre-timeout */
3300Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x0 },		/* DataByte 4: timer expir. */
3310Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0xa },		/* DataByte 5: init countdown */
3320Sstevel@tonic-gate 	{ CC_SMS_WR_END,	0x0 },		/* DataByte 6: init countdown */
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	{ CC_SMS_WR_START,	0x18 },		/* NetFn/LUN */
3350Sstevel@tonic-gate 	{ CC_SMS_WR_END,	0x22 }		/* Cmd RESET_WATCHDOG_TIMER */
3360Sstevel@tonic-gate };
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static	struct {
3390Sstevel@tonic-gate 	int	port;
3400Sstevel@tonic-gate 	uchar_t	data;
3410Sstevel@tonic-gate } sitka_bmc[] = {
3420Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_START },
3430Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x18 },		/* NetFn/LUN */
3440Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x24 },		/* Cmd SET_WATCHDOG_TIMER */
3450Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x84 },		/* DataByte 1: SMS/OS no log */
3460Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x2 },		/* DataByte 2: Power Down */
3470Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 3: no pre-timeout */
3480Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 4: timer expir. */
3490Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0xa },		/* DataByte 5: init countdown */
3500Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_END },
3510Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 6: init countdown */
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_START },
3540Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x18 },		/* NetFn/LUN */
3550Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_END },
3560Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x22 }		/* Cmd RESET_WATCHDOG_TIMER */
3570Sstevel@tonic-gate };
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /* Patchable global variables. */
3600Sstevel@tonic-gate int		apic_kmdb_on_nmi = 0;		/* 0 - no, 1 - yes enter kmdb */
3612992Sdmick uint32_t	apic_divide_reg_init = 0;	/* 0 - divide by 2 */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  *	This is the loadable module wrapper
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate int
3680Sstevel@tonic-gate _init(void)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	if (apic_coarse_hrtime)
3710Sstevel@tonic-gate 		apic_ops.psm_gethrtime = &apic_gettime;
3720Sstevel@tonic-gate 	return (psm_mod_init(&apic_hdlp, &apic_psm_info));
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate int
3760Sstevel@tonic-gate _fini(void)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	return (psm_mod_fini(&apic_hdlp, &apic_psm_info));
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate int
3820Sstevel@tonic-gate _info(struct modinfo *modinfop)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	return (psm_mod_info(&apic_hdlp, &apic_psm_info, modinfop));
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static int
3890Sstevel@tonic-gate apic_probe()
3900Sstevel@tonic-gate {
3913446Smrj 	return (apic_probe_common(apic_psm_info.p_mach_idstring));
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate void
3950Sstevel@tonic-gate apic_init()
3960Sstevel@tonic-gate {
3973446Smrj 	int i;
3983446Smrj 	int	j = 1;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */
4010Sstevel@tonic-gate 	for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
4020Sstevel@tonic-gate 		if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) &&
4030Sstevel@tonic-gate 		    (apic_vectortoipl[i + 1] == apic_vectortoipl[i]))
4040Sstevel@tonic-gate 			/* get to highest vector at the same ipl */
4050Sstevel@tonic-gate 			continue;
4060Sstevel@tonic-gate 		for (; j <= apic_vectortoipl[i]; j++) {
4070Sstevel@tonic-gate 			apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
4080Sstevel@tonic-gate 			    APIC_BASE_VECT;
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	for (; j < MAXIPL + 1; j++)
4120Sstevel@tonic-gate 		/* fill up any empty ipltopri slots */
4130Sstevel@tonic-gate 		apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT;
4143446Smrj 	apic_init_common();
4150Sstevel@tonic-gate #if defined(__amd64)
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Make cpu-specific interrupt info point to cr8pri vector
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	for (i = 0; i <= MAXIPL; i++)
4200Sstevel@tonic-gate 		apic_cr8pri[i] = apic_ipltopri[i] >> APIC_IPL_SHIFT;
4210Sstevel@tonic-gate 	CPU->cpu_pri_data = apic_cr8pri;
4220Sstevel@tonic-gate #endif	/* __amd64 */
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * handler for APIC Error interrupt. Just print a warning and continue
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate static int
4290Sstevel@tonic-gate apic_error_intr()
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	uint_t	error0, error1, error;
4320Sstevel@tonic-gate 	uint_t	i;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * We need to write before read as per 7.4.17 of system prog manual.
4360Sstevel@tonic-gate 	 * We do both and or the results to be safe
4370Sstevel@tonic-gate 	 */
4387282Smishra 	error0 = apic_reg_ops->apic_read(APIC_ERROR_STATUS);
4397282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4407282Smishra 	error1 = apic_reg_ops->apic_read(APIC_ERROR_STATUS);
4410Sstevel@tonic-gate 	error = error0 | error1;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
444846Ssethg 	 * Clear the APIC error status (do this on all cpus that enter here)
445846Ssethg 	 * (two writes are required due to the semantics of accessing the
446846Ssethg 	 * error status register.)
447846Ssethg 	 */
4487282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4497282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
450846Ssethg 
451846Ssethg 	/*
4520Sstevel@tonic-gate 	 * Prevent more than 1 CPU from handling error interrupt causing
4530Sstevel@tonic-gate 	 * double printing (interleave of characters from multiple
4540Sstevel@tonic-gate 	 * CPU's when using prom_printf)
4550Sstevel@tonic-gate 	 */
4560Sstevel@tonic-gate 	if (lock_try(&apic_error_lock) == 0)
4570Sstevel@tonic-gate 		return (error ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
4580Sstevel@tonic-gate 	if (error) {
4590Sstevel@tonic-gate #if	DEBUG
4600Sstevel@tonic-gate 		if (apic_debug)
4610Sstevel@tonic-gate 			debug_enter("pcplusmp: APIC Error interrupt received");
4620Sstevel@tonic-gate #endif /* DEBUG */
4630Sstevel@tonic-gate 		if (apic_panic_on_apic_error)
4640Sstevel@tonic-gate 			cmn_err(CE_PANIC,
4650Sstevel@tonic-gate 			    "APIC Error interrupt on CPU %d. Status = %x\n",
4660Sstevel@tonic-gate 			    psm_get_cpu_id(), error);
4670Sstevel@tonic-gate 		else {
4680Sstevel@tonic-gate 			if ((error & ~APIC_CS_ERRORS) == 0) {
4690Sstevel@tonic-gate 				/* cksum error only */
4700Sstevel@tonic-gate 				apic_error |= APIC_ERR_APIC_ERROR;
4710Sstevel@tonic-gate 				apic_apic_error |= error;
4720Sstevel@tonic-gate 				apic_num_apic_errors++;
4730Sstevel@tonic-gate 				apic_num_cksum_errors++;
4740Sstevel@tonic-gate 			} else {
4750Sstevel@tonic-gate 				/*
4760Sstevel@tonic-gate 				 * prom_printf is the best shot we have of
4770Sstevel@tonic-gate 				 * something which is problem free from
4780Sstevel@tonic-gate 				 * high level/NMI type of interrupts
4790Sstevel@tonic-gate 				 */
4800Sstevel@tonic-gate 				prom_printf("APIC Error interrupt on CPU %d. "
4810Sstevel@tonic-gate 				    "Status 0 = %x, Status 1 = %x\n",
4820Sstevel@tonic-gate 				    psm_get_cpu_id(), error0, error1);
4830Sstevel@tonic-gate 				apic_error |= APIC_ERR_APIC_ERROR;
4840Sstevel@tonic-gate 				apic_apic_error |= error;
4850Sstevel@tonic-gate 				apic_num_apic_errors++;
4860Sstevel@tonic-gate 				for (i = 0; i < apic_error_display_delay; i++) {
4870Sstevel@tonic-gate 					tenmicrosec();
4880Sstevel@tonic-gate 				}
4890Sstevel@tonic-gate 				/*
4900Sstevel@tonic-gate 				 * provide more delay next time limited to
4910Sstevel@tonic-gate 				 * roughly 1 clock tick time
4920Sstevel@tonic-gate 				 */
4930Sstevel@tonic-gate 				if (apic_error_display_delay < 500)
4940Sstevel@tonic-gate 					apic_error_display_delay *= 2;
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 		lock_clear(&apic_error_lock);
4980Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
4990Sstevel@tonic-gate 	} else {
5000Sstevel@tonic-gate 		lock_clear(&apic_error_lock);
5010Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	/* NOTREACHED */
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * Turn off the mask bit in the performance counter Local Vector Table entry.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static void
5100Sstevel@tonic-gate apic_cpcovf_mask_clear(void)
5110Sstevel@tonic-gate {
5127282Smishra 	apic_reg_ops->apic_write(APIC_PCINT_VECT,
5137282Smishra 	    (apic_reg_ops->apic_read(APIC_PCINT_VECT) & ~APIC_LVT_MASK));
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5167349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5177349SAdrian.Frost@Sun.COM static int
5187349SAdrian.Frost@Sun.COM apic_cmci_enable(xc_arg_t arg1, xc_arg_t arg2, xc_arg_t arg3)
5197349SAdrian.Frost@Sun.COM {
5207349SAdrian.Frost@Sun.COM 	apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
5217349SAdrian.Frost@Sun.COM 	return (0);
5227349SAdrian.Frost@Sun.COM }
5237349SAdrian.Frost@Sun.COM 
5247349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5257349SAdrian.Frost@Sun.COM static int
5267349SAdrian.Frost@Sun.COM apic_cmci_disable(xc_arg_t arg1, xc_arg_t arg2, xc_arg_t arg3)
5277349SAdrian.Frost@Sun.COM {
5287349SAdrian.Frost@Sun.COM 	apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect | AV_MASK);
5297349SAdrian.Frost@Sun.COM 	return (0);
5307349SAdrian.Frost@Sun.COM }
5317349SAdrian.Frost@Sun.COM 
5327349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5337349SAdrian.Frost@Sun.COM static int
5347349SAdrian.Frost@Sun.COM cmci_cpu_setup(cpu_setup_t what, int cpuid, void *arg)
5357349SAdrian.Frost@Sun.COM {
5367349SAdrian.Frost@Sun.COM 	cpuset_t	cpu_set;
5377349SAdrian.Frost@Sun.COM 
5387349SAdrian.Frost@Sun.COM 	CPUSET_ONLY(cpu_set, cpuid);
5397349SAdrian.Frost@Sun.COM 
5407349SAdrian.Frost@Sun.COM 	switch (what) {
5417349SAdrian.Frost@Sun.COM 		case CPU_ON:
5427349SAdrian.Frost@Sun.COM 			xc_call(NULL, NULL, NULL, X_CALL_HIPRI, cpu_set,
5437349SAdrian.Frost@Sun.COM 			    (xc_func_t)apic_cmci_enable);
5447349SAdrian.Frost@Sun.COM 			break;
5457349SAdrian.Frost@Sun.COM 
5467349SAdrian.Frost@Sun.COM 		case CPU_OFF:
5477349SAdrian.Frost@Sun.COM 			xc_call(NULL, NULL, NULL, X_CALL_HIPRI, cpu_set,
5487349SAdrian.Frost@Sun.COM 			    (xc_func_t)apic_cmci_disable);
5497349SAdrian.Frost@Sun.COM 			break;
5507349SAdrian.Frost@Sun.COM 
5517349SAdrian.Frost@Sun.COM 		default:
5527349SAdrian.Frost@Sun.COM 			break;
5537349SAdrian.Frost@Sun.COM 	}
5547349SAdrian.Frost@Sun.COM 
5557349SAdrian.Frost@Sun.COM 	return (0);
5567349SAdrian.Frost@Sun.COM }
5577349SAdrian.Frost@Sun.COM 
5580Sstevel@tonic-gate static void
5590Sstevel@tonic-gate apic_init_intr()
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	processorid_t	cpun = psm_get_cpu_id();
5626896Sdmick 	uint_t nlvt;
5637282Smishra 	uint32_t svr = AV_UNIT_ENABLE | APIC_SPUR_INTR;
5640Sstevel@tonic-gate 
5657282Smishra 	apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
5660Sstevel@tonic-gate 
5677282Smishra 	if (apic_mode == LOCAL_APIC) {
5687282Smishra 		/*
5697282Smishra 		 * We are running APIC in MMIO mode.
5707282Smishra 		 */
5717282Smishra 		if (apic_flat_model) {
5727282Smishra 			apic_reg_ops->apic_write(APIC_FORMAT_REG,
5737282Smishra 			    APIC_FLAT_MODEL);
5747282Smishra 		} else {
5757282Smishra 			apic_reg_ops->apic_write(APIC_FORMAT_REG,
5767282Smishra 			    APIC_CLUSTER_MODEL);
5777282Smishra 		}
5787282Smishra 
5797282Smishra 		apic_reg_ops->apic_write(APIC_DEST_REG,
5807282Smishra 		    AV_HIGH_ORDER >> cpun);
5817282Smishra 	}
5827282Smishra 
5837282Smishra 	if (apic_direct_EOI) {
5847282Smishra 		/*
5857282Smishra 		 * Set 12th bit in Spurious Interrupt Vector
5867282Smishra 		 * Register to support level triggered interrupt
5877282Smishra 		 * directed EOI.
5887282Smishra 		 */
5897282Smishra 		svr |= (0x1 << APIC_SVR);
5907282Smishra 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/* need to enable APIC before unmasking NMI */
5937282Smishra 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, svr);
5940Sstevel@tonic-gate 
5956896Sdmick 	/*
5966896Sdmick 	 * Presence of an invalid vector with delivery mode AV_FIXED can
5976896Sdmick 	 * cause an error interrupt, even if the entry is masked...so
5986896Sdmick 	 * write a valid vector to LVT entries along with the mask bit
5996896Sdmick 	 */
6006896Sdmick 
6016896Sdmick 	/* All APICs have timer and LINT0/1 */
6027282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK|APIC_RESV_IRQ);
6037282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK|APIC_RESV_IRQ);
6047282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT1, AV_NMI);	/* enable NMI */
6050Sstevel@tonic-gate 
6066896Sdmick 	/*
6076896Sdmick 	 * On integrated APICs, the number of LVT entries is
6086896Sdmick 	 * 'Max LVT entry' + 1; on 82489DX's (non-integrated
6096896Sdmick 	 * APICs), nlvt is "3" (LINT0, LINT1, and timer)
6106896Sdmick 	 */
6110Sstevel@tonic-gate 
6126896Sdmick 	if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) {
6136896Sdmick 		nlvt = 3;
6146896Sdmick 	} else {
6156896Sdmick 		nlvt = ((apicadr[APIC_VERS_REG] >> 16) & 0xFF) + 1;
6166896Sdmick 	}
6176896Sdmick 
6186896Sdmick 	if (nlvt >= 5) {
6196896Sdmick 		/* Enable performance counter overflow interrupt */
6206896Sdmick 
6216896Sdmick 		if ((x86_feature & X86_MSR) != X86_MSR)
6226896Sdmick 			apic_enable_cpcovf_intr = 0;
6236896Sdmick 		if (apic_enable_cpcovf_intr) {
6246896Sdmick 			if (apic_cpcovf_vect == 0) {
6256896Sdmick 				int ipl = APIC_PCINT_IPL;
6266896Sdmick 				int irq = apic_get_ipivect(ipl, -1);
6270Sstevel@tonic-gate 
6286896Sdmick 				ASSERT(irq != -1);
6296896Sdmick 				apic_cpcovf_vect =
6306896Sdmick 				    apic_irq_table[irq]->airq_vector;
6316896Sdmick 				ASSERT(apic_cpcovf_vect);
6326896Sdmick 				(void) add_avintr(NULL, ipl,
6336896Sdmick 				    (avfunc)kcpc_hw_overflow_intr,
6346896Sdmick 				    "apic pcint", irq, NULL, NULL, NULL, NULL);
6356896Sdmick 				kcpc_hw_overflow_intr_installed = 1;
6366896Sdmick 				kcpc_hw_enable_cpc_intr =
6376896Sdmick 				    apic_cpcovf_mask_clear;
6386896Sdmick 			}
6397282Smishra 			apic_reg_ops->apic_write(APIC_PCINT_VECT,
6407282Smishra 			    apic_cpcovf_vect);
6416896Sdmick 		}
6426896Sdmick 	}
6430Sstevel@tonic-gate 
6446896Sdmick 	if (nlvt >= 6) {
6456896Sdmick 		/* Only mask TM intr if the BIOS apparently doesn't use it */
6466896Sdmick 
6476896Sdmick 		uint32_t lvtval;
6486896Sdmick 
6497282Smishra 		lvtval = apic_reg_ops->apic_read(APIC_THERM_VECT);
6506896Sdmick 		if (((lvtval & AV_MASK) == AV_MASK) ||
6516896Sdmick 		    ((lvtval & AV_DELIV_MODE) != AV_SMI)) {
6527282Smishra 			apic_reg_ops->apic_write(APIC_THERM_VECT,
6537282Smishra 			    AV_MASK|APIC_RESV_IRQ);
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/* Enable error interrupt */
6580Sstevel@tonic-gate 
6596896Sdmick 	if (nlvt >= 4 && apic_enable_error_intr) {
6600Sstevel@tonic-gate 		if (apic_errvect == 0) {
6610Sstevel@tonic-gate 			int ipl = 0xf;	/* get highest priority intr */
6620Sstevel@tonic-gate 			int irq = apic_get_ipivect(ipl, -1);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 			ASSERT(irq != -1);
6650Sstevel@tonic-gate 			apic_errvect = apic_irq_table[irq]->airq_vector;
6660Sstevel@tonic-gate 			ASSERT(apic_errvect);
6670Sstevel@tonic-gate 			/*
6680Sstevel@tonic-gate 			 * Not PSMI compliant, but we are going to merge
6690Sstevel@tonic-gate 			 * with ON anyway
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			(void) add_avintr((void *)NULL, ipl,
6720Sstevel@tonic-gate 			    (avfunc)apic_error_intr, "apic error intr",
673916Sschwartz 			    irq, NULL, NULL, NULL, NULL);
6740Sstevel@tonic-gate 		}
6757282Smishra 		apic_reg_ops->apic_write(APIC_ERR_VECT, apic_errvect);
6767282Smishra 		apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
6777282Smishra 		apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
6780Sstevel@tonic-gate 	}
6796896Sdmick 
6807349SAdrian.Frost@Sun.COM 	/* Enable CMCI interrupt */
6817349SAdrian.Frost@Sun.COM 	if (cmi_enable_cmci) {
6827349SAdrian.Frost@Sun.COM 
6837349SAdrian.Frost@Sun.COM 		mutex_enter(&cmci_cpu_setup_lock);
6847349SAdrian.Frost@Sun.COM 		if (cmci_cpu_setup_registered == 0) {
6857349SAdrian.Frost@Sun.COM 			mutex_enter(&cpu_lock);
6867349SAdrian.Frost@Sun.COM 			register_cpu_setup_func(cmci_cpu_setup, NULL);
6877349SAdrian.Frost@Sun.COM 			mutex_exit(&cpu_lock);
6887349SAdrian.Frost@Sun.COM 			cmci_cpu_setup_registered = 1;
6897349SAdrian.Frost@Sun.COM 		}
6907349SAdrian.Frost@Sun.COM 		mutex_exit(&cmci_cpu_setup_lock);
6917349SAdrian.Frost@Sun.COM 
6927349SAdrian.Frost@Sun.COM 		if (apic_cmci_vect == 0) {
6937349SAdrian.Frost@Sun.COM 			int ipl = 0x2;
6947349SAdrian.Frost@Sun.COM 			int irq = apic_get_ipivect(ipl, -1);
6957349SAdrian.Frost@Sun.COM 
6967349SAdrian.Frost@Sun.COM 			ASSERT(irq != -1);
6977349SAdrian.Frost@Sun.COM 			apic_cmci_vect = apic_irq_table[irq]->airq_vector;
6987349SAdrian.Frost@Sun.COM 			ASSERT(apic_cmci_vect);
6997349SAdrian.Frost@Sun.COM 
7007349SAdrian.Frost@Sun.COM 			(void) add_avintr(NULL, ipl,
7017349SAdrian.Frost@Sun.COM 			    (avfunc)cmi_cmci_trap,
7027349SAdrian.Frost@Sun.COM 			    "apic cmci intr", irq, NULL, NULL, NULL, NULL);
7037349SAdrian.Frost@Sun.COM 		}
7047349SAdrian.Frost@Sun.COM 		apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
7057349SAdrian.Frost@Sun.COM 	}
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate static void
7090Sstevel@tonic-gate apic_disable_local_apic()
7100Sstevel@tonic-gate {
7117282Smishra 	apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
7127282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK);
7137282Smishra 
7147282Smishra 	/* local intr reg 0 */
7157282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK);
7167282Smishra 
7177282Smishra 	/* disable NMI */
7187282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT1, AV_MASK);
7197282Smishra 
7207282Smishra 	/* and error interrupt */
7217282Smishra 	apic_reg_ops->apic_write(APIC_ERR_VECT, AV_MASK);
7227282Smishra 
7237282Smishra 	/* and perf counter intr */
7247282Smishra 	apic_reg_ops->apic_write(APIC_PCINT_VECT, AV_MASK);
7257282Smishra 
7267282Smishra 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, APIC_SPUR_INTR);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate static void
7300Sstevel@tonic-gate apic_picinit(void)
7310Sstevel@tonic-gate {
7323446Smrj 	int i, j;
7330Sstevel@tonic-gate 	uint_t isr;
7347282Smishra 	uint32_t ver;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * On UniSys Model 6520, the BIOS leaves vector 0x20 isr
7380Sstevel@tonic-gate 	 * bit on without clearing it with EOI.  Since softint
7390Sstevel@tonic-gate 	 * uses vector 0x20 to interrupt itself, so softint will
7400Sstevel@tonic-gate 	 * not work on this machine.  In order to fix this problem
7410Sstevel@tonic-gate 	 * a check is made to verify all the isr bits are clear.
7420Sstevel@tonic-gate 	 * If not, EOIs are issued to clear the bits.
7430Sstevel@tonic-gate 	 */
7440Sstevel@tonic-gate 	for (i = 7; i >= 1; i--) {
7457282Smishra 		isr = apic_reg_ops->apic_read(APIC_ISR_REG + (i * 4));
7467282Smishra 		if (isr != 0)
7470Sstevel@tonic-gate 			for (j = 0; ((j < 32) && (isr != 0)); j++)
7480Sstevel@tonic-gate 				if (isr & (1 << j)) {
7497282Smishra 					apic_reg_ops->apic_write(
7507282Smishra 					    APIC_EOI_REG, 0);
7510Sstevel@tonic-gate 					isr &= ~(1 << j);
7520Sstevel@tonic-gate 					apic_error |= APIC_ERR_BOOT_EOI;
7530Sstevel@tonic-gate 				}
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	/* set a flag so we know we have run apic_picinit() */
7575084Sjohnlev 	apic_picinit_called = 1;
7580Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_gethrtime_lock);
7590Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_ioapic_lock);
7600Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_error_lock);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	picsetup();	 /* initialise the 8259 */
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/* add nmi handler - least priority nmi handler */
7650Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_nmi_lock);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr,
7680Sstevel@tonic-gate 	    "pcplusmp NMI handler", (caddr_t)NULL))
7690Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler");
7700Sstevel@tonic-gate 
7717282Smishra 	ver = apic_reg_ops->apic_read(APIC_VERS_REG);
7727282Smishra 	/*
7737282Smishra 	 * In order to determine support for Directed EOI capability,
7747282Smishra 	 * we check for 24th bit in Local APIC Version Register.
7757282Smishra 	 */
7767282Smishra 	if (ver & (0x1 << APIC_DIRECTED_EOI)) {
7777282Smishra 		apic_direct_EOI = 1;
7787282Smishra 		apic_change_eoi();
7797282Smishra 	}
7807282Smishra 
7810Sstevel@tonic-gate 	apic_init_intr();
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/* enable apic mode if imcr present */
7840Sstevel@tonic-gate 	if (apic_imcrp) {
7850Sstevel@tonic-gate 		outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
7860Sstevel@tonic-gate 		outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7893446Smrj 	ioapic_init_intr(IOAPIC_MASK);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 
7933446Smrj /*ARGSUSED1*/
7943446Smrj static int
7953446Smrj apic_cpu_start(processorid_t cpun, caddr_t arg)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	int		loop_count;
7980Sstevel@tonic-gate 	uint32_t	vector;
7993446Smrj 	uint_t		cpu_id;
8003446Smrj 	ulong_t		iflag;
8010Sstevel@tonic-gate 
8027282Smishra 	cpu_id =  apic_cpus[cpun].aci_local_id;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	apic_cmos_ssb_set = 1;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * Interrupts on BSP cpu will be disabled during these startup
8080Sstevel@tonic-gate 	 * steps in order to avoid unwanted side effects from
8090Sstevel@tonic-gate 	 * executing interrupt handlers on a problematic BIOS.
8100Sstevel@tonic-gate 	 */
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	iflag = intr_clear();
8130Sstevel@tonic-gate 	outb(CMOS_ADDR, SSB);
8140Sstevel@tonic-gate 	outb(CMOS_DATA, BIOS_SHUTDOWN);
8150Sstevel@tonic-gate 
8167798SSaurabh.Mishra@Sun.COM 	/*
817*7986SSaurabh.Mishra@Sun.COM 	 * According to X2APIC specification in section '2.3.5.1' of
8187798SSaurabh.Mishra@Sun.COM 	 * Interrupt Command Register Semantics, the semantics of
8197798SSaurabh.Mishra@Sun.COM 	 * programming the Interrupt Command Register to dispatch an interrupt
8207798SSaurabh.Mishra@Sun.COM 	 * is simplified. A single MSR write to the 64-bit ICR is required
8217798SSaurabh.Mishra@Sun.COM 	 * for dispatching an interrupt. Specifically, with the 64-bit MSR
8227798SSaurabh.Mishra@Sun.COM 	 * interface to ICR, system software is not required to check the
8237798SSaurabh.Mishra@Sun.COM 	 * status of the delivery status bit prior to writing to the ICR
8247798SSaurabh.Mishra@Sun.COM 	 * to send an IPI. With the removal of the Delivery Status bit,
8257798SSaurabh.Mishra@Sun.COM 	 * system software no longer has a reason to read the ICR. It remains
8267798SSaurabh.Mishra@Sun.COM 	 * readable only to aid in debugging.
8277798SSaurabh.Mishra@Sun.COM 	 */
8287798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
8297798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
8307798SSaurabh.Mishra@Sun.COM #else
8317798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC) {
8327798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
8337798SSaurabh.Mishra@Sun.COM 	}
8347798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/* for integrated - make sure there is one INIT IPI in buffer */
8370Sstevel@tonic-gate 	/* for external - it will wake up the cpu */
8387282Smishra 	apic_reg_ops->apic_write_int_cmd(cpu_id, AV_ASSERT | AV_RESET);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/* If only 1 CPU is installed, PENDING bit will not go low */
8417798SSaurabh.Mishra@Sun.COM 	for (loop_count = 0x1000; loop_count; loop_count--) {
8427798SSaurabh.Mishra@Sun.COM 		if (apic_mode == LOCAL_APIC &&
8437798SSaurabh.Mishra@Sun.COM 		    apic_reg_ops->apic_read(APIC_INT_CMD1) & AV_PENDING)
8440Sstevel@tonic-gate 			apic_ret();
8450Sstevel@tonic-gate 		else
8460Sstevel@tonic-gate 			break;
8477798SSaurabh.Mishra@Sun.COM 	}
8480Sstevel@tonic-gate 
8497282Smishra 	apic_reg_ops->apic_write_int_cmd(cpu_id, AV_DEASSERT | AV_RESET);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	drv_usecwait(20000);		/* 20 milli sec */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (apic_cpus[cpun].aci_local_ver >= APIC_INTEGRATED_VERS) {
8540Sstevel@tonic-gate 		/* integrated apic */
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		vector = (rm_platter_pa >> MMU_PAGESHIFT) &
8570Sstevel@tonic-gate 		    (APIC_VECTOR_MASK | APIC_IPL_MASK);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		/* to offset the INIT IPI queue up in the buffer */
8607282Smishra 		apic_reg_ops->apic_write_int_cmd(cpu_id, vector | AV_STARTUP);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		drv_usecwait(200);		/* 20 micro sec */
8630Sstevel@tonic-gate 
8647282Smishra 		apic_reg_ops->apic_write_int_cmd(cpu_id, vector | AV_STARTUP);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		drv_usecwait(200);		/* 20 micro sec */
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 	intr_restore(iflag);
8693446Smrj 	return (0);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate #ifdef	DEBUG
8740Sstevel@tonic-gate int	apic_break_on_cpu = 9;
8750Sstevel@tonic-gate int	apic_stretch_interrupts = 0;
8760Sstevel@tonic-gate int	apic_stretch_ISR = 1 << 3;	/* IPL of 3 matches nothing now */
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate void
8790Sstevel@tonic-gate apic_break()
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate #endif /* DEBUG */
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate /*
8850Sstevel@tonic-gate  * platform_intr_enter
8860Sstevel@tonic-gate  *
8870Sstevel@tonic-gate  *	Called at the beginning of the interrupt service routine to
8880Sstevel@tonic-gate  *	mask all level equal to and below the interrupt priority
8890Sstevel@tonic-gate  *	of the interrupting vector.  An EOI should be given to
8900Sstevel@tonic-gate  *	the interrupt controller to enable other HW interrupts.
8910Sstevel@tonic-gate  *
8920Sstevel@tonic-gate  *	Return -1 for spurious interrupts
8930Sstevel@tonic-gate  *
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate /*ARGSUSED*/
8960Sstevel@tonic-gate static int
8970Sstevel@tonic-gate apic_intr_enter(int ipl, int *vectorp)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	uchar_t vector;
9000Sstevel@tonic-gate 	int nipl;
9013446Smrj 	int irq;
9023446Smrj 	ulong_t iflag;
9030Sstevel@tonic-gate 	apic_cpus_info_t *cpu_infop;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/*
9063745Ssethg 	 * The real vector delivered is (*vectorp + 0x20), but our caller
9073745Ssethg 	 * subtracts 0x20 from the vector before passing it to us.
9083745Ssethg 	 * (That's why APIC_BASE_VECT is 0x20.)
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	vector = (uchar_t)*vectorp;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/* if interrupted by the clock, increment apic_nsec_since_boot */
9130Sstevel@tonic-gate 	if (vector == apic_clkvect) {
9140Sstevel@tonic-gate 		if (!apic_oneshot) {
9150Sstevel@tonic-gate 			/* NOTE: this is not MT aware */
9160Sstevel@tonic-gate 			apic_hrtime_stamp++;
9170Sstevel@tonic-gate 			apic_nsec_since_boot += apic_nsec_per_intr;
9180Sstevel@tonic-gate 			apic_hrtime_stamp++;
9190Sstevel@tonic-gate 			last_count_read = apic_hertz_count;
9200Sstevel@tonic-gate 			apic_redistribute_compute();
9210Sstevel@tonic-gate 		}
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		/* We will avoid all the book keeping overhead for clock */
9243745Ssethg 		nipl = apic_ipls[vector];
9253745Ssethg 
9267282Smishra 		*vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT];
9277282Smishra 		if (apic_mode == LOCAL_APIC) {
9280Sstevel@tonic-gate #if defined(__amd64)
9297282Smishra 			setcr8((ulong_t)(apic_ipltopri[nipl] >>
9307282Smishra 			    APIC_IPL_SHIFT));
9310Sstevel@tonic-gate #else
9327282Smishra 			LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
9337282Smishra 			    (uint32_t)apic_ipltopri[nipl]);
9340Sstevel@tonic-gate #endif
9357282Smishra 			LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
9367282Smishra 		} else {
9377282Smishra 			X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
9387282Smishra 			X2APIC_WRITE(APIC_EOI_REG, 0);
9397282Smishra 		}
9407282Smishra 
9410Sstevel@tonic-gate 		return (nipl);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	cpu_infop = &apic_cpus[psm_get_cpu_id()];
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) {
9470Sstevel@tonic-gate 		cpu_infop->aci_spur_cnt++;
9480Sstevel@tonic-gate 		return (APIC_INT_SPURIOUS);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/* Check if the vector we got is really what we need */
9520Sstevel@tonic-gate 	if (apic_revector_pending) {
9530Sstevel@tonic-gate 		/*
9540Sstevel@tonic-gate 		 * Disable interrupts for the duration of
9550Sstevel@tonic-gate 		 * the vector translation to prevent a self-race for
9560Sstevel@tonic-gate 		 * the apic_revector_lock.  This cannot be done
9570Sstevel@tonic-gate 		 * in apic_xlate_vector because it is recursive and
9580Sstevel@tonic-gate 		 * we want the vector translation to be atomic with
9590Sstevel@tonic-gate 		 * respect to other (higher-priority) interrupts.
9600Sstevel@tonic-gate 		 */
9610Sstevel@tonic-gate 		iflag = intr_clear();
9620Sstevel@tonic-gate 		vector = apic_xlate_vector(vector + APIC_BASE_VECT) -
9630Sstevel@tonic-gate 		    APIC_BASE_VECT;
9640Sstevel@tonic-gate 		intr_restore(iflag);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9673745Ssethg 	nipl = apic_ipls[vector];
9680Sstevel@tonic-gate 	*vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT];
9690Sstevel@tonic-gate 
9707282Smishra 	if (apic_mode == LOCAL_APIC) {
9710Sstevel@tonic-gate #if defined(__amd64)
9727282Smishra 		setcr8((ulong_t)(apic_ipltopri[nipl] >> APIC_IPL_SHIFT));
9730Sstevel@tonic-gate #else
9747282Smishra 		LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
9757282Smishra 		    (uint32_t)apic_ipltopri[nipl]);
9760Sstevel@tonic-gate #endif
9777282Smishra 	} else {
9787282Smishra 		X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
9797282Smishra 	}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	cpu_infop->aci_current[nipl] = (uchar_t)irq;
9820Sstevel@tonic-gate 	cpu_infop->aci_curipl = (uchar_t)nipl;
9830Sstevel@tonic-gate 	cpu_infop->aci_ISR_in_progress |= 1 << nipl;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * apic_level_intr could have been assimilated into the irq struct.
9870Sstevel@tonic-gate 	 * but, having it as a character array is more efficient in terms of
9880Sstevel@tonic-gate 	 * cache usage. So, we leave it as is.
9890Sstevel@tonic-gate 	 */
9907282Smishra 	if (!apic_level_intr[irq]) {
9917282Smishra 		if (apic_mode == LOCAL_APIC)
9927282Smishra 			LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
9937282Smishra 		else
9947282Smishra 			X2APIC_WRITE(APIC_EOI_REG, 0);
9957282Smishra 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate #ifdef	DEBUG
9980Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(vector);
9990Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(irq);
10000Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(nipl);
10010Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(psm_get_cpu_id());
10020Sstevel@tonic-gate 	if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl)))
10030Sstevel@tonic-gate 		drv_usecwait(apic_stretch_interrupts);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	if (apic_break_on_cpu == psm_get_cpu_id())
10060Sstevel@tonic-gate 		apic_break();
10070Sstevel@tonic-gate #endif /* DEBUG */
10080Sstevel@tonic-gate 	return (nipl);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10117282Smishra /*
1012*7986SSaurabh.Mishra@Sun.COM  * This macro is a common code used by MMIO local apic and X2APIC
10137282Smishra  * local apic.
10147282Smishra  */
10157282Smishra #define	APIC_INTR_EXIT() \
10167282Smishra { \
10177282Smishra 	cpu_infop = &apic_cpus[psm_get_cpu_id()]; \
10187282Smishra 	if (apic_level_intr[irq]) \
10197282Smishra 		apic_reg_ops->apic_send_eoi(irq); \
10207282Smishra 	cpu_infop->aci_curipl = (uchar_t)prev_ipl; \
10217282Smishra 	/* ISR above current pri could not be in progress */ \
10227282Smishra 	cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; \
10237282Smishra }
10247282Smishra 
10257282Smishra /*
1026*7986SSaurabh.Mishra@Sun.COM  * Any changes made to this function must also change X2APIC
10277282Smishra  * version of intr_exit.
10287282Smishra  */
10293446Smrj void
10300Sstevel@tonic-gate apic_intr_exit(int prev_ipl, int irq)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	apic_cpus_info_t *cpu_infop;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate #if defined(__amd64)
10350Sstevel@tonic-gate 	setcr8((ulong_t)apic_cr8pri[prev_ipl]);
10360Sstevel@tonic-gate #else
10370Sstevel@tonic-gate 	apicadr[APIC_TASK_REG] = apic_ipltopri[prev_ipl];
10380Sstevel@tonic-gate #endif
10390Sstevel@tonic-gate 
10407282Smishra 	APIC_INTR_EXIT();
10417282Smishra }
10420Sstevel@tonic-gate 
10437282Smishra /*
10447282Smishra  * Same as apic_intr_exit() except it uses MSR rather than MMIO
10457282Smishra  * to access local apic registers.
10467282Smishra  */
10477282Smishra void
10487282Smishra x2apic_intr_exit(int prev_ipl, int irq)
10497282Smishra {
10507282Smishra 	apic_cpus_info_t *cpu_infop;
10517282Smishra 
10527282Smishra 	X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[prev_ipl]);
10537282Smishra 	APIC_INTR_EXIT();
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10565084Sjohnlev intr_exit_fn_t
10575084Sjohnlev psm_intr_exit_fn(void)
10585084Sjohnlev {
10597282Smishra 	if (apic_mode == LOCAL_X2APIC)
10607282Smishra 		return (x2apic_intr_exit);
10617282Smishra 
10625084Sjohnlev 	return (apic_intr_exit);
10635084Sjohnlev }
10645084Sjohnlev 
10650Sstevel@tonic-gate /*
10667282Smishra  * Mask all interrupts below or equal to the given IPL.
1067*7986SSaurabh.Mishra@Sun.COM  * Any changes made to this function must also change X2APIC
10687282Smishra  * version of setspl.
10690Sstevel@tonic-gate  */
10700Sstevel@tonic-gate static void
10710Sstevel@tonic-gate apic_setspl(int ipl)
10720Sstevel@tonic-gate {
10730Sstevel@tonic-gate #if defined(__amd64)
10740Sstevel@tonic-gate 	setcr8((ulong_t)apic_cr8pri[ipl]);
10750Sstevel@tonic-gate #else
10760Sstevel@tonic-gate 	apicadr[APIC_TASK_REG] = apic_ipltopri[ipl];
10770Sstevel@tonic-gate #endif
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	/* interrupts at ipl above this cannot be in progress */
10800Sstevel@tonic-gate 	apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * this is a patch fix for the ALR QSMP P5 machine, so that interrupts
10830Sstevel@tonic-gate 	 * have enough time to come in before the priority is raised again
10840Sstevel@tonic-gate 	 * during the idle() loop.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	if (apic_setspl_delay)
10877282Smishra 		(void) apic_reg_ops->apic_get_pri();
10887282Smishra }
10897282Smishra 
10907282Smishra /*
1091*7986SSaurabh.Mishra@Sun.COM  * X2APIC version of setspl.
10927282Smishra  * Mask all interrupts below or equal to the given IPL
10937282Smishra  */
10947282Smishra static void
10957282Smishra x2apic_setspl(int ipl)
10967282Smishra {
10977282Smishra 	X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[ipl]);
10987282Smishra 
10997282Smishra 	/* interrupts at ipl above this cannot be in progress */
11007282Smishra 	apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
1104*7986SSaurabh.Mishra@Sun.COM  * generates an interprocessor interrupt to another CPU. Any changes made to
1105*7986SSaurabh.Mishra@Sun.COM  * this routine must be accompanied by similar changes to
1106*7986SSaurabh.Mishra@Sun.COM  * apic_common_send_ipi().
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate static void
11090Sstevel@tonic-gate apic_send_ipi(int cpun, int ipl)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	int vector;
11123446Smrj 	ulong_t flag;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	vector = apic_resv_vector[ipl];
11150Sstevel@tonic-gate 
11166896Sdmick 	ASSERT((vector >= APIC_BASE_VECT) && (vector <= APIC_SPUR_INTR));
11176896Sdmick 
11180Sstevel@tonic-gate 	flag = intr_clear();
11190Sstevel@tonic-gate 
11207798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
11210Sstevel@tonic-gate 
11227282Smishra 	apic_reg_ops->apic_write_int_cmd(apic_cpus[cpun].aci_local_id,
11237282Smishra 	    vector);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	intr_restore(flag);
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate /*ARGSUSED*/
11300Sstevel@tonic-gate static void
11310Sstevel@tonic-gate apic_set_idlecpu(processorid_t cpun)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate /*ARGSUSED*/
11360Sstevel@tonic-gate static void
11370Sstevel@tonic-gate apic_unset_idlecpu(processorid_t cpun)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 
11427282Smishra void
11430Sstevel@tonic-gate apic_ret()
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * If apic_coarse_time == 1, then apic_gettime() is used instead of
11490Sstevel@tonic-gate  * apic_gethrtime().  This is used for performance instead of accuracy.
11500Sstevel@tonic-gate  */
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate static hrtime_t
11530Sstevel@tonic-gate apic_gettime()
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	int old_hrtime_stamp;
11560Sstevel@tonic-gate 	hrtime_t temp;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * In one-shot mode, we do not keep time, so if anyone
11600Sstevel@tonic-gate 	 * calls psm_gettime() directly, we vector over to
11610Sstevel@tonic-gate 	 * gethrtime().
11620Sstevel@tonic-gate 	 * one-shot mode MUST NOT be enabled if this psm is the source of
11630Sstevel@tonic-gate 	 * hrtime.
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (apic_oneshot)
11670Sstevel@tonic-gate 		return (gethrtime());
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate gettime_again:
11710Sstevel@tonic-gate 	while ((old_hrtime_stamp = apic_hrtime_stamp) & 1)
11720Sstevel@tonic-gate 		apic_ret();
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	temp = apic_nsec_since_boot;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	if (apic_hrtime_stamp != old_hrtime_stamp) {	/* got an interrupt */
11770Sstevel@tonic-gate 		goto gettime_again;
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 	return (temp);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /*
11830Sstevel@tonic-gate  * Here we return the number of nanoseconds since booting.  Note every
11840Sstevel@tonic-gate  * clock interrupt increments apic_nsec_since_boot by the appropriate
11850Sstevel@tonic-gate  * amount.
11860Sstevel@tonic-gate  */
11870Sstevel@tonic-gate static hrtime_t
11880Sstevel@tonic-gate apic_gethrtime()
11890Sstevel@tonic-gate {
11903446Smrj 	int curr_timeval, countval, elapsed_ticks;
11910Sstevel@tonic-gate 	int old_hrtime_stamp, status;
11920Sstevel@tonic-gate 	hrtime_t temp;
11937282Smishra 	uint32_t cpun;
11943446Smrj 	ulong_t oflags;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/*
11970Sstevel@tonic-gate 	 * In one-shot mode, we do not keep time, so if anyone
11980Sstevel@tonic-gate 	 * calls psm_gethrtime() directly, we vector over to
11990Sstevel@tonic-gate 	 * gethrtime().
12000Sstevel@tonic-gate 	 * one-shot mode MUST NOT be enabled if this psm is the source of
12010Sstevel@tonic-gate 	 * hrtime.
12020Sstevel@tonic-gate 	 */
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	if (apic_oneshot)
12050Sstevel@tonic-gate 		return (gethrtime());
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	oflags = intr_clear();	/* prevent migration */
12080Sstevel@tonic-gate 
12097282Smishra 	cpun = apic_reg_ops->apic_read(APIC_LID_REG);
12107282Smishra 	if (apic_mode == LOCAL_APIC)
12117282Smishra 		cpun >>= APIC_ID_BIT_OFFSET;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	lock_set(&apic_gethrtime_lock);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate gethrtime_again:
12160Sstevel@tonic-gate 	while ((old_hrtime_stamp = apic_hrtime_stamp) & 1)
12170Sstevel@tonic-gate 		apic_ret();
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	/*
12200Sstevel@tonic-gate 	 * Check to see which CPU we are on.  Note the time is kept on
12210Sstevel@tonic-gate 	 * the local APIC of CPU 0.  If on CPU 0, simply read the current
12220Sstevel@tonic-gate 	 * counter.  If on another CPU, issue a remote read command to CPU 0.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate 	if (cpun == apic_cpus[0].aci_local_id) {
12257282Smishra 		countval = apic_reg_ops->apic_read(APIC_CURR_COUNT);
12260Sstevel@tonic-gate 	} else {
12277798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
12287798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
12297798SSaurabh.Mishra@Sun.COM #else
12307798SSaurabh.Mishra@Sun.COM 		if (apic_mode == LOCAL_APIC)
12317798SSaurabh.Mishra@Sun.COM 			APIC_AV_PENDING_SET();
12327798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
12330Sstevel@tonic-gate 
12347282Smishra 		apic_reg_ops->apic_write_int_cmd(
12357282Smishra 		    apic_cpus[0].aci_local_id, APIC_CURR_ADD | AV_REMOTE);
12360Sstevel@tonic-gate 
12377282Smishra 		while ((status = apic_reg_ops->apic_read(APIC_INT_CMD1))
12387282Smishra 		    & AV_READ_PENDING) {
12390Sstevel@tonic-gate 			apic_ret();
12407282Smishra 		}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		if (status & AV_REMOTE_STATUS)	/* 1 = valid */
12437282Smishra 			countval = apic_reg_ops->apic_read(APIC_REMOTE_READ);
12440Sstevel@tonic-gate 		else {	/* 0 = invalid */
12450Sstevel@tonic-gate 			apic_remote_hrterr++;
12460Sstevel@tonic-gate 			/*
12470Sstevel@tonic-gate 			 * return last hrtime right now, will need more
12480Sstevel@tonic-gate 			 * testing if change to retry
12490Sstevel@tonic-gate 			 */
12500Sstevel@tonic-gate 			temp = apic_last_hrtime;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 			lock_clear(&apic_gethrtime_lock);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 			intr_restore(oflags);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			return (temp);
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 	if (countval > last_count_read)
12600Sstevel@tonic-gate 		countval = 0;
12610Sstevel@tonic-gate 	else
12620Sstevel@tonic-gate 		last_count_read = countval;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	elapsed_ticks = apic_hertz_count - countval;
12650Sstevel@tonic-gate 
12662992Sdmick 	curr_timeval = APIC_TICKS_TO_NSECS(elapsed_ticks);
12670Sstevel@tonic-gate 	temp = apic_nsec_since_boot + curr_timeval;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	if (apic_hrtime_stamp != old_hrtime_stamp) {	/* got an interrupt */
12700Sstevel@tonic-gate 		/* we might have clobbered last_count_read. Restore it */
12710Sstevel@tonic-gate 		last_count_read = apic_hertz_count;
12720Sstevel@tonic-gate 		goto gethrtime_again;
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	if (temp < apic_last_hrtime) {
12760Sstevel@tonic-gate 		/* return last hrtime if error occurs */
12770Sstevel@tonic-gate 		apic_hrtime_error++;
12780Sstevel@tonic-gate 		temp = apic_last_hrtime;
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 	else
12810Sstevel@tonic-gate 		apic_last_hrtime = temp;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	lock_clear(&apic_gethrtime_lock);
12840Sstevel@tonic-gate 	intr_restore(oflags);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	return (temp);
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate /* apic NMI handler */
12900Sstevel@tonic-gate /*ARGSUSED*/
12910Sstevel@tonic-gate static void
12925084Sjohnlev apic_nmi_intr(caddr_t arg, struct regs *rp)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	if (apic_shutdown_processors) {
12950Sstevel@tonic-gate 		apic_disable_local_apic();
12960Sstevel@tonic-gate 		return;
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 
12995084Sjohnlev 	apic_error |= APIC_ERR_NMI;
13005084Sjohnlev 
13015084Sjohnlev 	if (!lock_try(&apic_nmi_lock))
13025084Sjohnlev 		return;
13035084Sjohnlev 	apic_num_nmis++;
13045084Sjohnlev 
13055084Sjohnlev 	if (apic_kmdb_on_nmi && psm_debugger()) {
13065084Sjohnlev 		debug_enter("NMI received: entering kmdb\n");
13075084Sjohnlev 	} else if (apic_panic_on_nmi) {
13085084Sjohnlev 		/* Keep panic from entering kmdb. */
13095084Sjohnlev 		nopanicdebug = 1;
13105084Sjohnlev 		panic("NMI received\n");
13115084Sjohnlev 	} else {
13125084Sjohnlev 		/*
13135084Sjohnlev 		 * prom_printf is the best shot we have of something which is
13145084Sjohnlev 		 * problem free from high level/NMI type of interrupts
13155084Sjohnlev 		 */
13165084Sjohnlev 		prom_printf("NMI received\n");
13170Sstevel@tonic-gate 	}
13185084Sjohnlev 
13195084Sjohnlev 	lock_clear(&apic_nmi_lock);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate /*ARGSUSED*/
13230Sstevel@tonic-gate static int
13240Sstevel@tonic-gate apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
13250Sstevel@tonic-gate {
13263446Smrj 	return (apic_addspl_common(irqno, ipl, min_ipl, max_ipl));
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate static int
13300Sstevel@tonic-gate apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
13310Sstevel@tonic-gate {
13323446Smrj 	return (apic_delspl_common(irqno, ipl, min_ipl,  max_ipl));
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate static int
13360Sstevel@tonic-gate apic_post_cpu_start()
13370Sstevel@tonic-gate {
13386749Ssherrym 	int cpun;
1339*7986SSaurabh.Mishra@Sun.COM 	static int cpus_started = 1;
1340*7986SSaurabh.Mishra@Sun.COM 	struct psm_ops *pops = &apic_ops;
1341*7986SSaurabh.Mishra@Sun.COM 
1342*7986SSaurabh.Mishra@Sun.COM 	/* We know this CPU + BSP  started successfully. */
1343*7986SSaurabh.Mishra@Sun.COM 	cpus_started++;
13440Sstevel@tonic-gate 
13457798SSaurabh.Mishra@Sun.COM 	/*
13467798SSaurabh.Mishra@Sun.COM 	 * On BSP we would have enabled X2APIC, if supported by processor,
13477798SSaurabh.Mishra@Sun.COM 	 * in acpi_probe(), but on AP we do it here.
1348*7986SSaurabh.Mishra@Sun.COM 	 *
1349*7986SSaurabh.Mishra@Sun.COM 	 * We enable X2APIC mode only if BSP is running in X2APIC & the
1350*7986SSaurabh.Mishra@Sun.COM 	 * local APIC mode of the current CPU is MMIO (xAPIC).
13517798SSaurabh.Mishra@Sun.COM 	 */
1352*7986SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_X2APIC && apic_detect_x2apic() &&
1353*7986SSaurabh.Mishra@Sun.COM 	    apic_local_mode() == LOCAL_APIC) {
13547798SSaurabh.Mishra@Sun.COM 		apic_enable_x2apic();
13557798SSaurabh.Mishra@Sun.COM 	}
13567798SSaurabh.Mishra@Sun.COM 
1357*7986SSaurabh.Mishra@Sun.COM 	/*
1358*7986SSaurabh.Mishra@Sun.COM 	 * We change psm_send_ipi and send_dirintf only if Solaris
1359*7986SSaurabh.Mishra@Sun.COM 	 * is booted in kmdb & the current CPU is the last CPU being
1360*7986SSaurabh.Mishra@Sun.COM 	 * brought up. We don't need to do anything if Solaris is running
1361*7986SSaurabh.Mishra@Sun.COM 	 * in MMIO mode (xAPIC).
1362*7986SSaurabh.Mishra@Sun.COM 	 */
1363*7986SSaurabh.Mishra@Sun.COM 	if ((boothowto & RB_DEBUG) &&
1364*7986SSaurabh.Mishra@Sun.COM 	    (cpus_started == boot_ncpus || cpus_started == apic_nproc) &&
1365*7986SSaurabh.Mishra@Sun.COM 	    apic_mode == LOCAL_X2APIC) {
1366*7986SSaurabh.Mishra@Sun.COM 		/*
1367*7986SSaurabh.Mishra@Sun.COM 		 * We no longer need help from apic_common_send_ipi()
1368*7986SSaurabh.Mishra@Sun.COM 		 * since we will not start any more CPUs.
1369*7986SSaurabh.Mishra@Sun.COM 		 *
1370*7986SSaurabh.Mishra@Sun.COM 		 * We will need to revisit this if we start supporting
1371*7986SSaurabh.Mishra@Sun.COM 		 * hot-plugging of CPUs.
1372*7986SSaurabh.Mishra@Sun.COM 		 */
1373*7986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi = x2apic_send_ipi;
1374*7986SSaurabh.Mishra@Sun.COM 		send_dirintf = pops->psm_send_ipi;
1375*7986SSaurabh.Mishra@Sun.COM 	}
1376*7986SSaurabh.Mishra@Sun.COM 
13777798SSaurabh.Mishra@Sun.COM 	splx(ipltospl(LOCK_LEVEL));
13780Sstevel@tonic-gate 	apic_init_intr();
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	/*
13810Sstevel@tonic-gate 	 * since some systems don't enable the internal cache on the non-boot
13820Sstevel@tonic-gate 	 * cpus, so we have to enable them here
13830Sstevel@tonic-gate 	 */
13843446Smrj 	setcr0(getcr0() & ~(CR0_CD | CR0_NW));
13850Sstevel@tonic-gate 
13867798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
13877798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
13887798SSaurabh.Mishra@Sun.COM #else
13897798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC)
13907798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
13917798SSaurabh.Mishra@Sun.COM #endif	/* DEBUG */
13920Sstevel@tonic-gate 
13937113Sbholler 	/*
13947113Sbholler 	 * We may be booting, or resuming from suspend; aci_status will
13957113Sbholler 	 * be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
13967113Sbholler 	 * APIC_CPU_ONLINE flag here rather than setting aci_status completely.
13977113Sbholler 	 */
13980Sstevel@tonic-gate 	cpun = psm_get_cpu_id();
13997113Sbholler 	apic_cpus[cpun].aci_status |= APIC_CPU_ONLINE;
14000Sstevel@tonic-gate 
14017282Smishra 	apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
14020Sstevel@tonic-gate 	return (PSM_SUCCESS);
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate processorid_t
14060Sstevel@tonic-gate apic_get_next_processorid(processorid_t cpu_id)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	int i;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	if (cpu_id == -1)
14120Sstevel@tonic-gate 		return ((processorid_t)0);
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	for (i = cpu_id + 1; i < NCPU; i++) {
14152006Sandrei 		if (CPU_IN_SET(apic_cpumask, i))
14160Sstevel@tonic-gate 			return (i);
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	return ((processorid_t)-1);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * type == -1 indicates it is an internal request. Do not change
14250Sstevel@tonic-gate  * resv_vector for these requests
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate static int
14280Sstevel@tonic-gate apic_get_ipivect(int ipl, int type)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	uchar_t vector;
14310Sstevel@tonic-gate 	int irq;
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	if (irq = apic_allocate_irq(APIC_VECTOR(ipl))) {
14340Sstevel@tonic-gate 		if (vector = apic_allocate_vector(ipl, irq, 1)) {
14350Sstevel@tonic-gate 			apic_irq_table[irq]->airq_mps_intr_index =
14360Sstevel@tonic-gate 			    RESERVE_INDEX;
14370Sstevel@tonic-gate 			apic_irq_table[irq]->airq_vector = vector;
14380Sstevel@tonic-gate 			if (type != -1) {
14390Sstevel@tonic-gate 				apic_resv_vector[ipl] = vector;
14400Sstevel@tonic-gate 			}
14410Sstevel@tonic-gate 			return (irq);
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 	apic_error |= APIC_ERR_GET_IPIVECT_FAIL;
14450Sstevel@tonic-gate 	return (-1);	/* shouldn't happen */
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate static int
14490Sstevel@tonic-gate apic_getclkirq(int ipl)
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate 	int	irq;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if ((irq = apic_get_ipivect(ipl, -1)) == -1)
14540Sstevel@tonic-gate 		return (-1);
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * Note the vector in apic_clkvect for per clock handling.
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT;
14590Sstevel@tonic-gate 	APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n",
14600Sstevel@tonic-gate 	    apic_clkvect));
14610Sstevel@tonic-gate 	return (irq);
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate 
14642992Sdmick 
14650Sstevel@tonic-gate /*
14660Sstevel@tonic-gate  * Return the number of APIC clock ticks elapsed for 8245 to decrement
14670Sstevel@tonic-gate  * (APIC_TIME_COUNT + pit_ticks_adj) ticks.
14680Sstevel@tonic-gate  */
14690Sstevel@tonic-gate static uint_t
14700Sstevel@tonic-gate apic_calibrate(volatile uint32_t *addr, uint16_t *pit_ticks_adj)
14710Sstevel@tonic-gate {
14720Sstevel@tonic-gate 	uint8_t		pit_tick_lo;
14730Sstevel@tonic-gate 	uint16_t	pit_tick, target_pit_tick;
14740Sstevel@tonic-gate 	uint32_t	start_apic_tick, end_apic_tick;
14753446Smrj 	ulong_t		iflag;
14767282Smishra 	uint32_t	reg;
14770Sstevel@tonic-gate 
14787282Smishra 	reg = addr + APIC_CURR_COUNT - apicadr;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	iflag = intr_clear();
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	do {
14830Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
14840Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
14850Sstevel@tonic-gate 	} while (pit_tick < APIC_TIME_MIN ||
14860Sstevel@tonic-gate 	    pit_tick_lo <= APIC_LB_MIN || pit_tick_lo >= APIC_LB_MAX);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * Wait for the 8254 to decrement by 5 ticks to ensure
14900Sstevel@tonic-gate 	 * we didn't start in the middle of a tick.
14910Sstevel@tonic-gate 	 * Compare with 0x10 for the wrap around case.
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	target_pit_tick = pit_tick - 5;
14940Sstevel@tonic-gate 	do {
14950Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
14960Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
14970Sstevel@tonic-gate 	} while (pit_tick > target_pit_tick || pit_tick_lo < 0x10);
14980Sstevel@tonic-gate 
14997282Smishra 	start_apic_tick = apic_reg_ops->apic_read(reg);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Wait for the 8254 to decrement by
15030Sstevel@tonic-gate 	 * (APIC_TIME_COUNT + pit_ticks_adj) ticks
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	target_pit_tick = pit_tick - APIC_TIME_COUNT;
15060Sstevel@tonic-gate 	do {
15070Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
15080Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
15090Sstevel@tonic-gate 	} while (pit_tick > target_pit_tick || pit_tick_lo < 0x10);
15100Sstevel@tonic-gate 
15117282Smishra 	end_apic_tick = apic_reg_ops->apic_read(reg);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	*pit_ticks_adj = target_pit_tick - pit_tick;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	intr_restore(iflag);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	return (start_apic_tick - end_apic_tick);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate /*
15210Sstevel@tonic-gate  * Initialise the APIC timer on the local APIC of CPU 0 to the desired
15220Sstevel@tonic-gate  * frequency.  Note at this stage in the boot sequence, the boot processor
15230Sstevel@tonic-gate  * is the only active processor.
15240Sstevel@tonic-gate  * hertz value of 0 indicates a one-shot mode request.  In this case
15250Sstevel@tonic-gate  * the function returns the resolution (in nanoseconds) for the hardware
15260Sstevel@tonic-gate  * timer interrupt.  If one-shot mode capability is not available,
15270Sstevel@tonic-gate  * the return value will be 0. apic_enable_oneshot is a global switch
15280Sstevel@tonic-gate  * for disabling the functionality.
15290Sstevel@tonic-gate  * A non-zero positive value for hertz indicates a periodic mode request.
15300Sstevel@tonic-gate  * In this case the hardware will be programmed to generate clock interrupts
15310Sstevel@tonic-gate  * at hertz frequency and returns the resolution of interrupts in
15320Sstevel@tonic-gate  * nanosecond.
15330Sstevel@tonic-gate  */
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate static int
15360Sstevel@tonic-gate apic_clkinit(int hertz)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	uint_t		apic_ticks = 0;
15392992Sdmick 	uint_t		pit_ticks;
15400Sstevel@tonic-gate 	int		ret;
15410Sstevel@tonic-gate 	uint16_t	pit_ticks_adj;
15420Sstevel@tonic-gate 	static int	firsttime = 1;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (firsttime) {
15452992Sdmick 		/* first time calibrate on CPU0 only */
15462992Sdmick 
15477282Smishra 		apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
15487282Smishra 		apic_reg_ops->apic_write(APIC_INIT_COUNT, APIC_MAXVAL);
15490Sstevel@tonic-gate 		apic_ticks = apic_calibrate(apicadr, &pit_ticks_adj);
15500Sstevel@tonic-gate 
15512992Sdmick 		/* total number of PIT ticks corresponding to apic_ticks */
15522992Sdmick 		pit_ticks = APIC_TIME_COUNT + pit_ticks_adj;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 		/*
15550Sstevel@tonic-gate 		 * Determine the number of nanoseconds per APIC clock tick
15560Sstevel@tonic-gate 		 * and then determine how many APIC ticks to interrupt at the
15570Sstevel@tonic-gate 		 * desired frequency
15582992Sdmick 		 * apic_ticks / (pitticks / PIT_HZ) = apic_ticks_per_s
15592992Sdmick 		 * (apic_ticks * PIT_HZ) / pitticks = apic_ticks_per_s
15602992Sdmick 		 * apic_ticks_per_ns = (apic_ticks * PIT_HZ) / (pitticks * 10^9)
15613446Smrj 		 * pic_ticks_per_SFns =
15622992Sdmick 		 *   (SF * apic_ticks * PIT_HZ) / (pitticks * 10^9)
15630Sstevel@tonic-gate 		 */
15642992Sdmick 		apic_ticks_per_SFnsecs =
15652992Sdmick 		    ((SF * apic_ticks * PIT_HZ) /
15662992Sdmick 		    ((uint64_t)pit_ticks * NANOSEC));
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		/* the interval timer initial count is 32 bit max */
15692992Sdmick 		apic_nsec_max = APIC_TICKS_TO_NSECS(APIC_MAXVAL);
15700Sstevel@tonic-gate 		firsttime = 0;
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	if (hertz != 0) {
15740Sstevel@tonic-gate 		/* periodic */
15750Sstevel@tonic-gate 		apic_nsec_per_intr = NANOSEC / hertz;
15762992Sdmick 		apic_hertz_count = APIC_NSECS_TO_TICKS(apic_nsec_per_intr);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	apic_int_busy_mark = (apic_int_busy_mark *
15800Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
15810Sstevel@tonic-gate 	apic_int_free_mark = (apic_int_free_mark *
15820Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
15830Sstevel@tonic-gate 	apic_diff_for_redistribution = (apic_diff_for_redistribution *
15840Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if (hertz == 0) {
15870Sstevel@tonic-gate 		/* requested one_shot */
15885084Sjohnlev 		if (!tsc_gethrtime_enable || !apic_oneshot_enable)
15890Sstevel@tonic-gate 			return (0);
15900Sstevel@tonic-gate 		apic_oneshot = 1;
15912992Sdmick 		ret = (int)APIC_TICKS_TO_NSECS(1);
15920Sstevel@tonic-gate 	} else {
15930Sstevel@tonic-gate 		/* program the local APIC to interrupt at the given frequency */
15947282Smishra 		apic_reg_ops->apic_write(APIC_INIT_COUNT, apic_hertz_count);
15957282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
15967282Smishra 		    (apic_clkvect + APIC_BASE_VECT) | AV_TIME);
15970Sstevel@tonic-gate 		apic_oneshot = 0;
15980Sstevel@tonic-gate 		ret = NANOSEC / hertz;
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	return (ret);
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate /*
16060Sstevel@tonic-gate  * apic_preshutdown:
16070Sstevel@tonic-gate  * Called early in shutdown whilst we can still access filesystems to do
16080Sstevel@tonic-gate  * things like loading modules which will be required to complete shutdown
16090Sstevel@tonic-gate  * after filesystems are all unmounted.
16100Sstevel@tonic-gate  */
16110Sstevel@tonic-gate static void
16120Sstevel@tonic-gate apic_preshutdown(int cmd, int fcn)
16130Sstevel@tonic-gate {
16140Sstevel@tonic-gate 	APIC_VERBOSE_POWEROFF(("apic_preshutdown(%d,%d); m=%d a=%d\n",
16150Sstevel@tonic-gate 	    cmd, fcn, apic_poweroff_method, apic_enable_acpi));
16160Sstevel@tonic-gate 
16175295Srandyf 	if ((cmd != A_SHUTDOWN) || (fcn != AD_POWEROFF)) {
16185295Srandyf 		return;
16195295Srandyf 	}
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate static void
16230Sstevel@tonic-gate apic_shutdown(int cmd, int fcn)
16240Sstevel@tonic-gate {
16253446Smrj 	int restarts, attempts;
16263446Smrj 	int i;
16270Sstevel@tonic-gate 	uchar_t	byte;
16283446Smrj 	ulong_t iflag;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/* Send NMI to all CPUs except self to do per processor shutdown */
16310Sstevel@tonic-gate 	iflag = intr_clear();
16327798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
16337798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
16347798SSaurabh.Mishra@Sun.COM #else
16357798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC)
16367798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
16377798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
16380Sstevel@tonic-gate 	apic_shutdown_processors = 1;
16397282Smishra 	apic_reg_ops->apic_write(APIC_INT_CMD1,
16407282Smishra 	    AV_NMI | AV_LEVEL | AV_SH_ALL_EXCSELF);
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	/* restore cmos shutdown byte before reboot */
16430Sstevel@tonic-gate 	if (apic_cmos_ssb_set) {
16440Sstevel@tonic-gate 		outb(CMOS_ADDR, SSB);
16450Sstevel@tonic-gate 		outb(CMOS_DATA, 0);
16460Sstevel@tonic-gate 	}
16473446Smrj 
16483446Smrj 	ioapic_disable_redirection();
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	/*	disable apic mode if imcr present	*/
16510Sstevel@tonic-gate 	if (apic_imcrp) {
16520Sstevel@tonic-gate 		outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
16530Sstevel@tonic-gate 		outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_PIC);
16540Sstevel@tonic-gate 	}
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	apic_disable_local_apic();
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	intr_restore(iflag);
16590Sstevel@tonic-gate 
16603472Smyers 	/* remainder of function is for shutdown cases only */
16613472Smyers 	if (cmd != A_SHUTDOWN)
16620Sstevel@tonic-gate 		return;
16633472Smyers 
16644189Smyers 	/*
16654189Smyers 	 * Switch system back into Legacy-Mode if using ACPI and
16664189Smyers 	 * not powering-off.  Some BIOSes need to remain in ACPI-mode
16674189Smyers 	 * for power-off to succeed (Dell Dimension 4600)
16687656SSherry.Moore@Sun.COM 	 * Do not disable ACPI while doing fastreboot
16694189Smyers 	 */
16707656SSherry.Moore@Sun.COM 	if (apic_enable_acpi && fcn != AD_POWEROFF && fcn != AD_FASTREBOOT)
16713472Smyers 		(void) AcpiDisable();
16723472Smyers 
16737656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
16747656SSherry.Moore@Sun.COM 		apicadr[APIC_INT_CMD1] = AV_ASSERT | AV_RESET |
16757656SSherry.Moore@Sun.COM 		    AV_SH_ALL_EXCSELF;
16767656SSherry.Moore@Sun.COM 	}
16777656SSherry.Moore@Sun.COM 
16783472Smyers 	/* remainder of function is for shutdown+poweroff case only */
16793472Smyers 	if (fcn != AD_POWEROFF)
16803472Smyers 		return;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	switch (apic_poweroff_method) {
16830Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_RTC:
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 			/* select the extended NVRAM bank in the RTC */
16860Sstevel@tonic-gate 			outb(CMOS_ADDR, RTC_REGA);
16870Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
16880Sstevel@tonic-gate 			outb(CMOS_DATA, (byte | EXT_BANK));
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			outb(CMOS_ADDR, PFR_REG);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 			/* for Predator must toggle the PAB bit */
16930Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 			/*
16960Sstevel@tonic-gate 			 * clear power active bar, wakeup alarm and
16970Sstevel@tonic-gate 			 * kickstart
16980Sstevel@tonic-gate 			 */
16990Sstevel@tonic-gate 			byte &= ~(PAB_CBIT | WF_FLAG | KS_FLAG);
17000Sstevel@tonic-gate 			outb(CMOS_DATA, byte);
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 			/* delay before next write */
17030Sstevel@tonic-gate 			drv_usecwait(1000);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 			/* for S40 the following would suffice */
17060Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 			/* power active bar control bit */
17090Sstevel@tonic-gate 			byte |= PAB_CBIT;
17100Sstevel@tonic-gate 			outb(CMOS_DATA, byte);
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 			break;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_ASPEN_BMC:
17150Sstevel@tonic-gate 			restarts = 0;
17160Sstevel@tonic-gate restart_aspen_bmc:
17170Sstevel@tonic-gate 			if (++restarts == 3)
17180Sstevel@tonic-gate 				break;
17190Sstevel@tonic-gate 			attempts = 0;
17200Sstevel@tonic-gate 			do {
17210Sstevel@tonic-gate 				byte = inb(MISMIC_FLAG_REGISTER);
17220Sstevel@tonic-gate 				byte &= MISMIC_BUSY_MASK;
17230Sstevel@tonic-gate 				if (byte != 0) {
17240Sstevel@tonic-gate 					drv_usecwait(1000);
17250Sstevel@tonic-gate 					if (attempts >= 3)
17260Sstevel@tonic-gate 						goto restart_aspen_bmc;
17270Sstevel@tonic-gate 					++attempts;
17280Sstevel@tonic-gate 				}
17290Sstevel@tonic-gate 			} while (byte != 0);
17300Sstevel@tonic-gate 			outb(MISMIC_CNTL_REGISTER, CC_SMS_GET_STATUS);
17310Sstevel@tonic-gate 			byte = inb(MISMIC_FLAG_REGISTER);
17320Sstevel@tonic-gate 			byte |= 0x1;
17330Sstevel@tonic-gate 			outb(MISMIC_FLAG_REGISTER, byte);
17340Sstevel@tonic-gate 			i = 0;
17350Sstevel@tonic-gate 			for (; i < (sizeof (aspen_bmc)/sizeof (aspen_bmc[0]));
17360Sstevel@tonic-gate 			    i++) {
17370Sstevel@tonic-gate 				attempts = 0;
17380Sstevel@tonic-gate 				do {
17390Sstevel@tonic-gate 					byte = inb(MISMIC_FLAG_REGISTER);
17400Sstevel@tonic-gate 					byte &= MISMIC_BUSY_MASK;
17410Sstevel@tonic-gate 					if (byte != 0) {
17420Sstevel@tonic-gate 						drv_usecwait(1000);
17430Sstevel@tonic-gate 						if (attempts >= 3)
17440Sstevel@tonic-gate 							goto restart_aspen_bmc;
17450Sstevel@tonic-gate 						++attempts;
17460Sstevel@tonic-gate 					}
17470Sstevel@tonic-gate 				} while (byte != 0);
17480Sstevel@tonic-gate 				outb(MISMIC_CNTL_REGISTER, aspen_bmc[i].cntl);
17490Sstevel@tonic-gate 				outb(MISMIC_DATA_REGISTER, aspen_bmc[i].data);
17500Sstevel@tonic-gate 				byte = inb(MISMIC_FLAG_REGISTER);
17510Sstevel@tonic-gate 				byte |= 0x1;
17520Sstevel@tonic-gate 				outb(MISMIC_FLAG_REGISTER, byte);
17530Sstevel@tonic-gate 			}
17540Sstevel@tonic-gate 			break;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_SITKA_BMC:
17570Sstevel@tonic-gate 			restarts = 0;
17580Sstevel@tonic-gate restart_sitka_bmc:
17590Sstevel@tonic-gate 			if (++restarts == 3)
17600Sstevel@tonic-gate 				break;
17610Sstevel@tonic-gate 			attempts = 0;
17620Sstevel@tonic-gate 			do {
17630Sstevel@tonic-gate 				byte = inb(SMS_STATUS_REGISTER);
17640Sstevel@tonic-gate 				byte &= SMS_STATE_MASK;
17650Sstevel@tonic-gate 				if ((byte == SMS_READ_STATE) ||
17660Sstevel@tonic-gate 				    (byte == SMS_WRITE_STATE)) {
17670Sstevel@tonic-gate 					drv_usecwait(1000);
17680Sstevel@tonic-gate 					if (attempts >= 3)
17690Sstevel@tonic-gate 						goto restart_sitka_bmc;
17700Sstevel@tonic-gate 					++attempts;
17710Sstevel@tonic-gate 				}
17720Sstevel@tonic-gate 			} while ((byte == SMS_READ_STATE) ||
17730Sstevel@tonic-gate 			    (byte == SMS_WRITE_STATE));
17740Sstevel@tonic-gate 			outb(SMS_COMMAND_REGISTER, SMS_GET_STATUS);
17750Sstevel@tonic-gate 			i = 0;
17760Sstevel@tonic-gate 			for (; i < (sizeof (sitka_bmc)/sizeof (sitka_bmc[0]));
17770Sstevel@tonic-gate 			    i++) {
17780Sstevel@tonic-gate 				attempts = 0;
17790Sstevel@tonic-gate 				do {
17800Sstevel@tonic-gate 					byte = inb(SMS_STATUS_REGISTER);
17810Sstevel@tonic-gate 					byte &= SMS_IBF_MASK;
17820Sstevel@tonic-gate 					if (byte != 0) {
17830Sstevel@tonic-gate 						drv_usecwait(1000);
17840Sstevel@tonic-gate 						if (attempts >= 3)
17850Sstevel@tonic-gate 							goto restart_sitka_bmc;
17860Sstevel@tonic-gate 						++attempts;
17870Sstevel@tonic-gate 					}
17880Sstevel@tonic-gate 				} while (byte != 0);
17890Sstevel@tonic-gate 				outb(sitka_bmc[i].port, sitka_bmc[i].data);
17900Sstevel@tonic-gate 			}
17910Sstevel@tonic-gate 			break;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		case APIC_POWEROFF_NONE:
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 			/* If no APIC direct method, we will try using ACPI */
17960Sstevel@tonic-gate 			if (apic_enable_acpi) {
17970Sstevel@tonic-gate 				if (acpi_poweroff() == 1)
17980Sstevel@tonic-gate 					return;
17990Sstevel@tonic-gate 			} else
18000Sstevel@tonic-gate 				return;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 			break;
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 	/*
18050Sstevel@tonic-gate 	 * Wait a limited time here for power to go off.
18060Sstevel@tonic-gate 	 * If the power does not go off, then there was a
18070Sstevel@tonic-gate 	 * problem and we should continue to the halt which
18080Sstevel@tonic-gate 	 * prints a message for the user to press a key to
18090Sstevel@tonic-gate 	 * reboot.
18100Sstevel@tonic-gate 	 */
18110Sstevel@tonic-gate 	drv_usecwait(7000000); /* wait seven seconds */
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate }
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate /*
18160Sstevel@tonic-gate  * Try and disable all interrupts. We just assign interrupts to other
18170Sstevel@tonic-gate  * processors based on policy. If any were bound by user request, we
18180Sstevel@tonic-gate  * let them continue and return failure. We do not bother to check
18190Sstevel@tonic-gate  * for cache affinity while rebinding.
18200Sstevel@tonic-gate  */
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate static int
18230Sstevel@tonic-gate apic_disable_intr(processorid_t cpun)
18240Sstevel@tonic-gate {
18253446Smrj 	int bind_cpu = 0, i, hardbound = 0;
18260Sstevel@tonic-gate 	apic_irq_t *irq_ptr;
18273446Smrj 	ulong_t iflag;
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	iflag = intr_clear();
18300Sstevel@tonic-gate 	lock_set(&apic_ioapic_lock);
18313139Ssethg 
18323139Ssethg 	for (i = 0; i <= APIC_MAX_VECTOR; i++) {
18333139Ssethg 		if (apic_reprogram_info[i].done == B_FALSE) {
18343139Ssethg 			if (apic_reprogram_info[i].bindcpu == cpun) {
18353139Ssethg 				/*
18363139Ssethg 				 * CPU is busy -- it's the target of
18373139Ssethg 				 * a pending reprogramming attempt
18383139Ssethg 				 */
18393139Ssethg 				lock_clear(&apic_ioapic_lock);
18403139Ssethg 				intr_restore(iflag);
18413139Ssethg 				return (PSM_FAILURE);
18423139Ssethg 			}
18433139Ssethg 		}
18443139Ssethg 	}
18453139Ssethg 
18460Sstevel@tonic-gate 	apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE;
18473139Ssethg 
18480Sstevel@tonic-gate 	apic_cpus[cpun].aci_curipl = 0;
18493139Ssethg 
18500Sstevel@tonic-gate 	i = apic_min_device_irq;
18510Sstevel@tonic-gate 	for (; i <= apic_max_device_irq; i++) {
18520Sstevel@tonic-gate 		/*
18530Sstevel@tonic-gate 		 * If there are bound interrupts on this cpu, then
18540Sstevel@tonic-gate 		 * rebind them to other processors.
18550Sstevel@tonic-gate 		 */
18560Sstevel@tonic-gate 		if ((irq_ptr = apic_irq_table[i]) != NULL) {
18570Sstevel@tonic-gate 			ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) ||
18580Sstevel@tonic-gate 			    (irq_ptr->airq_temp_cpu == IRQ_UNINIT) ||
18590Sstevel@tonic-gate 			    ((irq_ptr->airq_temp_cpu & ~IRQ_USER_BOUND) <
18600Sstevel@tonic-gate 			    apic_nproc));
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 			if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) {
18630Sstevel@tonic-gate 				hardbound = 1;
18640Sstevel@tonic-gate 				continue;
18650Sstevel@tonic-gate 			}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 			if (irq_ptr->airq_temp_cpu == cpun) {
18680Sstevel@tonic-gate 				do {
18693446Smrj 					bind_cpu = apic_next_bind_cpu++;
18700Sstevel@tonic-gate 					if (bind_cpu >= apic_nproc) {
18710Sstevel@tonic-gate 						apic_next_bind_cpu = 1;
18720Sstevel@tonic-gate 						bind_cpu = 0;
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 					}
18753139Ssethg 				} while (apic_rebind_all(irq_ptr, bind_cpu));
18760Sstevel@tonic-gate 			}
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	}
18793139Ssethg 
18803139Ssethg 	lock_clear(&apic_ioapic_lock);
18813139Ssethg 	intr_restore(iflag);
18823139Ssethg 
18830Sstevel@tonic-gate 	if (hardbound) {
18840Sstevel@tonic-gate 		cmn_err(CE_WARN, "Could not disable interrupts on %d"
18850Sstevel@tonic-gate 		    "due to user bound interrupts", cpun);
18860Sstevel@tonic-gate 		return (PSM_FAILURE);
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 	else
18890Sstevel@tonic-gate 		return (PSM_SUCCESS);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18927113Sbholler /*
18937113Sbholler  * Bind interrupts to the CPU's local APIC.
18947113Sbholler  * Interrupts should not be bound to a CPU's local APIC until the CPU
18957113Sbholler  * is ready to receive interrupts.
18967113Sbholler  */
18970Sstevel@tonic-gate static void
18980Sstevel@tonic-gate apic_enable_intr(processorid_t cpun)
18990Sstevel@tonic-gate {
19003446Smrj 	int	i;
19010Sstevel@tonic-gate 	apic_irq_t *irq_ptr;
19023446Smrj 	ulong_t iflag;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	iflag = intr_clear();
19050Sstevel@tonic-gate 	lock_set(&apic_ioapic_lock);
19063139Ssethg 
19070Sstevel@tonic-gate 	apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	i = apic_min_device_irq;
19100Sstevel@tonic-gate 	for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
19110Sstevel@tonic-gate 		if ((irq_ptr = apic_irq_table[i]) != NULL) {
19120Sstevel@tonic-gate 			if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) {
19130Sstevel@tonic-gate 				(void) apic_rebind_all(irq_ptr,
19143139Ssethg 				    irq_ptr->airq_cpu);
19150Sstevel@tonic-gate 			}
19160Sstevel@tonic-gate 		}
19170Sstevel@tonic-gate 	}
19183139Ssethg 
19193139Ssethg 	lock_clear(&apic_ioapic_lock);
19203139Ssethg 	intr_restore(iflag);
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate  * This function will reprogram the timer.
19260Sstevel@tonic-gate  *
19270Sstevel@tonic-gate  * When in oneshot mode the argument is the absolute time in future to
19280Sstevel@tonic-gate  * generate the interrupt at.
19290Sstevel@tonic-gate  *
19300Sstevel@tonic-gate  * When in periodic mode, the argument is the interval at which the
19310Sstevel@tonic-gate  * interrupts should be generated. There is no need to support the periodic
19320Sstevel@tonic-gate  * mode timer change at this time.
19330Sstevel@tonic-gate  */
19340Sstevel@tonic-gate static void
19350Sstevel@tonic-gate apic_timer_reprogram(hrtime_t time)
19360Sstevel@tonic-gate {
19370Sstevel@tonic-gate 	hrtime_t now;
19380Sstevel@tonic-gate 	uint_t ticks;
19393446Smrj 	int64_t delta;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/*
19420Sstevel@tonic-gate 	 * We should be called from high PIL context (CBE_HIGH_PIL),
19430Sstevel@tonic-gate 	 * so kpreempt is disabled.
19440Sstevel@tonic-gate 	 */
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	if (!apic_oneshot) {
19470Sstevel@tonic-gate 		/* time is the interval for periodic mode */
19482992Sdmick 		ticks = APIC_NSECS_TO_TICKS(time);
19490Sstevel@tonic-gate 	} else {
19500Sstevel@tonic-gate 		/* one shot mode */
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 		now = gethrtime();
19532992Sdmick 		delta = time - now;
19542992Sdmick 
19552992Sdmick 		if (delta <= 0) {
19560Sstevel@tonic-gate 			/*
19570Sstevel@tonic-gate 			 * requested to generate an interrupt in the past
19580Sstevel@tonic-gate 			 * generate an interrupt as soon as possible
19590Sstevel@tonic-gate 			 */
19600Sstevel@tonic-gate 			ticks = apic_min_timer_ticks;
19612992Sdmick 		} else if (delta > apic_nsec_max) {
19620Sstevel@tonic-gate 			/*
19630Sstevel@tonic-gate 			 * requested to generate an interrupt at a time
19640Sstevel@tonic-gate 			 * further than what we are capable of. Set to max
19650Sstevel@tonic-gate 			 * the hardware can handle
19660Sstevel@tonic-gate 			 */
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 			ticks = APIC_MAXVAL;
19690Sstevel@tonic-gate #ifdef DEBUG
19700Sstevel@tonic-gate 			cmn_err(CE_CONT, "apic_timer_reprogram, request at"
19710Sstevel@tonic-gate 			    "  %lld  too far in future, current time"
19720Sstevel@tonic-gate 			    "  %lld \n", time, now);
19732992Sdmick #endif
19740Sstevel@tonic-gate 		} else
19752992Sdmick 			ticks = APIC_NSECS_TO_TICKS(delta);
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	if (ticks < apic_min_timer_ticks)
19790Sstevel@tonic-gate 		ticks = apic_min_timer_ticks;
19800Sstevel@tonic-gate 
19817282Smishra 	apic_reg_ops->apic_write(APIC_INIT_COUNT, ticks);
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate /*
19850Sstevel@tonic-gate  * This function will enable timer interrupts.
19860Sstevel@tonic-gate  */
19870Sstevel@tonic-gate static void
19880Sstevel@tonic-gate apic_timer_enable(void)
19890Sstevel@tonic-gate {
19900Sstevel@tonic-gate 	/*
19910Sstevel@tonic-gate 	 * We should be Called from high PIL context (CBE_HIGH_PIL),
19920Sstevel@tonic-gate 	 * so kpreempt is disabled.
19930Sstevel@tonic-gate 	 */
19940Sstevel@tonic-gate 
19957282Smishra 	if (!apic_oneshot) {
19967282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
19977282Smishra 		    (apic_clkvect + APIC_BASE_VECT) | AV_TIME);
19987282Smishra 	} else {
19990Sstevel@tonic-gate 		/* one shot */
20007282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
20017282Smishra 		    (apic_clkvect + APIC_BASE_VECT));
20020Sstevel@tonic-gate 	}
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate /*
20060Sstevel@tonic-gate  * This function will disable timer interrupts.
20070Sstevel@tonic-gate  */
20080Sstevel@tonic-gate static void
20090Sstevel@tonic-gate apic_timer_disable(void)
20100Sstevel@tonic-gate {
20110Sstevel@tonic-gate 	/*
20120Sstevel@tonic-gate 	 * We should be Called from high PIL context (CBE_HIGH_PIL),
20130Sstevel@tonic-gate 	 * so kpreempt is disabled.
20140Sstevel@tonic-gate 	 */
20157282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
20167282Smishra 	    (apic_clkvect + APIC_BASE_VECT) | AV_MASK);
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 
20205107Seota ddi_periodic_t apic_periodic_id;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20235107Seota  * If this module needs a periodic handler for the interrupt distribution, it
20245107Seota  * can be added here. The argument to the periodic handler is not currently
20255107Seota  * used, but is reserved for future.
20260Sstevel@tonic-gate  */
20270Sstevel@tonic-gate static void
20280Sstevel@tonic-gate apic_post_cyclic_setup(void *arg)
20290Sstevel@tonic-gate {
20300Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
20310Sstevel@tonic-gate 	/* cpu_lock is held */
20325107Seota 	/* set up a periodic handler for intr redistribution */
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	/*
20350Sstevel@tonic-gate 	 * In peridoc mode intr redistribution processing is done in
20360Sstevel@tonic-gate 	 * apic_intr_enter during clk intr processing
20370Sstevel@tonic-gate 	 */
20380Sstevel@tonic-gate 	if (!apic_oneshot)
20390Sstevel@tonic-gate 		return;
20405107Seota 	/*
20415107Seota 	 * Register a periodical handler for the redistribution processing.
20425107Seota 	 * On X86, CY_LOW_LEVEL is mapped to the level 2 interrupt, so
20435107Seota 	 * DDI_IPL_2 should be passed to ddi_periodic_add() here.
20445107Seota 	 */
20455107Seota 	apic_periodic_id = ddi_periodic_add(
20465107Seota 	    (void (*)(void *))apic_redistribute_compute, NULL,
20475107Seota 	    apic_redistribute_sample_interval, DDI_IPL_2);
20480Sstevel@tonic-gate }
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate static void
20510Sstevel@tonic-gate apic_redistribute_compute(void)
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate 	int	i, j, max_busy;
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	if (apic_enable_dynamic_migration) {
20560Sstevel@tonic-gate 		if (++apic_nticks == apic_sample_factor_redistribution) {
20570Sstevel@tonic-gate 			/*
20580Sstevel@tonic-gate 			 * Time to call apic_intr_redistribute().
20590Sstevel@tonic-gate 			 * reset apic_nticks. This will cause max_busy
20600Sstevel@tonic-gate 			 * to be calculated below and if it is more than
20610Sstevel@tonic-gate 			 * apic_int_busy, we will do the whole thing
20620Sstevel@tonic-gate 			 */
20630Sstevel@tonic-gate 			apic_nticks = 0;
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 		max_busy = 0;
20660Sstevel@tonic-gate 		for (i = 0; i < apic_nproc; i++) {
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 			/*
20690Sstevel@tonic-gate 			 * Check if curipl is non zero & if ISR is in
20700Sstevel@tonic-gate 			 * progress
20710Sstevel@tonic-gate 			 */
20720Sstevel@tonic-gate 			if (((j = apic_cpus[i].aci_curipl) != 0) &&
20730Sstevel@tonic-gate 			    (apic_cpus[i].aci_ISR_in_progress & (1 << j))) {
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 				int	irq;
20760Sstevel@tonic-gate 				apic_cpus[i].aci_busy++;
20770Sstevel@tonic-gate 				irq = apic_cpus[i].aci_current[j];
20780Sstevel@tonic-gate 				apic_irq_table[irq]->airq_busy++;
20790Sstevel@tonic-gate 			}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 			if (!apic_nticks &&
20820Sstevel@tonic-gate 			    (apic_cpus[i].aci_busy > max_busy))
20830Sstevel@tonic-gate 				max_busy = apic_cpus[i].aci_busy;
20840Sstevel@tonic-gate 		}
20850Sstevel@tonic-gate 		if (!apic_nticks) {
20860Sstevel@tonic-gate 			if (max_busy > apic_int_busy_mark) {
20870Sstevel@tonic-gate 			/*
20880Sstevel@tonic-gate 			 * We could make the following check be
20890Sstevel@tonic-gate 			 * skipped > 1 in which case, we get a
20900Sstevel@tonic-gate 			 * redistribution at half the busy mark (due to
20910Sstevel@tonic-gate 			 * double interval). Need to be able to collect
20920Sstevel@tonic-gate 			 * more empirical data to decide if that is a
20930Sstevel@tonic-gate 			 * good strategy. Punt for now.
20940Sstevel@tonic-gate 			 */
20953446Smrj 				if (apic_skipped_redistribute) {
20960Sstevel@tonic-gate 					apic_cleanup_busy();
20973446Smrj 					apic_skipped_redistribute = 0;
20983446Smrj 				} else {
20990Sstevel@tonic-gate 					apic_intr_redistribute();
21003446Smrj 				}
21010Sstevel@tonic-gate 			} else
21020Sstevel@tonic-gate 				apic_skipped_redistribute++;
21030Sstevel@tonic-gate 		}
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate }
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 
21083446Smrj /*
21093446Smrj  * The following functions are in the platform specific file so that they
21103446Smrj  * can be different functions depending on whether we are running on
21113446Smrj  * bare metal or a hypervisor.
21123446Smrj  */
21130Sstevel@tonic-gate 
21143446Smrj /*
21153446Smrj  * map an apic for memory-mapped access
21163446Smrj  */
21173446Smrj uint32_t *
21183446Smrj mapin_apic(uint32_t addr, size_t len, int flags)
21193446Smrj {
21203446Smrj 	/*LINTED: pointer cast may result in improper alignment */
21213446Smrj 	return ((uint32_t *)psm_map_phys(addr, len, flags));
21223446Smrj }
21230Sstevel@tonic-gate 
21243446Smrj uint32_t *
21253446Smrj mapin_ioapic(uint32_t addr, size_t len, int flags)
21263446Smrj {
21273446Smrj 	return (mapin_apic(addr, len, flags));
21280Sstevel@tonic-gate }
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate /*
21313446Smrj  * unmap an apic
21323139Ssethg  */
21333446Smrj void
21343446Smrj mapout_apic(caddr_t addr, size_t len)
21353139Ssethg {
21363446Smrj 	psm_unmap_phys(addr, len);
21373139Ssethg }
21383139Ssethg 
21393446Smrj void
21403446Smrj mapout_ioapic(caddr_t addr, size_t len)
21413139Ssethg {
21423446Smrj 	mapout_apic(addr, len);
21433139Ssethg }
21443139Ssethg 
21453139Ssethg /*
21464937Sjohnny  * Check to make sure there are enough irq slots
21473139Ssethg  */
21483446Smrj int
21494937Sjohnny apic_check_free_irqs(int count)
21504937Sjohnny {
21514937Sjohnny 	int i, avail;
21524937Sjohnny 
21534937Sjohnny 	avail = 0;
21544937Sjohnny 	for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
21554937Sjohnny 		if ((apic_irq_table[i] == NULL) ||
21564937Sjohnny 		    apic_irq_table[i]->airq_mps_intr_index == FREE_INDEX) {
21574937Sjohnny 			if (++avail >= count)
21584937Sjohnny 				return (PSM_SUCCESS);
21594937Sjohnny 		}
21604937Sjohnny 	}
21614937Sjohnny 	return (PSM_FAILURE);
21624937Sjohnny }
21634937Sjohnny 
21644937Sjohnny /*
21654937Sjohnny  * This function allocates "count" MSI vector(s) for the given "dip/pri/type"
21664937Sjohnny  */
21674937Sjohnny int
21684937Sjohnny apic_alloc_msi_vectors(dev_info_t *dip, int inum, int count, int pri,
21693446Smrj     int behavior)
21703139Ssethg {
21713446Smrj 	int	rcount, i;
21727282Smishra 	uchar_t	start, irqno;
21737282Smishra 	uint32_t cpu;
21743446Smrj 	major_t	major;
21753446Smrj 	apic_irq_t	*irqptr;
21763139Ssethg 
21774937Sjohnny 	DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: dip=0x%p "
21783446Smrj 	    "inum=0x%x  pri=0x%x count=0x%x behavior=%d\n",
21794937Sjohnny 	    (void *)dip, inum, pri, count, behavior));
21803139Ssethg 
21813446Smrj 	if (count > 1) {
21823446Smrj 		if (behavior == DDI_INTR_ALLOC_STRICT &&
21833446Smrj 		    (apic_multi_msi_enable == 0 || count > apic_multi_msi_max))
21843446Smrj 			return (0);
21853139Ssethg 
21863446Smrj 		if (apic_multi_msi_enable == 0)
21873446Smrj 			count = 1;
21883446Smrj 		else if (count > apic_multi_msi_max)
21893446Smrj 			count = apic_multi_msi_max;
21903446Smrj 	}
21913139Ssethg 
21923446Smrj 	if ((rcount = apic_navail_vector(dip, pri)) > count)
21933446Smrj 		rcount = count;
21943446Smrj 	else if (rcount == 0 || (rcount < count &&
21953446Smrj 	    behavior == DDI_INTR_ALLOC_STRICT))
21963446Smrj 		return (0);
21973139Ssethg 
21983446Smrj 	/* if not ISP2, then round it down */
21993446Smrj 	if (!ISP2(rcount))
22003446Smrj 		rcount = 1 << (highbit(rcount) - 1);
22013139Ssethg 
22023446Smrj 	mutex_enter(&airq_mutex);
22033446Smrj 
22043446Smrj 	for (start = 0; rcount > 0; rcount >>= 1) {
22053446Smrj 		if ((start = apic_find_multi_vectors(pri, rcount)) != 0 ||
22063446Smrj 		    behavior == DDI_INTR_ALLOC_STRICT)
22073446Smrj 			break;
22083139Ssethg 	}
22093139Ssethg 
22103446Smrj 	if (start == 0) {
22113446Smrj 		/* no vector available */
22123446Smrj 		mutex_exit(&airq_mutex);
22133446Smrj 		return (0);
22143446Smrj 	}
22153446Smrj 
22164937Sjohnny 	if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
22174937Sjohnny 		/* not enough free irq slots available */
22184937Sjohnny 		mutex_exit(&airq_mutex);
22194937Sjohnny 		return (0);
22204937Sjohnny 	}
22214937Sjohnny 
22223446Smrj 	major = (dip != NULL) ? ddi_name_to_major(ddi_get_name(dip)) : 0;
22233446Smrj 	for (i = 0; i < rcount; i++) {
22243446Smrj 		if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
22253446Smrj 		    (uchar_t)-1) {
22264937Sjohnny 			/*
22274937Sjohnny 			 * shouldn't happen because of the
22284937Sjohnny 			 * apic_check_free_irqs() check earlier
22294937Sjohnny 			 */
22303446Smrj 			mutex_exit(&airq_mutex);
22314937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
22323446Smrj 			    "apic_allocate_irq failed\n"));
22333446Smrj 			return (i);
22343446Smrj 		}
22353446Smrj 		apic_max_device_irq = max(irqno, apic_max_device_irq);
22363446Smrj 		apic_min_device_irq = min(irqno, apic_min_device_irq);
22373446Smrj 		irqptr = apic_irq_table[irqno];
22383446Smrj #ifdef	DEBUG
22393446Smrj 		if (apic_vector_to_irq[start + i] != APIC_RESV_IRQ)
22404937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
22413446Smrj 			    "apic_vector_to_irq is not APIC_RESV_IRQ\n"));
22423446Smrj #endif
22433446Smrj 		apic_vector_to_irq[start + i] = (uchar_t)irqno;
22443446Smrj 
22453446Smrj 		irqptr->airq_vector = (uchar_t)(start + i);
22463446Smrj 		irqptr->airq_ioapicindex = (uchar_t)inum;	/* start */
22473446Smrj 		irqptr->airq_intin_no = (uchar_t)rcount;
22483446Smrj 		irqptr->airq_ipl = pri;
22493446Smrj 		irqptr->airq_vector = start + i;
22503446Smrj 		irqptr->airq_origirq = (uchar_t)(inum + i);
22513446Smrj 		irqptr->airq_share_id = 0;
22523446Smrj 		irqptr->airq_mps_intr_index = MSI_INDEX;
22533446Smrj 		irqptr->airq_dip = dip;
22543446Smrj 		irqptr->airq_major = major;
22553446Smrj 		if (i == 0) /* they all bound to the same cpu */
22563446Smrj 			cpu = irqptr->airq_cpu = apic_bind_intr(dip, irqno,
22574397Sschwartz 			    0xff, 0xff);
22583446Smrj 		else
22593446Smrj 			irqptr->airq_cpu = cpu;
22604937Sjohnny 		DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: irq=0x%x "
22613446Smrj 		    "dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
22623446Smrj 		    (void *)irqptr->airq_dip, irqptr->airq_vector,
22633446Smrj 		    irqptr->airq_origirq, pri));
22643446Smrj 	}
22653446Smrj 	mutex_exit(&airq_mutex);
22663446Smrj 	return (rcount);
22673139Ssethg }
22683139Ssethg 
22693139Ssethg /*
22704937Sjohnny  * This function allocates "count" MSI-X vector(s) for the given "dip/pri/type"
22714937Sjohnny  */
22724937Sjohnny int
22734937Sjohnny apic_alloc_msix_vectors(dev_info_t *dip, int inum, int count, int pri,
22744937Sjohnny     int behavior)
22754937Sjohnny {
22764937Sjohnny 	int	rcount, i;
22774937Sjohnny 	major_t	major;
22784937Sjohnny 
22794937Sjohnny 	if (count > 1) {
22804937Sjohnny 		if (behavior == DDI_INTR_ALLOC_STRICT) {
22814937Sjohnny 			if (count > apic_msix_max)
22824937Sjohnny 				return (0);
22834937Sjohnny 		} else if (count > apic_msix_max)
22844937Sjohnny 			count = apic_msix_max;
22854937Sjohnny 	}
22864937Sjohnny 
22874937Sjohnny 	mutex_enter(&airq_mutex);
22884937Sjohnny 
22894937Sjohnny 	if ((rcount = apic_navail_vector(dip, pri)) > count)
22904937Sjohnny 		rcount = count;
22914937Sjohnny 	else if (rcount == 0 || (rcount < count &&
22924937Sjohnny 	    behavior == DDI_INTR_ALLOC_STRICT)) {
22934937Sjohnny 		rcount = 0;
22944937Sjohnny 		goto out;
22954937Sjohnny 	}
22964937Sjohnny 
22974937Sjohnny 	if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
22984937Sjohnny 		/* not enough free irq slots available */
22994937Sjohnny 		rcount = 0;
23004937Sjohnny 		goto out;
23014937Sjohnny 	}
23024937Sjohnny 
23034937Sjohnny 	major = (dip != NULL) ? ddi_name_to_major(ddi_get_name(dip)) : 0;
23044937Sjohnny 	for (i = 0; i < rcount; i++) {
23054937Sjohnny 		uchar_t	vector, irqno;
23064937Sjohnny 		apic_irq_t	*irqptr;
23074937Sjohnny 
23084937Sjohnny 		if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
23094937Sjohnny 		    (uchar_t)-1) {
23104937Sjohnny 			/*
23114937Sjohnny 			 * shouldn't happen because of the
23124937Sjohnny 			 * apic_check_free_irqs() check earlier
23134937Sjohnny 			 */
23144937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
23154937Sjohnny 			    "apic_allocate_irq failed\n"));
23164937Sjohnny 			rcount = i;
23174937Sjohnny 			goto out;
23184937Sjohnny 		}
23194937Sjohnny 		if ((vector = apic_allocate_vector(pri, irqno, 1)) == 0) {
23204937Sjohnny 			/*
23214937Sjohnny 			 * shouldn't happen because of the
23224937Sjohnny 			 * apic_navail_vector() call earlier
23234937Sjohnny 			 */
23244937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
23254937Sjohnny 			    "apic_allocate_vector failed\n"));
23264937Sjohnny 			rcount = i;
23274937Sjohnny 			goto out;
23284937Sjohnny 		}
23294937Sjohnny 		apic_max_device_irq = max(irqno, apic_max_device_irq);
23304937Sjohnny 		apic_min_device_irq = min(irqno, apic_min_device_irq);
23314937Sjohnny 		irqptr = apic_irq_table[irqno];
23324937Sjohnny 		irqptr->airq_vector = (uchar_t)vector;
23334937Sjohnny 		irqptr->airq_ipl = pri;
23344937Sjohnny 		irqptr->airq_origirq = (uchar_t)(inum + i);
23354937Sjohnny 		irqptr->airq_share_id = 0;
23364937Sjohnny 		irqptr->airq_mps_intr_index = MSIX_INDEX;
23374937Sjohnny 		irqptr->airq_dip = dip;
23384937Sjohnny 		irqptr->airq_major = major;
23394937Sjohnny 		irqptr->airq_cpu = apic_bind_intr(dip, irqno, 0xff, 0xff);
23404937Sjohnny 	}
23414937Sjohnny out:
23424937Sjohnny 	mutex_exit(&airq_mutex);
23434937Sjohnny 	return (rcount);
23444937Sjohnny }
23454937Sjohnny 
23464937Sjohnny /*
23473446Smrj  * Allocate a free vector for irq at ipl. Takes care of merging of multiple
23483446Smrj  * IPLs into a single APIC level as well as stretching some IPLs onto multiple
23493446Smrj  * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
23503446Smrj  * requests and allocated only when pri is set.
23510Sstevel@tonic-gate  */
23523446Smrj uchar_t
23533446Smrj apic_allocate_vector(int ipl, int irq, int pri)
23540Sstevel@tonic-gate {
23553446Smrj 	int	lowest, highest, i;
23560Sstevel@tonic-gate 
23573446Smrj 	highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK;
23583446Smrj 	lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL;
23590Sstevel@tonic-gate 
23603446Smrj 	if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
23613446Smrj 		lowest -= APIC_VECTOR_PER_IPL;
23623139Ssethg 
23633446Smrj #ifdef	DEBUG
23643446Smrj 	if (apic_restrict_vector)	/* for testing shared interrupt logic */
23653446Smrj 		highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS;
23663446Smrj #endif /* DEBUG */
23673446Smrj 	if (pri == 0)
23683446Smrj 		highest -= APIC_HI_PRI_VECTS;
23693139Ssethg 
23703446Smrj 	for (i = lowest; i < highest; i++) {
23713446Smrj 		if (APIC_CHECK_RESERVE_VECTORS(i))
23723446Smrj 			continue;
23733446Smrj 		if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
23743446Smrj 			apic_vector_to_irq[i] = (uchar_t)irq;
23753446Smrj 			return (i);
23760Sstevel@tonic-gate 		}
23770Sstevel@tonic-gate 	}
23780Sstevel@tonic-gate 
23793446Smrj 	return (0);
23803446Smrj }
23813446Smrj 
23823446Smrj /* Mark vector as not being used by any irq */
23833446Smrj void
23843446Smrj apic_free_vector(uchar_t vector)
23853446Smrj {
23863446Smrj 	apic_vector_to_irq[vector] = APIC_RESV_IRQ;
23873446Smrj }
23883446Smrj 
23893446Smrj uint32_t
23903446Smrj ioapic_read(int ioapic_ix, uint32_t reg)
23913446Smrj {
23923446Smrj 	volatile uint32_t *ioapic;
23933446Smrj 
23943446Smrj 	ioapic = apicioadr[ioapic_ix];
23953446Smrj 	ioapic[APIC_IO_REG] = reg;
23963446Smrj 	return (ioapic[APIC_IO_DATA]);
23973446Smrj }
23983139Ssethg 
23993446Smrj void
24003446Smrj ioapic_write(int ioapic_ix, uint32_t reg, uint32_t value)
24013446Smrj {
24023446Smrj 	volatile uint32_t *ioapic;
24033446Smrj 
24043446Smrj 	ioapic = apicioadr[ioapic_ix];
24053446Smrj 	ioapic[APIC_IO_REG] = reg;
24063446Smrj 	ioapic[APIC_IO_DATA] = value;
24073446Smrj }
24083446Smrj 
24097282Smishra void
24107282Smishra ioapic_write_eoi(int ioapic_ix, uint32_t value)
24117282Smishra {
24127282Smishra 	volatile uint32_t *ioapic;
24137282Smishra 
24147282Smishra 	ioapic = apicioadr[ioapic_ix];
24157282Smishra 	ioapic[APIC_IO_EOI] = value;
24167282Smishra }
24177282Smishra 
24183446Smrj static processorid_t
24193446Smrj apic_find_cpu(int flag)
24203446Smrj {
24213446Smrj 	processorid_t acid = 0;
24223446Smrj 	int i;
24233446Smrj 
24243446Smrj 	/* Find the first CPU with the passed-in flag set */
24253446Smrj 	for (i = 0; i < apic_nproc; i++) {
24263446Smrj 		if (apic_cpus[i].aci_status & flag) {
24273446Smrj 			acid = i;
24283446Smrj 			break;
24293446Smrj 		}
24303446Smrj 	}
24313446Smrj 
24323446Smrj 	ASSERT((apic_cpus[acid].aci_status & flag) != 0);
24333446Smrj 	return (acid);
24343446Smrj }
24353139Ssethg 
24363446Smrj /*
24373446Smrj  * Call rebind to do the actual programming.
24383446Smrj  * Must be called with interrupts disabled and apic_ioapic_lock held
24393446Smrj  * 'p' is polymorphic -- if this function is called to process a deferred
24403446Smrj  * reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
24413446Smrj  * the irq pointer is retrieved.  If not doing deferred reprogramming,
24423446Smrj  * p is of the type 'apic_irq_t *'.
24433446Smrj  *
24443446Smrj  * apic_ioapic_lock must be held across this call, as it protects apic_rebind
24453446Smrj  * and it protects apic_find_cpu() from a race in which a CPU can be taken
24463446Smrj  * offline after a cpu is selected, but before apic_rebind is called to
24473446Smrj  * bind interrupts to it.
24483446Smrj  */
24493446Smrj int
24503446Smrj apic_setup_io_intr(void *p, int irq, boolean_t deferred)
24513446Smrj {
24523446Smrj 	apic_irq_t *irqptr;
24533446Smrj 	struct ioapic_reprogram_data *drep = NULL;
24543446Smrj 	int rv;
24553446Smrj 
24563446Smrj 	if (deferred) {
24573446Smrj 		drep = (struct ioapic_reprogram_data *)p;
24583446Smrj 		ASSERT(drep != NULL);
24593446Smrj 		irqptr = drep->irqp;
24603446Smrj 	} else
24613446Smrj 		irqptr = (apic_irq_t *)p;
24623446Smrj 
24633446Smrj 	ASSERT(irqptr != NULL);
24643446Smrj 
24653446Smrj 	rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, drep);
24663446Smrj 	if (rv) {
24673446Smrj 		/*
24683446Smrj 		 * CPU is not up or interrupts are disabled. Fall back to
24693446Smrj 		 * the first available CPU
24703446Smrj 		 */
24713446Smrj 		rv = apic_rebind(irqptr, apic_find_cpu(APIC_CPU_INTR_ENABLE),
24723446Smrj 		    drep);
24733446Smrj 	}
24743446Smrj 
24753446Smrj 	return (rv);
24760Sstevel@tonic-gate }
24773446Smrj 
24783446Smrj 
24793446Smrj uchar_t
24803446Smrj apic_modify_vector(uchar_t vector, int irq)
24813446Smrj {
24823446Smrj 	apic_vector_to_irq[vector] = (uchar_t)irq;
24833446Smrj 	return (vector);
24843446Smrj }
24854397Sschwartz 
24864397Sschwartz char *
24874397Sschwartz apic_get_apic_type()
24884397Sschwartz {
24894397Sschwartz 	return (apic_psm_info.p_mach_idstring);
24904397Sschwartz }
24917282Smishra 
24927282Smishra void
24937282Smishra x2apic_update_psm()
24947282Smishra {
24957282Smishra 	struct psm_ops *pops = &apic_ops;
24967282Smishra 
24977282Smishra 	ASSERT(pops != NULL);
24987282Smishra 
2499*7986SSaurabh.Mishra@Sun.COM 	/*
2500*7986SSaurabh.Mishra@Sun.COM 	 * We don't need to do any magic if one of the following
2501*7986SSaurabh.Mishra@Sun.COM 	 * conditions is true :
2502*7986SSaurabh.Mishra@Sun.COM 	 * - Not being run under kernel debugger.
2503*7986SSaurabh.Mishra@Sun.COM 	 * - MP is not set.
2504*7986SSaurabh.Mishra@Sun.COM 	 * - Booted with one CPU only.
2505*7986SSaurabh.Mishra@Sun.COM 	 * - One CPU configured.
2506*7986SSaurabh.Mishra@Sun.COM 	 *
2507*7986SSaurabh.Mishra@Sun.COM 	 * We set apic_common_send_ipi() since kernel debuggers
2508*7986SSaurabh.Mishra@Sun.COM 	 * attempt to send IPIs to other slave CPUs during
2509*7986SSaurabh.Mishra@Sun.COM 	 * entry (exit) from (to) debugger.
2510*7986SSaurabh.Mishra@Sun.COM 	 */
2511*7986SSaurabh.Mishra@Sun.COM 	if (!(boothowto & RB_DEBUG) || use_mp == 0 ||
2512*7986SSaurabh.Mishra@Sun.COM 	    apic_nproc == 1 || boot_ncpus == 1) {
2513*7986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi =  x2apic_send_ipi;
2514*7986SSaurabh.Mishra@Sun.COM 	} else {
2515*7986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi =  apic_common_send_ipi;
2516*7986SSaurabh.Mishra@Sun.COM 	}
2517*7986SSaurabh.Mishra@Sun.COM 
25187282Smishra 	pops->psm_intr_exit = x2apic_intr_exit;
25197282Smishra 	pops->psm_setspl = x2apic_setspl;
25207282Smishra 
25217282Smishra 	send_dirintf = pops->psm_send_ipi;
2522*7986SSaurabh.Mishra@Sun.COM 
2523*7986SSaurabh.Mishra@Sun.COM 	apic_mode = LOCAL_X2APIC;
2524*7986SSaurabh.Mishra@Sun.COM 	apic_change_ops();
25257282Smishra }
2526