xref: /onnv-gate/usr/src/uts/i86pc/io/pcplusmp/apic.c (revision 11641:d71a0c77a32e)
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 /*
2311465SKerry.Shu@Sun.COM  * Copyright 2010 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>
707986SSaurabh.Mishra@Sun.COM #include <sys/reboot.h>
718906SEric.Saxe@Sun.COM #include <sys/hpet.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  *	Local Function Prototypes
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static void apic_init_intr();
775084Sjohnlev static void apic_nmi_intr(caddr_t arg, struct regs *rp);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  *	standard MP entries
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static int	apic_probe();
830Sstevel@tonic-gate static int	apic_clkinit();
840Sstevel@tonic-gate static int	apic_getclkirq(int ipl);
850Sstevel@tonic-gate static uint_t	apic_calibrate(volatile uint32_t *addr,
860Sstevel@tonic-gate     uint16_t *pit_ticks_adj);
870Sstevel@tonic-gate static hrtime_t apic_gettime();
880Sstevel@tonic-gate static hrtime_t apic_gethrtime();
890Sstevel@tonic-gate static void	apic_init();
900Sstevel@tonic-gate static void	apic_picinit(void);
913446Smrj static int	apic_cpu_start(processorid_t, caddr_t);
920Sstevel@tonic-gate static int	apic_post_cpu_start(void);
930Sstevel@tonic-gate static void	apic_send_ipi(int cpun, int ipl);
940Sstevel@tonic-gate static void	apic_set_idlecpu(processorid_t cpun);
950Sstevel@tonic-gate static void	apic_unset_idlecpu(processorid_t cpun);
960Sstevel@tonic-gate static int	apic_intr_enter(int ipl, int *vect);
970Sstevel@tonic-gate static void	apic_setspl(int ipl);
987282Smishra static void	x2apic_setspl(int ipl);
990Sstevel@tonic-gate static int	apic_addspl(int ipl, int vector, int min_ipl, int max_ipl);
1000Sstevel@tonic-gate static int	apic_delspl(int ipl, int vector, int min_ipl, int max_ipl);
1010Sstevel@tonic-gate static void	apic_shutdown(int cmd, int fcn);
1020Sstevel@tonic-gate static void	apic_preshutdown(int cmd, int fcn);
1030Sstevel@tonic-gate static int	apic_disable_intr(processorid_t cpun);
1040Sstevel@tonic-gate static void	apic_enable_intr(processorid_t cpun);
1050Sstevel@tonic-gate static processorid_t	apic_get_next_processorid(processorid_t cpun);
1060Sstevel@tonic-gate static int		apic_get_ipivect(int ipl, int type);
1070Sstevel@tonic-gate static void	apic_timer_reprogram(hrtime_t time);
1080Sstevel@tonic-gate static void	apic_timer_enable(void);
1090Sstevel@tonic-gate static void	apic_timer_disable(void);
1100Sstevel@tonic-gate static void	apic_post_cyclic_setup(void *arg);
11111600SVikram.Hegde@Sun.COM static void	apic_intrmap_init(int apic_mode);
1128675SVikram.Hegde@Sun.COM static void	apic_record_ioapic_rdt(apic_irq_t *irq_ptr, ioapic_rdt_t *irdt);
1138675SVikram.Hegde@Sun.COM static void	apic_record_msi(apic_irq_t *irq_ptr, msi_regs_t *mregs);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static int	apic_oneshot = 0;
1160Sstevel@tonic-gate int	apic_oneshot_enable = 1; /* to allow disabling one-shot capability */
1170Sstevel@tonic-gate 
1183446Smrj /* Now the ones for Dynamic Interrupt distribution */
1193446Smrj int	apic_enable_dynamic_migration = 0;
1203446Smrj 
12110252SJoe.Bonasera@sun.com extern int apic_have_32bit_cr8;
1223446Smrj 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * These variables are frequently accessed in apic_intr_enter(),
1250Sstevel@tonic-gate  * apic_intr_exit and apic_setspl, so group them together
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate volatile uint32_t *apicadr =  NULL;	/* virtual addr of local APIC	*/
1280Sstevel@tonic-gate int apic_setspl_delay = 1;		/* apic_setspl - delay enable	*/
1290Sstevel@tonic-gate int apic_clkvect;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /* vector at which error interrupts come in */
1320Sstevel@tonic-gate int apic_errvect;
1330Sstevel@tonic-gate int apic_enable_error_intr = 1;
1340Sstevel@tonic-gate int apic_error_display_delay = 100;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /* vector at which performance counter overflow interrupts come in */
1370Sstevel@tonic-gate int apic_cpcovf_vect;
1380Sstevel@tonic-gate int apic_enable_cpcovf_intr = 1;
1390Sstevel@tonic-gate 
1407349SAdrian.Frost@Sun.COM /* vector at which CMCI interrupts come in */
1417349SAdrian.Frost@Sun.COM int apic_cmci_vect;
1427349SAdrian.Frost@Sun.COM extern int cmi_enable_cmci;
1437349SAdrian.Frost@Sun.COM extern void cmi_cmci_trap(void);
1447349SAdrian.Frost@Sun.COM 
1457349SAdrian.Frost@Sun.COM static kmutex_t cmci_cpu_setup_lock;	/* protects cmci_cpu_setup_registered */
1467349SAdrian.Frost@Sun.COM static int cmci_cpu_setup_registered;
1477349SAdrian.Frost@Sun.COM 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * The following vector assignments influence the value of ipltopri and
1500Sstevel@tonic-gate  * vectortoipl. Note that vectors 0 - 0x1f are not used. We can program
1513745Ssethg  * idle to 0 and IPL 0 to 0xf to differentiate idle in case
1520Sstevel@tonic-gate  * we care to do so in future. Note some IPLs which are rarely used
1530Sstevel@tonic-gate  * will share the vector ranges and heavily used IPLs (5 and 6) have
1540Sstevel@tonic-gate  * a wide range.
1553745Ssethg  *
1563745Ssethg  * This array is used to initialize apic_ipls[] (in apic_init()).
1573745Ssethg  *
1580Sstevel@tonic-gate  *	IPL		Vector range.		as passed to intr_enter
1590Sstevel@tonic-gate  *	0		none.
1600Sstevel@tonic-gate  *	1,2,3		0x20-0x2f		0x0-0xf
1610Sstevel@tonic-gate  *	4		0x30-0x3f		0x10-0x1f
1620Sstevel@tonic-gate  *	5		0x40-0x5f		0x20-0x3f
1630Sstevel@tonic-gate  *	6		0x60-0x7f		0x40-0x5f
1640Sstevel@tonic-gate  *	7,8,9		0x80-0x8f		0x60-0x6f
1650Sstevel@tonic-gate  *	10		0x90-0x9f		0x70-0x7f
1660Sstevel@tonic-gate  *	11		0xa0-0xaf		0x80-0x8f
1670Sstevel@tonic-gate  *	...		...
1683745Ssethg  *	15		0xe0-0xef		0xc0-0xcf
1693745Ssethg  *	15		0xf0-0xff		0xd0-0xdf
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate uchar_t apic_vectortoipl[APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL] = {
1723745Ssethg 	3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 15
1730Sstevel@tonic-gate };
1740Sstevel@tonic-gate 	/*
1753745Ssethg 	 * The ipl of an ISR at vector X is apic_vectortoipl[X>>4]
1760Sstevel@tonic-gate 	 * NOTE that this is vector as passed into intr_enter which is
1770Sstevel@tonic-gate 	 * programmed vector - 0x20 (APIC_BASE_VECT)
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate uchar_t	apic_ipltopri[MAXIPL + 1];	/* unix ipl to apic pri	*/
1810Sstevel@tonic-gate 	/* The taskpri to be programmed into apic to mask given ipl */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #if defined(__amd64)
1840Sstevel@tonic-gate uchar_t	apic_cr8pri[MAXIPL + 1];	/* unix ipl to cr8 pri	*/
1850Sstevel@tonic-gate #endif
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1883745Ssethg  * Correlation of the hardware vector to the IPL in use, initialized
1893745Ssethg  * from apic_vectortoipl[] in apic_init().  The final IPLs may not correlate
1903745Ssethg  * to the IPLs in apic_vectortoipl on some systems that share interrupt lines
1913745Ssethg  * connected to errata-stricken IOAPICs
1923745Ssethg  */
1933745Ssethg uchar_t apic_ipls[APIC_AVAIL_VECTOR];
1943745Ssethg 
1953745Ssethg /*
1960Sstevel@tonic-gate  * Patchable global variables.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate int	apic_forceload = 0;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int	apic_coarse_hrtime = 1;		/* 0 - use accurate slow gethrtime() */
2010Sstevel@tonic-gate 					/* 1 - use gettime() for performance */
2020Sstevel@tonic-gate int	apic_flat_model = 0;		/* 0 - clustered. 1 - flat */
2030Sstevel@tonic-gate int	apic_enable_hwsoftint = 0;	/* 0 - disable, 1 - enable	*/
2040Sstevel@tonic-gate int	apic_enable_bind_log = 1;	/* 1 - display interrupt binding log */
2050Sstevel@tonic-gate int	apic_panic_on_nmi = 0;
2060Sstevel@tonic-gate int	apic_panic_on_apic_error = 0;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate int	apic_verbose = 0;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /* minimum number of timer ticks to program to */
2110Sstevel@tonic-gate int apic_min_timer_ticks = 1;
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  *	Local static data
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate static struct	psm_ops apic_ops = {
2160Sstevel@tonic-gate 	apic_probe,
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	apic_init,
2190Sstevel@tonic-gate 	apic_picinit,
2200Sstevel@tonic-gate 	apic_intr_enter,
2210Sstevel@tonic-gate 	apic_intr_exit,
2220Sstevel@tonic-gate 	apic_setspl,
2230Sstevel@tonic-gate 	apic_addspl,
2240Sstevel@tonic-gate 	apic_delspl,
2250Sstevel@tonic-gate 	apic_disable_intr,
2260Sstevel@tonic-gate 	apic_enable_intr,
2274652Scwb 	(int (*)(int))NULL,		/* psm_softlvl_to_irq */
2284652Scwb 	(void (*)(int))NULL,		/* psm_set_softintr */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	apic_set_idlecpu,
2310Sstevel@tonic-gate 	apic_unset_idlecpu,
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	apic_clkinit,
2340Sstevel@tonic-gate 	apic_getclkirq,
2350Sstevel@tonic-gate 	(void (*)(void))NULL,		/* psm_hrtimeinit */
2360Sstevel@tonic-gate 	apic_gethrtime,
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	apic_get_next_processorid,
2390Sstevel@tonic-gate 	apic_cpu_start,
2400Sstevel@tonic-gate 	apic_post_cpu_start,
2410Sstevel@tonic-gate 	apic_shutdown,
2420Sstevel@tonic-gate 	apic_get_ipivect,
2430Sstevel@tonic-gate 	apic_send_ipi,
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	(int (*)(dev_info_t *, int))NULL,	/* psm_translate_irq */
2460Sstevel@tonic-gate 	(void (*)(int, char *))NULL,	/* psm_notify_error */
2470Sstevel@tonic-gate 	(void (*)(int))NULL,		/* psm_notify_func */
2480Sstevel@tonic-gate 	apic_timer_reprogram,
2490Sstevel@tonic-gate 	apic_timer_enable,
2500Sstevel@tonic-gate 	apic_timer_disable,
2510Sstevel@tonic-gate 	apic_post_cyclic_setup,
2520Sstevel@tonic-gate 	apic_preshutdown,
2535295Srandyf 	apic_intr_ops,			/* Advanced DDI Interrupt framework */
2545295Srandyf 	apic_state,			/* save, restore apic state for S3 */
2550Sstevel@tonic-gate };
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static struct	psm_info apic_psm_info = {
2595295Srandyf 	PSM_INFO_VER01_6,			/* version */
2600Sstevel@tonic-gate 	PSM_OWN_EXCLUSIVE,			/* ownership */
2610Sstevel@tonic-gate 	(struct psm_ops *)&apic_ops,		/* operation */
2624397Sschwartz 	APIC_PCPLUSMP_NAME,			/* machine name */
2636896Sdmick 	"pcplusmp v1.4 compatible",
2640Sstevel@tonic-gate };
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static void *apic_hdlp;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #ifdef DEBUG
2690Sstevel@tonic-gate int	apic_debug = 0;
2700Sstevel@tonic-gate int	apic_restrict_vector = 0;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate int	apic_debug_msgbuf[APIC_DEBUG_MSGBUFSIZE];
2730Sstevel@tonic-gate int	apic_debug_msgbufindex = 0;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate #endif /* DEBUG */
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate apic_cpus_info_t	*apic_cpus;
2780Sstevel@tonic-gate 
2793446Smrj cpuset_t	apic_cpumask;
2805084Sjohnlev uint_t	apic_picinit_called;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /* Flag to indicate that we need to shut down all processors */
2830Sstevel@tonic-gate static uint_t	apic_shutdown_processors;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate uint_t apic_nsec_per_intr = 0;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * apic_let_idle_redistribute can have the following values:
2890Sstevel@tonic-gate  * 0 - If clock decremented it from 1 to 0, clock has to call redistribute.
2900Sstevel@tonic-gate  * apic_redistribute_lock prevents multiple idle cpus from redistributing
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate int	apic_num_idle_redistributions = 0;
2930Sstevel@tonic-gate static	int apic_let_idle_redistribute = 0;
2940Sstevel@tonic-gate static	uint_t apic_nticks = 0;
2950Sstevel@tonic-gate static	uint_t apic_skipped_redistribute = 0;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /* to gather intr data and redistribute */
2980Sstevel@tonic-gate static void apic_redistribute_compute(void);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static	uint_t last_count_read = 0;
3010Sstevel@tonic-gate static	lock_t	apic_gethrtime_lock;
3020Sstevel@tonic-gate volatile int	apic_hrtime_stamp = 0;
3030Sstevel@tonic-gate volatile hrtime_t apic_nsec_since_boot = 0;
3042992Sdmick static uint_t apic_hertz_count;
3052992Sdmick 
3062992Sdmick uint64_t apic_ticks_per_SFnsecs;	/* # of ticks in SF nsecs */
3072992Sdmick 
3080Sstevel@tonic-gate static hrtime_t apic_nsec_max;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static	hrtime_t	apic_last_hrtime = 0;
3110Sstevel@tonic-gate int		apic_hrtime_error = 0;
3120Sstevel@tonic-gate int		apic_remote_hrterr = 0;
3130Sstevel@tonic-gate int		apic_num_nmis = 0;
3140Sstevel@tonic-gate int		apic_apic_error = 0;
3150Sstevel@tonic-gate int		apic_num_apic_errors = 0;
3160Sstevel@tonic-gate int		apic_num_cksum_errors = 0;
3170Sstevel@tonic-gate 
3183446Smrj int	apic_error = 0;
3190Sstevel@tonic-gate static	int	apic_cmos_ssb_set = 0;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /* use to make sure only one cpu handles the nmi */
3220Sstevel@tonic-gate static	lock_t	apic_nmi_lock;
3230Sstevel@tonic-gate /* use to make sure only one cpu handles the error interrupt */
3240Sstevel@tonic-gate static	lock_t	apic_error_lock;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate static	struct {
3270Sstevel@tonic-gate 	uchar_t	cntl;
3280Sstevel@tonic-gate 	uchar_t	data;
3290Sstevel@tonic-gate } aspen_bmc[] = {
3300Sstevel@tonic-gate 	{ CC_SMS_WR_START,	0x18 },		/* NetFn/LUN */
3310Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x24 },		/* Cmd SET_WATCHDOG_TIMER */
3320Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x84 },		/* DataByte 1: SMS/OS no log */
3330Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x2 },		/* DataByte 2: Power Down */
3340Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x0 },		/* DataByte 3: no pre-timeout */
3350Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0x0 },		/* DataByte 4: timer expir. */
3360Sstevel@tonic-gate 	{ CC_SMS_WR_NEXT,	0xa },		/* DataByte 5: init countdown */
3370Sstevel@tonic-gate 	{ CC_SMS_WR_END,	0x0 },		/* DataByte 6: init countdown */
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	{ CC_SMS_WR_START,	0x18 },		/* NetFn/LUN */
3400Sstevel@tonic-gate 	{ CC_SMS_WR_END,	0x22 }		/* Cmd RESET_WATCHDOG_TIMER */
3410Sstevel@tonic-gate };
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static	struct {
3440Sstevel@tonic-gate 	int	port;
3450Sstevel@tonic-gate 	uchar_t	data;
3460Sstevel@tonic-gate } sitka_bmc[] = {
3470Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_START },
3480Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x18 },		/* NetFn/LUN */
3490Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x24 },		/* Cmd SET_WATCHDOG_TIMER */
3500Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x84 },		/* DataByte 1: SMS/OS no log */
3510Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x2 },		/* DataByte 2: Power Down */
3520Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 3: no pre-timeout */
3530Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 4: timer expir. */
3540Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0xa },		/* DataByte 5: init countdown */
3550Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_END },
3560Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x0 },		/* DataByte 6: init countdown */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_START },
3590Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x18 },		/* NetFn/LUN */
3600Sstevel@tonic-gate 	{ SMS_COMMAND_REGISTER,	SMS_WRITE_END },
3610Sstevel@tonic-gate 	{ SMS_DATA_REGISTER,	0x22 }		/* Cmd RESET_WATCHDOG_TIMER */
3620Sstevel@tonic-gate };
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /* Patchable global variables. */
3650Sstevel@tonic-gate int		apic_kmdb_on_nmi = 0;		/* 0 - no, 1 - yes enter kmdb */
3662992Sdmick uint32_t	apic_divide_reg_init = 0;	/* 0 - divide by 2 */
3670Sstevel@tonic-gate 
3688675SVikram.Hegde@Sun.COM /* default apic ops without interrupt remapping */
36911600SVikram.Hegde@Sun.COM static apic_intrmap_ops_t apic_nointrmap_ops = {
3708675SVikram.Hegde@Sun.COM 	(int (*)(int))return_instr,
37110345SSaurabh.Mishra@Sun.COM 	(void (*)(int))return_instr,
3728675SVikram.Hegde@Sun.COM 	(void (*)(apic_irq_t *))return_instr,
3738675SVikram.Hegde@Sun.COM 	(void (*)(apic_irq_t *, void *))return_instr,
3748675SVikram.Hegde@Sun.COM 	(void (*)(apic_irq_t *))return_instr,
3758675SVikram.Hegde@Sun.COM 	apic_record_ioapic_rdt,
3768675SVikram.Hegde@Sun.COM 	apic_record_msi,
3778675SVikram.Hegde@Sun.COM };
3788675SVikram.Hegde@Sun.COM 
37911600SVikram.Hegde@Sun.COM apic_intrmap_ops_t *apic_vt_ops = &apic_nointrmap_ops;
3808675SVikram.Hegde@Sun.COM 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  *	This is the loadable module wrapper
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate int
3860Sstevel@tonic-gate _init(void)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	if (apic_coarse_hrtime)
3890Sstevel@tonic-gate 		apic_ops.psm_gethrtime = &apic_gettime;
3900Sstevel@tonic-gate 	return (psm_mod_init(&apic_hdlp, &apic_psm_info));
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate int
3940Sstevel@tonic-gate _fini(void)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	return (psm_mod_fini(&apic_hdlp, &apic_psm_info));
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate int
4000Sstevel@tonic-gate _info(struct modinfo *modinfop)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	return (psm_mod_info(&apic_hdlp, &apic_psm_info, modinfop));
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static int
4070Sstevel@tonic-gate apic_probe()
4080Sstevel@tonic-gate {
4093446Smrj 	return (apic_probe_common(apic_psm_info.p_mach_idstring));
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate void
4130Sstevel@tonic-gate apic_init()
4140Sstevel@tonic-gate {
4153446Smrj 	int i;
4163446Smrj 	int	j = 1;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */
4190Sstevel@tonic-gate 	for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
4200Sstevel@tonic-gate 		if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) &&
4210Sstevel@tonic-gate 		    (apic_vectortoipl[i + 1] == apic_vectortoipl[i]))
4220Sstevel@tonic-gate 			/* get to highest vector at the same ipl */
4230Sstevel@tonic-gate 			continue;
4240Sstevel@tonic-gate 		for (; j <= apic_vectortoipl[i]; j++) {
4250Sstevel@tonic-gate 			apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
4260Sstevel@tonic-gate 			    APIC_BASE_VECT;
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	for (; j < MAXIPL + 1; j++)
4300Sstevel@tonic-gate 		/* fill up any empty ipltopri slots */
4310Sstevel@tonic-gate 		apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT;
4323446Smrj 	apic_init_common();
4330Sstevel@tonic-gate #if defined(__amd64)
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * Make cpu-specific interrupt info point to cr8pri vector
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	for (i = 0; i <= MAXIPL; i++)
4380Sstevel@tonic-gate 		apic_cr8pri[i] = apic_ipltopri[i] >> APIC_IPL_SHIFT;
4390Sstevel@tonic-gate 	CPU->cpu_pri_data = apic_cr8pri;
44010080SJoe.Bonasera@sun.com #else
44110080SJoe.Bonasera@sun.com 	if (cpuid_have_cr8access(CPU))
44210080SJoe.Bonasera@sun.com 		apic_have_32bit_cr8 = 1;
4430Sstevel@tonic-gate #endif	/* __amd64 */
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * handler for APIC Error interrupt. Just print a warning and continue
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate static int
4500Sstevel@tonic-gate apic_error_intr()
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	uint_t	error0, error1, error;
4530Sstevel@tonic-gate 	uint_t	i;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * We need to write before read as per 7.4.17 of system prog manual.
4570Sstevel@tonic-gate 	 * We do both and or the results to be safe
4580Sstevel@tonic-gate 	 */
4597282Smishra 	error0 = apic_reg_ops->apic_read(APIC_ERROR_STATUS);
4607282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4617282Smishra 	error1 = apic_reg_ops->apic_read(APIC_ERROR_STATUS);
4620Sstevel@tonic-gate 	error = error0 | error1;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
465846Ssethg 	 * Clear the APIC error status (do this on all cpus that enter here)
466846Ssethg 	 * (two writes are required due to the semantics of accessing the
467846Ssethg 	 * error status register.)
468846Ssethg 	 */
4697282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4707282Smishra 	apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
471846Ssethg 
472846Ssethg 	/*
4730Sstevel@tonic-gate 	 * Prevent more than 1 CPU from handling error interrupt causing
4740Sstevel@tonic-gate 	 * double printing (interleave of characters from multiple
4750Sstevel@tonic-gate 	 * CPU's when using prom_printf)
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (lock_try(&apic_error_lock) == 0)
4780Sstevel@tonic-gate 		return (error ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
4790Sstevel@tonic-gate 	if (error) {
4800Sstevel@tonic-gate #if	DEBUG
4810Sstevel@tonic-gate 		if (apic_debug)
4820Sstevel@tonic-gate 			debug_enter("pcplusmp: APIC Error interrupt received");
4830Sstevel@tonic-gate #endif /* DEBUG */
4840Sstevel@tonic-gate 		if (apic_panic_on_apic_error)
4850Sstevel@tonic-gate 			cmn_err(CE_PANIC,
4860Sstevel@tonic-gate 			    "APIC Error interrupt on CPU %d. Status = %x\n",
4870Sstevel@tonic-gate 			    psm_get_cpu_id(), error);
4880Sstevel@tonic-gate 		else {
4890Sstevel@tonic-gate 			if ((error & ~APIC_CS_ERRORS) == 0) {
4900Sstevel@tonic-gate 				/* cksum error only */
4910Sstevel@tonic-gate 				apic_error |= APIC_ERR_APIC_ERROR;
4920Sstevel@tonic-gate 				apic_apic_error |= error;
4930Sstevel@tonic-gate 				apic_num_apic_errors++;
4940Sstevel@tonic-gate 				apic_num_cksum_errors++;
4950Sstevel@tonic-gate 			} else {
4960Sstevel@tonic-gate 				/*
4970Sstevel@tonic-gate 				 * prom_printf is the best shot we have of
4980Sstevel@tonic-gate 				 * something which is problem free from
4990Sstevel@tonic-gate 				 * high level/NMI type of interrupts
5000Sstevel@tonic-gate 				 */
5010Sstevel@tonic-gate 				prom_printf("APIC Error interrupt on CPU %d. "
5020Sstevel@tonic-gate 				    "Status 0 = %x, Status 1 = %x\n",
5030Sstevel@tonic-gate 				    psm_get_cpu_id(), error0, error1);
5040Sstevel@tonic-gate 				apic_error |= APIC_ERR_APIC_ERROR;
5050Sstevel@tonic-gate 				apic_apic_error |= error;
5060Sstevel@tonic-gate 				apic_num_apic_errors++;
5070Sstevel@tonic-gate 				for (i = 0; i < apic_error_display_delay; i++) {
5080Sstevel@tonic-gate 					tenmicrosec();
5090Sstevel@tonic-gate 				}
5100Sstevel@tonic-gate 				/*
5110Sstevel@tonic-gate 				 * provide more delay next time limited to
5120Sstevel@tonic-gate 				 * roughly 1 clock tick time
5130Sstevel@tonic-gate 				 */
5140Sstevel@tonic-gate 				if (apic_error_display_delay < 500)
5150Sstevel@tonic-gate 					apic_error_display_delay *= 2;
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		lock_clear(&apic_error_lock);
5190Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
5200Sstevel@tonic-gate 	} else {
5210Sstevel@tonic-gate 		lock_clear(&apic_error_lock);
5220Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	/* NOTREACHED */
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * Turn off the mask bit in the performance counter Local Vector Table entry.
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate static void
5310Sstevel@tonic-gate apic_cpcovf_mask_clear(void)
5320Sstevel@tonic-gate {
5337282Smishra 	apic_reg_ops->apic_write(APIC_PCINT_VECT,
5347282Smishra 	    (apic_reg_ops->apic_read(APIC_PCINT_VECT) & ~APIC_LVT_MASK));
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5377349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5387349SAdrian.Frost@Sun.COM static int
5397349SAdrian.Frost@Sun.COM apic_cmci_enable(xc_arg_t arg1, xc_arg_t arg2, xc_arg_t arg3)
5407349SAdrian.Frost@Sun.COM {
5417349SAdrian.Frost@Sun.COM 	apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
5427349SAdrian.Frost@Sun.COM 	return (0);
5437349SAdrian.Frost@Sun.COM }
5447349SAdrian.Frost@Sun.COM 
5457349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5467349SAdrian.Frost@Sun.COM static int
5477349SAdrian.Frost@Sun.COM apic_cmci_disable(xc_arg_t arg1, xc_arg_t arg2, xc_arg_t arg3)
5487349SAdrian.Frost@Sun.COM {
5497349SAdrian.Frost@Sun.COM 	apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect | AV_MASK);
5507349SAdrian.Frost@Sun.COM 	return (0);
5517349SAdrian.Frost@Sun.COM }
5527349SAdrian.Frost@Sun.COM 
5537349SAdrian.Frost@Sun.COM /*ARGSUSED*/
5547349SAdrian.Frost@Sun.COM static int
5557349SAdrian.Frost@Sun.COM cmci_cpu_setup(cpu_setup_t what, int cpuid, void *arg)
5567349SAdrian.Frost@Sun.COM {
5577349SAdrian.Frost@Sun.COM 	cpuset_t	cpu_set;
5587349SAdrian.Frost@Sun.COM 
5597349SAdrian.Frost@Sun.COM 	CPUSET_ONLY(cpu_set, cpuid);
5607349SAdrian.Frost@Sun.COM 
5617349SAdrian.Frost@Sun.COM 	switch (what) {
5627349SAdrian.Frost@Sun.COM 		case CPU_ON:
5639489SJoe.Bonasera@sun.com 			xc_call(NULL, NULL, NULL, CPUSET2BV(cpu_set),
5647349SAdrian.Frost@Sun.COM 			    (xc_func_t)apic_cmci_enable);
5657349SAdrian.Frost@Sun.COM 			break;
5667349SAdrian.Frost@Sun.COM 
5677349SAdrian.Frost@Sun.COM 		case CPU_OFF:
5689489SJoe.Bonasera@sun.com 			xc_call(NULL, NULL, NULL, CPUSET2BV(cpu_set),
5697349SAdrian.Frost@Sun.COM 			    (xc_func_t)apic_cmci_disable);
5707349SAdrian.Frost@Sun.COM 			break;
5717349SAdrian.Frost@Sun.COM 
5727349SAdrian.Frost@Sun.COM 		default:
5737349SAdrian.Frost@Sun.COM 			break;
5747349SAdrian.Frost@Sun.COM 	}
5757349SAdrian.Frost@Sun.COM 
5767349SAdrian.Frost@Sun.COM 	return (0);
5777349SAdrian.Frost@Sun.COM }
5787349SAdrian.Frost@Sun.COM 
5790Sstevel@tonic-gate static void
5800Sstevel@tonic-gate apic_init_intr()
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	processorid_t	cpun = psm_get_cpu_id();
5836896Sdmick 	uint_t nlvt;
5847282Smishra 	uint32_t svr = AV_UNIT_ENABLE | APIC_SPUR_INTR;
5850Sstevel@tonic-gate 
5867282Smishra 	apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
5870Sstevel@tonic-gate 
5887282Smishra 	if (apic_mode == LOCAL_APIC) {
5897282Smishra 		/*
5907282Smishra 		 * We are running APIC in MMIO mode.
5917282Smishra 		 */
5927282Smishra 		if (apic_flat_model) {
5937282Smishra 			apic_reg_ops->apic_write(APIC_FORMAT_REG,
5947282Smishra 			    APIC_FLAT_MODEL);
5957282Smishra 		} else {
5967282Smishra 			apic_reg_ops->apic_write(APIC_FORMAT_REG,
5977282Smishra 			    APIC_CLUSTER_MODEL);
5987282Smishra 		}
5997282Smishra 
6007282Smishra 		apic_reg_ops->apic_write(APIC_DEST_REG,
6017282Smishra 		    AV_HIGH_ORDER >> cpun);
6027282Smishra 	}
6037282Smishra 
60410345SSaurabh.Mishra@Sun.COM 	if (apic_directed_EOI_supported()) {
6057282Smishra 		/*
60610345SSaurabh.Mishra@Sun.COM 		 * Setting the 12th bit in the Spurious Interrupt Vector
60710345SSaurabh.Mishra@Sun.COM 		 * Register suppresses broadcast EOIs generated by the local
60810345SSaurabh.Mishra@Sun.COM 		 * APIC. The suppression of broadcast EOIs happens only when
60910345SSaurabh.Mishra@Sun.COM 		 * interrupts are level-triggered.
6107282Smishra 		 */
61110345SSaurabh.Mishra@Sun.COM 		svr |= APIC_SVR_SUPPRESS_BROADCAST_EOI;
6127282Smishra 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/* need to enable APIC before unmasking NMI */
6157282Smishra 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, svr);
6160Sstevel@tonic-gate 
6176896Sdmick 	/*
6186896Sdmick 	 * Presence of an invalid vector with delivery mode AV_FIXED can
6196896Sdmick 	 * cause an error interrupt, even if the entry is masked...so
6206896Sdmick 	 * write a valid vector to LVT entries along with the mask bit
6216896Sdmick 	 */
6226896Sdmick 
6236896Sdmick 	/* All APICs have timer and LINT0/1 */
6247282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK|APIC_RESV_IRQ);
6257282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK|APIC_RESV_IRQ);
6267282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT1, AV_NMI);	/* enable NMI */
6270Sstevel@tonic-gate 
6286896Sdmick 	/*
6296896Sdmick 	 * On integrated APICs, the number of LVT entries is
6306896Sdmick 	 * 'Max LVT entry' + 1; on 82489DX's (non-integrated
6316896Sdmick 	 * APICs), nlvt is "3" (LINT0, LINT1, and timer)
6326896Sdmick 	 */
6330Sstevel@tonic-gate 
6346896Sdmick 	if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) {
6356896Sdmick 		nlvt = 3;
6366896Sdmick 	} else {
6378767SSaurabh.Mishra@Sun.COM 		nlvt = ((apic_reg_ops->apic_read(APIC_VERS_REG) >> 16) &
6388767SSaurabh.Mishra@Sun.COM 		    0xFF) + 1;
6396896Sdmick 	}
6406896Sdmick 
6416896Sdmick 	if (nlvt >= 5) {
6426896Sdmick 		/* Enable performance counter overflow interrupt */
6436896Sdmick 
6446896Sdmick 		if ((x86_feature & X86_MSR) != X86_MSR)
6456896Sdmick 			apic_enable_cpcovf_intr = 0;
6466896Sdmick 		if (apic_enable_cpcovf_intr) {
6476896Sdmick 			if (apic_cpcovf_vect == 0) {
6486896Sdmick 				int ipl = APIC_PCINT_IPL;
6496896Sdmick 				int irq = apic_get_ipivect(ipl, -1);
6500Sstevel@tonic-gate 
6516896Sdmick 				ASSERT(irq != -1);
6526896Sdmick 				apic_cpcovf_vect =
6536896Sdmick 				    apic_irq_table[irq]->airq_vector;
6546896Sdmick 				ASSERT(apic_cpcovf_vect);
6556896Sdmick 				(void) add_avintr(NULL, ipl,
6566896Sdmick 				    (avfunc)kcpc_hw_overflow_intr,
6576896Sdmick 				    "apic pcint", irq, NULL, NULL, NULL, NULL);
6586896Sdmick 				kcpc_hw_overflow_intr_installed = 1;
6596896Sdmick 				kcpc_hw_enable_cpc_intr =
6606896Sdmick 				    apic_cpcovf_mask_clear;
6616896Sdmick 			}
6627282Smishra 			apic_reg_ops->apic_write(APIC_PCINT_VECT,
6637282Smishra 			    apic_cpcovf_vect);
6646896Sdmick 		}
6656896Sdmick 	}
6660Sstevel@tonic-gate 
6676896Sdmick 	if (nlvt >= 6) {
6686896Sdmick 		/* Only mask TM intr if the BIOS apparently doesn't use it */
6696896Sdmick 
6706896Sdmick 		uint32_t lvtval;
6716896Sdmick 
6727282Smishra 		lvtval = apic_reg_ops->apic_read(APIC_THERM_VECT);
6736896Sdmick 		if (((lvtval & AV_MASK) == AV_MASK) ||
6746896Sdmick 		    ((lvtval & AV_DELIV_MODE) != AV_SMI)) {
6757282Smishra 			apic_reg_ops->apic_write(APIC_THERM_VECT,
6767282Smishra 			    AV_MASK|APIC_RESV_IRQ);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/* Enable error interrupt */
6810Sstevel@tonic-gate 
6826896Sdmick 	if (nlvt >= 4 && apic_enable_error_intr) {
6830Sstevel@tonic-gate 		if (apic_errvect == 0) {
6840Sstevel@tonic-gate 			int ipl = 0xf;	/* get highest priority intr */
6850Sstevel@tonic-gate 			int irq = apic_get_ipivect(ipl, -1);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 			ASSERT(irq != -1);
6880Sstevel@tonic-gate 			apic_errvect = apic_irq_table[irq]->airq_vector;
6890Sstevel@tonic-gate 			ASSERT(apic_errvect);
6900Sstevel@tonic-gate 			/*
6910Sstevel@tonic-gate 			 * Not PSMI compliant, but we are going to merge
6920Sstevel@tonic-gate 			 * with ON anyway
6930Sstevel@tonic-gate 			 */
6940Sstevel@tonic-gate 			(void) add_avintr((void *)NULL, ipl,
6950Sstevel@tonic-gate 			    (avfunc)apic_error_intr, "apic error intr",
696916Sschwartz 			    irq, NULL, NULL, NULL, NULL);
6970Sstevel@tonic-gate 		}
6987282Smishra 		apic_reg_ops->apic_write(APIC_ERR_VECT, apic_errvect);
6997282Smishra 		apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
7007282Smishra 		apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
7010Sstevel@tonic-gate 	}
7026896Sdmick 
7037349SAdrian.Frost@Sun.COM 	/* Enable CMCI interrupt */
7047349SAdrian.Frost@Sun.COM 	if (cmi_enable_cmci) {
7057349SAdrian.Frost@Sun.COM 
7067349SAdrian.Frost@Sun.COM 		mutex_enter(&cmci_cpu_setup_lock);
7077349SAdrian.Frost@Sun.COM 		if (cmci_cpu_setup_registered == 0) {
7087349SAdrian.Frost@Sun.COM 			mutex_enter(&cpu_lock);
7097349SAdrian.Frost@Sun.COM 			register_cpu_setup_func(cmci_cpu_setup, NULL);
7107349SAdrian.Frost@Sun.COM 			mutex_exit(&cpu_lock);
7117349SAdrian.Frost@Sun.COM 			cmci_cpu_setup_registered = 1;
7127349SAdrian.Frost@Sun.COM 		}
7137349SAdrian.Frost@Sun.COM 		mutex_exit(&cmci_cpu_setup_lock);
7147349SAdrian.Frost@Sun.COM 
7157349SAdrian.Frost@Sun.COM 		if (apic_cmci_vect == 0) {
7167349SAdrian.Frost@Sun.COM 			int ipl = 0x2;
7177349SAdrian.Frost@Sun.COM 			int irq = apic_get_ipivect(ipl, -1);
7187349SAdrian.Frost@Sun.COM 
7197349SAdrian.Frost@Sun.COM 			ASSERT(irq != -1);
7207349SAdrian.Frost@Sun.COM 			apic_cmci_vect = apic_irq_table[irq]->airq_vector;
7217349SAdrian.Frost@Sun.COM 			ASSERT(apic_cmci_vect);
7227349SAdrian.Frost@Sun.COM 
7237349SAdrian.Frost@Sun.COM 			(void) add_avintr(NULL, ipl,
7247349SAdrian.Frost@Sun.COM 			    (avfunc)cmi_cmci_trap,
7257349SAdrian.Frost@Sun.COM 			    "apic cmci intr", irq, NULL, NULL, NULL, NULL);
7267349SAdrian.Frost@Sun.COM 		}
7277349SAdrian.Frost@Sun.COM 		apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
7287349SAdrian.Frost@Sun.COM 	}
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate static void
7320Sstevel@tonic-gate apic_disable_local_apic()
7330Sstevel@tonic-gate {
7347282Smishra 	apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
7357282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK);
7367282Smishra 
7377282Smishra 	/* local intr reg 0 */
7387282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK);
7397282Smishra 
7407282Smishra 	/* disable NMI */
7417282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT1, AV_MASK);
7427282Smishra 
7437282Smishra 	/* and error interrupt */
7447282Smishra 	apic_reg_ops->apic_write(APIC_ERR_VECT, AV_MASK);
7457282Smishra 
7467282Smishra 	/* and perf counter intr */
7477282Smishra 	apic_reg_ops->apic_write(APIC_PCINT_VECT, AV_MASK);
7487282Smishra 
7497282Smishra 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, APIC_SPUR_INTR);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate static void
7530Sstevel@tonic-gate apic_picinit(void)
7540Sstevel@tonic-gate {
7553446Smrj 	int i, j;
7560Sstevel@tonic-gate 	uint_t isr;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*
75911383SSaurabh.Mishra@Sun.COM 	 * Initialize and enable interrupt remapping before apic
7609940SVikram.Hegde@Sun.COM 	 * hardware initialization
7619940SVikram.Hegde@Sun.COM 	 */
76211600SVikram.Hegde@Sun.COM 	apic_intrmap_init(apic_mode);
7639940SVikram.Hegde@Sun.COM 
7649940SVikram.Hegde@Sun.COM 	/*
7650Sstevel@tonic-gate 	 * On UniSys Model 6520, the BIOS leaves vector 0x20 isr
7660Sstevel@tonic-gate 	 * bit on without clearing it with EOI.  Since softint
7670Sstevel@tonic-gate 	 * uses vector 0x20 to interrupt itself, so softint will
7680Sstevel@tonic-gate 	 * not work on this machine.  In order to fix this problem
7690Sstevel@tonic-gate 	 * a check is made to verify all the isr bits are clear.
7700Sstevel@tonic-gate 	 * If not, EOIs are issued to clear the bits.
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	for (i = 7; i >= 1; i--) {
7737282Smishra 		isr = apic_reg_ops->apic_read(APIC_ISR_REG + (i * 4));
7747282Smishra 		if (isr != 0)
7750Sstevel@tonic-gate 			for (j = 0; ((j < 32) && (isr != 0)); j++)
7760Sstevel@tonic-gate 				if (isr & (1 << j)) {
7777282Smishra 					apic_reg_ops->apic_write(
7787282Smishra 					    APIC_EOI_REG, 0);
7790Sstevel@tonic-gate 					isr &= ~(1 << j);
7800Sstevel@tonic-gate 					apic_error |= APIC_ERR_BOOT_EOI;
7810Sstevel@tonic-gate 				}
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/* set a flag so we know we have run apic_picinit() */
7855084Sjohnlev 	apic_picinit_called = 1;
7860Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_gethrtime_lock);
7870Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_ioapic_lock);
7880Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_error_lock);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	picsetup();	 /* initialise the 8259 */
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* add nmi handler - least priority nmi handler */
7930Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&apic_nmi_lock);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr,
7960Sstevel@tonic-gate 	    "pcplusmp NMI handler", (caddr_t)NULL))
7970Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler");
7980Sstevel@tonic-gate 
79911383SSaurabh.Mishra@Sun.COM 	/*
80011383SSaurabh.Mishra@Sun.COM 	 * Check for directed-EOI capability in the local APIC.
80111383SSaurabh.Mishra@Sun.COM 	 */
80211383SSaurabh.Mishra@Sun.COM 	if (apic_directed_EOI_supported() == 1) {
80311383SSaurabh.Mishra@Sun.COM 		apic_set_directed_EOI_handler();
80411383SSaurabh.Mishra@Sun.COM 	}
80511383SSaurabh.Mishra@Sun.COM 
8060Sstevel@tonic-gate 	apic_init_intr();
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	/* enable apic mode if imcr present */
8090Sstevel@tonic-gate 	if (apic_imcrp) {
8100Sstevel@tonic-gate 		outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
8110Sstevel@tonic-gate 		outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8143446Smrj 	ioapic_init_intr(IOAPIC_MASK);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 
8183446Smrj /*ARGSUSED1*/
8193446Smrj static int
8203446Smrj apic_cpu_start(processorid_t cpun, caddr_t arg)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	int		loop_count;
8230Sstevel@tonic-gate 	uint32_t	vector;
8243446Smrj 	uint_t		cpu_id;
8253446Smrj 	ulong_t		iflag;
8260Sstevel@tonic-gate 
8277282Smishra 	cpu_id =  apic_cpus[cpun].aci_local_id;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	apic_cmos_ssb_set = 1;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * Interrupts on BSP cpu will be disabled during these startup
8330Sstevel@tonic-gate 	 * steps in order to avoid unwanted side effects from
8340Sstevel@tonic-gate 	 * executing interrupt handlers on a problematic BIOS.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	iflag = intr_clear();
8380Sstevel@tonic-gate 	outb(CMOS_ADDR, SSB);
8390Sstevel@tonic-gate 	outb(CMOS_DATA, BIOS_SHUTDOWN);
8400Sstevel@tonic-gate 
8417798SSaurabh.Mishra@Sun.COM 	/*
8427986SSaurabh.Mishra@Sun.COM 	 * According to X2APIC specification in section '2.3.5.1' of
8437798SSaurabh.Mishra@Sun.COM 	 * Interrupt Command Register Semantics, the semantics of
8447798SSaurabh.Mishra@Sun.COM 	 * programming the Interrupt Command Register to dispatch an interrupt
8457798SSaurabh.Mishra@Sun.COM 	 * is simplified. A single MSR write to the 64-bit ICR is required
8467798SSaurabh.Mishra@Sun.COM 	 * for dispatching an interrupt. Specifically, with the 64-bit MSR
8477798SSaurabh.Mishra@Sun.COM 	 * interface to ICR, system software is not required to check the
8487798SSaurabh.Mishra@Sun.COM 	 * status of the delivery status bit prior to writing to the ICR
8497798SSaurabh.Mishra@Sun.COM 	 * to send an IPI. With the removal of the Delivery Status bit,
8507798SSaurabh.Mishra@Sun.COM 	 * system software no longer has a reason to read the ICR. It remains
8517798SSaurabh.Mishra@Sun.COM 	 * readable only to aid in debugging.
8527798SSaurabh.Mishra@Sun.COM 	 */
8537798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
8547798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
8557798SSaurabh.Mishra@Sun.COM #else
8567798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC) {
8577798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
8587798SSaurabh.Mishra@Sun.COM 	}
8597798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/* for integrated - make sure there is one INIT IPI in buffer */
8620Sstevel@tonic-gate 	/* for external - it will wake up the cpu */
8637282Smishra 	apic_reg_ops->apic_write_int_cmd(cpu_id, AV_ASSERT | AV_RESET);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/* If only 1 CPU is installed, PENDING bit will not go low */
8667798SSaurabh.Mishra@Sun.COM 	for (loop_count = 0x1000; loop_count; loop_count--) {
8677798SSaurabh.Mishra@Sun.COM 		if (apic_mode == LOCAL_APIC &&
8687798SSaurabh.Mishra@Sun.COM 		    apic_reg_ops->apic_read(APIC_INT_CMD1) & AV_PENDING)
8690Sstevel@tonic-gate 			apic_ret();
8700Sstevel@tonic-gate 		else
8710Sstevel@tonic-gate 			break;
8727798SSaurabh.Mishra@Sun.COM 	}
8730Sstevel@tonic-gate 
8747282Smishra 	apic_reg_ops->apic_write_int_cmd(cpu_id, AV_DEASSERT | AV_RESET);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	drv_usecwait(20000);		/* 20 milli sec */
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (apic_cpus[cpun].aci_local_ver >= APIC_INTEGRATED_VERS) {
8790Sstevel@tonic-gate 		/* integrated apic */
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		vector = (rm_platter_pa >> MMU_PAGESHIFT) &
8820Sstevel@tonic-gate 		    (APIC_VECTOR_MASK | APIC_IPL_MASK);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		/* to offset the INIT IPI queue up in the buffer */
8857282Smishra 		apic_reg_ops->apic_write_int_cmd(cpu_id, vector | AV_STARTUP);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		drv_usecwait(200);		/* 20 micro sec */
8880Sstevel@tonic-gate 
8897282Smishra 		apic_reg_ops->apic_write_int_cmd(cpu_id, vector | AV_STARTUP);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		drv_usecwait(200);		/* 20 micro sec */
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 	intr_restore(iflag);
8943446Smrj 	return (0);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate #ifdef	DEBUG
8990Sstevel@tonic-gate int	apic_break_on_cpu = 9;
9000Sstevel@tonic-gate int	apic_stretch_interrupts = 0;
9010Sstevel@tonic-gate int	apic_stretch_ISR = 1 << 3;	/* IPL of 3 matches nothing now */
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate void
9040Sstevel@tonic-gate apic_break()
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate #endif /* DEBUG */
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate  * platform_intr_enter
9110Sstevel@tonic-gate  *
9120Sstevel@tonic-gate  *	Called at the beginning of the interrupt service routine to
9130Sstevel@tonic-gate  *	mask all level equal to and below the interrupt priority
9140Sstevel@tonic-gate  *	of the interrupting vector.  An EOI should be given to
9150Sstevel@tonic-gate  *	the interrupt controller to enable other HW interrupts.
9160Sstevel@tonic-gate  *
9170Sstevel@tonic-gate  *	Return -1 for spurious interrupts
9180Sstevel@tonic-gate  *
9190Sstevel@tonic-gate  */
9200Sstevel@tonic-gate /*ARGSUSED*/
9210Sstevel@tonic-gate static int
9220Sstevel@tonic-gate apic_intr_enter(int ipl, int *vectorp)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	uchar_t vector;
9250Sstevel@tonic-gate 	int nipl;
9263446Smrj 	int irq;
9273446Smrj 	ulong_t iflag;
9280Sstevel@tonic-gate 	apic_cpus_info_t *cpu_infop;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/*
9313745Ssethg 	 * The real vector delivered is (*vectorp + 0x20), but our caller
9323745Ssethg 	 * subtracts 0x20 from the vector before passing it to us.
9333745Ssethg 	 * (That's why APIC_BASE_VECT is 0x20.)
9340Sstevel@tonic-gate 	 */
9350Sstevel@tonic-gate 	vector = (uchar_t)*vectorp;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/* if interrupted by the clock, increment apic_nsec_since_boot */
9380Sstevel@tonic-gate 	if (vector == apic_clkvect) {
9390Sstevel@tonic-gate 		if (!apic_oneshot) {
9400Sstevel@tonic-gate 			/* NOTE: this is not MT aware */
9410Sstevel@tonic-gate 			apic_hrtime_stamp++;
9420Sstevel@tonic-gate 			apic_nsec_since_boot += apic_nsec_per_intr;
9430Sstevel@tonic-gate 			apic_hrtime_stamp++;
9440Sstevel@tonic-gate 			last_count_read = apic_hertz_count;
9450Sstevel@tonic-gate 			apic_redistribute_compute();
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		/* We will avoid all the book keeping overhead for clock */
9493745Ssethg 		nipl = apic_ipls[vector];
9503745Ssethg 
9517282Smishra 		*vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT];
9527282Smishra 		if (apic_mode == LOCAL_APIC) {
9530Sstevel@tonic-gate #if defined(__amd64)
9547282Smishra 			setcr8((ulong_t)(apic_ipltopri[nipl] >>
9557282Smishra 			    APIC_IPL_SHIFT));
9560Sstevel@tonic-gate #else
95710080SJoe.Bonasera@sun.com 			if (apic_have_32bit_cr8)
95810080SJoe.Bonasera@sun.com 				setcr8((ulong_t)(apic_ipltopri[nipl] >>
95910080SJoe.Bonasera@sun.com 				    APIC_IPL_SHIFT));
96010080SJoe.Bonasera@sun.com 			else
96110080SJoe.Bonasera@sun.com 				LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
96210080SJoe.Bonasera@sun.com 				    (uint32_t)apic_ipltopri[nipl]);
9630Sstevel@tonic-gate #endif
9647282Smishra 			LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
9657282Smishra 		} else {
9667282Smishra 			X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
9677282Smishra 			X2APIC_WRITE(APIC_EOI_REG, 0);
9687282Smishra 		}
9697282Smishra 
9700Sstevel@tonic-gate 		return (nipl);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	cpu_infop = &apic_cpus[psm_get_cpu_id()];
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) {
9760Sstevel@tonic-gate 		cpu_infop->aci_spur_cnt++;
9770Sstevel@tonic-gate 		return (APIC_INT_SPURIOUS);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* Check if the vector we got is really what we need */
9810Sstevel@tonic-gate 	if (apic_revector_pending) {
9820Sstevel@tonic-gate 		/*
9830Sstevel@tonic-gate 		 * Disable interrupts for the duration of
9840Sstevel@tonic-gate 		 * the vector translation to prevent a self-race for
9850Sstevel@tonic-gate 		 * the apic_revector_lock.  This cannot be done
9860Sstevel@tonic-gate 		 * in apic_xlate_vector because it is recursive and
9870Sstevel@tonic-gate 		 * we want the vector translation to be atomic with
9880Sstevel@tonic-gate 		 * respect to other (higher-priority) interrupts.
9890Sstevel@tonic-gate 		 */
9900Sstevel@tonic-gate 		iflag = intr_clear();
9910Sstevel@tonic-gate 		vector = apic_xlate_vector(vector + APIC_BASE_VECT) -
9920Sstevel@tonic-gate 		    APIC_BASE_VECT;
9930Sstevel@tonic-gate 		intr_restore(iflag);
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9963745Ssethg 	nipl = apic_ipls[vector];
9970Sstevel@tonic-gate 	*vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT];
9980Sstevel@tonic-gate 
9997282Smishra 	if (apic_mode == LOCAL_APIC) {
10000Sstevel@tonic-gate #if defined(__amd64)
10017282Smishra 		setcr8((ulong_t)(apic_ipltopri[nipl] >> APIC_IPL_SHIFT));
10020Sstevel@tonic-gate #else
100310080SJoe.Bonasera@sun.com 		if (apic_have_32bit_cr8)
100410080SJoe.Bonasera@sun.com 			setcr8((ulong_t)(apic_ipltopri[nipl] >>
100510080SJoe.Bonasera@sun.com 			    APIC_IPL_SHIFT));
100610080SJoe.Bonasera@sun.com 		else
100710080SJoe.Bonasera@sun.com 			LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
100810080SJoe.Bonasera@sun.com 			    (uint32_t)apic_ipltopri[nipl]);
10090Sstevel@tonic-gate #endif
10107282Smishra 	} else {
10117282Smishra 		X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
10127282Smishra 	}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	cpu_infop->aci_current[nipl] = (uchar_t)irq;
10150Sstevel@tonic-gate 	cpu_infop->aci_curipl = (uchar_t)nipl;
10160Sstevel@tonic-gate 	cpu_infop->aci_ISR_in_progress |= 1 << nipl;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * apic_level_intr could have been assimilated into the irq struct.
10200Sstevel@tonic-gate 	 * but, having it as a character array is more efficient in terms of
10210Sstevel@tonic-gate 	 * cache usage. So, we leave it as is.
10220Sstevel@tonic-gate 	 */
10237282Smishra 	if (!apic_level_intr[irq]) {
10248918SSaurabh.Mishra@Sun.COM 		if (apic_mode == LOCAL_APIC) {
10257282Smishra 			LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
10268918SSaurabh.Mishra@Sun.COM 		} else {
10277282Smishra 			X2APIC_WRITE(APIC_EOI_REG, 0);
10288918SSaurabh.Mishra@Sun.COM 		}
10297282Smishra 	}
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate #ifdef	DEBUG
10320Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(vector);
10330Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(irq);
10340Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(nipl);
10350Sstevel@tonic-gate 	APIC_DEBUG_BUF_PUT(psm_get_cpu_id());
10360Sstevel@tonic-gate 	if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl)))
10370Sstevel@tonic-gate 		drv_usecwait(apic_stretch_interrupts);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	if (apic_break_on_cpu == psm_get_cpu_id())
10400Sstevel@tonic-gate 		apic_break();
10410Sstevel@tonic-gate #endif /* DEBUG */
10420Sstevel@tonic-gate 	return (nipl);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10457282Smishra /*
10467986SSaurabh.Mishra@Sun.COM  * This macro is a common code used by MMIO local apic and X2APIC
10477282Smishra  * local apic.
10487282Smishra  */
10497282Smishra #define	APIC_INTR_EXIT() \
10507282Smishra { \
10517282Smishra 	cpu_infop = &apic_cpus[psm_get_cpu_id()]; \
10527282Smishra 	if (apic_level_intr[irq]) \
10537282Smishra 		apic_reg_ops->apic_send_eoi(irq); \
10547282Smishra 	cpu_infop->aci_curipl = (uchar_t)prev_ipl; \
10557282Smishra 	/* ISR above current pri could not be in progress */ \
10567282Smishra 	cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; \
10577282Smishra }
10587282Smishra 
10597282Smishra /*
10607986SSaurabh.Mishra@Sun.COM  * Any changes made to this function must also change X2APIC
10617282Smishra  * version of intr_exit.
10627282Smishra  */
10633446Smrj void
10640Sstevel@tonic-gate apic_intr_exit(int prev_ipl, int irq)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	apic_cpus_info_t *cpu_infop;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate #if defined(__amd64)
10690Sstevel@tonic-gate 	setcr8((ulong_t)apic_cr8pri[prev_ipl]);
10700Sstevel@tonic-gate #else
107110080SJoe.Bonasera@sun.com 	if (apic_have_32bit_cr8)
107210080SJoe.Bonasera@sun.com 		setcr8((ulong_t)(apic_ipltopri[prev_ipl] >> APIC_IPL_SHIFT));
107310080SJoe.Bonasera@sun.com 	else
107410080SJoe.Bonasera@sun.com 		apicadr[APIC_TASK_REG] = apic_ipltopri[prev_ipl];
10750Sstevel@tonic-gate #endif
10760Sstevel@tonic-gate 
10777282Smishra 	APIC_INTR_EXIT();
10787282Smishra }
10790Sstevel@tonic-gate 
10807282Smishra /*
10817282Smishra  * Same as apic_intr_exit() except it uses MSR rather than MMIO
10827282Smishra  * to access local apic registers.
10837282Smishra  */
10847282Smishra void
10857282Smishra x2apic_intr_exit(int prev_ipl, int irq)
10867282Smishra {
10877282Smishra 	apic_cpus_info_t *cpu_infop;
10887282Smishra 
10897282Smishra 	X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[prev_ipl]);
10907282Smishra 	APIC_INTR_EXIT();
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10935084Sjohnlev intr_exit_fn_t
10945084Sjohnlev psm_intr_exit_fn(void)
10955084Sjohnlev {
10967282Smishra 	if (apic_mode == LOCAL_X2APIC)
10977282Smishra 		return (x2apic_intr_exit);
10987282Smishra 
10995084Sjohnlev 	return (apic_intr_exit);
11005084Sjohnlev }
11015084Sjohnlev 
11020Sstevel@tonic-gate /*
11037282Smishra  * Mask all interrupts below or equal to the given IPL.
11047986SSaurabh.Mishra@Sun.COM  * Any changes made to this function must also change X2APIC
11057282Smishra  * version of setspl.
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate static void
11080Sstevel@tonic-gate apic_setspl(int ipl)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate #if defined(__amd64)
11110Sstevel@tonic-gate 	setcr8((ulong_t)apic_cr8pri[ipl]);
11120Sstevel@tonic-gate #else
111310080SJoe.Bonasera@sun.com 	if (apic_have_32bit_cr8)
111410080SJoe.Bonasera@sun.com 		setcr8((ulong_t)(apic_ipltopri[ipl] >> APIC_IPL_SHIFT));
111510080SJoe.Bonasera@sun.com 	else
111610080SJoe.Bonasera@sun.com 		apicadr[APIC_TASK_REG] = apic_ipltopri[ipl];
11170Sstevel@tonic-gate #endif
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/* interrupts at ipl above this cannot be in progress */
11200Sstevel@tonic-gate 	apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
11210Sstevel@tonic-gate 	/*
11220Sstevel@tonic-gate 	 * this is a patch fix for the ALR QSMP P5 machine, so that interrupts
11230Sstevel@tonic-gate 	 * have enough time to come in before the priority is raised again
11240Sstevel@tonic-gate 	 * during the idle() loop.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	if (apic_setspl_delay)
11277282Smishra 		(void) apic_reg_ops->apic_get_pri();
11287282Smishra }
11297282Smishra 
11307282Smishra /*
11317986SSaurabh.Mishra@Sun.COM  * X2APIC version of setspl.
11327282Smishra  * Mask all interrupts below or equal to the given IPL
11337282Smishra  */
11347282Smishra static void
11357282Smishra x2apic_setspl(int ipl)
11367282Smishra {
11377282Smishra 	X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[ipl]);
11387282Smishra 
11397282Smishra 	/* interrupts at ipl above this cannot be in progress */
11407282Smishra 	apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate /*
11447986SSaurabh.Mishra@Sun.COM  * generates an interprocessor interrupt to another CPU. Any changes made to
11457986SSaurabh.Mishra@Sun.COM  * this routine must be accompanied by similar changes to
11467986SSaurabh.Mishra@Sun.COM  * apic_common_send_ipi().
11470Sstevel@tonic-gate  */
11480Sstevel@tonic-gate static void
11490Sstevel@tonic-gate apic_send_ipi(int cpun, int ipl)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	int vector;
11523446Smrj 	ulong_t flag;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	vector = apic_resv_vector[ipl];
11550Sstevel@tonic-gate 
11566896Sdmick 	ASSERT((vector >= APIC_BASE_VECT) && (vector <= APIC_SPUR_INTR));
11576896Sdmick 
11580Sstevel@tonic-gate 	flag = intr_clear();
11590Sstevel@tonic-gate 
11607798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
11610Sstevel@tonic-gate 
11627282Smishra 	apic_reg_ops->apic_write_int_cmd(apic_cpus[cpun].aci_local_id,
11637282Smishra 	    vector);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	intr_restore(flag);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*ARGSUSED*/
11700Sstevel@tonic-gate static void
11710Sstevel@tonic-gate apic_set_idlecpu(processorid_t cpun)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate /*ARGSUSED*/
11760Sstevel@tonic-gate static void
11770Sstevel@tonic-gate apic_unset_idlecpu(processorid_t cpun)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 
11827282Smishra void
11830Sstevel@tonic-gate apic_ret()
11840Sstevel@tonic-gate {
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate  * If apic_coarse_time == 1, then apic_gettime() is used instead of
11890Sstevel@tonic-gate  * apic_gethrtime().  This is used for performance instead of accuracy.
11900Sstevel@tonic-gate  */
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate static hrtime_t
11930Sstevel@tonic-gate apic_gettime()
11940Sstevel@tonic-gate {
11950Sstevel@tonic-gate 	int old_hrtime_stamp;
11960Sstevel@tonic-gate 	hrtime_t temp;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	/*
11990Sstevel@tonic-gate 	 * In one-shot mode, we do not keep time, so if anyone
12000Sstevel@tonic-gate 	 * calls psm_gettime() directly, we vector over to
12010Sstevel@tonic-gate 	 * gethrtime().
12020Sstevel@tonic-gate 	 * one-shot mode MUST NOT be enabled if this psm is the source of
12030Sstevel@tonic-gate 	 * hrtime.
12040Sstevel@tonic-gate 	 */
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	if (apic_oneshot)
12070Sstevel@tonic-gate 		return (gethrtime());
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate gettime_again:
12110Sstevel@tonic-gate 	while ((old_hrtime_stamp = apic_hrtime_stamp) & 1)
12120Sstevel@tonic-gate 		apic_ret();
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	temp = apic_nsec_since_boot;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (apic_hrtime_stamp != old_hrtime_stamp) {	/* got an interrupt */
12170Sstevel@tonic-gate 		goto gettime_again;
12180Sstevel@tonic-gate 	}
12190Sstevel@tonic-gate 	return (temp);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate /*
12230Sstevel@tonic-gate  * Here we return the number of nanoseconds since booting.  Note every
12240Sstevel@tonic-gate  * clock interrupt increments apic_nsec_since_boot by the appropriate
12250Sstevel@tonic-gate  * amount.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate static hrtime_t
12280Sstevel@tonic-gate apic_gethrtime()
12290Sstevel@tonic-gate {
12303446Smrj 	int curr_timeval, countval, elapsed_ticks;
12310Sstevel@tonic-gate 	int old_hrtime_stamp, status;
12320Sstevel@tonic-gate 	hrtime_t temp;
12337282Smishra 	uint32_t cpun;
12343446Smrj 	ulong_t oflags;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * In one-shot mode, we do not keep time, so if anyone
12380Sstevel@tonic-gate 	 * calls psm_gethrtime() directly, we vector over to
12390Sstevel@tonic-gate 	 * gethrtime().
12400Sstevel@tonic-gate 	 * one-shot mode MUST NOT be enabled if this psm is the source of
12410Sstevel@tonic-gate 	 * hrtime.
12420Sstevel@tonic-gate 	 */
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (apic_oneshot)
12450Sstevel@tonic-gate 		return (gethrtime());
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	oflags = intr_clear();	/* prevent migration */
12480Sstevel@tonic-gate 
12497282Smishra 	cpun = apic_reg_ops->apic_read(APIC_LID_REG);
12507282Smishra 	if (apic_mode == LOCAL_APIC)
12517282Smishra 		cpun >>= APIC_ID_BIT_OFFSET;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	lock_set(&apic_gethrtime_lock);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate gethrtime_again:
12560Sstevel@tonic-gate 	while ((old_hrtime_stamp = apic_hrtime_stamp) & 1)
12570Sstevel@tonic-gate 		apic_ret();
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12600Sstevel@tonic-gate 	 * Check to see which CPU we are on.  Note the time is kept on
12610Sstevel@tonic-gate 	 * the local APIC of CPU 0.  If on CPU 0, simply read the current
12620Sstevel@tonic-gate 	 * counter.  If on another CPU, issue a remote read command to CPU 0.
12630Sstevel@tonic-gate 	 */
12640Sstevel@tonic-gate 	if (cpun == apic_cpus[0].aci_local_id) {
12657282Smishra 		countval = apic_reg_ops->apic_read(APIC_CURR_COUNT);
12660Sstevel@tonic-gate 	} else {
12677798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
12687798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
12697798SSaurabh.Mishra@Sun.COM #else
12707798SSaurabh.Mishra@Sun.COM 		if (apic_mode == LOCAL_APIC)
12717798SSaurabh.Mishra@Sun.COM 			APIC_AV_PENDING_SET();
12727798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
12730Sstevel@tonic-gate 
12747282Smishra 		apic_reg_ops->apic_write_int_cmd(
12757282Smishra 		    apic_cpus[0].aci_local_id, APIC_CURR_ADD | AV_REMOTE);
12760Sstevel@tonic-gate 
12777282Smishra 		while ((status = apic_reg_ops->apic_read(APIC_INT_CMD1))
12787282Smishra 		    & AV_READ_PENDING) {
12790Sstevel@tonic-gate 			apic_ret();
12807282Smishra 		}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		if (status & AV_REMOTE_STATUS)	/* 1 = valid */
12837282Smishra 			countval = apic_reg_ops->apic_read(APIC_REMOTE_READ);
12840Sstevel@tonic-gate 		else {	/* 0 = invalid */
12850Sstevel@tonic-gate 			apic_remote_hrterr++;
12860Sstevel@tonic-gate 			/*
12870Sstevel@tonic-gate 			 * return last hrtime right now, will need more
12880Sstevel@tonic-gate 			 * testing if change to retry
12890Sstevel@tonic-gate 			 */
12900Sstevel@tonic-gate 			temp = apic_last_hrtime;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 			lock_clear(&apic_gethrtime_lock);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 			intr_restore(oflags);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 			return (temp);
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 	if (countval > last_count_read)
13000Sstevel@tonic-gate 		countval = 0;
13010Sstevel@tonic-gate 	else
13020Sstevel@tonic-gate 		last_count_read = countval;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	elapsed_ticks = apic_hertz_count - countval;
13050Sstevel@tonic-gate 
13062992Sdmick 	curr_timeval = APIC_TICKS_TO_NSECS(elapsed_ticks);
13070Sstevel@tonic-gate 	temp = apic_nsec_since_boot + curr_timeval;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if (apic_hrtime_stamp != old_hrtime_stamp) {	/* got an interrupt */
13100Sstevel@tonic-gate 		/* we might have clobbered last_count_read. Restore it */
13110Sstevel@tonic-gate 		last_count_read = apic_hertz_count;
13120Sstevel@tonic-gate 		goto gethrtime_again;
13130Sstevel@tonic-gate 	}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	if (temp < apic_last_hrtime) {
13160Sstevel@tonic-gate 		/* return last hrtime if error occurs */
13170Sstevel@tonic-gate 		apic_hrtime_error++;
13180Sstevel@tonic-gate 		temp = apic_last_hrtime;
13190Sstevel@tonic-gate 	}
13200Sstevel@tonic-gate 	else
13210Sstevel@tonic-gate 		apic_last_hrtime = temp;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	lock_clear(&apic_gethrtime_lock);
13240Sstevel@tonic-gate 	intr_restore(oflags);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	return (temp);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate /* apic NMI handler */
13300Sstevel@tonic-gate /*ARGSUSED*/
13310Sstevel@tonic-gate static void
13325084Sjohnlev apic_nmi_intr(caddr_t arg, struct regs *rp)
13330Sstevel@tonic-gate {
13340Sstevel@tonic-gate 	if (apic_shutdown_processors) {
13350Sstevel@tonic-gate 		apic_disable_local_apic();
13360Sstevel@tonic-gate 		return;
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13395084Sjohnlev 	apic_error |= APIC_ERR_NMI;
13405084Sjohnlev 
13415084Sjohnlev 	if (!lock_try(&apic_nmi_lock))
13425084Sjohnlev 		return;
13435084Sjohnlev 	apic_num_nmis++;
13445084Sjohnlev 
13455084Sjohnlev 	if (apic_kmdb_on_nmi && psm_debugger()) {
13465084Sjohnlev 		debug_enter("NMI received: entering kmdb\n");
13475084Sjohnlev 	} else if (apic_panic_on_nmi) {
13485084Sjohnlev 		/* Keep panic from entering kmdb. */
13495084Sjohnlev 		nopanicdebug = 1;
13505084Sjohnlev 		panic("NMI received\n");
13515084Sjohnlev 	} else {
13525084Sjohnlev 		/*
13535084Sjohnlev 		 * prom_printf is the best shot we have of something which is
13545084Sjohnlev 		 * problem free from high level/NMI type of interrupts
13555084Sjohnlev 		 */
13565084Sjohnlev 		prom_printf("NMI received\n");
13570Sstevel@tonic-gate 	}
13585084Sjohnlev 
13595084Sjohnlev 	lock_clear(&apic_nmi_lock);
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate /*ARGSUSED*/
13630Sstevel@tonic-gate static int
13640Sstevel@tonic-gate apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
13650Sstevel@tonic-gate {
13663446Smrj 	return (apic_addspl_common(irqno, ipl, min_ipl, max_ipl));
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate static int
13700Sstevel@tonic-gate apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
13710Sstevel@tonic-gate {
13723446Smrj 	return (apic_delspl_common(irqno, ipl, min_ipl,  max_ipl));
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate static int
13760Sstevel@tonic-gate apic_post_cpu_start()
13770Sstevel@tonic-gate {
13786749Ssherrym 	int cpun;
13797986SSaurabh.Mishra@Sun.COM 	static int cpus_started = 1;
13807986SSaurabh.Mishra@Sun.COM 	struct psm_ops *pops = &apic_ops;
13817986SSaurabh.Mishra@Sun.COM 
13827986SSaurabh.Mishra@Sun.COM 	/* We know this CPU + BSP  started successfully. */
13837986SSaurabh.Mishra@Sun.COM 	cpus_started++;
13840Sstevel@tonic-gate 
13857798SSaurabh.Mishra@Sun.COM 	/*
13867798SSaurabh.Mishra@Sun.COM 	 * On BSP we would have enabled X2APIC, if supported by processor,
13877798SSaurabh.Mishra@Sun.COM 	 * in acpi_probe(), but on AP we do it here.
13887986SSaurabh.Mishra@Sun.COM 	 *
13897986SSaurabh.Mishra@Sun.COM 	 * We enable X2APIC mode only if BSP is running in X2APIC & the
13907986SSaurabh.Mishra@Sun.COM 	 * local APIC mode of the current CPU is MMIO (xAPIC).
13917798SSaurabh.Mishra@Sun.COM 	 */
13927986SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_X2APIC && apic_detect_x2apic() &&
13937986SSaurabh.Mishra@Sun.COM 	    apic_local_mode() == LOCAL_APIC) {
13947798SSaurabh.Mishra@Sun.COM 		apic_enable_x2apic();
13957798SSaurabh.Mishra@Sun.COM 	}
13967798SSaurabh.Mishra@Sun.COM 
13977986SSaurabh.Mishra@Sun.COM 	/*
13987986SSaurabh.Mishra@Sun.COM 	 * We change psm_send_ipi and send_dirintf only if Solaris
13997986SSaurabh.Mishra@Sun.COM 	 * is booted in kmdb & the current CPU is the last CPU being
14007986SSaurabh.Mishra@Sun.COM 	 * brought up. We don't need to do anything if Solaris is running
14017986SSaurabh.Mishra@Sun.COM 	 * in MMIO mode (xAPIC).
14027986SSaurabh.Mishra@Sun.COM 	 */
14037986SSaurabh.Mishra@Sun.COM 	if ((boothowto & RB_DEBUG) &&
14047986SSaurabh.Mishra@Sun.COM 	    (cpus_started == boot_ncpus || cpus_started == apic_nproc) &&
14057986SSaurabh.Mishra@Sun.COM 	    apic_mode == LOCAL_X2APIC) {
14067986SSaurabh.Mishra@Sun.COM 		/*
14077986SSaurabh.Mishra@Sun.COM 		 * We no longer need help from apic_common_send_ipi()
14087986SSaurabh.Mishra@Sun.COM 		 * since we will not start any more CPUs.
14097986SSaurabh.Mishra@Sun.COM 		 *
14107986SSaurabh.Mishra@Sun.COM 		 * We will need to revisit this if we start supporting
14117986SSaurabh.Mishra@Sun.COM 		 * hot-plugging of CPUs.
14127986SSaurabh.Mishra@Sun.COM 		 */
14137986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi = x2apic_send_ipi;
14147986SSaurabh.Mishra@Sun.COM 		send_dirintf = pops->psm_send_ipi;
14157986SSaurabh.Mishra@Sun.COM 	}
14167986SSaurabh.Mishra@Sun.COM 
14177798SSaurabh.Mishra@Sun.COM 	splx(ipltospl(LOCK_LEVEL));
14180Sstevel@tonic-gate 	apic_init_intr();
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * since some systems don't enable the internal cache on the non-boot
14220Sstevel@tonic-gate 	 * cpus, so we have to enable them here
14230Sstevel@tonic-gate 	 */
14243446Smrj 	setcr0(getcr0() & ~(CR0_CD | CR0_NW));
14250Sstevel@tonic-gate 
14267798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
14277798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
14287798SSaurabh.Mishra@Sun.COM #else
14297798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC)
14307798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
14317798SSaurabh.Mishra@Sun.COM #endif	/* DEBUG */
14320Sstevel@tonic-gate 
14337113Sbholler 	/*
14347113Sbholler 	 * We may be booting, or resuming from suspend; aci_status will
14357113Sbholler 	 * be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
14367113Sbholler 	 * APIC_CPU_ONLINE flag here rather than setting aci_status completely.
14377113Sbholler 	 */
14380Sstevel@tonic-gate 	cpun = psm_get_cpu_id();
14397113Sbholler 	apic_cpus[cpun].aci_status |= APIC_CPU_ONLINE;
14400Sstevel@tonic-gate 
14417282Smishra 	apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
14420Sstevel@tonic-gate 	return (PSM_SUCCESS);
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate processorid_t
14460Sstevel@tonic-gate apic_get_next_processorid(processorid_t cpu_id)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	int i;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (cpu_id == -1)
14520Sstevel@tonic-gate 		return ((processorid_t)0);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	for (i = cpu_id + 1; i < NCPU; i++) {
14552006Sandrei 		if (CPU_IN_SET(apic_cpumask, i))
14560Sstevel@tonic-gate 			return (i);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	return ((processorid_t)-1);
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate  * type == -1 indicates it is an internal request. Do not change
14650Sstevel@tonic-gate  * resv_vector for these requests
14660Sstevel@tonic-gate  */
14670Sstevel@tonic-gate static int
14680Sstevel@tonic-gate apic_get_ipivect(int ipl, int type)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate 	uchar_t vector;
14710Sstevel@tonic-gate 	int irq;
14720Sstevel@tonic-gate 
147311465SKerry.Shu@Sun.COM 	if ((irq = apic_allocate_irq(APIC_VECTOR(ipl))) != -1) {
14740Sstevel@tonic-gate 		if (vector = apic_allocate_vector(ipl, irq, 1)) {
14750Sstevel@tonic-gate 			apic_irq_table[irq]->airq_mps_intr_index =
14760Sstevel@tonic-gate 			    RESERVE_INDEX;
14770Sstevel@tonic-gate 			apic_irq_table[irq]->airq_vector = vector;
14780Sstevel@tonic-gate 			if (type != -1) {
14790Sstevel@tonic-gate 				apic_resv_vector[ipl] = vector;
14800Sstevel@tonic-gate 			}
14810Sstevel@tonic-gate 			return (irq);
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 	apic_error |= APIC_ERR_GET_IPIVECT_FAIL;
14850Sstevel@tonic-gate 	return (-1);	/* shouldn't happen */
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate static int
14890Sstevel@tonic-gate apic_getclkirq(int ipl)
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	int	irq;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if ((irq = apic_get_ipivect(ipl, -1)) == -1)
14940Sstevel@tonic-gate 		return (-1);
14950Sstevel@tonic-gate 	/*
14960Sstevel@tonic-gate 	 * Note the vector in apic_clkvect for per clock handling.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate 	apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT;
14990Sstevel@tonic-gate 	APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n",
15000Sstevel@tonic-gate 	    apic_clkvect));
15010Sstevel@tonic-gate 	return (irq);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15042992Sdmick 
15050Sstevel@tonic-gate /*
15060Sstevel@tonic-gate  * Return the number of APIC clock ticks elapsed for 8245 to decrement
15070Sstevel@tonic-gate  * (APIC_TIME_COUNT + pit_ticks_adj) ticks.
15080Sstevel@tonic-gate  */
15090Sstevel@tonic-gate static uint_t
15100Sstevel@tonic-gate apic_calibrate(volatile uint32_t *addr, uint16_t *pit_ticks_adj)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate 	uint8_t		pit_tick_lo;
15130Sstevel@tonic-gate 	uint16_t	pit_tick, target_pit_tick;
15140Sstevel@tonic-gate 	uint32_t	start_apic_tick, end_apic_tick;
15153446Smrj 	ulong_t		iflag;
15167282Smishra 	uint32_t	reg;
15170Sstevel@tonic-gate 
15187282Smishra 	reg = addr + APIC_CURR_COUNT - apicadr;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	iflag = intr_clear();
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	do {
15230Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
15240Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
15250Sstevel@tonic-gate 	} while (pit_tick < APIC_TIME_MIN ||
15260Sstevel@tonic-gate 	    pit_tick_lo <= APIC_LB_MIN || pit_tick_lo >= APIC_LB_MAX);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	/*
15290Sstevel@tonic-gate 	 * Wait for the 8254 to decrement by 5 ticks to ensure
15300Sstevel@tonic-gate 	 * we didn't start in the middle of a tick.
15310Sstevel@tonic-gate 	 * Compare with 0x10 for the wrap around case.
15320Sstevel@tonic-gate 	 */
15330Sstevel@tonic-gate 	target_pit_tick = pit_tick - 5;
15340Sstevel@tonic-gate 	do {
15350Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
15360Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
15370Sstevel@tonic-gate 	} while (pit_tick > target_pit_tick || pit_tick_lo < 0x10);
15380Sstevel@tonic-gate 
15397282Smishra 	start_apic_tick = apic_reg_ops->apic_read(reg);
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	/*
15420Sstevel@tonic-gate 	 * Wait for the 8254 to decrement by
15430Sstevel@tonic-gate 	 * (APIC_TIME_COUNT + pit_ticks_adj) ticks
15440Sstevel@tonic-gate 	 */
15450Sstevel@tonic-gate 	target_pit_tick = pit_tick - APIC_TIME_COUNT;
15460Sstevel@tonic-gate 	do {
15470Sstevel@tonic-gate 		pit_tick_lo = inb(PITCTR0_PORT);
15480Sstevel@tonic-gate 		pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo;
15490Sstevel@tonic-gate 	} while (pit_tick > target_pit_tick || pit_tick_lo < 0x10);
15500Sstevel@tonic-gate 
15517282Smishra 	end_apic_tick = apic_reg_ops->apic_read(reg);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	*pit_ticks_adj = target_pit_tick - pit_tick;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	intr_restore(iflag);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (start_apic_tick - end_apic_tick);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate /*
15610Sstevel@tonic-gate  * Initialise the APIC timer on the local APIC of CPU 0 to the desired
15620Sstevel@tonic-gate  * frequency.  Note at this stage in the boot sequence, the boot processor
15630Sstevel@tonic-gate  * is the only active processor.
15640Sstevel@tonic-gate  * hertz value of 0 indicates a one-shot mode request.  In this case
15650Sstevel@tonic-gate  * the function returns the resolution (in nanoseconds) for the hardware
15660Sstevel@tonic-gate  * timer interrupt.  If one-shot mode capability is not available,
15670Sstevel@tonic-gate  * the return value will be 0. apic_enable_oneshot is a global switch
15680Sstevel@tonic-gate  * for disabling the functionality.
15690Sstevel@tonic-gate  * A non-zero positive value for hertz indicates a periodic mode request.
15700Sstevel@tonic-gate  * In this case the hardware will be programmed to generate clock interrupts
15710Sstevel@tonic-gate  * at hertz frequency and returns the resolution of interrupts in
15720Sstevel@tonic-gate  * nanosecond.
15730Sstevel@tonic-gate  */
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate static int
15760Sstevel@tonic-gate apic_clkinit(int hertz)
15770Sstevel@tonic-gate {
15780Sstevel@tonic-gate 	uint_t		apic_ticks = 0;
15792992Sdmick 	uint_t		pit_ticks;
15800Sstevel@tonic-gate 	int		ret;
15810Sstevel@tonic-gate 	uint16_t	pit_ticks_adj;
15820Sstevel@tonic-gate 	static int	firsttime = 1;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	if (firsttime) {
15852992Sdmick 		/* first time calibrate on CPU0 only */
15862992Sdmick 
15877282Smishra 		apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
15887282Smishra 		apic_reg_ops->apic_write(APIC_INIT_COUNT, APIC_MAXVAL);
15890Sstevel@tonic-gate 		apic_ticks = apic_calibrate(apicadr, &pit_ticks_adj);
15900Sstevel@tonic-gate 
15912992Sdmick 		/* total number of PIT ticks corresponding to apic_ticks */
15922992Sdmick 		pit_ticks = APIC_TIME_COUNT + pit_ticks_adj;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 		/*
15950Sstevel@tonic-gate 		 * Determine the number of nanoseconds per APIC clock tick
15960Sstevel@tonic-gate 		 * and then determine how many APIC ticks to interrupt at the
15970Sstevel@tonic-gate 		 * desired frequency
15982992Sdmick 		 * apic_ticks / (pitticks / PIT_HZ) = apic_ticks_per_s
15992992Sdmick 		 * (apic_ticks * PIT_HZ) / pitticks = apic_ticks_per_s
16002992Sdmick 		 * apic_ticks_per_ns = (apic_ticks * PIT_HZ) / (pitticks * 10^9)
16013446Smrj 		 * pic_ticks_per_SFns =
16022992Sdmick 		 *   (SF * apic_ticks * PIT_HZ) / (pitticks * 10^9)
16030Sstevel@tonic-gate 		 */
16042992Sdmick 		apic_ticks_per_SFnsecs =
16052992Sdmick 		    ((SF * apic_ticks * PIT_HZ) /
16062992Sdmick 		    ((uint64_t)pit_ticks * NANOSEC));
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 		/* the interval timer initial count is 32 bit max */
16092992Sdmick 		apic_nsec_max = APIC_TICKS_TO_NSECS(APIC_MAXVAL);
16100Sstevel@tonic-gate 		firsttime = 0;
16110Sstevel@tonic-gate 	}
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	if (hertz != 0) {
16140Sstevel@tonic-gate 		/* periodic */
16150Sstevel@tonic-gate 		apic_nsec_per_intr = NANOSEC / hertz;
16162992Sdmick 		apic_hertz_count = APIC_NSECS_TO_TICKS(apic_nsec_per_intr);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	apic_int_busy_mark = (apic_int_busy_mark *
16200Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
16210Sstevel@tonic-gate 	apic_int_free_mark = (apic_int_free_mark *
16220Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
16230Sstevel@tonic-gate 	apic_diff_for_redistribution = (apic_diff_for_redistribution *
16240Sstevel@tonic-gate 	    apic_sample_factor_redistribution) / 100;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	if (hertz == 0) {
16270Sstevel@tonic-gate 		/* requested one_shot */
16285084Sjohnlev 		if (!tsc_gethrtime_enable || !apic_oneshot_enable)
16290Sstevel@tonic-gate 			return (0);
16300Sstevel@tonic-gate 		apic_oneshot = 1;
16312992Sdmick 		ret = (int)APIC_TICKS_TO_NSECS(1);
16320Sstevel@tonic-gate 	} else {
16330Sstevel@tonic-gate 		/* program the local APIC to interrupt at the given frequency */
16347282Smishra 		apic_reg_ops->apic_write(APIC_INIT_COUNT, apic_hertz_count);
16357282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
16367282Smishra 		    (apic_clkvect + APIC_BASE_VECT) | AV_TIME);
16370Sstevel@tonic-gate 		apic_oneshot = 0;
16380Sstevel@tonic-gate 		ret = NANOSEC / hertz;
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	return (ret);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate  * apic_preshutdown:
16470Sstevel@tonic-gate  * Called early in shutdown whilst we can still access filesystems to do
16480Sstevel@tonic-gate  * things like loading modules which will be required to complete shutdown
16490Sstevel@tonic-gate  * after filesystems are all unmounted.
16500Sstevel@tonic-gate  */
16510Sstevel@tonic-gate static void
16520Sstevel@tonic-gate apic_preshutdown(int cmd, int fcn)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	APIC_VERBOSE_POWEROFF(("apic_preshutdown(%d,%d); m=%d a=%d\n",
16550Sstevel@tonic-gate 	    cmd, fcn, apic_poweroff_method, apic_enable_acpi));
16560Sstevel@tonic-gate 
16575295Srandyf 	if ((cmd != A_SHUTDOWN) || (fcn != AD_POWEROFF)) {
16585295Srandyf 		return;
16595295Srandyf 	}
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate static void
16630Sstevel@tonic-gate apic_shutdown(int cmd, int fcn)
16640Sstevel@tonic-gate {
16653446Smrj 	int restarts, attempts;
16663446Smrj 	int i;
16670Sstevel@tonic-gate 	uchar_t	byte;
16683446Smrj 	ulong_t iflag;
16690Sstevel@tonic-gate 
16708906SEric.Saxe@Sun.COM 	hpet_acpi_fini();
16718906SEric.Saxe@Sun.COM 
16720Sstevel@tonic-gate 	/* Send NMI to all CPUs except self to do per processor shutdown */
16730Sstevel@tonic-gate 	iflag = intr_clear();
16747798SSaurabh.Mishra@Sun.COM #ifdef	DEBUG
16757798SSaurabh.Mishra@Sun.COM 	APIC_AV_PENDING_SET();
16767798SSaurabh.Mishra@Sun.COM #else
16777798SSaurabh.Mishra@Sun.COM 	if (apic_mode == LOCAL_APIC)
16787798SSaurabh.Mishra@Sun.COM 		APIC_AV_PENDING_SET();
16797798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
16800Sstevel@tonic-gate 	apic_shutdown_processors = 1;
16817282Smishra 	apic_reg_ops->apic_write(APIC_INT_CMD1,
16827282Smishra 	    AV_NMI | AV_LEVEL | AV_SH_ALL_EXCSELF);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/* restore cmos shutdown byte before reboot */
16850Sstevel@tonic-gate 	if (apic_cmos_ssb_set) {
16860Sstevel@tonic-gate 		outb(CMOS_ADDR, SSB);
16870Sstevel@tonic-gate 		outb(CMOS_DATA, 0);
16880Sstevel@tonic-gate 	}
16893446Smrj 
16903446Smrj 	ioapic_disable_redirection();
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	/*	disable apic mode if imcr present	*/
16930Sstevel@tonic-gate 	if (apic_imcrp) {
16940Sstevel@tonic-gate 		outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
16950Sstevel@tonic-gate 		outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_PIC);
16960Sstevel@tonic-gate 	}
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	apic_disable_local_apic();
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	intr_restore(iflag);
17010Sstevel@tonic-gate 
17023472Smyers 	/* remainder of function is for shutdown cases only */
17033472Smyers 	if (cmd != A_SHUTDOWN)
17040Sstevel@tonic-gate 		return;
17053472Smyers 
17064189Smyers 	/*
17074189Smyers 	 * Switch system back into Legacy-Mode if using ACPI and
17084189Smyers 	 * not powering-off.  Some BIOSes need to remain in ACPI-mode
17094189Smyers 	 * for power-off to succeed (Dell Dimension 4600)
17107656SSherry.Moore@Sun.COM 	 * Do not disable ACPI while doing fastreboot
17114189Smyers 	 */
17127656SSherry.Moore@Sun.COM 	if (apic_enable_acpi && fcn != AD_POWEROFF && fcn != AD_FASTREBOOT)
17133472Smyers 		(void) AcpiDisable();
17143472Smyers 
17157656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
17168767SSaurabh.Mishra@Sun.COM 		apic_reg_ops->apic_write(APIC_INT_CMD1,
17178767SSaurabh.Mishra@Sun.COM 		    AV_ASSERT | AV_RESET | AV_SH_ALL_EXCSELF);
17187656SSherry.Moore@Sun.COM 	}
17197656SSherry.Moore@Sun.COM 
17203472Smyers 	/* remainder of function is for shutdown+poweroff case only */
17213472Smyers 	if (fcn != AD_POWEROFF)
17223472Smyers 		return;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	switch (apic_poweroff_method) {
17250Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_RTC:
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 			/* select the extended NVRAM bank in the RTC */
17280Sstevel@tonic-gate 			outb(CMOS_ADDR, RTC_REGA);
17290Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
17300Sstevel@tonic-gate 			outb(CMOS_DATA, (byte | EXT_BANK));
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 			outb(CMOS_ADDR, PFR_REG);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 			/* for Predator must toggle the PAB bit */
17350Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 			/*
17380Sstevel@tonic-gate 			 * clear power active bar, wakeup alarm and
17390Sstevel@tonic-gate 			 * kickstart
17400Sstevel@tonic-gate 			 */
17410Sstevel@tonic-gate 			byte &= ~(PAB_CBIT | WF_FLAG | KS_FLAG);
17420Sstevel@tonic-gate 			outb(CMOS_DATA, byte);
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			/* delay before next write */
17450Sstevel@tonic-gate 			drv_usecwait(1000);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			/* for S40 the following would suffice */
17480Sstevel@tonic-gate 			byte = inb(CMOS_DATA);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 			/* power active bar control bit */
17510Sstevel@tonic-gate 			byte |= PAB_CBIT;
17520Sstevel@tonic-gate 			outb(CMOS_DATA, byte);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 			break;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_ASPEN_BMC:
17570Sstevel@tonic-gate 			restarts = 0;
17580Sstevel@tonic-gate restart_aspen_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(MISMIC_FLAG_REGISTER);
17640Sstevel@tonic-gate 				byte &= MISMIC_BUSY_MASK;
17650Sstevel@tonic-gate 				if (byte != 0) {
17660Sstevel@tonic-gate 					drv_usecwait(1000);
17670Sstevel@tonic-gate 					if (attempts >= 3)
17680Sstevel@tonic-gate 						goto restart_aspen_bmc;
17690Sstevel@tonic-gate 					++attempts;
17700Sstevel@tonic-gate 				}
17710Sstevel@tonic-gate 			} while (byte != 0);
17720Sstevel@tonic-gate 			outb(MISMIC_CNTL_REGISTER, CC_SMS_GET_STATUS);
17730Sstevel@tonic-gate 			byte = inb(MISMIC_FLAG_REGISTER);
17740Sstevel@tonic-gate 			byte |= 0x1;
17750Sstevel@tonic-gate 			outb(MISMIC_FLAG_REGISTER, byte);
17760Sstevel@tonic-gate 			i = 0;
17770Sstevel@tonic-gate 			for (; i < (sizeof (aspen_bmc)/sizeof (aspen_bmc[0]));
17780Sstevel@tonic-gate 			    i++) {
17790Sstevel@tonic-gate 				attempts = 0;
17800Sstevel@tonic-gate 				do {
17810Sstevel@tonic-gate 					byte = inb(MISMIC_FLAG_REGISTER);
17820Sstevel@tonic-gate 					byte &= MISMIC_BUSY_MASK;
17830Sstevel@tonic-gate 					if (byte != 0) {
17840Sstevel@tonic-gate 						drv_usecwait(1000);
17850Sstevel@tonic-gate 						if (attempts >= 3)
17860Sstevel@tonic-gate 							goto restart_aspen_bmc;
17870Sstevel@tonic-gate 						++attempts;
17880Sstevel@tonic-gate 					}
17890Sstevel@tonic-gate 				} while (byte != 0);
17900Sstevel@tonic-gate 				outb(MISMIC_CNTL_REGISTER, aspen_bmc[i].cntl);
17910Sstevel@tonic-gate 				outb(MISMIC_DATA_REGISTER, aspen_bmc[i].data);
17920Sstevel@tonic-gate 				byte = inb(MISMIC_FLAG_REGISTER);
17930Sstevel@tonic-gate 				byte |= 0x1;
17940Sstevel@tonic-gate 				outb(MISMIC_FLAG_REGISTER, byte);
17950Sstevel@tonic-gate 			}
17960Sstevel@tonic-gate 			break;
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		case APIC_POWEROFF_VIA_SITKA_BMC:
17990Sstevel@tonic-gate 			restarts = 0;
18000Sstevel@tonic-gate restart_sitka_bmc:
18010Sstevel@tonic-gate 			if (++restarts == 3)
18020Sstevel@tonic-gate 				break;
18030Sstevel@tonic-gate 			attempts = 0;
18040Sstevel@tonic-gate 			do {
18050Sstevel@tonic-gate 				byte = inb(SMS_STATUS_REGISTER);
18060Sstevel@tonic-gate 				byte &= SMS_STATE_MASK;
18070Sstevel@tonic-gate 				if ((byte == SMS_READ_STATE) ||
18080Sstevel@tonic-gate 				    (byte == SMS_WRITE_STATE)) {
18090Sstevel@tonic-gate 					drv_usecwait(1000);
18100Sstevel@tonic-gate 					if (attempts >= 3)
18110Sstevel@tonic-gate 						goto restart_sitka_bmc;
18120Sstevel@tonic-gate 					++attempts;
18130Sstevel@tonic-gate 				}
18140Sstevel@tonic-gate 			} while ((byte == SMS_READ_STATE) ||
18150Sstevel@tonic-gate 			    (byte == SMS_WRITE_STATE));
18160Sstevel@tonic-gate 			outb(SMS_COMMAND_REGISTER, SMS_GET_STATUS);
18170Sstevel@tonic-gate 			i = 0;
18180Sstevel@tonic-gate 			for (; i < (sizeof (sitka_bmc)/sizeof (sitka_bmc[0]));
18190Sstevel@tonic-gate 			    i++) {
18200Sstevel@tonic-gate 				attempts = 0;
18210Sstevel@tonic-gate 				do {
18220Sstevel@tonic-gate 					byte = inb(SMS_STATUS_REGISTER);
18230Sstevel@tonic-gate 					byte &= SMS_IBF_MASK;
18240Sstevel@tonic-gate 					if (byte != 0) {
18250Sstevel@tonic-gate 						drv_usecwait(1000);
18260Sstevel@tonic-gate 						if (attempts >= 3)
18270Sstevel@tonic-gate 							goto restart_sitka_bmc;
18280Sstevel@tonic-gate 						++attempts;
18290Sstevel@tonic-gate 					}
18300Sstevel@tonic-gate 				} while (byte != 0);
18310Sstevel@tonic-gate 				outb(sitka_bmc[i].port, sitka_bmc[i].data);
18320Sstevel@tonic-gate 			}
18330Sstevel@tonic-gate 			break;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 		case APIC_POWEROFF_NONE:
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 			/* If no APIC direct method, we will try using ACPI */
18380Sstevel@tonic-gate 			if (apic_enable_acpi) {
18390Sstevel@tonic-gate 				if (acpi_poweroff() == 1)
18400Sstevel@tonic-gate 					return;
18410Sstevel@tonic-gate 			} else
18420Sstevel@tonic-gate 				return;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 			break;
18450Sstevel@tonic-gate 	}
18460Sstevel@tonic-gate 	/*
18470Sstevel@tonic-gate 	 * Wait a limited time here for power to go off.
18480Sstevel@tonic-gate 	 * If the power does not go off, then there was a
18490Sstevel@tonic-gate 	 * problem and we should continue to the halt which
18500Sstevel@tonic-gate 	 * prints a message for the user to press a key to
18510Sstevel@tonic-gate 	 * reboot.
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	drv_usecwait(7000000); /* wait seven seconds */
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate /*
18580Sstevel@tonic-gate  * Try and disable all interrupts. We just assign interrupts to other
18590Sstevel@tonic-gate  * processors based on policy. If any were bound by user request, we
18600Sstevel@tonic-gate  * let them continue and return failure. We do not bother to check
18610Sstevel@tonic-gate  * for cache affinity while rebinding.
18620Sstevel@tonic-gate  */
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate static int
18650Sstevel@tonic-gate apic_disable_intr(processorid_t cpun)
18660Sstevel@tonic-gate {
18673446Smrj 	int bind_cpu = 0, i, hardbound = 0;
18680Sstevel@tonic-gate 	apic_irq_t *irq_ptr;
18693446Smrj 	ulong_t iflag;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	iflag = intr_clear();
18720Sstevel@tonic-gate 	lock_set(&apic_ioapic_lock);
18733139Ssethg 
18743139Ssethg 	for (i = 0; i <= APIC_MAX_VECTOR; i++) {
18753139Ssethg 		if (apic_reprogram_info[i].done == B_FALSE) {
18763139Ssethg 			if (apic_reprogram_info[i].bindcpu == cpun) {
18773139Ssethg 				/*
18783139Ssethg 				 * CPU is busy -- it's the target of
18793139Ssethg 				 * a pending reprogramming attempt
18803139Ssethg 				 */
18813139Ssethg 				lock_clear(&apic_ioapic_lock);
18823139Ssethg 				intr_restore(iflag);
18833139Ssethg 				return (PSM_FAILURE);
18843139Ssethg 			}
18853139Ssethg 		}
18863139Ssethg 	}
18873139Ssethg 
18880Sstevel@tonic-gate 	apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE;
18893139Ssethg 
18900Sstevel@tonic-gate 	apic_cpus[cpun].aci_curipl = 0;
18913139Ssethg 
18920Sstevel@tonic-gate 	i = apic_min_device_irq;
18930Sstevel@tonic-gate 	for (; i <= apic_max_device_irq; i++) {
18940Sstevel@tonic-gate 		/*
18950Sstevel@tonic-gate 		 * If there are bound interrupts on this cpu, then
18960Sstevel@tonic-gate 		 * rebind them to other processors.
18970Sstevel@tonic-gate 		 */
18980Sstevel@tonic-gate 		if ((irq_ptr = apic_irq_table[i]) != NULL) {
18990Sstevel@tonic-gate 			ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) ||
19000Sstevel@tonic-gate 			    (irq_ptr->airq_temp_cpu == IRQ_UNINIT) ||
19010Sstevel@tonic-gate 			    ((irq_ptr->airq_temp_cpu & ~IRQ_USER_BOUND) <
19020Sstevel@tonic-gate 			    apic_nproc));
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 			if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) {
19050Sstevel@tonic-gate 				hardbound = 1;
19060Sstevel@tonic-gate 				continue;
19070Sstevel@tonic-gate 			}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 			if (irq_ptr->airq_temp_cpu == cpun) {
19100Sstevel@tonic-gate 				do {
19113446Smrj 					bind_cpu = apic_next_bind_cpu++;
19120Sstevel@tonic-gate 					if (bind_cpu >= apic_nproc) {
19130Sstevel@tonic-gate 						apic_next_bind_cpu = 1;
19140Sstevel@tonic-gate 						bind_cpu = 0;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 					}
19173139Ssethg 				} while (apic_rebind_all(irq_ptr, bind_cpu));
19180Sstevel@tonic-gate 			}
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 	}
19213139Ssethg 
19223139Ssethg 	lock_clear(&apic_ioapic_lock);
19233139Ssethg 	intr_restore(iflag);
19243139Ssethg 
19250Sstevel@tonic-gate 	if (hardbound) {
19260Sstevel@tonic-gate 		cmn_err(CE_WARN, "Could not disable interrupts on %d"
19270Sstevel@tonic-gate 		    "due to user bound interrupts", cpun);
19280Sstevel@tonic-gate 		return (PSM_FAILURE);
19290Sstevel@tonic-gate 	}
19300Sstevel@tonic-gate 	else
19310Sstevel@tonic-gate 		return (PSM_SUCCESS);
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate 
19347113Sbholler /*
19357113Sbholler  * Bind interrupts to the CPU's local APIC.
19367113Sbholler  * Interrupts should not be bound to a CPU's local APIC until the CPU
19377113Sbholler  * is ready to receive interrupts.
19387113Sbholler  */
19390Sstevel@tonic-gate static void
19400Sstevel@tonic-gate apic_enable_intr(processorid_t cpun)
19410Sstevel@tonic-gate {
19423446Smrj 	int	i;
19430Sstevel@tonic-gate 	apic_irq_t *irq_ptr;
19443446Smrj 	ulong_t iflag;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	iflag = intr_clear();
19470Sstevel@tonic-gate 	lock_set(&apic_ioapic_lock);
19483139Ssethg 
19490Sstevel@tonic-gate 	apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE;
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	i = apic_min_device_irq;
19520Sstevel@tonic-gate 	for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
19530Sstevel@tonic-gate 		if ((irq_ptr = apic_irq_table[i]) != NULL) {
19540Sstevel@tonic-gate 			if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) {
19550Sstevel@tonic-gate 				(void) apic_rebind_all(irq_ptr,
19563139Ssethg 				    irq_ptr->airq_cpu);
19570Sstevel@tonic-gate 			}
19580Sstevel@tonic-gate 		}
19590Sstevel@tonic-gate 	}
19603139Ssethg 
19613139Ssethg 	lock_clear(&apic_ioapic_lock);
19623139Ssethg 	intr_restore(iflag);
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate /*
19670Sstevel@tonic-gate  * This function will reprogram the timer.
19680Sstevel@tonic-gate  *
19690Sstevel@tonic-gate  * When in oneshot mode the argument is the absolute time in future to
19700Sstevel@tonic-gate  * generate the interrupt at.
19710Sstevel@tonic-gate  *
19720Sstevel@tonic-gate  * When in periodic mode, the argument is the interval at which the
19730Sstevel@tonic-gate  * interrupts should be generated. There is no need to support the periodic
19740Sstevel@tonic-gate  * mode timer change at this time.
19750Sstevel@tonic-gate  */
19760Sstevel@tonic-gate static void
19770Sstevel@tonic-gate apic_timer_reprogram(hrtime_t time)
19780Sstevel@tonic-gate {
19790Sstevel@tonic-gate 	hrtime_t now;
19800Sstevel@tonic-gate 	uint_t ticks;
19813446Smrj 	int64_t delta;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	/*
19840Sstevel@tonic-gate 	 * We should be called from high PIL context (CBE_HIGH_PIL),
19850Sstevel@tonic-gate 	 * so kpreempt is disabled.
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	if (!apic_oneshot) {
19890Sstevel@tonic-gate 		/* time is the interval for periodic mode */
19902992Sdmick 		ticks = APIC_NSECS_TO_TICKS(time);
19910Sstevel@tonic-gate 	} else {
19920Sstevel@tonic-gate 		/* one shot mode */
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		now = gethrtime();
19952992Sdmick 		delta = time - now;
19962992Sdmick 
19972992Sdmick 		if (delta <= 0) {
19980Sstevel@tonic-gate 			/*
19990Sstevel@tonic-gate 			 * requested to generate an interrupt in the past
20000Sstevel@tonic-gate 			 * generate an interrupt as soon as possible
20010Sstevel@tonic-gate 			 */
20020Sstevel@tonic-gate 			ticks = apic_min_timer_ticks;
20032992Sdmick 		} else if (delta > apic_nsec_max) {
20040Sstevel@tonic-gate 			/*
20050Sstevel@tonic-gate 			 * requested to generate an interrupt at a time
20060Sstevel@tonic-gate 			 * further than what we are capable of. Set to max
20070Sstevel@tonic-gate 			 * the hardware can handle
20080Sstevel@tonic-gate 			 */
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 			ticks = APIC_MAXVAL;
20110Sstevel@tonic-gate #ifdef DEBUG
20120Sstevel@tonic-gate 			cmn_err(CE_CONT, "apic_timer_reprogram, request at"
20130Sstevel@tonic-gate 			    "  %lld  too far in future, current time"
20140Sstevel@tonic-gate 			    "  %lld \n", time, now);
20152992Sdmick #endif
20160Sstevel@tonic-gate 		} else
20172992Sdmick 			ticks = APIC_NSECS_TO_TICKS(delta);
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	if (ticks < apic_min_timer_ticks)
20210Sstevel@tonic-gate 		ticks = apic_min_timer_ticks;
20220Sstevel@tonic-gate 
20237282Smishra 	apic_reg_ops->apic_write(APIC_INIT_COUNT, ticks);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * This function will enable timer interrupts.
20280Sstevel@tonic-gate  */
20290Sstevel@tonic-gate static void
20300Sstevel@tonic-gate apic_timer_enable(void)
20310Sstevel@tonic-gate {
20320Sstevel@tonic-gate 	/*
20330Sstevel@tonic-gate 	 * We should be Called from high PIL context (CBE_HIGH_PIL),
20340Sstevel@tonic-gate 	 * so kpreempt is disabled.
20350Sstevel@tonic-gate 	 */
20360Sstevel@tonic-gate 
20377282Smishra 	if (!apic_oneshot) {
20387282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
20397282Smishra 		    (apic_clkvect + APIC_BASE_VECT) | AV_TIME);
20407282Smishra 	} else {
20410Sstevel@tonic-gate 		/* one shot */
20427282Smishra 		apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
20437282Smishra 		    (apic_clkvect + APIC_BASE_VECT));
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate  * This function will disable timer interrupts.
20490Sstevel@tonic-gate  */
20500Sstevel@tonic-gate static void
20510Sstevel@tonic-gate apic_timer_disable(void)
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * We should be Called from high PIL context (CBE_HIGH_PIL),
20550Sstevel@tonic-gate 	 * so kpreempt is disabled.
20560Sstevel@tonic-gate 	 */
20577282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER,
20587282Smishra 	    (apic_clkvect + APIC_BASE_VECT) | AV_MASK);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20618906SEric.Saxe@Sun.COM /*
20628906SEric.Saxe@Sun.COM  * Set timer far into the future and return timer
20638906SEric.Saxe@Sun.COM  * current Count in nanoseconds.
20648906SEric.Saxe@Sun.COM  */
20658906SEric.Saxe@Sun.COM hrtime_t
20668906SEric.Saxe@Sun.COM apic_timer_stop_count(void)
20678906SEric.Saxe@Sun.COM {
20688906SEric.Saxe@Sun.COM 	hrtime_t	ns_val;
20698906SEric.Saxe@Sun.COM 	int		enable_val, count_val;
20708906SEric.Saxe@Sun.COM 
20718906SEric.Saxe@Sun.COM 	/*
20728906SEric.Saxe@Sun.COM 	 * Should be called with interrupts disabled.
20738906SEric.Saxe@Sun.COM 	 */
20748906SEric.Saxe@Sun.COM 	ASSERT(!interrupts_enabled());
20758906SEric.Saxe@Sun.COM 
20768906SEric.Saxe@Sun.COM 	enable_val = apic_reg_ops->apic_read(APIC_LOCAL_TIMER);
20778906SEric.Saxe@Sun.COM 	if ((enable_val & AV_MASK) == AV_MASK)
20788906SEric.Saxe@Sun.COM 		return ((hrtime_t)-1);		/* timer is disabled */
20798906SEric.Saxe@Sun.COM 
20808906SEric.Saxe@Sun.COM 	count_val = apic_reg_ops->apic_read(APIC_CURR_COUNT);
20818906SEric.Saxe@Sun.COM 	ns_val = APIC_TICKS_TO_NSECS(count_val);
20828906SEric.Saxe@Sun.COM 
20838906SEric.Saxe@Sun.COM 	apic_reg_ops->apic_write(APIC_INIT_COUNT, APIC_MAXVAL);
20848906SEric.Saxe@Sun.COM 
20858906SEric.Saxe@Sun.COM 	return (ns_val);
20868906SEric.Saxe@Sun.COM }
20878906SEric.Saxe@Sun.COM 
20888906SEric.Saxe@Sun.COM /*
20898906SEric.Saxe@Sun.COM  * Reprogram timer after Deep C-State.
20908906SEric.Saxe@Sun.COM  */
20918906SEric.Saxe@Sun.COM void
20928906SEric.Saxe@Sun.COM apic_timer_restart(hrtime_t time)
20938906SEric.Saxe@Sun.COM {
20948906SEric.Saxe@Sun.COM 	apic_timer_reprogram(time);
20958906SEric.Saxe@Sun.COM }
20960Sstevel@tonic-gate 
20975107Seota ddi_periodic_t apic_periodic_id;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate /*
21005107Seota  * If this module needs a periodic handler for the interrupt distribution, it
21015107Seota  * can be added here. The argument to the periodic handler is not currently
21025107Seota  * used, but is reserved for future.
21030Sstevel@tonic-gate  */
21040Sstevel@tonic-gate static void
21050Sstevel@tonic-gate apic_post_cyclic_setup(void *arg)
21060Sstevel@tonic-gate {
21070Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
21080Sstevel@tonic-gate 	/* cpu_lock is held */
21095107Seota 	/* set up a periodic handler for intr redistribution */
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	/*
21120Sstevel@tonic-gate 	 * In peridoc mode intr redistribution processing is done in
21130Sstevel@tonic-gate 	 * apic_intr_enter during clk intr processing
21140Sstevel@tonic-gate 	 */
21150Sstevel@tonic-gate 	if (!apic_oneshot)
21160Sstevel@tonic-gate 		return;
21175107Seota 	/*
21185107Seota 	 * Register a periodical handler for the redistribution processing.
21195107Seota 	 * On X86, CY_LOW_LEVEL is mapped to the level 2 interrupt, so
21205107Seota 	 * DDI_IPL_2 should be passed to ddi_periodic_add() here.
21215107Seota 	 */
21225107Seota 	apic_periodic_id = ddi_periodic_add(
21235107Seota 	    (void (*)(void *))apic_redistribute_compute, NULL,
21245107Seota 	    apic_redistribute_sample_interval, DDI_IPL_2);
21250Sstevel@tonic-gate }
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate static void
21280Sstevel@tonic-gate apic_redistribute_compute(void)
21290Sstevel@tonic-gate {
21300Sstevel@tonic-gate 	int	i, j, max_busy;
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	if (apic_enable_dynamic_migration) {
21330Sstevel@tonic-gate 		if (++apic_nticks == apic_sample_factor_redistribution) {
21340Sstevel@tonic-gate 			/*
21350Sstevel@tonic-gate 			 * Time to call apic_intr_redistribute().
21360Sstevel@tonic-gate 			 * reset apic_nticks. This will cause max_busy
21370Sstevel@tonic-gate 			 * to be calculated below and if it is more than
21380Sstevel@tonic-gate 			 * apic_int_busy, we will do the whole thing
21390Sstevel@tonic-gate 			 */
21400Sstevel@tonic-gate 			apic_nticks = 0;
21410Sstevel@tonic-gate 		}
21420Sstevel@tonic-gate 		max_busy = 0;
21430Sstevel@tonic-gate 		for (i = 0; i < apic_nproc; i++) {
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 			/*
21460Sstevel@tonic-gate 			 * Check if curipl is non zero & if ISR is in
21470Sstevel@tonic-gate 			 * progress
21480Sstevel@tonic-gate 			 */
21490Sstevel@tonic-gate 			if (((j = apic_cpus[i].aci_curipl) != 0) &&
21500Sstevel@tonic-gate 			    (apic_cpus[i].aci_ISR_in_progress & (1 << j))) {
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 				int	irq;
21530Sstevel@tonic-gate 				apic_cpus[i].aci_busy++;
21540Sstevel@tonic-gate 				irq = apic_cpus[i].aci_current[j];
21550Sstevel@tonic-gate 				apic_irq_table[irq]->airq_busy++;
21560Sstevel@tonic-gate 			}
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 			if (!apic_nticks &&
21590Sstevel@tonic-gate 			    (apic_cpus[i].aci_busy > max_busy))
21600Sstevel@tonic-gate 				max_busy = apic_cpus[i].aci_busy;
21610Sstevel@tonic-gate 		}
21620Sstevel@tonic-gate 		if (!apic_nticks) {
21630Sstevel@tonic-gate 			if (max_busy > apic_int_busy_mark) {
21640Sstevel@tonic-gate 			/*
21650Sstevel@tonic-gate 			 * We could make the following check be
21660Sstevel@tonic-gate 			 * skipped > 1 in which case, we get a
21670Sstevel@tonic-gate 			 * redistribution at half the busy mark (due to
21680Sstevel@tonic-gate 			 * double interval). Need to be able to collect
21690Sstevel@tonic-gate 			 * more empirical data to decide if that is a
21700Sstevel@tonic-gate 			 * good strategy. Punt for now.
21710Sstevel@tonic-gate 			 */
21723446Smrj 				if (apic_skipped_redistribute) {
21730Sstevel@tonic-gate 					apic_cleanup_busy();
21743446Smrj 					apic_skipped_redistribute = 0;
21753446Smrj 				} else {
21760Sstevel@tonic-gate 					apic_intr_redistribute();
21773446Smrj 				}
21780Sstevel@tonic-gate 			} else
21790Sstevel@tonic-gate 				apic_skipped_redistribute++;
21800Sstevel@tonic-gate 		}
21810Sstevel@tonic-gate 	}
21820Sstevel@tonic-gate }
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 
21853446Smrj /*
21863446Smrj  * The following functions are in the platform specific file so that they
21873446Smrj  * can be different functions depending on whether we are running on
21883446Smrj  * bare metal or a hypervisor.
21893446Smrj  */
21900Sstevel@tonic-gate 
21913446Smrj /*
21923446Smrj  * map an apic for memory-mapped access
21933446Smrj  */
21943446Smrj uint32_t *
21953446Smrj mapin_apic(uint32_t addr, size_t len, int flags)
21963446Smrj {
21973446Smrj 	/*LINTED: pointer cast may result in improper alignment */
21983446Smrj 	return ((uint32_t *)psm_map_phys(addr, len, flags));
21993446Smrj }
22000Sstevel@tonic-gate 
22013446Smrj uint32_t *
22023446Smrj mapin_ioapic(uint32_t addr, size_t len, int flags)
22033446Smrj {
22043446Smrj 	return (mapin_apic(addr, len, flags));
22050Sstevel@tonic-gate }
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate /*
22083446Smrj  * unmap an apic
22093139Ssethg  */
22103446Smrj void
22113446Smrj mapout_apic(caddr_t addr, size_t len)
22123139Ssethg {
22133446Smrj 	psm_unmap_phys(addr, len);
22143139Ssethg }
22153139Ssethg 
22163446Smrj void
22173446Smrj mapout_ioapic(caddr_t addr, size_t len)
22183139Ssethg {
22193446Smrj 	mapout_apic(addr, len);
22203139Ssethg }
22213139Ssethg 
22223139Ssethg /*
22234937Sjohnny  * Check to make sure there are enough irq slots
22243139Ssethg  */
22253446Smrj int
22264937Sjohnny apic_check_free_irqs(int count)
22274937Sjohnny {
22284937Sjohnny 	int i, avail;
22294937Sjohnny 
22304937Sjohnny 	avail = 0;
22314937Sjohnny 	for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
22324937Sjohnny 		if ((apic_irq_table[i] == NULL) ||
22334937Sjohnny 		    apic_irq_table[i]->airq_mps_intr_index == FREE_INDEX) {
22344937Sjohnny 			if (++avail >= count)
22354937Sjohnny 				return (PSM_SUCCESS);
22364937Sjohnny 		}
22374937Sjohnny 	}
22384937Sjohnny 	return (PSM_FAILURE);
22394937Sjohnny }
22404937Sjohnny 
22414937Sjohnny /*
22424937Sjohnny  * This function allocates "count" MSI vector(s) for the given "dip/pri/type"
22434937Sjohnny  */
22444937Sjohnny int
22454937Sjohnny apic_alloc_msi_vectors(dev_info_t *dip, int inum, int count, int pri,
22463446Smrj     int behavior)
22473139Ssethg {
22483446Smrj 	int	rcount, i;
22497282Smishra 	uchar_t	start, irqno;
22507282Smishra 	uint32_t cpu;
22513446Smrj 	major_t	major;
22523446Smrj 	apic_irq_t	*irqptr;
22533139Ssethg 
22544937Sjohnny 	DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: dip=0x%p "
22553446Smrj 	    "inum=0x%x  pri=0x%x count=0x%x behavior=%d\n",
22564937Sjohnny 	    (void *)dip, inum, pri, count, behavior));
22573139Ssethg 
22583446Smrj 	if (count > 1) {
22593446Smrj 		if (behavior == DDI_INTR_ALLOC_STRICT &&
22608925SEvan.Yan@Sun.COM 		    apic_multi_msi_enable == 0)
22613446Smrj 			return (0);
22623446Smrj 		if (apic_multi_msi_enable == 0)
22633446Smrj 			count = 1;
22643446Smrj 	}
22653139Ssethg 
22663446Smrj 	if ((rcount = apic_navail_vector(dip, pri)) > count)
22673446Smrj 		rcount = count;
22683446Smrj 	else if (rcount == 0 || (rcount < count &&
22693446Smrj 	    behavior == DDI_INTR_ALLOC_STRICT))
22703446Smrj 		return (0);
22713139Ssethg 
22723446Smrj 	/* if not ISP2, then round it down */
22733446Smrj 	if (!ISP2(rcount))
22743446Smrj 		rcount = 1 << (highbit(rcount) - 1);
22753139Ssethg 
22763446Smrj 	mutex_enter(&airq_mutex);
22773446Smrj 
22783446Smrj 	for (start = 0; rcount > 0; rcount >>= 1) {
22793446Smrj 		if ((start = apic_find_multi_vectors(pri, rcount)) != 0 ||
22803446Smrj 		    behavior == DDI_INTR_ALLOC_STRICT)
22813446Smrj 			break;
22823139Ssethg 	}
22833139Ssethg 
22843446Smrj 	if (start == 0) {
22853446Smrj 		/* no vector available */
22863446Smrj 		mutex_exit(&airq_mutex);
22873446Smrj 		return (0);
22883446Smrj 	}
22893446Smrj 
22904937Sjohnny 	if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
22914937Sjohnny 		/* not enough free irq slots available */
22924937Sjohnny 		mutex_exit(&airq_mutex);
22934937Sjohnny 		return (0);
22944937Sjohnny 	}
22954937Sjohnny 
22968459SJerry.Gilliam@Sun.COM 	major = (dip != NULL) ? ddi_driver_major(dip) : 0;
22973446Smrj 	for (i = 0; i < rcount; i++) {
22983446Smrj 		if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
22993446Smrj 		    (uchar_t)-1) {
23004937Sjohnny 			/*
23014937Sjohnny 			 * shouldn't happen because of the
23024937Sjohnny 			 * apic_check_free_irqs() check earlier
23034937Sjohnny 			 */
23043446Smrj 			mutex_exit(&airq_mutex);
23054937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
23063446Smrj 			    "apic_allocate_irq failed\n"));
23073446Smrj 			return (i);
23083446Smrj 		}
23093446Smrj 		apic_max_device_irq = max(irqno, apic_max_device_irq);
23103446Smrj 		apic_min_device_irq = min(irqno, apic_min_device_irq);
23113446Smrj 		irqptr = apic_irq_table[irqno];
23123446Smrj #ifdef	DEBUG
23133446Smrj 		if (apic_vector_to_irq[start + i] != APIC_RESV_IRQ)
23144937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
23153446Smrj 			    "apic_vector_to_irq is not APIC_RESV_IRQ\n"));
23163446Smrj #endif
23173446Smrj 		apic_vector_to_irq[start + i] = (uchar_t)irqno;
23183446Smrj 
23193446Smrj 		irqptr->airq_vector = (uchar_t)(start + i);
23203446Smrj 		irqptr->airq_ioapicindex = (uchar_t)inum;	/* start */
23213446Smrj 		irqptr->airq_intin_no = (uchar_t)rcount;
23223446Smrj 		irqptr->airq_ipl = pri;
23233446Smrj 		irqptr->airq_vector = start + i;
23243446Smrj 		irqptr->airq_origirq = (uchar_t)(inum + i);
23253446Smrj 		irqptr->airq_share_id = 0;
23263446Smrj 		irqptr->airq_mps_intr_index = MSI_INDEX;
23273446Smrj 		irqptr->airq_dip = dip;
23283446Smrj 		irqptr->airq_major = major;
23293446Smrj 		if (i == 0) /* they all bound to the same cpu */
23303446Smrj 			cpu = irqptr->airq_cpu = apic_bind_intr(dip, irqno,
23314397Sschwartz 			    0xff, 0xff);
23323446Smrj 		else
23333446Smrj 			irqptr->airq_cpu = cpu;
23344937Sjohnny 		DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: irq=0x%x "
23353446Smrj 		    "dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
23363446Smrj 		    (void *)irqptr->airq_dip, irqptr->airq_vector,
23373446Smrj 		    irqptr->airq_origirq, pri));
23383446Smrj 	}
23393446Smrj 	mutex_exit(&airq_mutex);
23403446Smrj 	return (rcount);
23413139Ssethg }
23423139Ssethg 
23433139Ssethg /*
23444937Sjohnny  * This function allocates "count" MSI-X vector(s) for the given "dip/pri/type"
23454937Sjohnny  */
23464937Sjohnny int
23474937Sjohnny apic_alloc_msix_vectors(dev_info_t *dip, int inum, int count, int pri,
23484937Sjohnny     int behavior)
23494937Sjohnny {
23504937Sjohnny 	int	rcount, i;
23514937Sjohnny 	major_t	major;
23524937Sjohnny 
23534937Sjohnny 	mutex_enter(&airq_mutex);
23544937Sjohnny 
23554937Sjohnny 	if ((rcount = apic_navail_vector(dip, pri)) > count)
23564937Sjohnny 		rcount = count;
23574937Sjohnny 	else if (rcount == 0 || (rcount < count &&
23584937Sjohnny 	    behavior == DDI_INTR_ALLOC_STRICT)) {
23594937Sjohnny 		rcount = 0;
23604937Sjohnny 		goto out;
23614937Sjohnny 	}
23624937Sjohnny 
23634937Sjohnny 	if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
23644937Sjohnny 		/* not enough free irq slots available */
23654937Sjohnny 		rcount = 0;
23664937Sjohnny 		goto out;
23674937Sjohnny 	}
23684937Sjohnny 
23698459SJerry.Gilliam@Sun.COM 	major = (dip != NULL) ? ddi_driver_major(dip) : 0;
23704937Sjohnny 	for (i = 0; i < rcount; i++) {
23714937Sjohnny 		uchar_t	vector, irqno;
23724937Sjohnny 		apic_irq_t	*irqptr;
23734937Sjohnny 
23744937Sjohnny 		if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
23754937Sjohnny 		    (uchar_t)-1) {
23764937Sjohnny 			/*
23774937Sjohnny 			 * shouldn't happen because of the
23784937Sjohnny 			 * apic_check_free_irqs() check earlier
23794937Sjohnny 			 */
23804937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
23814937Sjohnny 			    "apic_allocate_irq failed\n"));
23824937Sjohnny 			rcount = i;
23834937Sjohnny 			goto out;
23844937Sjohnny 		}
23854937Sjohnny 		if ((vector = apic_allocate_vector(pri, irqno, 1)) == 0) {
23864937Sjohnny 			/*
23874937Sjohnny 			 * shouldn't happen because of the
23884937Sjohnny 			 * apic_navail_vector() call earlier
23894937Sjohnny 			 */
23904937Sjohnny 			DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
23914937Sjohnny 			    "apic_allocate_vector failed\n"));
23924937Sjohnny 			rcount = i;
23934937Sjohnny 			goto out;
23944937Sjohnny 		}
23954937Sjohnny 		apic_max_device_irq = max(irqno, apic_max_device_irq);
23964937Sjohnny 		apic_min_device_irq = min(irqno, apic_min_device_irq);
23974937Sjohnny 		irqptr = apic_irq_table[irqno];
23984937Sjohnny 		irqptr->airq_vector = (uchar_t)vector;
23994937Sjohnny 		irqptr->airq_ipl = pri;
24004937Sjohnny 		irqptr->airq_origirq = (uchar_t)(inum + i);
24014937Sjohnny 		irqptr->airq_share_id = 0;
24024937Sjohnny 		irqptr->airq_mps_intr_index = MSIX_INDEX;
24034937Sjohnny 		irqptr->airq_dip = dip;
24044937Sjohnny 		irqptr->airq_major = major;
24054937Sjohnny 		irqptr->airq_cpu = apic_bind_intr(dip, irqno, 0xff, 0xff);
24064937Sjohnny 	}
24074937Sjohnny out:
24084937Sjohnny 	mutex_exit(&airq_mutex);
24094937Sjohnny 	return (rcount);
24104937Sjohnny }
24114937Sjohnny 
24124937Sjohnny /*
24133446Smrj  * Allocate a free vector for irq at ipl. Takes care of merging of multiple
24143446Smrj  * IPLs into a single APIC level as well as stretching some IPLs onto multiple
24153446Smrj  * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
24163446Smrj  * requests and allocated only when pri is set.
24170Sstevel@tonic-gate  */
24183446Smrj uchar_t
24193446Smrj apic_allocate_vector(int ipl, int irq, int pri)
24200Sstevel@tonic-gate {
24213446Smrj 	int	lowest, highest, i;
24220Sstevel@tonic-gate 
24233446Smrj 	highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK;
24243446Smrj 	lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL;
24250Sstevel@tonic-gate 
24263446Smrj 	if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
24273446Smrj 		lowest -= APIC_VECTOR_PER_IPL;
24283139Ssethg 
24293446Smrj #ifdef	DEBUG
24303446Smrj 	if (apic_restrict_vector)	/* for testing shared interrupt logic */
24313446Smrj 		highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS;
24323446Smrj #endif /* DEBUG */
24333446Smrj 	if (pri == 0)
24343446Smrj 		highest -= APIC_HI_PRI_VECTS;
24353139Ssethg 
2436*11641SKerry.Shu@Sun.COM 	for (i = lowest; i <= highest; i++) {
24373446Smrj 		if (APIC_CHECK_RESERVE_VECTORS(i))
24383446Smrj 			continue;
24393446Smrj 		if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
24403446Smrj 			apic_vector_to_irq[i] = (uchar_t)irq;
24413446Smrj 			return (i);
24420Sstevel@tonic-gate 		}
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24453446Smrj 	return (0);
24463446Smrj }
24473446Smrj 
24483446Smrj /* Mark vector as not being used by any irq */
24493446Smrj void
24503446Smrj apic_free_vector(uchar_t vector)
24513446Smrj {
24523446Smrj 	apic_vector_to_irq[vector] = APIC_RESV_IRQ;
24533446Smrj }
24543446Smrj 
24553446Smrj uint32_t
24563446Smrj ioapic_read(int ioapic_ix, uint32_t reg)
24573446Smrj {
24583446Smrj 	volatile uint32_t *ioapic;
24593446Smrj 
24603446Smrj 	ioapic = apicioadr[ioapic_ix];
24613446Smrj 	ioapic[APIC_IO_REG] = reg;
24623446Smrj 	return (ioapic[APIC_IO_DATA]);
24633446Smrj }
24643139Ssethg 
24653446Smrj void
24663446Smrj ioapic_write(int ioapic_ix, uint32_t reg, uint32_t value)
24673446Smrj {
24683446Smrj 	volatile uint32_t *ioapic;
24693446Smrj 
24703446Smrj 	ioapic = apicioadr[ioapic_ix];
24713446Smrj 	ioapic[APIC_IO_REG] = reg;
24723446Smrj 	ioapic[APIC_IO_DATA] = value;
24733446Smrj }
24743446Smrj 
24757282Smishra void
24767282Smishra ioapic_write_eoi(int ioapic_ix, uint32_t value)
24777282Smishra {
24787282Smishra 	volatile uint32_t *ioapic;
24797282Smishra 
24807282Smishra 	ioapic = apicioadr[ioapic_ix];
24817282Smishra 	ioapic[APIC_IO_EOI] = value;
24827282Smishra }
24837282Smishra 
24843446Smrj static processorid_t
24853446Smrj apic_find_cpu(int flag)
24863446Smrj {
24873446Smrj 	processorid_t acid = 0;
24883446Smrj 	int i;
24893446Smrj 
24903446Smrj 	/* Find the first CPU with the passed-in flag set */
24913446Smrj 	for (i = 0; i < apic_nproc; i++) {
24923446Smrj 		if (apic_cpus[i].aci_status & flag) {
24933446Smrj 			acid = i;
24943446Smrj 			break;
24953446Smrj 		}
24963446Smrj 	}
24973446Smrj 
24983446Smrj 	ASSERT((apic_cpus[acid].aci_status & flag) != 0);
24993446Smrj 	return (acid);
25003446Smrj }
25013139Ssethg 
25023446Smrj /*
25033446Smrj  * Call rebind to do the actual programming.
25043446Smrj  * Must be called with interrupts disabled and apic_ioapic_lock held
25053446Smrj  * 'p' is polymorphic -- if this function is called to process a deferred
25063446Smrj  * reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
25073446Smrj  * the irq pointer is retrieved.  If not doing deferred reprogramming,
25083446Smrj  * p is of the type 'apic_irq_t *'.
25093446Smrj  *
25103446Smrj  * apic_ioapic_lock must be held across this call, as it protects apic_rebind
25113446Smrj  * and it protects apic_find_cpu() from a race in which a CPU can be taken
25123446Smrj  * offline after a cpu is selected, but before apic_rebind is called to
25133446Smrj  * bind interrupts to it.
25143446Smrj  */
25153446Smrj int
25163446Smrj apic_setup_io_intr(void *p, int irq, boolean_t deferred)
25173446Smrj {
25183446Smrj 	apic_irq_t *irqptr;
25193446Smrj 	struct ioapic_reprogram_data *drep = NULL;
25203446Smrj 	int rv;
25213446Smrj 
25223446Smrj 	if (deferred) {
25233446Smrj 		drep = (struct ioapic_reprogram_data *)p;
25243446Smrj 		ASSERT(drep != NULL);
25253446Smrj 		irqptr = drep->irqp;
25263446Smrj 	} else
25273446Smrj 		irqptr = (apic_irq_t *)p;
25283446Smrj 
25293446Smrj 	ASSERT(irqptr != NULL);
25303446Smrj 
25313446Smrj 	rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, drep);
25323446Smrj 	if (rv) {
25333446Smrj 		/*
25343446Smrj 		 * CPU is not up or interrupts are disabled. Fall back to
25353446Smrj 		 * the first available CPU
25363446Smrj 		 */
25373446Smrj 		rv = apic_rebind(irqptr, apic_find_cpu(APIC_CPU_INTR_ENABLE),
25383446Smrj 		    drep);
25393446Smrj 	}
25403446Smrj 
25413446Smrj 	return (rv);
25420Sstevel@tonic-gate }
25433446Smrj 
25443446Smrj 
25453446Smrj uchar_t
25463446Smrj apic_modify_vector(uchar_t vector, int irq)
25473446Smrj {
25483446Smrj 	apic_vector_to_irq[vector] = (uchar_t)irq;
25493446Smrj 	return (vector);
25503446Smrj }
25514397Sschwartz 
25524397Sschwartz char *
25534397Sschwartz apic_get_apic_type()
25544397Sschwartz {
25554397Sschwartz 	return (apic_psm_info.p_mach_idstring);
25564397Sschwartz }
25577282Smishra 
25587282Smishra void
25597282Smishra x2apic_update_psm()
25607282Smishra {
25617282Smishra 	struct psm_ops *pops = &apic_ops;
25627282Smishra 
25637282Smishra 	ASSERT(pops != NULL);
25647282Smishra 
25657986SSaurabh.Mishra@Sun.COM 	/*
25667986SSaurabh.Mishra@Sun.COM 	 * We don't need to do any magic if one of the following
25677986SSaurabh.Mishra@Sun.COM 	 * conditions is true :
25687986SSaurabh.Mishra@Sun.COM 	 * - Not being run under kernel debugger.
25697986SSaurabh.Mishra@Sun.COM 	 * - MP is not set.
25707986SSaurabh.Mishra@Sun.COM 	 * - Booted with one CPU only.
25717986SSaurabh.Mishra@Sun.COM 	 * - One CPU configured.
25727986SSaurabh.Mishra@Sun.COM 	 *
25737986SSaurabh.Mishra@Sun.COM 	 * We set apic_common_send_ipi() since kernel debuggers
25747986SSaurabh.Mishra@Sun.COM 	 * attempt to send IPIs to other slave CPUs during
25757986SSaurabh.Mishra@Sun.COM 	 * entry (exit) from (to) debugger.
25767986SSaurabh.Mishra@Sun.COM 	 */
25777986SSaurabh.Mishra@Sun.COM 	if (!(boothowto & RB_DEBUG) || use_mp == 0 ||
25787986SSaurabh.Mishra@Sun.COM 	    apic_nproc == 1 || boot_ncpus == 1) {
25797986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi =  x2apic_send_ipi;
25807986SSaurabh.Mishra@Sun.COM 	} else {
25817986SSaurabh.Mishra@Sun.COM 		pops->psm_send_ipi =  apic_common_send_ipi;
25827986SSaurabh.Mishra@Sun.COM 	}
25837986SSaurabh.Mishra@Sun.COM 
25847282Smishra 	pops->psm_intr_exit = x2apic_intr_exit;
25857282Smishra 	pops->psm_setspl = x2apic_setspl;
25867282Smishra 
25877282Smishra 	send_dirintf = pops->psm_send_ipi;
25887986SSaurabh.Mishra@Sun.COM 
25897986SSaurabh.Mishra@Sun.COM 	apic_mode = LOCAL_X2APIC;
25907986SSaurabh.Mishra@Sun.COM 	apic_change_ops();
25917282Smishra }
25928675SVikram.Hegde@Sun.COM 
25938675SVikram.Hegde@Sun.COM static void
259411600SVikram.Hegde@Sun.COM apic_intrmap_init(int apic_mode)
25958675SVikram.Hegde@Sun.COM {
259610345SSaurabh.Mishra@Sun.COM 	int suppress_brdcst_eoi = 0;
259710345SSaurabh.Mishra@Sun.COM 
25988675SVikram.Hegde@Sun.COM 	if (psm_vt_ops != NULL) {
259911383SSaurabh.Mishra@Sun.COM 		/*
260011383SSaurabh.Mishra@Sun.COM 		 * Since X2APIC requires the use of interrupt remapping
260111383SSaurabh.Mishra@Sun.COM 		 * (though this is not documented explicitly in the Intel
260211383SSaurabh.Mishra@Sun.COM 		 * documentation (yet)), initialize interrupt remapping
260311383SSaurabh.Mishra@Sun.COM 		 * support before initializing the X2APIC unit.
260411383SSaurabh.Mishra@Sun.COM 		 */
260511600SVikram.Hegde@Sun.COM 		if (((apic_intrmap_ops_t *)psm_vt_ops)->
260611600SVikram.Hegde@Sun.COM 		    apic_intrmap_init(apic_mode) == DDI_SUCCESS) {
260711600SVikram.Hegde@Sun.COM 
26088675SVikram.Hegde@Sun.COM 			apic_vt_ops = psm_vt_ops;
260910345SSaurabh.Mishra@Sun.COM 
261010345SSaurabh.Mishra@Sun.COM 			/*
261110345SSaurabh.Mishra@Sun.COM 			 * We leverage the interrupt remapping engine to
261210345SSaurabh.Mishra@Sun.COM 			 * suppress broadcast EOI; thus we must send the
261310345SSaurabh.Mishra@Sun.COM 			 * directed EOI with the directed-EOI handler.
261410345SSaurabh.Mishra@Sun.COM 			 */
261510345SSaurabh.Mishra@Sun.COM 			if (apic_directed_EOI_supported() == 0) {
261610345SSaurabh.Mishra@Sun.COM 				suppress_brdcst_eoi = 1;
261710345SSaurabh.Mishra@Sun.COM 			}
261810345SSaurabh.Mishra@Sun.COM 
261911600SVikram.Hegde@Sun.COM 			apic_vt_ops->apic_intrmap_enable(suppress_brdcst_eoi);
262011383SSaurabh.Mishra@Sun.COM 
262111383SSaurabh.Mishra@Sun.COM 			if (apic_detect_x2apic()) {
262211383SSaurabh.Mishra@Sun.COM 				apic_enable_x2apic();
262311383SSaurabh.Mishra@Sun.COM 			}
262411383SSaurabh.Mishra@Sun.COM 
262511383SSaurabh.Mishra@Sun.COM 			if (apic_directed_EOI_supported() == 0) {
262611383SSaurabh.Mishra@Sun.COM 				apic_set_directed_EOI_handler();
262711383SSaurabh.Mishra@Sun.COM 			}
26288675SVikram.Hegde@Sun.COM 		}
26298675SVikram.Hegde@Sun.COM 	}
26308675SVikram.Hegde@Sun.COM }
26318675SVikram.Hegde@Sun.COM 
26328675SVikram.Hegde@Sun.COM /*ARGSUSED*/
26338675SVikram.Hegde@Sun.COM static void
26348675SVikram.Hegde@Sun.COM apic_record_ioapic_rdt(apic_irq_t *irq_ptr, ioapic_rdt_t *irdt)
26358675SVikram.Hegde@Sun.COM {
26368675SVikram.Hegde@Sun.COM 	irdt->ir_hi <<= APIC_ID_BIT_OFFSET;
26378675SVikram.Hegde@Sun.COM }
26388675SVikram.Hegde@Sun.COM 
26398675SVikram.Hegde@Sun.COM /*ARGSUSED*/
26408675SVikram.Hegde@Sun.COM static void
26418675SVikram.Hegde@Sun.COM apic_record_msi(apic_irq_t *irq_ptr, msi_regs_t *mregs)
26428675SVikram.Hegde@Sun.COM {
26438675SVikram.Hegde@Sun.COM 	mregs->mr_addr = MSI_ADDR_HDR |
26448675SVikram.Hegde@Sun.COM 	    (MSI_ADDR_RH_FIXED << MSI_ADDR_RH_SHIFT) |
26458675SVikram.Hegde@Sun.COM 	    (MSI_ADDR_DM_PHYSICAL << MSI_ADDR_DM_SHIFT) |
26468675SVikram.Hegde@Sun.COM 	    (mregs->mr_addr << MSI_ADDR_DEST_SHIFT);
26478675SVikram.Hegde@Sun.COM 	mregs->mr_data = (MSI_DATA_TM_EDGE << MSI_DATA_TM_SHIFT) |
26488675SVikram.Hegde@Sun.COM 	    mregs->mr_data;
26498675SVikram.Hegde@Sun.COM }
2650