xref: /illumos-gate/usr/src/uts/i86pc/io/psm/uppc.c (revision 918e0d92)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54303384aSanish  * Common Development and Distribution License (the "License").
64303384aSanish  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220f1b305eSSeth Goldberg  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
241c2d0470SPatrick Mooney  * Copyright 2018 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27a3114836SGerry Liu #define	PSMI_1_7
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
337c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
347c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
357c478bd9Sstevel@tonic-gate #include <sys/promif.h>
367c478bd9Sstevel@tonic-gate #include <sys/psm.h>
377c478bd9Sstevel@tonic-gate #include <sys/pit.h>
380f1b305eSSeth Goldberg #include <sys/apic.h>
397c478bd9Sstevel@tonic-gate #include <sys/psm_common.h>
407c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
41ae115bc7Smrj #include <sys/archsystm.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	NSEC_IN_SEC		1000000000
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Local Function Prototypes
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static void uppc_softinit(void);
497c478bd9Sstevel@tonic-gate static void uppc_picinit();
500f1b305eSSeth Goldberg static int uppc_post_cpu_start(void);
517c478bd9Sstevel@tonic-gate static int uppc_clkinit(int);
527c478bd9Sstevel@tonic-gate static int uppc_addspl(int irqno, int ipl, int min_ipl, int max_ipl);
537c478bd9Sstevel@tonic-gate static int uppc_delspl(int irqno, int ipl, int min_ipl, int max_ipl);
547c478bd9Sstevel@tonic-gate static processorid_t uppc_get_next_processorid(processorid_t cpu_id);
557c478bd9Sstevel@tonic-gate static int uppc_get_clockirq(int ipl);
567c478bd9Sstevel@tonic-gate static int uppc_probe(void);
577c478bd9Sstevel@tonic-gate static int uppc_translate_irq(dev_info_t *dip, int irqno);
587c478bd9Sstevel@tonic-gate static void uppc_shutdown(int cmd, int fcn);
597c478bd9Sstevel@tonic-gate static void uppc_preshutdown(int cmd, int fcn);
600f1b305eSSeth Goldberg static int uppc_state(psm_state_request_t *request);
617c478bd9Sstevel@tonic-gate static int uppc_init_acpi(void);
627c478bd9Sstevel@tonic-gate static void uppc_setspl(int);
637c478bd9Sstevel@tonic-gate static int uppc_intr_enter(int, int *);
647c478bd9Sstevel@tonic-gate static void uppc_intr_exit(int, int);
657c478bd9Sstevel@tonic-gate static hrtime_t uppc_gethrtime();
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int uppc_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
687c478bd9Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Global Data
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate static struct standard_pic pics0;
747c478bd9Sstevel@tonic-gate int uppc_use_acpi = 1;	/* Use ACPI by default */
757c478bd9Sstevel@tonic-gate int uppc_enable_acpi = 0;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * For interrupt link devices, if uppc_unconditional_srs is set, an irq resource
807c478bd9Sstevel@tonic-gate  * will be assigned (via _SRS). If it is not set, use the current
817c478bd9Sstevel@tonic-gate  * irq setting (via _CRS), but only if that irq is in the set of possible
827c478bd9Sstevel@tonic-gate  * irqs (returned by _PRS) for the device.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate int uppc_unconditional_srs = 1;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * For interrupt link devices, if uppc_prefer_crs is set when we are
887c478bd9Sstevel@tonic-gate  * assigning an IRQ resource to a device, prefer the current IRQ setting
897c478bd9Sstevel@tonic-gate  * over other possible irq settings under same conditions.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate int uppc_prefer_crs = 1;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int uppc_verbose = 0;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* flag definitions for uppc_verbose */
967c478bd9Sstevel@tonic-gate #define	UPPC_VERBOSE_IRQ_FLAG			0x00000001
977c478bd9Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF_FLAG		0x00000002
987c478bd9Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF_PAUSE_FLAG	0x00000004
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define	UPPC_VERBOSE_IRQ(fmt) \
1027c478bd9Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_IRQ_FLAG) \
1037c478bd9Sstevel@tonic-gate 		cmn_err fmt;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	UPPC_VERBOSE_POWEROFF(fmt) \
1067c478bd9Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_FLAG) \
1077c478bd9Sstevel@tonic-gate 		prom_printf fmt;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate uchar_t uppc_reserved_irqlist[MAX_ISA_IRQ + 1];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static uint16_t uppc_irq_shared_table[MAX_ISA_IRQ + 1];
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Contains SCI irqno from FADT after initialization
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static int uppc_sci = -1;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Local Static Data
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static lock_t uppc_gethrtime_lock;
1237c478bd9Sstevel@tonic-gate static hrtime_t uppc_lasthrtime;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #ifdef UPPC_DEBUG
1277c478bd9Sstevel@tonic-gate #define	DENT	0x0001
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int	uppc_debug = 0;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static struct	psm_ops uppc_ops = {
1367c478bd9Sstevel@tonic-gate 	uppc_probe,				/* psm_probe		*/
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	uppc_softinit,				/* psm_init		*/
1397c478bd9Sstevel@tonic-gate 	uppc_picinit,				/* psm_picinit		*/
1407c478bd9Sstevel@tonic-gate 	uppc_intr_enter,			/* psm_intr_enter	*/
1417c478bd9Sstevel@tonic-gate 	uppc_intr_exit,				/* psm_intr_exit	*/
1427c478bd9Sstevel@tonic-gate 	uppc_setspl,				/* psm_setspl		*/
1437c478bd9Sstevel@tonic-gate 	uppc_addspl,				/* psm_addspl		*/
1447c478bd9Sstevel@tonic-gate 	uppc_delspl,				/* psm_delspl		*/
1457c478bd9Sstevel@tonic-gate 	(int (*)(processorid_t))NULL,		/* psm_disable_intr	*/
1467c478bd9Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_enable_intr	*/
1477c478bd9Sstevel@tonic-gate 	(int (*)(int))NULL,			/* psm_softlvl_to_irq	*/
1487c478bd9Sstevel@tonic-gate 	(void (*)(int))NULL,			/* psm_set_softintr	*/
1497c478bd9Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_set_idlecpu	*/
1507c478bd9Sstevel@tonic-gate 	(void (*)(processorid_t))NULL,		/* psm_unset_idlecpu	*/
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	uppc_clkinit,				/* psm_clkinit		*/
1537c478bd9Sstevel@tonic-gate 	uppc_get_clockirq,			/* psm_get_clockirq	*/
1547c478bd9Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_hrtimeinit	*/
1557c478bd9Sstevel@tonic-gate 	uppc_gethrtime,				/* psm_gethrtime	*/
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	uppc_get_next_processorid,		/* psm_get_next_processorid */
158ae115bc7Smrj 	(int (*)(processorid_t, caddr_t))NULL,	/* psm_cpu_start	*/
1590f1b305eSSeth Goldberg 	uppc_post_cpu_start,			/* psm_post_cpu_start	*/
1607c478bd9Sstevel@tonic-gate 	uppc_shutdown,				/* psm_shutdown		*/
1617c478bd9Sstevel@tonic-gate 	(int (*)(int, int))NULL,		/* psm_get_ipivect	*/
1627c478bd9Sstevel@tonic-gate 	(void (*)(processorid_t, int))NULL,	/* psm_send_ipi		*/
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	uppc_translate_irq,			/* psm_translate_irq	*/
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	(void (*)(int, char *))NULL,		/* psm_notify_error	*/
1677c478bd9Sstevel@tonic-gate 	(void (*)(int msg))NULL,		/* psm_notify_func	*/
1687c478bd9Sstevel@tonic-gate 	(void (*)(hrtime_t time))NULL,		/* psm_timer_reprogram	*/
1697c478bd9Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_timer_enable	*/
1707c478bd9Sstevel@tonic-gate 	(void (*)(void))NULL,			/* psm_timer_disable	*/
1717c478bd9Sstevel@tonic-gate 	(void (*)(void *arg))NULL,		/* psm_post_cyclic_setup */
1727c478bd9Sstevel@tonic-gate 	uppc_preshutdown,			/* psm_preshutdown	*/
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(int (*)(dev_info_t *, ddi_intr_handle_impl_t *,
1750f1b305eSSeth Goldberg 	    psm_intr_op_t, int *))NULL,		/* psm_intr_ops		*/
1760f1b305eSSeth Goldberg 
177a3114836SGerry Liu 	uppc_state,				/* psm_state		*/
1781c2d0470SPatrick Mooney 	(int (*)(psm_cpu_request_t *))NULL,	/* psm_cpu_ops		*/
1791c2d0470SPatrick Mooney 
1801c2d0470SPatrick Mooney 	(int (*)(void))NULL,			/* psm_get_pir_ipivect	*/
1811c2d0470SPatrick Mooney 	(void (*)(processorid_t))NULL,		/* psm_send_pir_ipi	*/
182*918e0d92SRobert Mustacchi 	(void (*)(processorid_t, boolean_t))NULL	/* psm_cmci_setup */
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static struct	psm_info uppc_info = {
187a3114836SGerry Liu 	PSM_INFO_VER01_7,	/* version				*/
1887c478bd9Sstevel@tonic-gate 	PSM_OWN_SYS_DEFAULT,	/* ownership				*/
1897c478bd9Sstevel@tonic-gate 	(struct psm_ops *)&uppc_ops, /* operation			*/
1907c478bd9Sstevel@tonic-gate 	"uppc",			/* machine name				*/
1917c478bd9Sstevel@tonic-gate 	"UniProcessor PC",	/* machine descriptions			*/
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Configuration Data
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static void *uppc_hdlp;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate int
_init(void)2067c478bd9Sstevel@tonic-gate _init(void)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	return (psm_mod_init(&uppc_hdlp, &uppc_info));
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int
_fini(void)2127c478bd9Sstevel@tonic-gate _fini(void)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	return (psm_mod_fini(&uppc_hdlp, &uppc_info));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2187c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	return (psm_mod_info(&uppc_hdlp, &uppc_info, modinfop));
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Autoconfiguration Routines
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static int
uppc_probe(void)2287c478bd9Sstevel@tonic-gate uppc_probe(void)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	return (PSM_SUCCESS);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static void
uppc_softinit(void)2367c478bd9Sstevel@tonic-gate uppc_softinit(void)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	struct standard_pic *pp;
2397c478bd9Sstevel@tonic-gate 	int i;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	pp = &pics0;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (uppc_use_acpi && uppc_init_acpi()) {
2457c478bd9Sstevel@tonic-gate 		build_reserved_irqlist((uchar_t *)uppc_reserved_irqlist);
2467c478bd9Sstevel@tonic-gate 		for (i = 0; i <= MAX_ISA_IRQ; i++)
2477c478bd9Sstevel@tonic-gate 			uppc_irq_shared_table[i] = 0;
2487c478bd9Sstevel@tonic-gate 		uppc_enable_acpi = 1;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * initialize the ipl mask
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	for (i = 0; i < (MAXIPL << 1); i += 2) {
2557c478bd9Sstevel@tonic-gate 		/* enable slave lines on master */
2567c478bd9Sstevel@tonic-gate 		pp->c_iplmask[i] = 0xff;
2577c478bd9Sstevel@tonic-gate 		pp->c_iplmask[i+1] = (0xff & ~(1 << MASTERLINE));
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2627c478bd9Sstevel@tonic-gate static int
uppc_clkinit(int hertz)2637c478bd9Sstevel@tonic-gate uppc_clkinit(int hertz)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	ulong_t clkticks = PIT_HZ / hz;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (hertz == 0)
2687c478bd9Sstevel@tonic-gate 		return (0);	/* One shot mode not supported */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * program timer 0
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	outb(PITCTL_PORT, (PIT_C0|PIT_NDIVMODE|PIT_READMODE));
2747c478bd9Sstevel@tonic-gate 	outb(PITCTR0_PORT, (uchar_t)clkticks);
2757c478bd9Sstevel@tonic-gate 	outb(PITCTR0_PORT, (uchar_t)(clkticks>>8));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	return (NSEC_IN_SEC / hertz);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static void
uppc_picinit()2817c478bd9Sstevel@tonic-gate uppc_picinit()
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	picsetup();
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * If a valid SCI is present, manually addspl()
2877c478bd9Sstevel@tonic-gate 	 * since we're not set-up early enough in boot
2887c478bd9Sstevel@tonic-gate 	 * to do it "conventionally" (via add_avintr)
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	if (uppc_sci >= 0)
2917c478bd9Sstevel@tonic-gate 		(void) uppc_addspl(uppc_sci, SCI_IPL, SCI_IPL, SCI_IPL);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2940f1b305eSSeth Goldberg static int
uppc_post_cpu_start(void)2950f1b305eSSeth Goldberg uppc_post_cpu_start(void)
2960f1b305eSSeth Goldberg {
2970f1b305eSSeth Goldberg 	/*
2980f1b305eSSeth Goldberg 	 * On uppc machines psm_post_cpu_start is called during S3 resume
2990f1b305eSSeth Goldberg 	 * on the boot cpu from assembly, using the ap_mlsetup vector.
3000f1b305eSSeth Goldberg 	 */
3010f1b305eSSeth Goldberg 
3020f1b305eSSeth Goldberg 	/*
3030f1b305eSSeth Goldberg 	 * Init master and slave pic
3040f1b305eSSeth Goldberg 	 */
3050f1b305eSSeth Goldberg 	picsetup();
3060f1b305eSSeth Goldberg 
3070f1b305eSSeth Goldberg 	/*
3080f1b305eSSeth Goldberg 	 * program timer 0
3090f1b305eSSeth Goldberg 	 */
3100f1b305eSSeth Goldberg 	(void) uppc_clkinit(hz);
3110f1b305eSSeth Goldberg 
3120f1b305eSSeth Goldberg 	return (PSM_SUCCESS);
3130f1b305eSSeth Goldberg }
3140f1b305eSSeth Goldberg 
3157c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
3167c478bd9Sstevel@tonic-gate static int
uppc_addspl(int irqno,int ipl,int min_ipl,int max_ipl)3177c478bd9Sstevel@tonic-gate uppc_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	struct standard_pic *pp;
3207c478bd9Sstevel@tonic-gate 	int i;
3217c478bd9Sstevel@tonic-gate 	int startidx;
3227c478bd9Sstevel@tonic-gate 	uchar_t	vectmask;
3237c478bd9Sstevel@tonic-gate 
3244303384aSanish 	if (irqno <= MAX_ISA_IRQ)
3251a5e258fSJosef 'Jeff' Sipek 		atomic_inc_16(&uppc_irq_shared_table[irqno]);
3264303384aSanish 
3277c478bd9Sstevel@tonic-gate 	if (ipl != min_ipl)
3287c478bd9Sstevel@tonic-gate 		return (0);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (irqno > 7) {
3317c478bd9Sstevel@tonic-gate 		vectmask = 1 << (irqno - 8);
3327c478bd9Sstevel@tonic-gate 		startidx = (ipl << 1);
3337c478bd9Sstevel@tonic-gate 	} else {
3347c478bd9Sstevel@tonic-gate 		vectmask = 1 << irqno;
3357c478bd9Sstevel@tonic-gate 		startidx = (ipl << 1) + 1;
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/*
3397c478bd9Sstevel@tonic-gate 	 * mask intr same or above ipl
3407c478bd9Sstevel@tonic-gate 	 * level MAXIPL has all intr off as init. default
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	pp = &pics0;
3437c478bd9Sstevel@tonic-gate 	for (i = startidx; i < (MAXIPL << 1); i += 2) {
3447c478bd9Sstevel@tonic-gate 		if (pp->c_iplmask[i] & vectmask)
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 		pp->c_iplmask[i] |= vectmask;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * unmask intr below ipl
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	for (i = startidx-2; i >= 0; i -= 2) {
3537c478bd9Sstevel@tonic-gate 		if (!(pp->c_iplmask[i] & vectmask))
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		pp->c_iplmask[i] &= ~vectmask;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	return (0);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static int
uppc_delspl(int irqno,int ipl,int min_ipl,int max_ipl)3617c478bd9Sstevel@tonic-gate uppc_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct standard_pic *pp;
3647c478bd9Sstevel@tonic-gate 	int i;
3657c478bd9Sstevel@tonic-gate 	uchar_t	vectmask;
3667c478bd9Sstevel@tonic-gate 
3674303384aSanish 	if (irqno <= MAX_ISA_IRQ)
3681a5e258fSJosef 'Jeff' Sipek 		atomic_dec_16(&uppc_irq_shared_table[irqno]);
3694303384aSanish 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * skip if we are not deleting the last handler
3727c478bd9Sstevel@tonic-gate 	 * and the ipl is higher than minimum
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if ((max_ipl != PSM_INVALID_IPL) && (ipl >= min_ipl))
3757c478bd9Sstevel@tonic-gate 		return (0);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (irqno > 7) {
3787c478bd9Sstevel@tonic-gate 		vectmask = 1 << (irqno - 8);
3797c478bd9Sstevel@tonic-gate 		i = 0;
3807c478bd9Sstevel@tonic-gate 	} else {
3817c478bd9Sstevel@tonic-gate 		vectmask = 1 << irqno;
3827c478bd9Sstevel@tonic-gate 		i = 1;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	pp = &pics0;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * check any handlers left for this irqno
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	if (max_ipl != PSM_INVALID_IPL) {
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * unmasks all levels below the lowest priority
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 		i += ((min_ipl - 1) << 1);
3957c478bd9Sstevel@tonic-gate 		for (; i >= 0; i -= 2) {
3967c478bd9Sstevel@tonic-gate 			if (!(pp->c_iplmask[i] & vectmask))
3977c478bd9Sstevel@tonic-gate 				break;
3987c478bd9Sstevel@tonic-gate 			pp->c_iplmask[i] &= ~vectmask;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	} else {
4017c478bd9Sstevel@tonic-gate 		/*
4027c478bd9Sstevel@tonic-gate 		 * set mask to all levels
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		for (; i < (MAXIPL << 1); i += 2) {
4057c478bd9Sstevel@tonic-gate 			if (pp->c_iplmask[i] & vectmask)
4067c478bd9Sstevel@tonic-gate 				break;
4077c478bd9Sstevel@tonic-gate 			pp->c_iplmask[i] |= vectmask;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (0);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static processorid_t
uppc_get_next_processorid(processorid_t cpu_id)4147c478bd9Sstevel@tonic-gate uppc_get_next_processorid(processorid_t cpu_id)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	if (cpu_id == -1)
4177c478bd9Sstevel@tonic-gate 		return (0);
4187c478bd9Sstevel@tonic-gate 	return (-1);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4227c478bd9Sstevel@tonic-gate static int
uppc_get_clockirq(int ipl)4237c478bd9Sstevel@tonic-gate uppc_get_clockirq(int ipl)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	return (CLOCK_VECTOR);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate static int
uppc_init_acpi(void)4307c478bd9Sstevel@tonic-gate uppc_init_acpi(void)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	int verboseflags = 0;
4337c478bd9Sstevel@tonic-gate 	int	sci;
4347c478bd9Sstevel@tonic-gate 	iflag_t sci_flags;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Process SCI configuration here; this may return
4387c478bd9Sstevel@tonic-gate 	 * an error if acpi-user-options has specified
4397c478bd9Sstevel@tonic-gate 	 * legacy mode (use ACPI without ACPI mode or SCI)
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	if (acpica_get_sci(&sci, &sci_flags) != AE_OK)
4427c478bd9Sstevel@tonic-gate 		sci = -1;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * Initialize sub-system - if error is returns, ACPI is not
4467c478bd9Sstevel@tonic-gate 	 * used.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if (acpica_init() != AE_OK)
4497c478bd9Sstevel@tonic-gate 		return (0);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * uppc implies system is in PIC mode; set edge/level
4537c478bd9Sstevel@tonic-gate 	 * via ELCR based on return value from get_sci; this
4547c478bd9Sstevel@tonic-gate 	 * will default to level/low if no override present,
4557c478bd9Sstevel@tonic-gate 	 * as recommended by Intel ACPI CA team.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	if (sci >= 0) {
4587c478bd9Sstevel@tonic-gate 		ASSERT((sci_flags.intr_el == INTR_EL_LEVEL) ||
4597c478bd9Sstevel@tonic-gate 		    (sci_flags.intr_el == INTR_EL_EDGE));
4607c478bd9Sstevel@tonic-gate 
461450d6964Smyers 		psm_set_elcr(sci, sci_flags.intr_el == INTR_EL_LEVEL);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Remember SCI for later use
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	uppc_sci = sci;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_IRQ_FLAG)
4707c478bd9Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_IRQ_FLAG;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_FLAG)
4737c478bd9Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_POWEROFF_FLAG;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (uppc_verbose & UPPC_VERBOSE_POWEROFF_PAUSE_FLAG)
4767c478bd9Sstevel@tonic-gate 		verboseflags |= PSM_VERBOSE_POWEROFF_PAUSE_FLAG;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (acpi_psm_init(uppc_info.p_mach_idstring, verboseflags) ==
4797c478bd9Sstevel@tonic-gate 	    ACPI_PSM_FAILURE) {
4807c478bd9Sstevel@tonic-gate 		return (0);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (1);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static void
uppc_preshutdown(int cmd,int fcn)4887c478bd9Sstevel@tonic-gate uppc_preshutdown(int cmd, int fcn)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	UPPC_VERBOSE_POWEROFF(("uppc_preshutdown(%d,%d);\n", cmd, fcn));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static void
uppc_shutdown(int cmd,int fcn)4957c478bd9Sstevel@tonic-gate uppc_shutdown(int cmd, int fcn)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	UPPC_VERBOSE_POWEROFF(("uppc_shutdown(%d,%d);\n", cmd, fcn));
4987c478bd9Sstevel@tonic-gate 
4990f2cfdbbSmyers 	/*
5000f2cfdbbSmyers 	 * Return if passed a command other than A_SHUTDOWN or
5010f2cfdbbSmyers 	 * if we're not using ACPI.
5020f2cfdbbSmyers 	 */
5030f2cfdbbSmyers 	if ((cmd != A_SHUTDOWN) || (!uppc_enable_acpi))
5040f2cfdbbSmyers 		return;
5050f2cfdbbSmyers 
50627004895Smyers 	/*
50727004895Smyers 	 * Switch system back into Legacy-Mode if using ACPI and
50827004895Smyers 	 * not powering-off.  Some BIOSes need to remain in ACPI-mode
50927004895Smyers 	 * for power-off to succeed (Dell Dimension 4600)
51027004895Smyers 	 */
51127004895Smyers 	if (fcn != AD_POWEROFF) {
51227004895Smyers 		(void) AcpiDisable();
5137c478bd9Sstevel@tonic-gate 		return;
51427004895Smyers 	}
5150f2cfdbbSmyers 
5160f2cfdbbSmyers 	(void) acpi_poweroff();
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5190f1b305eSSeth Goldberg 
5200f1b305eSSeth Goldberg static int
uppc_acpi_enter_picmode(void)5210f1b305eSSeth Goldberg uppc_acpi_enter_picmode(void)
5220f1b305eSSeth Goldberg {
5230f1b305eSSeth Goldberg 	ACPI_OBJECT_LIST	arglist;
5240f1b305eSSeth Goldberg 	ACPI_OBJECT		arg;
5250f1b305eSSeth Goldberg 	ACPI_STATUS		status;
5260f1b305eSSeth Goldberg 
5270f1b305eSSeth Goldberg 	/* Setup parameter object */
5280f1b305eSSeth Goldberg 	arglist.Count = 1;
5290f1b305eSSeth Goldberg 	arglist.Pointer = &arg;
5300f1b305eSSeth Goldberg 	arg.Type = ACPI_TYPE_INTEGER;
5310f1b305eSSeth Goldberg 	arg.Integer.Value = ACPI_PIC_MODE;
5320f1b305eSSeth Goldberg 
5330f1b305eSSeth Goldberg 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
5340f1b305eSSeth Goldberg 	if (ACPI_FAILURE(status))
5350f1b305eSSeth Goldberg 		return (PSM_FAILURE);
5360f1b305eSSeth Goldberg 	else
5370f1b305eSSeth Goldberg 		return (PSM_SUCCESS);
5380f1b305eSSeth Goldberg }
5390f1b305eSSeth Goldberg 
5400f1b305eSSeth Goldberg 
5410f1b305eSSeth Goldberg struct pic_state {
5420f1b305eSSeth Goldberg 	int8_t		mmask;
5430f1b305eSSeth Goldberg 	int8_t		smask;
5440f1b305eSSeth Goldberg 	uint16_t	elcr;
5450f1b305eSSeth Goldberg };
5460f1b305eSSeth Goldberg 
5470f1b305eSSeth Goldberg 
5480f1b305eSSeth Goldberg static void
pic_save_state(struct pic_state * sp)5490f1b305eSSeth Goldberg pic_save_state(struct pic_state *sp)
5500f1b305eSSeth Goldberg {
5510f1b305eSSeth Goldberg 	struct standard_pic *pp;
5520f1b305eSSeth Goldberg 	int	vecno;
5530f1b305eSSeth Goldberg 
5540f1b305eSSeth Goldberg 	/*
5550f1b305eSSeth Goldberg 	 * Only the PIC masks and the ELCR can be saved;
5560f1b305eSSeth Goldberg 	 * other 8259 state is write-only
5570f1b305eSSeth Goldberg 	 */
5580f1b305eSSeth Goldberg 
5590f1b305eSSeth Goldberg 	/*
5600f1b305eSSeth Goldberg 	 * save current master and slave interrupt mask
5610f1b305eSSeth Goldberg 	 */
5620f1b305eSSeth Goldberg 	pp = &pics0;
5630f1b305eSSeth Goldberg 	sp->smask = pp->c_curmask[0];
5640f1b305eSSeth Goldberg 	sp->mmask = pp->c_curmask[1];
5650f1b305eSSeth Goldberg 
5660f1b305eSSeth Goldberg 	/*
5670f1b305eSSeth Goldberg 	 * save edge/level configuration for isa interrupts
5680f1b305eSSeth Goldberg 	 */
5690f1b305eSSeth Goldberg 	sp->elcr = 0;
5700f1b305eSSeth Goldberg 	for (vecno = 0; vecno <= MAX_ISA_IRQ; vecno++)
5710f1b305eSSeth Goldberg 		sp->elcr |= psm_get_elcr(vecno) << vecno;
5720f1b305eSSeth Goldberg }
5730f1b305eSSeth Goldberg 
5740f1b305eSSeth Goldberg static void
pic_restore_state(struct pic_state * sp)5750f1b305eSSeth Goldberg pic_restore_state(struct pic_state *sp)
5760f1b305eSSeth Goldberg {
5770f1b305eSSeth Goldberg 	int	vecno;
5780f1b305eSSeth Goldberg 
5790f1b305eSSeth Goldberg 	/* Restore master and slave interrupt masks */
5800f1b305eSSeth Goldberg 	outb(SIMR_PORT, sp->smask);
5810f1b305eSSeth Goldberg 	outb(MIMR_PORT, sp->mmask);
5820f1b305eSSeth Goldberg 
5830f1b305eSSeth Goldberg 	/* Read master to allow pics to settle */
5840f1b305eSSeth Goldberg 	(void) inb(MIMR_PORT);
5850f1b305eSSeth Goldberg 
5860f1b305eSSeth Goldberg 	/* Restore edge/level configuration for isa interupts */
5870f1b305eSSeth Goldberg 	for (vecno = 0; vecno <= MAX_ISA_IRQ; vecno++)
5880f1b305eSSeth Goldberg 		psm_set_elcr(vecno, sp->elcr & (1 << vecno));
5890f1b305eSSeth Goldberg 
5900f1b305eSSeth Goldberg 	/* Reenter PIC mode before restoring LNK devices */
5910f1b305eSSeth Goldberg 	(void) uppc_acpi_enter_picmode();
5920f1b305eSSeth Goldberg 
5930f1b305eSSeth Goldberg 	/* Restore ACPI link device mappings */
5940f1b305eSSeth Goldberg 	acpi_restore_link_devices();
5950f1b305eSSeth Goldberg }
5960f1b305eSSeth Goldberg 
5970f1b305eSSeth Goldberg static int
uppc_state(psm_state_request_t * rp)5980f1b305eSSeth Goldberg uppc_state(psm_state_request_t *rp)
5990f1b305eSSeth Goldberg {
6000f1b305eSSeth Goldberg 	switch (rp->psr_cmd) {
6010f1b305eSSeth Goldberg 	case PSM_STATE_ALLOC:
6020f1b305eSSeth Goldberg 		rp->req.psm_state_req.psr_state =
6030f1b305eSSeth Goldberg 		    kmem_zalloc(sizeof (struct pic_state), KM_NOSLEEP);
6040f1b305eSSeth Goldberg 		if (rp->req.psm_state_req.psr_state == NULL)
6050f1b305eSSeth Goldberg 			return (ENOMEM);
6060f1b305eSSeth Goldberg 		rp->req.psm_state_req.psr_state_size =
6070f1b305eSSeth Goldberg 		    sizeof (struct pic_state);
6080f1b305eSSeth Goldberg 		return (0);
6090f1b305eSSeth Goldberg 	case PSM_STATE_FREE:
6100f1b305eSSeth Goldberg 		kmem_free(rp->req.psm_state_req.psr_state,
6110f1b305eSSeth Goldberg 		    rp->req.psm_state_req.psr_state_size);
6120f1b305eSSeth Goldberg 		return (0);
6130f1b305eSSeth Goldberg 	case PSM_STATE_SAVE:
6140f1b305eSSeth Goldberg 		pic_save_state(rp->req.psm_state_req.psr_state);
6150f1b305eSSeth Goldberg 		return (0);
6160f1b305eSSeth Goldberg 	case PSM_STATE_RESTORE:
6170f1b305eSSeth Goldberg 		pic_restore_state(rp->req.psm_state_req.psr_state);
6180f1b305eSSeth Goldberg 		return (0);
6190f1b305eSSeth Goldberg 	default:
6200f1b305eSSeth Goldberg 		return (EINVAL);
6210f1b305eSSeth Goldberg 	}
6220f1b305eSSeth Goldberg }
6230f1b305eSSeth Goldberg 
6240f1b305eSSeth Goldberg 
6257c478bd9Sstevel@tonic-gate static int
uppc_acpi_translate_pci_irq(dev_info_t * dip,int busid,int devid,int ipin,int * pci_irqp,iflag_t * intr_flagp)6267c478bd9Sstevel@tonic-gate uppc_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
6277c478bd9Sstevel@tonic-gate     int ipin, int *pci_irqp, iflag_t *intr_flagp)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	int status;
6307c478bd9Sstevel@tonic-gate 	acpi_psm_lnk_t acpipsmlnk;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if ((status = acpi_get_irq_cache_ent(busid, devid, ipin, pci_irqp,
6337c478bd9Sstevel@tonic-gate 	    intr_flagp)) == ACPI_PSM_SUCCESS) {
6347c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Found irqno %d "
6357c478bd9Sstevel@tonic-gate 		    "from cache for device %s, instance #%d\n", *pci_irqp,
6367c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
6377c478bd9Sstevel@tonic-gate 		return (status);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	bzero(&acpipsmlnk, sizeof (acpi_psm_lnk_t));
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if ((status = acpi_translate_pci_irq(dip, ipin, pci_irqp,
6437c478bd9Sstevel@tonic-gate 	    intr_flagp, &acpipsmlnk)) == ACPI_PSM_FAILURE) {
6447c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: "
6457c478bd9Sstevel@tonic-gate 		    " acpi_translate_pci_irq failed for device %s, instance"
6467c478bd9Sstevel@tonic-gate 		    " #%d\n", ddi_get_name(dip), ddi_get_instance(dip)));
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		return (status);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (status == ACPI_PSM_PARTIAL && acpipsmlnk.lnkobj != NULL) {
6527c478bd9Sstevel@tonic-gate 		status = uppc_acpi_irq_configure(&acpipsmlnk, dip, pci_irqp,
6537c478bd9Sstevel@tonic-gate 		    intr_flagp);
6547c478bd9Sstevel@tonic-gate 		if (status != ACPI_PSM_SUCCESS) {
6557c478bd9Sstevel@tonic-gate 			status = acpi_get_current_irq_resource(&acpipsmlnk,
6567c478bd9Sstevel@tonic-gate 			    pci_irqp, intr_flagp);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (status == ACPI_PSM_SUCCESS) {
6617c478bd9Sstevel@tonic-gate 		acpi_new_irq_cache_ent(busid, devid, ipin, *pci_irqp,
6627c478bd9Sstevel@tonic-gate 		    intr_flagp, &acpipsmlnk);
663*918e0d92SRobert Mustacchi 		psm_set_elcr(*pci_irqp, 1);	/* set IRQ to PCI mode */
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: [ACPI] "
6667c478bd9Sstevel@tonic-gate 		    "new irq %d for device %s, instance #%d\n",
6677c478bd9Sstevel@tonic-gate 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	return (status);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Configures the irq for the interrupt link device identified by
6757c478bd9Sstevel@tonic-gate  * acpipsmlnkp.
6767c478bd9Sstevel@tonic-gate  *
6777c478bd9Sstevel@tonic-gate  * Gets the current and the list of possible irq settings for the
6787c478bd9Sstevel@tonic-gate  * device. If uppc_unconditional_srs is not set, and the current
6797c478bd9Sstevel@tonic-gate  * resource setting is in the list of possible irq settings,
6807c478bd9Sstevel@tonic-gate  * current irq resource setting is passed to the caller.
6817c478bd9Sstevel@tonic-gate  *
6827c478bd9Sstevel@tonic-gate  * Otherwise, picks an irq number from the list of possible irq
6837c478bd9Sstevel@tonic-gate  * settings, and sets the irq of the device to this value.
6847c478bd9Sstevel@tonic-gate  * If prefer_crs is set, among a set of irq numbers in the list that have
6857c478bd9Sstevel@tonic-gate  * the least number of devices sharing the interrupt, we pick current irq
6867c478bd9Sstevel@tonic-gate  * resource setting if it is a member of this set.
6877c478bd9Sstevel@tonic-gate  *
6887c478bd9Sstevel@tonic-gate  * Passes the irq number in the value pointed to by pci_irqp, and
6897c478bd9Sstevel@tonic-gate  * polarity and sensitivity in the structure pointed to by dipintrflagp
6907c478bd9Sstevel@tonic-gate  * to the caller.
6917c478bd9Sstevel@tonic-gate  *
6927c478bd9Sstevel@tonic-gate  * Note that if setting the irq resource failed, but successfuly obtained
6937c478bd9Sstevel@tonic-gate  * the current irq resource settings, passes the current irq resources
6947c478bd9Sstevel@tonic-gate  * and considers it a success.
6957c478bd9Sstevel@tonic-gate  *
6967c478bd9Sstevel@tonic-gate  * Returns:
6977c478bd9Sstevel@tonic-gate  * ACPI_PSM_SUCCESS on success.
6987c478bd9Sstevel@tonic-gate  *
6997c478bd9Sstevel@tonic-gate  * ACPI_PSM_FAILURE if an error occured during the configuration or
7007c478bd9Sstevel@tonic-gate  * if a suitable irq was not found for this device, or if setting the
7017c478bd9Sstevel@tonic-gate  * irq resource and obtaining the current resource fails.
7027c478bd9Sstevel@tonic-gate  *
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate static int
uppc_acpi_irq_configure(acpi_psm_lnk_t * acpipsmlnkp,dev_info_t * dip,int * pci_irqp,iflag_t * dipintr_flagp)7057c478bd9Sstevel@tonic-gate uppc_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
7067c478bd9Sstevel@tonic-gate     int *pci_irqp, iflag_t *dipintr_flagp)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	int i, min_share, foundnow, done = 0;
7097c478bd9Sstevel@tonic-gate 	int32_t irq;
7107c478bd9Sstevel@tonic-gate 	int32_t share_irq = -1;
7117c478bd9Sstevel@tonic-gate 	int32_t chosen_irq = -1;
7127c478bd9Sstevel@tonic-gate 	int cur_irq = -1;
7137c478bd9Sstevel@tonic-gate 	acpi_irqlist_t *irqlistp;
7147c478bd9Sstevel@tonic-gate 	acpi_irqlist_t *irqlistent;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if ((acpi_get_possible_irq_resources(acpipsmlnkp, &irqlistp))
7177c478bd9Sstevel@tonic-gate 	    == ACPI_PSM_FAILURE) {
7187c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: Unable to determine "
7197c478bd9Sstevel@tonic-gate 		    "or assign IRQ for device %s, instance #%d: The system was "
7207c478bd9Sstevel@tonic-gate 		    "unable to get the list of potential IRQs from ACPI.",
7217c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if ((acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
7277c478bd9Sstevel@tonic-gate 	    dipintr_flagp) == ACPI_PSM_SUCCESS) && (!uppc_unconditional_srs) &&
7287c478bd9Sstevel@tonic-gate 	    (cur_irq > 0)) {
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if (acpi_irqlist_find_irq(irqlistp, cur_irq, NULL)
7317c478bd9Sstevel@tonic-gate 		    == ACPI_PSM_SUCCESS) {
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 			acpi_free_irqlist(irqlistp);
7347c478bd9Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
7357c478bd9Sstevel@tonic-gate 			*pci_irqp = cur_irq;
7367c478bd9Sstevel@tonic-gate 			return (ACPI_PSM_SUCCESS);
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: Could not find the "
7397c478bd9Sstevel@tonic-gate 		    "current irq %d for device %s, instance #%d in ACPI's "
7407c478bd9Sstevel@tonic-gate 		    "list of possible irqs for this device. Picking one from "
7417c478bd9Sstevel@tonic-gate 		    " the latter list.", cur_irq, ddi_get_name(dip),
7427c478bd9Sstevel@tonic-gate 		    ddi_get_instance(dip)));
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	irqlistent = irqlistp;
7477c478bd9Sstevel@tonic-gate 	min_share = 255;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	while (irqlistent != NULL) {
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		for (foundnow = 0, i = 0; i < irqlistent->num_irqs; i++) {
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			irq = irqlistp->irqs[i];
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 			if ((irq > MAX_ISA_IRQ) ||
7567c478bd9Sstevel@tonic-gate 			    (irqlistent->intr_flags.intr_el == INTR_EL_EDGE) ||
7577c478bd9Sstevel@tonic-gate 			    (irq == 0))
7587c478bd9Sstevel@tonic-gate 				continue;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 			if (uppc_reserved_irqlist[irq])
7617c478bd9Sstevel@tonic-gate 				continue;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 			if (uppc_irq_shared_table[irq] == 0) {
7647c478bd9Sstevel@tonic-gate 				chosen_irq = irq;
7657c478bd9Sstevel@tonic-gate 				foundnow = 1;
7667c478bd9Sstevel@tonic-gate 				if (!(uppc_prefer_crs) || (irq == cur_irq)) {
7677c478bd9Sstevel@tonic-gate 					done = 1;
7687c478bd9Sstevel@tonic-gate 					break;
7697c478bd9Sstevel@tonic-gate 				}
7707c478bd9Sstevel@tonic-gate 			}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 			if ((uppc_irq_shared_table[irq] < min_share) ||
7737c478bd9Sstevel@tonic-gate 			    ((uppc_irq_shared_table[irq] == min_share) &&
7747c478bd9Sstevel@tonic-gate 			    (cur_irq == irq) && (uppc_prefer_crs))) {
7757c478bd9Sstevel@tonic-gate 				min_share = uppc_irq_shared_table[irq];
7767c478bd9Sstevel@tonic-gate 				share_irq = irq;
7777c478bd9Sstevel@tonic-gate 				foundnow = 1;
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		/* If we found an IRQ in the inner loop, save the details */
7827c478bd9Sstevel@tonic-gate 		if (foundnow && ((chosen_irq != -1) || (share_irq != -1))) {
7837c478bd9Sstevel@tonic-gate 			/*
7847c478bd9Sstevel@tonic-gate 			 * Copy the acpi_prs_private_t and flags from this
7857c478bd9Sstevel@tonic-gate 			 * irq list entry, since we found an irq from this
7867c478bd9Sstevel@tonic-gate 			 * entry.
7877c478bd9Sstevel@tonic-gate 			 */
7887c478bd9Sstevel@tonic-gate 			acpipsmlnkp->acpi_prs_prv = irqlistent->acpi_prs_prv;
7897c478bd9Sstevel@tonic-gate 			*dipintr_flagp = irqlistent->intr_flags;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		if (done)
7937c478bd9Sstevel@tonic-gate 			break;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		/* Load the next entry in the irqlist */
7967c478bd9Sstevel@tonic-gate 		irqlistent = irqlistent->next;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	acpi_free_irqlist(irqlistp);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (chosen_irq != -1)
8027c478bd9Sstevel@tonic-gate 		irq = chosen_irq;
8037c478bd9Sstevel@tonic-gate 	else if (share_irq != -1)
8047c478bd9Sstevel@tonic-gate 		irq = share_irq;
8057c478bd9Sstevel@tonic-gate 	else {
8067c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Could not find a "
8077c478bd9Sstevel@tonic-gate 		    "suitable irq from the list of possible irqs for device "
8087c478bd9Sstevel@tonic-gate 		    "%s, instance #%d in ACPI's list of possible\n",
8097c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: Setting irq %d for device %s "
8167c478bd9Sstevel@tonic-gate 	    "instance #%d\n", irq, ddi_get_name(dip), ddi_get_instance(dip)));
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if ((acpi_set_irq_resource(acpipsmlnkp, irq)) == ACPI_PSM_SUCCESS) {
8197c478bd9Sstevel@tonic-gate 		/*
8207c478bd9Sstevel@tonic-gate 		 * setting irq was successful, check to make sure CRS
8217c478bd9Sstevel@tonic-gate 		 * reflects that. If CRS does not agree with what we
8227c478bd9Sstevel@tonic-gate 		 * set, return the irq that was set.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		if (acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
8267c478bd9Sstevel@tonic-gate 		    dipintr_flagp) == ACPI_PSM_SUCCESS) {
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 			if (cur_irq != irq)
8297c478bd9Sstevel@tonic-gate 				UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: "
8307c478bd9Sstevel@tonic-gate 				    "IRQ resource set (irqno %d) for device %s "
8317c478bd9Sstevel@tonic-gate 				    "instance #%d, differs from current "
8327c478bd9Sstevel@tonic-gate 				    "setting irqno %d",
8337c478bd9Sstevel@tonic-gate 				    irq, ddi_get_name(dip),
8347c478bd9Sstevel@tonic-gate 				    ddi_get_instance(dip), cur_irq));
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * return the irq that was set, and not what CRS reports,
8387c478bd9Sstevel@tonic-gate 		 * since CRS has been seen to be bogus on some systems
8397c478bd9Sstevel@tonic-gate 		 */
8407c478bd9Sstevel@tonic-gate 		cur_irq = irq;
8417c478bd9Sstevel@tonic-gate 	} else {
8427c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_WARN, "!uppc: set resource irq %d "
8437c478bd9Sstevel@tonic-gate 		    "failed for device %s instance #%d",
8447c478bd9Sstevel@tonic-gate 		    irq, ddi_get_name(dip), ddi_get_instance(dip)));
8457c478bd9Sstevel@tonic-gate 		if (cur_irq == -1)
8467c478bd9Sstevel@tonic-gate 			return (ACPI_PSM_FAILURE);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ASSERT(pci_irqp != NULL);
8507c478bd9Sstevel@tonic-gate 	*pci_irqp = cur_irq;
8517c478bd9Sstevel@tonic-gate 	return (ACPI_PSM_SUCCESS);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8567c478bd9Sstevel@tonic-gate static int
uppc_translate_irq(dev_info_t * dip,int irqno)8577c478bd9Sstevel@tonic-gate uppc_translate_irq(dev_info_t *dip, int irqno)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	char dev_type[16];
8607c478bd9Sstevel@tonic-gate 	int dev_len, pci_irq, devid, busid;
8617c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t cfg_handle;
862f6db9695Smyers 	uchar_t ipin, iline;
8637c478bd9Sstevel@tonic-gate 	iflag_t intr_flag;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
8667c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: irqno = %d"
8677c478bd9Sstevel@tonic-gate 		    " dip = NULL\n", irqno));
8687c478bd9Sstevel@tonic-gate 		return (irqno);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (!uppc_enable_acpi) {
8727c478bd9Sstevel@tonic-gate 		return (irqno);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	dev_len = sizeof (dev_type);
876a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_get_parent(dip),
8777c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "device_type", (caddr_t)dev_type,
8787c478bd9Sstevel@tonic-gate 	    &dev_len) != DDI_PROP_SUCCESS) {
8797c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: irqno %d"
8807c478bd9Sstevel@tonic-gate 		    "device %s instance %d no device_type\n", irqno,
8817c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
8827c478bd9Sstevel@tonic-gate 		return (irqno);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
88570025d76Sjohnny 	if ((strcmp(dev_type, "pci") == 0) ||
88670025d76Sjohnny 	    (strcmp(dev_type, "pciex") == 0)) {
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		/* pci device */
8897c478bd9Sstevel@tonic-gate 		if (acpica_get_bdf(dip, &busid, &devid, NULL) != 0)
8907c478bd9Sstevel@tonic-gate 			return (irqno);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 		if (pci_config_setup(dip, &cfg_handle) != DDI_SUCCESS)
8937c478bd9Sstevel@tonic-gate 			return (irqno);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		ipin = pci_config_get8(cfg_handle, PCI_CONF_IPIN) - PCI_INTA;
896f6db9695Smyers 		iline = pci_config_get8(cfg_handle, PCI_CONF_ILINE);
8977c478bd9Sstevel@tonic-gate 		if (uppc_acpi_translate_pci_irq(dip, busid, devid,
8987c478bd9Sstevel@tonic-gate 		    ipin, &pci_irq, &intr_flag) == ACPI_PSM_SUCCESS) {
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: [ACPI] new irq "
9017c478bd9Sstevel@tonic-gate 			    "%d old irq %d device %s, instance %d\n", pci_irq,
9027c478bd9Sstevel@tonic-gate 			    irqno, ddi_get_name(dip), ddi_get_instance(dip)));
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 			/*
9057c478bd9Sstevel@tonic-gate 			 * Make sure pci_irq is within range.
9067c478bd9Sstevel@tonic-gate 			 * Otherwise, fall through and return irqno.
9077c478bd9Sstevel@tonic-gate 			 */
908f6db9695Smyers 			if (pci_irq <= MAX_ISA_IRQ) {
909f6db9695Smyers 				if (iline != pci_irq) {
910f6db9695Smyers 					/*
911f6db9695Smyers 					 * Update the device's ILINE byte,
912f6db9695Smyers 					 * in case uppc_acpi_translate_pci_irq
913f6db9695Smyers 					 * has choosen a different pci_irq
914f6db9695Smyers 					 * than the BIOS has configured.
915f6db9695Smyers 					 * Some chipsets use the value in
916f6db9695Smyers 					 * ILINE to control interrupt routing,
917f6db9695Smyers 					 * in conflict with the PCI spec.
918f6db9695Smyers 					 */
919f6db9695Smyers 					pci_config_put8(cfg_handle,
920f6db9695Smyers 					    PCI_CONF_ILINE, pci_irq);
921f6db9695Smyers 				}
922f6db9695Smyers 				pci_config_teardown(&cfg_handle);
9237c478bd9Sstevel@tonic-gate 				return (pci_irq);
924f6db9695Smyers 			}
9257c478bd9Sstevel@tonic-gate 		}
926f6db9695Smyers 		pci_config_teardown(&cfg_handle);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		/* FALLTHRU to common case - returning irqno */
9297c478bd9Sstevel@tonic-gate 	} else {
930450d6964Smyers 		/* non-PCI; assumes ISA-style edge-triggered */
931*918e0d92SRobert Mustacchi 		psm_set_elcr(irqno, 0);		/* set IRQ to ISA mode */
932450d6964Smyers 
9337c478bd9Sstevel@tonic-gate 		UPPC_VERBOSE_IRQ((CE_CONT, "!uppc: non-pci,"
9347c478bd9Sstevel@tonic-gate 		    "irqno %d device %s instance %d\n", irqno,
9357c478bd9Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	return (irqno);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate  * uppc_intr_enter() raises the ipl to the level of the current interrupt,
9437c478bd9Sstevel@tonic-gate  * and sends EOI to the pics.
9447c478bd9Sstevel@tonic-gate  * If interrupt is 7 or 15 and not spurious interrupt, send specific EOI
9457c478bd9Sstevel@tonic-gate  * else send non-specific EOI
9467c478bd9Sstevel@tonic-gate  * uppc_intr_enter() returns the new priority level,
9477c478bd9Sstevel@tonic-gate  * or -1 for spurious interrupt
9487c478bd9Sstevel@tonic-gate  */
9497c478bd9Sstevel@tonic-gate static int
uppc_intr_enter(int ipl,int * vector)9507c478bd9Sstevel@tonic-gate uppc_intr_enter(int ipl, int *vector)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	int newipl;
9537c478bd9Sstevel@tonic-gate 	int intno;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	intno = (*vector);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	ASSERT(intno < 256);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	newipl = autovect[intno].avh_hi_pri;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * During wait_till_seen() periods when interrupt vector is being
9637c478bd9Sstevel@tonic-gate 	 * removed in remove_av(), the removed hardware interrupt could
9647c478bd9Sstevel@tonic-gate 	 * trigger and got here with newipl 0.  It has to send EOI
9657c478bd9Sstevel@tonic-gate 	 * as usual but no need to call setspl and returns -1 like spurious.
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	if ((intno & 7) != 7) {
9687c478bd9Sstevel@tonic-gate 		if (newipl)
9697c478bd9Sstevel@tonic-gate 			uppc_setspl(newipl);
9707c478bd9Sstevel@tonic-gate 		outb(MCMD_PORT, PIC_NSEOI);
9717c478bd9Sstevel@tonic-gate 		if (intno >= 8) {
9727c478bd9Sstevel@tonic-gate 			outb(SCMD_PORT, PIC_NSEOI);
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 	} else { /* int was 7 or 15 */
9757c478bd9Sstevel@tonic-gate 		if (newipl && newipl <= ipl) { /* Check for spurious int */
9767c478bd9Sstevel@tonic-gate 			if (intno != 7)
9777c478bd9Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_NSEOI);
9787c478bd9Sstevel@tonic-gate 			return (-1); /* Spurious int */
9797c478bd9Sstevel@tonic-gate 		} else {
9807c478bd9Sstevel@tonic-gate 			if (newipl)
9817c478bd9Sstevel@tonic-gate 				uppc_setspl(newipl);
9827c478bd9Sstevel@tonic-gate 			if (intno != 7) {
9837c478bd9Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_NSEOI);
9847c478bd9Sstevel@tonic-gate 				outb(SCMD_PORT, PIC_SEOI_LVL7);
9857c478bd9Sstevel@tonic-gate 			} else  {
9867c478bd9Sstevel@tonic-gate 				outb(MCMD_PORT, PIC_SEOI_LVL7);
9877c478bd9Sstevel@tonic-gate 			}
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (newipl)
9927c478bd9Sstevel@tonic-gate 		return (newipl);
9937c478bd9Sstevel@tonic-gate 	else
9947c478bd9Sstevel@tonic-gate 		return (-1); /* not real spurious int */
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * uppc_intr_exit() restores the old interrupt
9997c478bd9Sstevel@tonic-gate  * priority level after processing an interrupt.
10007c478bd9Sstevel@tonic-gate  * It is called with interrupts disabled, and does not enable interrupts.
10017c478bd9Sstevel@tonic-gate  */
10027c478bd9Sstevel@tonic-gate /* ARGSUSED */
10037c478bd9Sstevel@tonic-gate static void
uppc_intr_exit(int ipl,int vector)10047c478bd9Sstevel@tonic-gate uppc_intr_exit(int ipl, int vector)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	uppc_setspl(ipl);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate  * uppc_setspl() loads new interrupt masks into the pics
10117c478bd9Sstevel@tonic-gate  * based on input ipl.
10127c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate /* ARGSUSED */
10147c478bd9Sstevel@tonic-gate static void
uppc_setspl(int ipl)10157c478bd9Sstevel@tonic-gate uppc_setspl(int ipl)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	struct standard_pic *pp;
10187c478bd9Sstevel@tonic-gate 	uint8_t smask, mmask;
10197c478bd9Sstevel@tonic-gate 	uint8_t cursmask, curmmask;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	pp = &pics0;
10227c478bd9Sstevel@tonic-gate 	smask = pp->c_iplmask[ipl * 2];
10237c478bd9Sstevel@tonic-gate 	mmask = pp->c_iplmask[ipl * 2 + 1];
10247c478bd9Sstevel@tonic-gate 	cursmask = pp->c_curmask[0];
10257c478bd9Sstevel@tonic-gate 	curmmask = pp->c_curmask[1];
10267c478bd9Sstevel@tonic-gate 	if (cursmask == smask && curmmask == mmask)
10277c478bd9Sstevel@tonic-gate 		return;
10287c478bd9Sstevel@tonic-gate 	pp->c_curmask[0] = smask;
10297c478bd9Sstevel@tonic-gate 	pp->c_curmask[1] = mmask;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (cursmask != smask) {
10327c478bd9Sstevel@tonic-gate 		/*
10337c478bd9Sstevel@tonic-gate 		 * program new slave pic mask
10347c478bd9Sstevel@tonic-gate 		 */
10357c478bd9Sstevel@tonic-gate 		outb(SIMR_PORT, smask);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	if (curmmask != mmask) {
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * program new master pic mask
10407c478bd9Sstevel@tonic-gate 		 */
10417c478bd9Sstevel@tonic-gate 		outb(MIMR_PORT, mmask);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 	/*
10447c478bd9Sstevel@tonic-gate 	 * read master to allow pics to settle
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	(void) inb(MIMR_PORT);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate  * uppc_gethrtime() returns high resolution timer value
10517c478bd9Sstevel@tonic-gate  */
10527c478bd9Sstevel@tonic-gate static hrtime_t
uppc_gethrtime()10537c478bd9Sstevel@tonic-gate uppc_gethrtime()
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	hrtime_t timeval, temp;
1056ae115bc7Smrj 	unsigned int ctr0;
1057ae115bc7Smrj 	ulong_t oflags;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	oflags = intr_clear(); /* disable ints */
10607c478bd9Sstevel@tonic-gate 	lock_set(&uppc_gethrtime_lock);
10617c478bd9Sstevel@tonic-gate retry:
10627c478bd9Sstevel@tonic-gate 	temp = hrtime_base;
10637c478bd9Sstevel@tonic-gate 	outb(PITCTL_PORT, 0);	/* latch counter 0 */
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * read counter 0
10667c478bd9Sstevel@tonic-gate 	 */
10677c478bd9Sstevel@tonic-gate 	ctr0 = inb(PITCTR0_PORT);
10687c478bd9Sstevel@tonic-gate 	ctr0 |= inb(PITCTR0_PORT) << 8;
10697c478bd9Sstevel@tonic-gate 	timeval = (hrtime_t)ctr0 * (NANOSEC / PIT_HZ);
10707c478bd9Sstevel@tonic-gate 	if (temp != hrtime_base)
10717c478bd9Sstevel@tonic-gate 		goto retry;
10727c478bd9Sstevel@tonic-gate 	timeval -= temp;
10737c478bd9Sstevel@tonic-gate 	if (timeval < uppc_lasthrtime)
10747c478bd9Sstevel@tonic-gate 		timeval = uppc_lasthrtime;
10757c478bd9Sstevel@tonic-gate 	uppc_lasthrtime = timeval;
10767c478bd9Sstevel@tonic-gate 	lock_clear(&uppc_gethrtime_lock);
10777c478bd9Sstevel@tonic-gate 	intr_restore(oflags);
10787c478bd9Sstevel@tonic-gate 	return (timeval);
10797c478bd9Sstevel@tonic-gate }
1080