xref: /onnv-gate/usr/src/uts/i86pc/io/psm/uppc.c (revision 8550:0cc93b5e7ddc)
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
51742Sanish  * Common Development and Distribution License (the "License").
61742Sanish  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8550SSeth.Goldberg@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
265295Srandyf #define	PSMI_1_6
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/mutex.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/time.h>
310Sstevel@tonic-gate #include <sys/machlock.h>
320Sstevel@tonic-gate #include <sys/smp_impldefs.h>
330Sstevel@tonic-gate #include <sys/uadmin.h>
340Sstevel@tonic-gate #include <sys/promif.h>
350Sstevel@tonic-gate #include <sys/psm.h>
360Sstevel@tonic-gate #include <sys/pit.h>
37*8550SSeth.Goldberg@Sun.COM #include <sys/apic.h>
380Sstevel@tonic-gate #include <sys/psm_common.h>
390Sstevel@tonic-gate #include <sys/atomic.h>
403446Smrj #include <sys/archsystm.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	NSEC_IN_SEC		1000000000
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Local Function Prototypes
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate static void uppc_softinit(void);
480Sstevel@tonic-gate static void uppc_picinit();
49*8550SSeth.Goldberg@Sun.COM static int uppc_post_cpu_start(void);
500Sstevel@tonic-gate static int uppc_clkinit(int);
510Sstevel@tonic-gate static int uppc_addspl(int irqno, int ipl, int min_ipl, int max_ipl);
520Sstevel@tonic-gate static int uppc_delspl(int irqno, int ipl, int min_ipl, int max_ipl);
530Sstevel@tonic-gate static processorid_t uppc_get_next_processorid(processorid_t cpu_id);
540Sstevel@tonic-gate static int uppc_get_clockirq(int ipl);
550Sstevel@tonic-gate static int uppc_probe(void);
560Sstevel@tonic-gate static int uppc_translate_irq(dev_info_t *dip, int irqno);
570Sstevel@tonic-gate static void uppc_shutdown(int cmd, int fcn);
580Sstevel@tonic-gate static void uppc_preshutdown(int cmd, int fcn);
59*8550SSeth.Goldberg@Sun.COM static int uppc_state(psm_state_request_t *request);
600Sstevel@tonic-gate static int uppc_init_acpi(void);
610Sstevel@tonic-gate static void uppc_setspl(int);
620Sstevel@tonic-gate static int uppc_intr_enter(int, int *);
630Sstevel@tonic-gate static void uppc_intr_exit(int, int);
640Sstevel@tonic-gate static hrtime_t uppc_gethrtime();
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static int uppc_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
670Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Global Data
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate static struct standard_pic pics0;
730Sstevel@tonic-gate int uppc_use_acpi = 1;	/* Use ACPI by default */
740Sstevel@tonic-gate int uppc_enable_acpi = 0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * For interrupt link devices, if uppc_unconditional_srs is set, an irq resource
790Sstevel@tonic-gate  * will be assigned (via _SRS). If it is not set, use the current
800Sstevel@tonic-gate  * irq setting (via _CRS), but only if that irq is in the set of possible
810Sstevel@tonic-gate  * irqs (returned by _PRS) for the device.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate int uppc_unconditional_srs = 1;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * For interrupt link devices, if uppc_prefer_crs is set when we are
870Sstevel@tonic-gate  * assigning an IRQ resource to a device, prefer the current IRQ setting
880Sstevel@tonic-gate  * over other possible irq settings under same conditions.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate int uppc_prefer_crs = 1;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int uppc_verbose = 0;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* flag definitions for uppc_verbose */
950Sstevel@tonic-gate #define	UPPC_VERBOSE_IRQ_FLAG			0x00000001
960Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF_FLAG		0x00000002
970Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF_PAUSE_FLAG	0x00000004
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	UPPC_VERBOSE_IRQ(fmt) \
1010Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_IRQ_FLAG) \
1020Sstevel@tonic-gate 		cmn_err fmt;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF(fmt) \
1050Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_FLAG) \
1060Sstevel@tonic-gate 		prom_printf fmt;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate uchar_t uppc_reserved_irqlist[MAX_ISA_IRQ + 1];
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static uint16_t uppc_irq_shared_table[MAX_ISA_IRQ + 1];
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Contains SCI irqno from FADT after initialization
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate static int uppc_sci = -1;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Local Static Data
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static lock_t uppc_gethrtime_lock;
1220Sstevel@tonic-gate static hrtime_t uppc_lasthrtime;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #ifdef UPPC_DEBUG
1260Sstevel@tonic-gate #define	DENT	0x0001
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static int	uppc_debug = 0;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #endif
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static struct	psm_ops uppc_ops = {
1350Sstevel@tonic-gate 	uppc_probe,				/* psm_probe		*/
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	uppc_softinit,				/* psm_init		*/
1380Sstevel@tonic-gate 	uppc_picinit,				/* psm_picinit		*/
1390Sstevel@tonic-gate 	uppc_intr_enter,			/* psm_intr_enter	*/
1400Sstevel@tonic-gate 	uppc_intr_exit,				/* psm_intr_exit	*/
1410Sstevel@tonic-gate 	uppc_setspl,				/* psm_setspl		*/
1420Sstevel@tonic-gate 	uppc_addspl,				/* psm_addspl		*/
1430Sstevel@tonic-gate 	uppc_delspl,				/* psm_delspl		*/
1440Sstevel@tonic-gate 	(int (*)(processorid_t))NULL,		/* psm_disable_intr	*/
1450Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_enable_intr	*/
1460Sstevel@tonic-gate 	(int (*)(int))NULL,			/* psm_softlvl_to_irq	*/
1470Sstevel@tonic-gate 	(void (*)(int))NULL,			/* psm_set_softintr	*/
1480Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_set_idlecpu	*/
1490Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_unset_idlecpu	*/
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	uppc_clkinit,				/* psm_clkinit		*/
1520Sstevel@tonic-gate 	uppc_get_clockirq,			/* psm_get_clockirq	*/
1530Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_hrtimeinit	*/
1540Sstevel@tonic-gate 	uppc_gethrtime,				/* psm_gethrtime	*/
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	uppc_get_next_processorid,		/* psm_get_next_processorid */
1573446Smrj 	(int (*)(processorid_t, caddr_t))NULL,	/* psm_cpu_start	*/
158*8550SSeth.Goldberg@Sun.COM 	uppc_post_cpu_start,			/* psm_post_cpu_start	*/
1590Sstevel@tonic-gate 	uppc_shutdown,				/* psm_shutdown		*/
1600Sstevel@tonic-gate 	(int (*)(int, int))NULL,		/* psm_get_ipivect	*/
1610Sstevel@tonic-gate 	(void (*)(processorid_t, int))NULL,	/* psm_send_ipi		*/
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	uppc_translate_irq,			/* psm_translate_irq	*/
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	(void (*)(int, char *))NULL,		/* psm_notify_error	*/
1660Sstevel@tonic-gate 	(void (*)(int msg))NULL,		/* psm_notify_func	*/
1670Sstevel@tonic-gate 	(void (*)(hrtime_t time))NULL,		/* psm_timer_reprogram	*/
1680Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_timer_enable	*/
1690Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_timer_disable	*/
1700Sstevel@tonic-gate 	(void (*)(void *arg))NULL,		/* psm_post_cyclic_setup */
1710Sstevel@tonic-gate 	uppc_preshutdown,			/* psm_preshutdown	*/
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	(int (*)(dev_info_t *, ddi_intr_handle_impl_t *,
174*8550SSeth.Goldberg@Sun.COM 	    psm_intr_op_t, int *))NULL,		/* psm_intr_ops		*/
175*8550SSeth.Goldberg@Sun.COM 
176*8550SSeth.Goldberg@Sun.COM 	uppc_state				/* psm_state		*/
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static struct	psm_info uppc_info = {
181*8550SSeth.Goldberg@Sun.COM 	PSM_INFO_VER01_6,	/* version				*/
1820Sstevel@tonic-gate 	PSM_OWN_SYS_DEFAULT,	/* ownership				*/
1830Sstevel@tonic-gate 	(struct psm_ops *)&uppc_ops, /* operation			*/
1840Sstevel@tonic-gate 	"uppc",			/* machine name				*/
1850Sstevel@tonic-gate 	"UniProcessor PC",	/* machine descriptions			*/
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Configuration Data
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * This is the loadable module wrapper.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate #include <sys/modctl.h>
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static void *uppc_hdlp;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate int
2000Sstevel@tonic-gate _init(void)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	return (psm_mod_init(&uppc_hdlp, &uppc_info));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate int
2060Sstevel@tonic-gate _fini(void)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	return (psm_mod_fini(&uppc_hdlp, &uppc_info));
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int
2120Sstevel@tonic-gate _info(struct modinfo *modinfop)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	return (psm_mod_info(&uppc_hdlp, &uppc_info, modinfop));
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Autoconfiguration Routines
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static int
2220Sstevel@tonic-gate uppc_probe(void)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	return (PSM_SUCCESS);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static void
2300Sstevel@tonic-gate uppc_softinit(void)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	struct standard_pic *pp;
2330Sstevel@tonic-gate 	int i;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	pp = &pics0;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (uppc_use_acpi && uppc_init_acpi()) {
2390Sstevel@tonic-gate 		build_reserved_irqlist((uchar_t *)uppc_reserved_irqlist);
2400Sstevel@tonic-gate 		for (i = 0; i <= MAX_ISA_IRQ; i++)
2410Sstevel@tonic-gate 			uppc_irq_shared_table[i] = 0;
2420Sstevel@tonic-gate 		uppc_enable_acpi = 1;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * initialize the ipl mask
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	for (i = 0; i < (MAXIPL << 1); i += 2) {
2490Sstevel@tonic-gate 		/* enable slave lines on master */
2500Sstevel@tonic-gate 		pp->c_iplmask[i] = 0xff;
2510Sstevel@tonic-gate 		pp->c_iplmask[i+1] = (0xff & ~(1 << MASTERLINE));
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*ARGSUSED*/
2560Sstevel@tonic-gate static int
2570Sstevel@tonic-gate uppc_clkinit(int hertz)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	ulong_t clkticks = PIT_HZ / hz;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (hertz == 0)
2620Sstevel@tonic-gate 		return (0);	/* One shot mode not supported */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * program timer 0
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	outb(PITCTL_PORT, (PIT_C0|PIT_NDIVMODE|PIT_READMODE));
2680Sstevel@tonic-gate 	outb(PITCTR0_PORT, (uchar_t)clkticks);
2690Sstevel@tonic-gate 	outb(PITCTR0_PORT, (uchar_t)(clkticks>>8));
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	return (NSEC_IN_SEC / hertz);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static void
2750Sstevel@tonic-gate uppc_picinit()
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	picsetup();
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * If a valid SCI is present, manually addspl()
2810Sstevel@tonic-gate 	 * since we're not set-up early enough in boot
2820Sstevel@tonic-gate 	 * to do it "conventionally" (via add_avintr)
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	if (uppc_sci >= 0)
2850Sstevel@tonic-gate 		(void) uppc_addspl(uppc_sci, SCI_IPL, SCI_IPL, SCI_IPL);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
288*8550SSeth.Goldberg@Sun.COM static int
289*8550SSeth.Goldberg@Sun.COM uppc_post_cpu_start(void)
290*8550SSeth.Goldberg@Sun.COM {
291*8550SSeth.Goldberg@Sun.COM 	/*
292*8550SSeth.Goldberg@Sun.COM 	 * On uppc machines psm_post_cpu_start is called during S3 resume
293*8550SSeth.Goldberg@Sun.COM 	 * on the boot cpu from assembly, using the ap_mlsetup vector.
294*8550SSeth.Goldberg@Sun.COM 	 */
295*8550SSeth.Goldberg@Sun.COM 
296*8550SSeth.Goldberg@Sun.COM 	/*
297*8550SSeth.Goldberg@Sun.COM 	 * Init master and slave pic
298*8550SSeth.Goldberg@Sun.COM 	 */
299*8550SSeth.Goldberg@Sun.COM 	picsetup();
300*8550SSeth.Goldberg@Sun.COM 
301*8550SSeth.Goldberg@Sun.COM 	/*
302*8550SSeth.Goldberg@Sun.COM 	 * program timer 0
303*8550SSeth.Goldberg@Sun.COM 	 */
304*8550SSeth.Goldberg@Sun.COM 	(void) uppc_clkinit(hz);
305*8550SSeth.Goldberg@Sun.COM 
306*8550SSeth.Goldberg@Sun.COM 	return (PSM_SUCCESS);
307*8550SSeth.Goldberg@Sun.COM }
308*8550SSeth.Goldberg@Sun.COM 
3090Sstevel@tonic-gate /*ARGSUSED3*/
3100Sstevel@tonic-gate static int
3110Sstevel@tonic-gate uppc_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	struct standard_pic *pp;
3140Sstevel@tonic-gate 	int i;
3150Sstevel@tonic-gate 	int startidx;
3160Sstevel@tonic-gate 	uchar_t	vectmask;
3170Sstevel@tonic-gate 
3181742Sanish 	if (irqno <= MAX_ISA_IRQ)
3191742Sanish 		atomic_add_16(&uppc_irq_shared_table[irqno], 1);
3201742Sanish 
3210Sstevel@tonic-gate 	if (ipl != min_ipl)
3220Sstevel@tonic-gate 		return (0);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (irqno > 7) {
3250Sstevel@tonic-gate 		vectmask = 1 << (irqno - 8);
3260Sstevel@tonic-gate 		startidx = (ipl << 1);
3270Sstevel@tonic-gate 	} else {
3280Sstevel@tonic-gate 		vectmask = 1 << irqno;
3290Sstevel@tonic-gate 		startidx = (ipl << 1) + 1;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * mask intr same or above ipl
3340Sstevel@tonic-gate 	 * level MAXIPL has all intr off as init. default
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	pp = &pics0;
3370Sstevel@tonic-gate 	for (i = startidx; i < (MAXIPL << 1); i += 2) {
3380Sstevel@tonic-gate 		if (pp->c_iplmask[i] & vectmask)
3390Sstevel@tonic-gate 			break;
3400Sstevel@tonic-gate 		pp->c_iplmask[i] |= vectmask;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * unmask intr below ipl
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	for (i = startidx-2; i >= 0; i -= 2) {
3470Sstevel@tonic-gate 		if (!(pp->c_iplmask[i] & vectmask))
3480Sstevel@tonic-gate 			break;
3490Sstevel@tonic-gate 		pp->c_iplmask[i] &= ~vectmask;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	return (0);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate uppc_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	struct standard_pic *pp;
3580Sstevel@tonic-gate 	int i;
3590Sstevel@tonic-gate 	uchar_t	vectmask;
3600Sstevel@tonic-gate 
3611742Sanish 	if (irqno <= MAX_ISA_IRQ)
3621742Sanish 		atomic_add_16(&uppc_irq_shared_table[irqno], -1);
3631742Sanish 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * skip if we are not deleting the last handler
3660Sstevel@tonic-gate 	 * and the ipl is higher than minimum
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if ((max_ipl != PSM_INVALID_IPL) && (ipl >= min_ipl))
3690Sstevel@tonic-gate 		return (0);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (irqno > 7) {
3720Sstevel@tonic-gate 		vectmask = 1 << (irqno - 8);
3730Sstevel@tonic-gate 		i = 0;
3740Sstevel@tonic-gate 	} else {
3750Sstevel@tonic-gate 		vectmask = 1 << irqno;
3760Sstevel@tonic-gate 		i = 1;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	pp = &pics0;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3820Sstevel@tonic-gate 	 * check any handlers left for this irqno
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	if (max_ipl != PSM_INVALID_IPL) {
3850Sstevel@tonic-gate 		/*
3860Sstevel@tonic-gate 		 * unmasks all levels below the lowest priority
3870Sstevel@tonic-gate 		 */
3880Sstevel@tonic-gate 		i += ((min_ipl - 1) << 1);
3890Sstevel@tonic-gate 		for (; i >= 0; i -= 2) {
3900Sstevel@tonic-gate 			if (!(pp->c_iplmask[i] & vectmask))
3910Sstevel@tonic-gate 				break;
3920Sstevel@tonic-gate 			pp->c_iplmask[i] &= ~vectmask;
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 	} else {
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * set mask to all levels
3970Sstevel@tonic-gate 		 */
3980Sstevel@tonic-gate 		for (; i < (MAXIPL << 1); i += 2) {
3990Sstevel@tonic-gate 			if (pp->c_iplmask[i] & vectmask)
4000Sstevel@tonic-gate 				break;
4010Sstevel@tonic-gate 			pp->c_iplmask[i] |= vectmask;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	return (0);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static processorid_t
4080Sstevel@tonic-gate uppc_get_next_processorid(processorid_t cpu_id)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	if (cpu_id == -1)
4110Sstevel@tonic-gate 		return (0);
4120Sstevel@tonic-gate 	return (-1);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*ARGSUSED*/
4160Sstevel@tonic-gate static int
4170Sstevel@tonic-gate uppc_get_clockirq(int ipl)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	return (CLOCK_VECTOR);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate static int
4240Sstevel@tonic-gate uppc_init_acpi(void)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	int verboseflags = 0;
4270Sstevel@tonic-gate 	int	sci;
4280Sstevel@tonic-gate 	iflag_t sci_flags;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * Process SCI configuration here; this may return
4320Sstevel@tonic-gate 	 * an error if acpi-user-options has specified
4330Sstevel@tonic-gate 	 * legacy mode (use ACPI without ACPI mode or SCI)
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (acpica_get_sci(&sci, &sci_flags) != AE_OK)
4360Sstevel@tonic-gate 		sci = -1;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Initialize sub-system - if error is returns, ACPI is not
4400Sstevel@tonic-gate 	 * used.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	if (acpica_init() != AE_OK)
4430Sstevel@tonic-gate 		return (0);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * uppc implies system is in PIC mode; set edge/level
4470Sstevel@tonic-gate 	 * via ELCR based on return value from get_sci; this
4480Sstevel@tonic-gate 	 * will default to level/low if no override present,
4490Sstevel@tonic-gate 	 * as recommended by Intel ACPI CA team.
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if (sci >= 0) {
4520Sstevel@tonic-gate 		ASSERT((sci_flags.intr_el == INTR_EL_LEVEL) ||
4530Sstevel@tonic-gate 		    (sci_flags.intr_el == INTR_EL_EDGE));
4540Sstevel@tonic-gate 
455347Smyers 		psm_set_elcr(sci, sci_flags.intr_el == INTR_EL_LEVEL);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4590Sstevel@tonic-gate 	 * Remember SCI for later use
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	uppc_sci = sci;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_IRQ_FLAG)
4640Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_IRQ_FLAG;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_FLAG)
4670Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_POWEROFF_FLAG;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_PAUSE_FLAG)
4700Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_POWEROFF_PAUSE_FLAG;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (acpi_psm_init(uppc_info.p_mach_idstring, verboseflags) ==
4730Sstevel@tonic-gate 	    ACPI_PSM_FAILURE) {
4740Sstevel@tonic-gate 		return (0);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	return (1);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate static void
4820Sstevel@tonic-gate uppc_preshutdown(int cmd, int fcn)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	UPPC_VERBOSE_POWEROFF(("uppc_preshutdown(%d,%d);\n", cmd, fcn));
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate static void
4890Sstevel@tonic-gate uppc_shutdown(int cmd, int fcn)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	UPPC_VERBOSE_POWEROFF(("uppc_shutdown(%d,%d);\n", cmd, fcn));
4920Sstevel@tonic-gate 
4933472Smyers 	/*
4943472Smyers 	 * Return if passed a command other than A_SHUTDOWN or
4953472Smyers 	 * if we're not using ACPI.
4963472Smyers 	 */
4973472Smyers 	if ((cmd != A_SHUTDOWN) || (!uppc_enable_acpi))
4980Sstevel@tonic-gate 		return;
4993472Smyers 
5004189Smyers 	/*
5014189Smyers 	 * Switch system back into Legacy-Mode if using ACPI and
5024189Smyers 	 * not powering-off.  Some BIOSes need to remain in ACPI-mode
5034189Smyers 	 * for power-off to succeed (Dell Dimension 4600)
5044189Smyers 	 */
5054189Smyers 	if (fcn != AD_POWEROFF) {
5064189Smyers 		(void) AcpiDisable();
5073472Smyers 		return;
5084189Smyers 	}
5093472Smyers 
5103472Smyers 	(void) acpi_poweroff();
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
513*8550SSeth.Goldberg@Sun.COM 
514*8550SSeth.Goldberg@Sun.COM static int
515*8550SSeth.Goldberg@Sun.COM uppc_acpi_enter_picmode(void)
516*8550SSeth.Goldberg@Sun.COM {
517*8550SSeth.Goldberg@Sun.COM 	ACPI_OBJECT_LIST	arglist;
518*8550SSeth.Goldberg@Sun.COM 	ACPI_OBJECT		arg;
519*8550SSeth.Goldberg@Sun.COM 	ACPI_STATUS		status;
520*8550SSeth.Goldberg@Sun.COM 
521*8550SSeth.Goldberg@Sun.COM 	/* Setup parameter object */
522*8550SSeth.Goldberg@Sun.COM 	arglist.Count = 1;
523*8550SSeth.Goldberg@Sun.COM 	arglist.Pointer = &arg;
524*8550SSeth.Goldberg@Sun.COM 	arg.Type = ACPI_TYPE_INTEGER;
525*8550SSeth.Goldberg@Sun.COM 	arg.Integer.Value = ACPI_PIC_MODE;
526*8550SSeth.Goldberg@Sun.COM 
527*8550SSeth.Goldberg@Sun.COM 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
528*8550SSeth.Goldberg@Sun.COM 	if (ACPI_FAILURE(status))
529*8550SSeth.Goldberg@Sun.COM 		return (PSM_FAILURE);
530*8550SSeth.Goldberg@Sun.COM 	else
531*8550SSeth.Goldberg@Sun.COM 		return (PSM_SUCCESS);
532*8550SSeth.Goldberg@Sun.COM }
533*8550SSeth.Goldberg@Sun.COM 
534*8550SSeth.Goldberg@Sun.COM 
535*8550SSeth.Goldberg@Sun.COM struct pic_state {
536*8550SSeth.Goldberg@Sun.COM 	int8_t		mmask;
537*8550SSeth.Goldberg@Sun.COM 	int8_t		smask;
538*8550SSeth.Goldberg@Sun.COM 	uint16_t	elcr;
539*8550SSeth.Goldberg@Sun.COM };
540*8550SSeth.Goldberg@Sun.COM 
541*8550SSeth.Goldberg@Sun.COM 
542*8550SSeth.Goldberg@Sun.COM static void
543*8550SSeth.Goldberg@Sun.COM pic_save_state(struct pic_state *sp)
544*8550SSeth.Goldberg@Sun.COM {
545*8550SSeth.Goldberg@Sun.COM 	struct standard_pic *pp;
546*8550SSeth.Goldberg@Sun.COM 	int	vecno;
547*8550SSeth.Goldberg@Sun.COM 
548*8550SSeth.Goldberg@Sun.COM 	/*
549*8550SSeth.Goldberg@Sun.COM 	 * Only the PIC masks and the ELCR can be saved;
550*8550SSeth.Goldberg@Sun.COM 	 * other 8259 state is write-only
551*8550SSeth.Goldberg@Sun.COM 	 */
552*8550SSeth.Goldberg@Sun.COM 
553*8550SSeth.Goldberg@Sun.COM 	/*
554*8550SSeth.Goldberg@Sun.COM 	 * save current master and slave interrupt mask
555*8550SSeth.Goldberg@Sun.COM 	 */
556*8550SSeth.Goldberg@Sun.COM 	pp = &pics0;
557*8550SSeth.Goldberg@Sun.COM 	sp->smask = pp->c_curmask[0];
558*8550SSeth.Goldberg@Sun.COM 	sp->mmask = pp->c_curmask[1];
559*8550SSeth.Goldberg@Sun.COM 
560*8550SSeth.Goldberg@Sun.COM 	/*
561*8550SSeth.Goldberg@Sun.COM 	 * save edge/level configuration for isa interrupts
562*8550SSeth.Goldberg@Sun.COM 	 */
563*8550SSeth.Goldberg@Sun.COM 	sp->elcr = 0;
564*8550SSeth.Goldberg@Sun.COM 	for (vecno = 0; vecno <= MAX_ISA_IRQ; vecno++)
565*8550SSeth.Goldberg@Sun.COM 		sp->elcr |= psm_get_elcr(vecno) << vecno;
566*8550SSeth.Goldberg@Sun.COM }
567*8550SSeth.Goldberg@Sun.COM 
568*8550SSeth.Goldberg@Sun.COM static void
569*8550SSeth.Goldberg@Sun.COM pic_restore_state(struct pic_state *sp)
570*8550SSeth.Goldberg@Sun.COM {
571*8550SSeth.Goldberg@Sun.COM 	int	vecno;
572*8550SSeth.Goldberg@Sun.COM 
573*8550SSeth.Goldberg@Sun.COM 	/* Restore master and slave interrupt masks */
574*8550SSeth.Goldberg@Sun.COM 	outb(SIMR_PORT, sp->smask);
575*8550SSeth.Goldberg@Sun.COM 	outb(MIMR_PORT, sp->mmask);
576*8550SSeth.Goldberg@Sun.COM 
577*8550SSeth.Goldberg@Sun.COM 	/* Read master to allow pics to settle */
578*8550SSeth.Goldberg@Sun.COM 	(void) inb(MIMR_PORT);
579*8550SSeth.Goldberg@Sun.COM 
580*8550SSeth.Goldberg@Sun.COM 	/* Restore edge/level configuration for isa interupts */
581*8550SSeth.Goldberg@Sun.COM 	for (vecno = 0; vecno <= MAX_ISA_IRQ; vecno++)
582*8550SSeth.Goldberg@Sun.COM 		psm_set_elcr(vecno, sp->elcr & (1 << vecno));
583*8550SSeth.Goldberg@Sun.COM 
584*8550SSeth.Goldberg@Sun.COM 	/* Reenter PIC mode before restoring LNK devices */
585*8550SSeth.Goldberg@Sun.COM 	(void) uppc_acpi_enter_picmode();
586*8550SSeth.Goldberg@Sun.COM 
587*8550SSeth.Goldberg@Sun.COM 	/* Restore ACPI link device mappings */
588*8550SSeth.Goldberg@Sun.COM 	acpi_restore_link_devices();
589*8550SSeth.Goldberg@Sun.COM }
590*8550SSeth.Goldberg@Sun.COM 
591*8550SSeth.Goldberg@Sun.COM static int
592*8550SSeth.Goldberg@Sun.COM uppc_state(psm_state_request_t *rp)
593*8550SSeth.Goldberg@Sun.COM {
594*8550SSeth.Goldberg@Sun.COM 	switch (rp->psr_cmd) {
595*8550SSeth.Goldberg@Sun.COM 	case PSM_STATE_ALLOC:
596*8550SSeth.Goldberg@Sun.COM 		rp->req.psm_state_req.psr_state =
597*8550SSeth.Goldberg@Sun.COM 		    kmem_zalloc(sizeof (struct pic_state), KM_NOSLEEP);
598*8550SSeth.Goldberg@Sun.COM 		if (rp->req.psm_state_req.psr_state == NULL)
599*8550SSeth.Goldberg@Sun.COM 			return (ENOMEM);
600*8550SSeth.Goldberg@Sun.COM 		rp->req.psm_state_req.psr_state_size =
601*8550SSeth.Goldberg@Sun.COM 		    sizeof (struct pic_state);
602*8550SSeth.Goldberg@Sun.COM 		return (0);
603*8550SSeth.Goldberg@Sun.COM 	case PSM_STATE_FREE:
604*8550SSeth.Goldberg@Sun.COM 		kmem_free(rp->req.psm_state_req.psr_state,
605*8550SSeth.Goldberg@Sun.COM 		    rp->req.psm_state_req.psr_state_size);
606*8550SSeth.Goldberg@Sun.COM 		return (0);
607*8550SSeth.Goldberg@Sun.COM 	case PSM_STATE_SAVE:
608*8550SSeth.Goldberg@Sun.COM 		pic_save_state(rp->req.psm_state_req.psr_state);
609*8550SSeth.Goldberg@Sun.COM 		return (0);
610*8550SSeth.Goldberg@Sun.COM 	case PSM_STATE_RESTORE:
611*8550SSeth.Goldberg@Sun.COM 		pic_restore_state(rp->req.psm_state_req.psr_state);
612*8550SSeth.Goldberg@Sun.COM 		return (0);
613*8550SSeth.Goldberg@Sun.COM 	default:
614*8550SSeth.Goldberg@Sun.COM 		return (EINVAL);
615*8550SSeth.Goldberg@Sun.COM 	}
616*8550SSeth.Goldberg@Sun.COM }
617*8550SSeth.Goldberg@Sun.COM 
618*8550SSeth.Goldberg@Sun.COM 
6190Sstevel@tonic-gate static int
6200Sstevel@tonic-gate uppc_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
6210Sstevel@tonic-gate     int ipin, int *pci_irqp, iflag_t *intr_flagp)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	int status;
6240Sstevel@tonic-gate 	acpi_psm_lnk_t acpipsmlnk;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if ((status = acpi_get_irq_cache_ent(busid, devid, ipin, pci_irqp,
6270Sstevel@tonic-gate 	    intr_flagp)) == ACPI_PSM_SUCCESS) {
6280Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Found irqno %d "
6290Sstevel@tonic-gate 		    "from cache for device %s, instance #%d\n", *pci_irqp,
6300Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
6310Sstevel@tonic-gate 		return (status);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	bzero(&acpipsmlnk, sizeof (acpi_psm_lnk_t));
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((status = acpi_translate_pci_irq(dip, ipin, pci_irqp,
6370Sstevel@tonic-gate 	    intr_flagp, &acpipsmlnk)) == ACPI_PSM_FAILURE) {
6380Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: "
6390Sstevel@tonic-gate 		    " acpi_translate_pci_irq failed for device %s, instance"
6400Sstevel@tonic-gate 		    " #%d\n", ddi_get_name(dip), ddi_get_instance(dip)));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		return (status);
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if (status == ACPI_PSM_PARTIAL && acpipsmlnk.lnkobj != NULL) {
6460Sstevel@tonic-gate 		status = uppc_acpi_irq_configure(&acpipsmlnk, dip, pci_irqp,
6470Sstevel@tonic-gate 		    intr_flagp);
6480Sstevel@tonic-gate 		if (status != ACPI_PSM_SUCCESS) {
6490Sstevel@tonic-gate 			status = acpi_get_current_irq_resource(&acpipsmlnk,
6500Sstevel@tonic-gate 			    pci_irqp, intr_flagp);
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (status == ACPI_PSM_SUCCESS) {
6550Sstevel@tonic-gate 		acpi_new_irq_cache_ent(busid, devid, ipin, *pci_irqp,
6560Sstevel@tonic-gate 		    intr_flagp, &acpipsmlnk);
657347Smyers 		psm_set_elcr(*pci_irqp, 1); 	/* set IRQ to PCI mode */
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: [ACPI] "
6600Sstevel@tonic-gate 		    "new irq %d for device %s, instance #%d\n",
6610Sstevel@tonic-gate 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	return (status);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * Configures the irq for the interrupt link device identified by
6690Sstevel@tonic-gate  * acpipsmlnkp.
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * Gets the current and the list of possible irq settings for the
6720Sstevel@tonic-gate  * device. If uppc_unconditional_srs is not set, and the current
6730Sstevel@tonic-gate  * resource setting is in the list of possible irq settings,
6740Sstevel@tonic-gate  * current irq resource setting is passed to the caller.
6750Sstevel@tonic-gate  *
6760Sstevel@tonic-gate  * Otherwise, picks an irq number from the list of possible irq
6770Sstevel@tonic-gate  * settings, and sets the irq of the device to this value.
6780Sstevel@tonic-gate  * If prefer_crs is set, among a set of irq numbers in the list that have
6790Sstevel@tonic-gate  * the least number of devices sharing the interrupt, we pick current irq
6800Sstevel@tonic-gate  * resource setting if it is a member of this set.
6810Sstevel@tonic-gate  *
6820Sstevel@tonic-gate  * Passes the irq number in the value pointed to by pci_irqp, and
6830Sstevel@tonic-gate  * polarity and sensitivity in the structure pointed to by dipintrflagp
6840Sstevel@tonic-gate  * to the caller.
6850Sstevel@tonic-gate  *
6860Sstevel@tonic-gate  * Note that if setting the irq resource failed, but successfuly obtained
6870Sstevel@tonic-gate  * the current irq resource settings, passes the current irq resources
6880Sstevel@tonic-gate  * and considers it a success.
6890Sstevel@tonic-gate  *
6900Sstevel@tonic-gate  * Returns:
6910Sstevel@tonic-gate  * ACPI_PSM_SUCCESS on success.
6920Sstevel@tonic-gate  *
6930Sstevel@tonic-gate  * ACPI_PSM_FAILURE if an error occured during the configuration or
6940Sstevel@tonic-gate  * if a suitable irq was not found for this device, or if setting the
6950Sstevel@tonic-gate  * irq resource and obtaining the current resource fails.
6960Sstevel@tonic-gate  *
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate static int
6990Sstevel@tonic-gate uppc_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
7000Sstevel@tonic-gate     int *pci_irqp, iflag_t *dipintr_flagp)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	int i, min_share, foundnow, done = 0;
7030Sstevel@tonic-gate 	int32_t irq;
7040Sstevel@tonic-gate 	int32_t share_irq = -1;
7050Sstevel@tonic-gate 	int32_t chosen_irq = -1;
7060Sstevel@tonic-gate 	int cur_irq = -1;
7070Sstevel@tonic-gate 	acpi_irqlist_t *irqlistp;
7080Sstevel@tonic-gate 	acpi_irqlist_t *irqlistent;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if ((acpi_get_possible_irq_resources(acpipsmlnkp, &irqlistp))
7110Sstevel@tonic-gate 	    == ACPI_PSM_FAILURE) {
7120Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: Unable to determine "
7130Sstevel@tonic-gate 		    "or assign IRQ for device %s, instance #%d: The system was "
7140Sstevel@tonic-gate 		    "unable to get the list of potential IRQs from ACPI.",
7150Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if ((acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
7210Sstevel@tonic-gate 	    dipintr_flagp) == ACPI_PSM_SUCCESS) && (!uppc_unconditional_srs) &&
7220Sstevel@tonic-gate 	    (cur_irq > 0)) {
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		if (acpi_irqlist_find_irq(irqlistp, cur_irq, NULL)
7250Sstevel@tonic-gate 		    == ACPI_PSM_SUCCESS) {
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 			acpi_free_irqlist(irqlistp);
7280Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
7290Sstevel@tonic-gate 			*pci_irqp = cur_irq;
7300Sstevel@tonic-gate 			return (ACPI_PSM_SUCCESS);
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: Could not find the "
7330Sstevel@tonic-gate 		    "current irq %d for device %s, instance #%d in ACPI's "
7340Sstevel@tonic-gate 		    "list of possible irqs for this device. Picking one from "
7350Sstevel@tonic-gate 		    " the latter list.", cur_irq, ddi_get_name(dip),
7360Sstevel@tonic-gate 		    ddi_get_instance(dip)));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	irqlistent = irqlistp;
7410Sstevel@tonic-gate 	min_share = 255;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	while (irqlistent != NULL) {
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		for (foundnow = 0, i = 0; i < irqlistent->num_irqs; i++) {
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 			irq = irqlistp->irqs[i];
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 			if ((irq > MAX_ISA_IRQ) ||
7500Sstevel@tonic-gate 			    (irqlistent->intr_flags.intr_el == INTR_EL_EDGE) ||
7510Sstevel@tonic-gate 			    (irq == 0))
7520Sstevel@tonic-gate 				continue;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 			if (uppc_reserved_irqlist[irq])
7550Sstevel@tonic-gate 				continue;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 			if (uppc_irq_shared_table[irq] == 0) {
7580Sstevel@tonic-gate 				chosen_irq = irq;
7590Sstevel@tonic-gate 				foundnow = 1;
7600Sstevel@tonic-gate 				if (!(uppc_prefer_crs) || (irq == cur_irq)) {
7610Sstevel@tonic-gate 					done = 1;
7620Sstevel@tonic-gate 					break;
7630Sstevel@tonic-gate 				}
7640Sstevel@tonic-gate 			}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 			if ((uppc_irq_shared_table[irq] < min_share) ||
7670Sstevel@tonic-gate 			    ((uppc_irq_shared_table[irq] == min_share) &&
7680Sstevel@tonic-gate 			    (cur_irq == irq) && (uppc_prefer_crs))) {
7690Sstevel@tonic-gate 				min_share = uppc_irq_shared_table[irq];
7700Sstevel@tonic-gate 				share_irq = irq;
7710Sstevel@tonic-gate 				foundnow = 1;
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 		/* If we found an IRQ in the inner loop, save the details */
7760Sstevel@tonic-gate 		if (foundnow && ((chosen_irq != -1) || (share_irq != -1))) {
7770Sstevel@tonic-gate 			/*
7780Sstevel@tonic-gate 			 * Copy the acpi_prs_private_t and flags from this
7790Sstevel@tonic-gate 			 * irq list entry, since we found an irq from this
7800Sstevel@tonic-gate 			 * entry.
7810Sstevel@tonic-gate 			 */
7820Sstevel@tonic-gate 			acpipsmlnkp->acpi_prs_prv = irqlistent->acpi_prs_prv;
7830Sstevel@tonic-gate 			*dipintr_flagp = irqlistent->intr_flags;
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (done)
7870Sstevel@tonic-gate 			break;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		/* Load the next entry in the irqlist */
7900Sstevel@tonic-gate 		irqlistent = irqlistent->next;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	acpi_free_irqlist(irqlistp);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (chosen_irq != -1)
7960Sstevel@tonic-gate 		irq = chosen_irq;
7970Sstevel@tonic-gate 	else if (share_irq != -1)
7980Sstevel@tonic-gate 		irq = share_irq;
7990Sstevel@tonic-gate 	else {
8000Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Could not find a "
8010Sstevel@tonic-gate 		    "suitable irq from the list of possible irqs for device "
8020Sstevel@tonic-gate 		    "%s, instance #%d in ACPI's list of possible\n",
8030Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Setting irq %d for device %s "
8100Sstevel@tonic-gate 	    "instance #%d\n", irq, ddi_get_name(dip), ddi_get_instance(dip)));
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if ((acpi_set_irq_resource(acpipsmlnkp, irq)) == ACPI_PSM_SUCCESS) {
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * setting irq was successful, check to make sure CRS
8150Sstevel@tonic-gate 		 * reflects that. If CRS does not agree with what we
8160Sstevel@tonic-gate 		 * set, return the irq that was set.
8170Sstevel@tonic-gate 		 */
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		if (acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
8200Sstevel@tonic-gate 		    dipintr_flagp) == ACPI_PSM_SUCCESS) {
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 			if (cur_irq != irq)
8230Sstevel@tonic-gate 				UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: "
8240Sstevel@tonic-gate 				    "IRQ resource set (irqno %d) for device %s "
8250Sstevel@tonic-gate 				    "instance #%d, differs from current "
8260Sstevel@tonic-gate 				    "setting irqno %d",
8270Sstevel@tonic-gate 				    irq, ddi_get_name(dip),
8280Sstevel@tonic-gate 				    ddi_get_instance(dip), cur_irq));
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * return the irq that was set, and not what CRS reports,
8320Sstevel@tonic-gate 		 * since CRS has been seen to be bogus on some systems
8330Sstevel@tonic-gate 		 */
8340Sstevel@tonic-gate 		cur_irq = irq;
8350Sstevel@tonic-gate 	} else {
8360Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: set resource irq %d "
8370Sstevel@tonic-gate 		    "failed for device %s instance #%d",
8380Sstevel@tonic-gate 		    irq, ddi_get_name(dip), ddi_get_instance(dip)));
8390Sstevel@tonic-gate 		if (cur_irq == -1)
8400Sstevel@tonic-gate 			return (ACPI_PSM_FAILURE);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	ASSERT(pci_irqp != NULL);
8440Sstevel@tonic-gate 	*pci_irqp = cur_irq;
8450Sstevel@tonic-gate 	return (ACPI_PSM_SUCCESS);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /*ARGSUSED*/
8500Sstevel@tonic-gate static int
8510Sstevel@tonic-gate uppc_translate_irq(dev_info_t *dip, int irqno)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	char dev_type[16];
8540Sstevel@tonic-gate 	int dev_len, pci_irq, devid, busid;
8550Sstevel@tonic-gate 	ddi_acc_handle_t cfg_handle;
856926Smyers 	uchar_t ipin, iline;
8570Sstevel@tonic-gate 	iflag_t intr_flag;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (dip == NULL) {
8600Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: irqno = %d"
8610Sstevel@tonic-gate 		    " dip = NULL\n", irqno));
8620Sstevel@tonic-gate 		return (irqno);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	if (!uppc_enable_acpi) {
8660Sstevel@tonic-gate 		return (irqno);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	dev_len = sizeof (dev_type);
870506Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_get_parent(dip),
8710Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "device_type", (caddr_t)dev_type,
8720Sstevel@tonic-gate 	    &dev_len) != DDI_PROP_SUCCESS) {
8730Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: irqno %d"
8740Sstevel@tonic-gate 		    "device %s instance %d no device_type\n", irqno,
8750Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
8760Sstevel@tonic-gate 		return (irqno);
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
879881Sjohnny 	if ((strcmp(dev_type, "pci") == 0) ||
880881Sjohnny 	    (strcmp(dev_type, "pciex") == 0)) {
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		/* pci device */
8830Sstevel@tonic-gate 		if (acpica_get_bdf(dip, &busid, &devid, NULL) != 0)
8840Sstevel@tonic-gate 			return (irqno);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		if (pci_config_setup(dip, &cfg_handle) != DDI_SUCCESS)
8870Sstevel@tonic-gate 			return (irqno);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		ipin = pci_config_get8(cfg_handle, PCI_CONF_IPIN) - PCI_INTA;
890926Smyers 		iline = pci_config_get8(cfg_handle, PCI_CONF_ILINE);
8910Sstevel@tonic-gate 		if (uppc_acpi_translate_pci_irq(dip, busid, devid,
8920Sstevel@tonic-gate 		    ipin, &pci_irq, &intr_flag) == ACPI_PSM_SUCCESS) {
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 			UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: [ACPI] new irq "
8950Sstevel@tonic-gate 			    "%d old irq %d device %s, instance %d\n", pci_irq,
8960Sstevel@tonic-gate 			    irqno, ddi_get_name(dip), ddi_get_instance(dip)));
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			/*
8990Sstevel@tonic-gate 			 * Make sure pci_irq is within range.
9000Sstevel@tonic-gate 			 * Otherwise, fall through and return irqno.
9010Sstevel@tonic-gate 			 */
902926Smyers 			if (pci_irq <= MAX_ISA_IRQ) {
903926Smyers 				if (iline != pci_irq) {
904926Smyers 					/*
905926Smyers 					 * Update the device's ILINE byte,
906926Smyers 					 * in case uppc_acpi_translate_pci_irq
907926Smyers 					 * has choosen a different pci_irq
908926Smyers 					 * than the BIOS has configured.
909926Smyers 					 * Some chipsets use the value in
910926Smyers 					 * ILINE to control interrupt routing,
911926Smyers 					 * in conflict with the PCI spec.
912926Smyers 					 */
913926Smyers 					pci_config_put8(cfg_handle,
914926Smyers 					    PCI_CONF_ILINE, pci_irq);
915926Smyers 				}
916926Smyers 				pci_config_teardown(&cfg_handle);
9170Sstevel@tonic-gate 				return (pci_irq);
918926Smyers 			}
9190Sstevel@tonic-gate 		}
920926Smyers 		pci_config_teardown(&cfg_handle);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		/* FALLTHRU to common case - returning irqno */
9230Sstevel@tonic-gate 	} else {
924347Smyers 		/* non-PCI; assumes ISA-style edge-triggered */
925347Smyers 		psm_set_elcr(irqno, 0); 	/* set IRQ to ISA mode */
926347Smyers 
9270Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: non-pci,"
9280Sstevel@tonic-gate 		    "irqno %d device %s instance %d\n", irqno,
9290Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	return (irqno);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate /*
9360Sstevel@tonic-gate  * uppc_intr_enter() raises the ipl to the level of the current interrupt,
9370Sstevel@tonic-gate  * and sends EOI to the pics.
9380Sstevel@tonic-gate  * If interrupt is 7 or 15 and not spurious interrupt, send specific EOI
9390Sstevel@tonic-gate  * else send non-specific EOI
9400Sstevel@tonic-gate  * uppc_intr_enter() returns the new priority level,
9410Sstevel@tonic-gate  * or -1 for spurious interrupt
9420Sstevel@tonic-gate  */
9430Sstevel@tonic-gate static int
9440Sstevel@tonic-gate uppc_intr_enter(int ipl, int *vector)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	int newipl;
9470Sstevel@tonic-gate 	int intno;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	intno = (*vector);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	ASSERT(intno < 256);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	newipl = autovect[intno].avh_hi_pri;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * During wait_till_seen() periods when interrupt vector is being
9570Sstevel@tonic-gate 	 * removed in remove_av(), the removed hardware interrupt could
9580Sstevel@tonic-gate 	 * trigger and got here with newipl 0.  It has to send EOI
9590Sstevel@tonic-gate 	 * as usual but no need to call setspl and returns -1 like spurious.
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	if ((intno & 7) != 7) {
9620Sstevel@tonic-gate 		if (newipl)
9630Sstevel@tonic-gate 			uppc_setspl(newipl);
9640Sstevel@tonic-gate 		outb(MCMD_PORT, PIC_NSEOI);
9650Sstevel@tonic-gate 		if (intno >= 8) {
9660Sstevel@tonic-gate 			outb(SCMD_PORT, PIC_NSEOI);
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 	} else { /* int was 7 or 15 */
9690Sstevel@tonic-gate 		if (newipl && newipl <= ipl) { /* Check for spurious int */
9700Sstevel@tonic-gate 			if (intno != 7)
9710Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_NSEOI);
9720Sstevel@tonic-gate 			return (-1); /* Spurious int */
9730Sstevel@tonic-gate 		} else {
9740Sstevel@tonic-gate 			if (newipl)
9750Sstevel@tonic-gate 				uppc_setspl(newipl);
9760Sstevel@tonic-gate 			if (intno != 7) {
9770Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_NSEOI);
9780Sstevel@tonic-gate 				outb(SCMD_PORT, PIC_SEOI_LVL7);
9790Sstevel@tonic-gate 			} else  {
9800Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_SEOI_LVL7);
9810Sstevel@tonic-gate 			}
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	if (newipl)
9860Sstevel@tonic-gate 		return (newipl);
9870Sstevel@tonic-gate 	else
9880Sstevel@tonic-gate 		return (-1); /* not real spurious int */
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * uppc_intr_exit() restores the old interrupt
9930Sstevel@tonic-gate  * priority level after processing an interrupt.
9940Sstevel@tonic-gate  * It is called with interrupts disabled, and does not enable interrupts.
9950Sstevel@tonic-gate  */
9960Sstevel@tonic-gate /* ARGSUSED */
9970Sstevel@tonic-gate static void
9980Sstevel@tonic-gate uppc_intr_exit(int ipl, int vector)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	uppc_setspl(ipl);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate  * uppc_setspl() loads new interrupt masks into the pics
10050Sstevel@tonic-gate  * based on input ipl.
10060Sstevel@tonic-gate  */
10070Sstevel@tonic-gate /* ARGSUSED */
10080Sstevel@tonic-gate static void
10090Sstevel@tonic-gate uppc_setspl(int ipl)
10100Sstevel@tonic-gate {
10110Sstevel@tonic-gate 	struct standard_pic *pp;
10120Sstevel@tonic-gate 	uint8_t smask, mmask;
10130Sstevel@tonic-gate 	uint8_t cursmask, curmmask;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	pp = &pics0;
10160Sstevel@tonic-gate 	smask = pp->c_iplmask[ipl * 2];
10170Sstevel@tonic-gate 	mmask = pp->c_iplmask[ipl * 2 + 1];
10180Sstevel@tonic-gate 	cursmask = pp->c_curmask[0];
10190Sstevel@tonic-gate 	curmmask = pp->c_curmask[1];
10200Sstevel@tonic-gate 	if (cursmask == smask && curmmask == mmask)
10210Sstevel@tonic-gate 		return;
10220Sstevel@tonic-gate 	pp->c_curmask[0] = smask;
10230Sstevel@tonic-gate 	pp->c_curmask[1] = mmask;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (cursmask != smask) {
10260Sstevel@tonic-gate 		/*
10270Sstevel@tonic-gate 		 * program new slave pic mask
10280Sstevel@tonic-gate 		 */
10290Sstevel@tonic-gate 		outb(SIMR_PORT, smask);
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 	if (curmmask != mmask) {
10320Sstevel@tonic-gate 		/*
10330Sstevel@tonic-gate 		 * program new master pic mask
10340Sstevel@tonic-gate 		 */
10350Sstevel@tonic-gate 		outb(MIMR_PORT, mmask);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	/*
10380Sstevel@tonic-gate 	 * read master to allow pics to settle
10390Sstevel@tonic-gate 	 */
10400Sstevel@tonic-gate 	(void) inb(MIMR_PORT);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate /*
10440Sstevel@tonic-gate  * uppc_gethrtime() returns high resolution timer value
10450Sstevel@tonic-gate  */
10460Sstevel@tonic-gate static hrtime_t
10470Sstevel@tonic-gate uppc_gethrtime()
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	hrtime_t timeval, temp;
10503446Smrj 	unsigned int ctr0;
10513446Smrj 	ulong_t oflags;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	oflags = intr_clear(); /* disable ints */
10540Sstevel@tonic-gate 	lock_set(&uppc_gethrtime_lock);
10550Sstevel@tonic-gate retry:
10560Sstevel@tonic-gate 	temp = hrtime_base;
10570Sstevel@tonic-gate 	outb(PITCTL_PORT, 0);	/* latch counter 0 */
10580Sstevel@tonic-gate 	/*
10590Sstevel@tonic-gate 	 * read counter 0
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	ctr0 = inb(PITCTR0_PORT);
10620Sstevel@tonic-gate 	ctr0 |= inb(PITCTR0_PORT) << 8;
10630Sstevel@tonic-gate 	timeval = (hrtime_t)ctr0 * (NANOSEC / PIT_HZ);
10640Sstevel@tonic-gate 	if (temp != hrtime_base)
10650Sstevel@tonic-gate 		goto retry;
10660Sstevel@tonic-gate 	timeval -= temp;
10670Sstevel@tonic-gate 	if (timeval < uppc_lasthrtime)
10680Sstevel@tonic-gate 		timeval = uppc_lasthrtime;
10690Sstevel@tonic-gate 	uppc_lasthrtime = timeval;
10700Sstevel@tonic-gate 	lock_clear(&uppc_gethrtime_lock);
10710Sstevel@tonic-gate 	intr_restore(oflags);
10720Sstevel@tonic-gate 	return (timeval);
10730Sstevel@tonic-gate }
1074