xref: /onnv-gate/usr/src/uts/i86pc/io/psm/psm_common.c (revision 347:04ff98e42cf2)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/promif.h>
330Sstevel@tonic-gate #include <sys/acpi/acpi.h>
340Sstevel@tonic-gate #include <sys/acpica.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/pci.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/psm_common.h>
410Sstevel@tonic-gate #include <sys/sunndi.h>
420Sstevel@tonic-gate #include <sys/ksynch.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * ACPI Poweroff patchable options in acpi_poweroff_opt for broken BIOS
460Sstevel@tonic-gate  * workarounds.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate #define	ACPI_PO_CLRWAK	0x0001
490Sstevel@tonic-gate #define	ACPI_PO_CLRALL	0x0002
500Sstevel@tonic-gate #define	ACPI_PO_RSTGPE	0x0004
510Sstevel@tonic-gate #define	ACPI_PO_DISARB	0x0008
520Sstevel@tonic-gate #define	ACPI_PO_2NDTRY	0x0010
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* Global configurables */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate char *psm_module_name;	/* used to store name of psm module */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * acpi_irq_check_elcr: when set elcr will also be consulted for building
60*347Smyers  * the reserved irq list.  When 0 (false), the existing state of the ELCR
61*347Smyers  * is ignored when selecting a vector during IRQ translation, and the ELCR
62*347Smyers  * is programmed to the proper setting for the type of bus (level-triggered
63*347Smyers  * for PCI, edge-triggered for non-PCI).  When non-zero (true), vectors
64*347Smyers  * set to edge-mode will not be used when in PIC-mode.  The default value
65*347Smyers  * is 0 (false).  Note that ACPI's SCI vector is always set to conform to
66*347Smyers  * ACPI-specification regardless of this.
67*347Smyers  *
680Sstevel@tonic-gate  */
69*347Smyers int acpi_irq_check_elcr = 0;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * acpi_s5_slp_typ:
730Sstevel@tonic-gate  * If >= 0 then override the \_S5 parameter return value. This is useful
740Sstevel@tonic-gate  * for systems with broken \_S5 methods which return the wrong value for
750Sstevel@tonic-gate  * the chipset in use.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate int acpi_s5_slp_typ = -1;
780Sstevel@tonic-gate int acpi_s5_slp_typ2 = -1;	/* second parameter (only patch if different) */
790Sstevel@tonic-gate int acpi_poweroff_opt = 0;	/* patchable poweroff options */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate int psm_verbose = 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	PSM_VERBOSE_IRQ(fmt)	\
840Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_IRQ_FLAG) \
850Sstevel@tonic-gate 			cmn_err fmt;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	PSM_VERBOSE_POWEROFF(fmt)  \
880Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_POWEROFF_FLAG || \
890Sstevel@tonic-gate 		    psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) \
900Sstevel@tonic-gate 			prom_printf fmt;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	PSM_VERBOSE_POWEROFF_PAUSE(fmt) \
930Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_POWEROFF_FLAG || \
940Sstevel@tonic-gate 		    psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) {\
950Sstevel@tonic-gate 			prom_printf fmt; \
960Sstevel@tonic-gate 			if (psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) \
970Sstevel@tonic-gate 				(void) goany(); \
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* Local storage */
1020Sstevel@tonic-gate static ACPI_HANDLE acpi_sbobj = NULL;
1030Sstevel@tonic-gate static kmutex_t acpi_irq_cache_mutex;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #define	D2A_INITLEN	20
1060Sstevel@tonic-gate static int d2a_len = 0;
1070Sstevel@tonic-gate static int d2a_valid = 0;
1080Sstevel@tonic-gate static d2a *d2a_table;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * irq_cache_table is a list that serves a two-key cache. It is used
1120Sstevel@tonic-gate  * as a pci busid/devid/ipin <-> irq cache and also as a acpi
1130Sstevel@tonic-gate  * interrupt lnk <-> irq cache.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate static irq_cache_t *irq_cache_table;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #define	IRQ_CACHE_INITLEN	20
1180Sstevel@tonic-gate static int irq_cache_len = 0;
1190Sstevel@tonic-gate static int irq_cache_valid = 0;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static int acpi_get_gsiv(dev_info_t *dip, ACPI_HANDLE pciobj, int devno,
1220Sstevel@tonic-gate 	int ipin, int *pci_irqp, iflag_t *iflagp,  acpi_psm_lnk_t *acpipsmlnkp);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static int acpi_eval_lnk(dev_info_t *dip, char *lnkname,
1250Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static int acpi_get_irq_lnk_cache_ent(ACPI_HANDLE lnkobj, int *pci_irqp,
1280Sstevel@tonic-gate     iflag_t *intr_flagp);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate extern int goany(void);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #define	NEXT_PRT_ITEM(p)	\
1340Sstevel@tonic-gate 		(ACPI_PCI_ROUTING_TABLE *)(((char *)(p)) + (p)->Length)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int
1370Sstevel@tonic-gate acpi_get_gsiv(dev_info_t *dip, ACPI_HANDLE pciobj, int devno, int ipin,
1380Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	ACPI_BUFFER rb;
1410Sstevel@tonic-gate 	ACPI_PCI_ROUTING_TABLE *prtp;
1420Sstevel@tonic-gate 	int status;
1430Sstevel@tonic-gate 	int dev_adr;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Get the IRQ routing table
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	rb.Pointer = NULL;
1490Sstevel@tonic-gate 	rb.Length = ACPI_ALLOCATE_BUFFER;
1500Sstevel@tonic-gate 	if (AcpiGetIrqRoutingTable(pciobj, &rb) != AE_OK) {
1510Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	status = ACPI_PSM_FAILURE;
1550Sstevel@tonic-gate 	dev_adr = (devno << 16 | 0xffff);
1560Sstevel@tonic-gate 	for (prtp = rb.Pointer; prtp->Length != 0; prtp = NEXT_PRT_ITEM(prtp)) {
1570Sstevel@tonic-gate 		/* look until a matching dev/pin is found */
1580Sstevel@tonic-gate 		if (dev_adr != prtp->Address || ipin != prtp->Pin)
1590Sstevel@tonic-gate 			continue;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		/* NULL Source name means index is GSIV */
1620Sstevel@tonic-gate 		if (*prtp->Source == 0) {
1630Sstevel@tonic-gate 			intr_flagp->intr_el = TRIGGER_LEVEL;
1640Sstevel@tonic-gate 			intr_flagp->intr_po = POLARITY_ACTIVE_LOW;
1650Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
1660Sstevel@tonic-gate 			*pci_irqp = prtp->SourceIndex;
1670Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
1680Sstevel@tonic-gate 		} else
1690Sstevel@tonic-gate 			status = acpi_eval_lnk(dip, prtp->Source, pci_irqp,
1700Sstevel@tonic-gate 			    intr_flagp, acpipsmlnkp);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		break;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	AcpiOsFree(rb.Pointer);
1770Sstevel@tonic-gate 	return (status);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  * If the interrupt link device is already configured,
1830Sstevel@tonic-gate  * stores polarity and sensitivity in the structure pointed to by
1840Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * Returns:
1870Sstevel@tonic-gate  *	ACPI_PSM_SUCCESS if the interrupt link device is already configured.
1880Sstevel@tonic-gate  *	ACPI_PSM_PARTIAL if configuration is needed.
1890Sstevel@tonic-gate  * 	ACPI_PSM_FAILURE in case of error.
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  * When two devices share the same interrupt link device, and the
1920Sstevel@tonic-gate  * link device is already configured (i.e. found in the irq cache)
1930Sstevel@tonic-gate  * we need to use the already configured irq instead of reconfiguring
1940Sstevel@tonic-gate  * the link device.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate static int
1970Sstevel@tonic-gate acpi_eval_lnk(dev_info_t *dip, char *lnkname, int *pci_irqp,
1980Sstevel@tonic-gate iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	ACPI_HANDLE	tmpobj;
2010Sstevel@tonic-gate 	ACPI_HANDLE	lnkobj;
2020Sstevel@tonic-gate 	int status;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Convert the passed-in link device name to a handle
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	if (AcpiGetHandle(NULL, lnkname, &lnkobj) != AE_OK) {
2080Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * Assume that the link device is invalid if no _CRS method
2130Sstevel@tonic-gate 	 * exists, since _CRS method is a required method
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	if (AcpiGetHandle(lnkobj, "_CRS", &tmpobj) != AE_OK) {
2160Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
2200Sstevel@tonic-gate 	acpipsmlnkp->lnkobj = lnkobj;
2210Sstevel@tonic-gate 	if ((acpi_get_irq_lnk_cache_ent(lnkobj, pci_irqp, intr_flagp)) ==
2220Sstevel@tonic-gate 	    ACPI_PSM_SUCCESS) {
2230Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_CONT, "!psm: link object found from cache "
2240Sstevel@tonic-gate 		    " for device %s, instance #%d, irq no %d\n",
2250Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip), *pci_irqp));
2260Sstevel@tonic-gate 		return (ACPI_PSM_SUCCESS);
2270Sstevel@tonic-gate 	} else {
2280Sstevel@tonic-gate 		if (acpica_eval_int(lnkobj, "_STA", &status) == AE_OK) {
2290Sstevel@tonic-gate 			acpipsmlnkp->device_status = (uchar_t)status;
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		return (ACPI_PSM_PARTIAL);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate int
2370Sstevel@tonic-gate acpi_psm_init(char *module_name, int verbose_flags)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	psm_module_name = module_name;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	psm_verbose = verbose_flags;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (AcpiGetHandle(NULL, "\\_SB", &acpi_sbobj) != AE_OK) {
2440Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: get _SB failed");
2450Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	mutex_init(&acpi_irq_cache_mutex, NULL, MUTEX_DEFAULT, NULL);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	return (ACPI_PSM_SUCCESS);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * Return bus/dev/fn for PCI dip (note: not the parent "pci" node).
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate int
2590Sstevel@tonic-gate get_bdf(dev_info_t *dip, int *bus, int *device, int *func)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
2620Sstevel@tonic-gate 	int len;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2650Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, (uint_t *)&len) != DDI_SUCCESS)
2660Sstevel@tonic-gate 		return (-1);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (len < (sizeof (pci_regspec_t) / sizeof (int))) {
2690Sstevel@tonic-gate 		ddi_prop_free(pci_rp);
2700Sstevel@tonic-gate 		return (-1);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	if (bus != NULL)
2730Sstevel@tonic-gate 		*bus = (int)PCI_REG_BUS_G(pci_rp->pci_phys_hi);
2740Sstevel@tonic-gate 	if (device != NULL)
2750Sstevel@tonic-gate 		*device = (int)PCI_REG_DEV_G(pci_rp->pci_phys_hi);
2760Sstevel@tonic-gate 	if (func != NULL)
2770Sstevel@tonic-gate 		*func = (int)PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
2780Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
2790Sstevel@tonic-gate 	return (0);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * Build the reserved ISA irq list, and store it in the table pointed to by
2850Sstevel@tonic-gate  * reserved_irqs_table. The caller is responsible for allocating this table
2860Sstevel@tonic-gate  * with a minimum of MAX_ISA_IRQ + 1 entries.
2870Sstevel@tonic-gate  *
2880Sstevel@tonic-gate  * The routine looks in the device tree at the subtree rooted at /isa
2890Sstevel@tonic-gate  * for each of the devices under that node, if an interrupts property
2900Sstevel@tonic-gate  * is present, its values are used to "reserve" irqs so that later ACPI
2910Sstevel@tonic-gate  * configuration won't choose those irqs.
2920Sstevel@tonic-gate  *
2930Sstevel@tonic-gate  * In addition, if acpi_irq_check_elcr is set, will use ELCR register
2940Sstevel@tonic-gate  * to identify reserved IRQs.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate void
2970Sstevel@tonic-gate build_reserved_irqlist(uchar_t *reserved_irqs_table)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	dev_info_t *isanode = ddi_find_devinfo("isa", -1, 0);
3000Sstevel@tonic-gate 	dev_info_t *isa_child = 0;
3010Sstevel@tonic-gate 	int i;
3020Sstevel@tonic-gate 	uint_t	elcrval;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* Initialize the reserved ISA IRQs: */
3050Sstevel@tonic-gate 	for (i = 0; i < MAX_ISA_IRQ; i++)
3060Sstevel@tonic-gate 		reserved_irqs_table[i] = 0;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (acpi_irq_check_elcr) {
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		elcrval = (inb(ELCR_PORT2) << 8) | (inb(ELCR_PORT1));
3110Sstevel@tonic-gate 		if (ELCR_EDGE(elcrval, 0) && ELCR_EDGE(elcrval, 1) &&
3120Sstevel@tonic-gate 		    ELCR_EDGE(elcrval, 2) && ELCR_EDGE(elcrval, 8) &&
3130Sstevel@tonic-gate 		    ELCR_EDGE(elcrval, 13)) {
3140Sstevel@tonic-gate 			/* valid ELCR */
3150Sstevel@tonic-gate 			for (i = 0; i < MAX_ISA_IRQ; i++)
3160Sstevel@tonic-gate 				if (!ELCR_LEVEL(elcrval, i))
3170Sstevel@tonic-gate 					reserved_irqs_table[i] = 1;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/* always check the isa devinfo nodes */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (isanode != 0) { /* Found ISA */
3240Sstevel@tonic-gate 		uint_t intcnt;		/* Interrupt count */
3250Sstevel@tonic-gate 		int *intrs;		/* Interrupt values */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		/* Load first child: */
3280Sstevel@tonic-gate 		isa_child = ddi_get_child(isanode);
3290Sstevel@tonic-gate 		while (isa_child != 0) { /* Iterate over /isa children */
3300Sstevel@tonic-gate 			/* if child has any interrupts, save them */
3310Sstevel@tonic-gate 			if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, isa_child,
3320Sstevel@tonic-gate 			    DDI_PROP_DONTPASS, "interrupts", &intrs, &intcnt)
3330Sstevel@tonic-gate 			    == DDI_PROP_SUCCESS) {
3340Sstevel@tonic-gate 				/*
3350Sstevel@tonic-gate 				 * iterate over child interrupt list, adding
3360Sstevel@tonic-gate 				 * them to the reserved irq list
3370Sstevel@tonic-gate 				 */
3380Sstevel@tonic-gate 				while (intcnt-- > 0) {
3390Sstevel@tonic-gate 					/*
3400Sstevel@tonic-gate 					 * Each value MUST be <= MAX_ISA_IRQ
3410Sstevel@tonic-gate 					 */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 					if ((intrs[intcnt] > MAX_ISA_IRQ) ||
3440Sstevel@tonic-gate 					    (intrs[intcnt] < 0))
3450Sstevel@tonic-gate 						continue;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 					reserved_irqs_table[intrs[intcnt]] = 1;
3480Sstevel@tonic-gate 				}
3490Sstevel@tonic-gate 				ddi_prop_free(intrs);
3500Sstevel@tonic-gate 			}
3510Sstevel@tonic-gate 			isa_child = ddi_get_next_sibling(isa_child);
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 		/* The isa node was held by ddi_find_devinfo, so release it */
3540Sstevel@tonic-gate 		ndi_rele_devi(isanode);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Reserve IRQ14 & IRQ15 for IDE.  It shouldn't be hard-coded
3590Sstevel@tonic-gate 	 * here but there's no other way to find the irqs for
3600Sstevel@tonic-gate 	 * legacy-mode ata (since it's hard-coded in pci-ide also).
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	reserved_irqs_table[14] = 1;
3630Sstevel@tonic-gate 	reserved_irqs_table[15] = 1;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Examine devinfo node to determine if it is a PCI-PCI bridge
3680Sstevel@tonic-gate  *
3690Sstevel@tonic-gate  * Returns:
3700Sstevel@tonic-gate  *	0 if not a bridge or error
3710Sstevel@tonic-gate  *	1 if a bridge
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate static int
3740Sstevel@tonic-gate psm_is_pci_bridge(dev_info_t *dip)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	ddi_acc_handle_t cfg_handle;
3770Sstevel@tonic-gate 	int rv = 0;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_handle) == DDI_SUCCESS) {
3800Sstevel@tonic-gate 		rv = ((pci_config_get8(cfg_handle, PCI_CONF_BASCLASS) ==
3810Sstevel@tonic-gate 		    PCI_CLASS_BRIDGE) && (pci_config_get8(cfg_handle,
3820Sstevel@tonic-gate 		    PCI_CONF_SUBCLASS) == PCI_BRIDGE_PCI));
3830Sstevel@tonic-gate 		pci_config_teardown(&cfg_handle);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	return (rv);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * Examines ACPI node for presence of _PRT object
3920Sstevel@tonic-gate  *
3930Sstevel@tonic-gate  * Returns:
3940Sstevel@tonic-gate  *	0 if no _PRT or error
3950Sstevel@tonic-gate  *	1 if _PRT is present
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate psm_node_has_prt(ACPI_HANDLE *ah)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	ACPI_HANDLE rh;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	return (AcpiGetHandle(ah, "_PRT", &rh) == AE_OK);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * Look first for an ACPI PCI bus node matching busid, then for a _PRT on the
4080Sstevel@tonic-gate  * parent node; then drop into the bridge-chasing code (which will also
4090Sstevel@tonic-gate  * look for _PRTs on the way up the tree of bridges)
4100Sstevel@tonic-gate  *
4110Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by
4120Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.  *
4130Sstevel@tonic-gate  * Returns:
4140Sstevel@tonic-gate  *  	ACPI_PSM_SUCCESS on success.
4150Sstevel@tonic-gate  *	ACPI_PSM_PARTIAL to indicate need to configure the interrupt
4160Sstevel@tonic-gate  *	link device.
4170Sstevel@tonic-gate  * 	ACPI_PSM_FAILURE  if an error prevented the system from
4180Sstevel@tonic-gate  *	obtaining irq information for dip.
4190Sstevel@tonic-gate  */
4200Sstevel@tonic-gate int
4210Sstevel@tonic-gate acpi_translate_pci_irq(dev_info_t *dip, int ipin, int *pci_irqp,
4220Sstevel@tonic-gate     iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	ACPI_HANDLE pciobj;
4250Sstevel@tonic-gate 	int status = AE_ERROR;
4260Sstevel@tonic-gate 	dev_info_t *curdip, *parentdip;
4270Sstevel@tonic-gate 	int curpin, curbus, curdev;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	curpin = ipin;
4310Sstevel@tonic-gate 	curdip = dip;
4320Sstevel@tonic-gate 	while (curdip != ddi_root_node()) {
4330Sstevel@tonic-gate 		parentdip = ddi_get_parent(curdip);
4340Sstevel@tonic-gate 		ASSERT(parentdip != NULL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		if (get_bdf(curdip, &curbus, &curdev, NULL) != 0) {
4370Sstevel@tonic-gate 			break;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		status = acpica_find_pciobj(parentdip, &pciobj);
4410Sstevel@tonic-gate 		if ((status == AE_OK) && psm_node_has_prt(pciobj)) {
4420Sstevel@tonic-gate 			return (acpi_get_gsiv(curdip, pciobj, curdev, curpin,
4430Sstevel@tonic-gate 			    pci_irqp, intr_flagp, acpipsmlnkp));
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		/* if we got here, we need to traverse a bridge upwards */
4470Sstevel@tonic-gate 		if (!psm_is_pci_bridge(parentdip))
4480Sstevel@tonic-gate 			break;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		/*
4510Sstevel@tonic-gate 		 * This is the rotating scheme that Compaq is using
4520Sstevel@tonic-gate 		 * and documented in the PCI-PCI spec.  Also, if the
4530Sstevel@tonic-gate 		 * PCI-PCI bridge is behind another PCI-PCI bridge,
4540Sstevel@tonic-gate 		 * then it needs to keep ascending until an interrupt
4550Sstevel@tonic-gate 		 * entry is found or the top is reached
4560Sstevel@tonic-gate 		 */
4570Sstevel@tonic-gate 		curpin = (curdev + curpin) % PCI_INTD;
4580Sstevel@tonic-gate 		curdip = parentdip;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/*
4620Sstevel@tonic-gate 	 * We should never, ever get here; didn't find a _PRT
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	return (ACPI_PSM_FAILURE);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * Sets the irq resource of the lnk object to the requested irq value.
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success, ACPI_PSM_FAILURE upon failure.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate int
4730Sstevel@tonic-gate acpi_set_irq_resource(acpi_psm_lnk_t *acpipsmlnkp, int irq)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	ACPI_BUFFER	rsb;
4760Sstevel@tonic-gate 	ACPI_RESOURCE	*resp;
4770Sstevel@tonic-gate 	ACPI_RESOURCE	*srsp;
4780Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
4790Sstevel@tonic-gate 	int status;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * Fetch the possible resources for the link
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	rsb.Pointer = NULL;
4900Sstevel@tonic-gate 	rsb.Length = ACPI_ALLOCATE_BUFFER;
4910Sstevel@tonic-gate 	status = AcpiGetPossibleResources(lnkobj, &rsb);
4920Sstevel@tonic-gate 	if (status != AE_OK) {
4930Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: set_irq: _PRS failed");
4940Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Find an IRQ resource descriptor to use as template
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 	srsp = NULL;
5010Sstevel@tonic-gate 	for (resp = rsb.Pointer; resp->Length != 0;
5020Sstevel@tonic-gate 	    resp = ACPI_NEXT_RESOURCE(resp)) {
5030Sstevel@tonic-gate 		if ((resp->Id == ACPI_RSTYPE_IRQ) ||
5040Sstevel@tonic-gate 		    (resp->Id == ACPI_RSTYPE_EXT_IRQ)) {
5050Sstevel@tonic-gate 			/*
5060Sstevel@tonic-gate 			 * Mild trickery here; allocate two
5070Sstevel@tonic-gate 			 * resource structures, and set the Length
5080Sstevel@tonic-gate 			 * field of the second one to 0 to terminate
5090Sstevel@tonic-gate 			 * the list. Copy the possible resource into
5100Sstevel@tonic-gate 			 * the first one as a template.
5110Sstevel@tonic-gate 			 */
5120Sstevel@tonic-gate 			srsp = kmem_zalloc(2 * sizeof (*srsp), KM_SLEEP);
5130Sstevel@tonic-gate 			srsp[1].Length = 0;
5140Sstevel@tonic-gate 			*srsp = *resp;
5150Sstevel@tonic-gate 			break;	/* drop out of the loop */
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/*
5200Sstevel@tonic-gate 	 * We're done with the PRS values, toss 'em lest we forget
5210Sstevel@tonic-gate 	 */
5220Sstevel@tonic-gate 	AcpiOsFree(rsb.Pointer);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (srsp == NULL)
5250Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * The Interrupts[] array is always at least one entry
5290Sstevel@tonic-gate 	 * long; see the definition of ACPI_RESOURCE.
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	switch (srsp->Id) {
5320Sstevel@tonic-gate 	case ACPI_RSTYPE_IRQ:
5330Sstevel@tonic-gate 		srsp->Data.Irq.NumberOfInterrupts = 1;
5340Sstevel@tonic-gate 		srsp->Data.Irq.Interrupts[0] = irq;
5350Sstevel@tonic-gate 		break;
5360Sstevel@tonic-gate 	case ACPI_RSTYPE_EXT_IRQ:
5370Sstevel@tonic-gate 		srsp->Data.ExtendedIrq.NumberOfInterrupts = 1;
5380Sstevel@tonic-gate 		srsp->Data.ExtendedIrq.Interrupts[0] = irq;
5390Sstevel@tonic-gate 		break;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	rsb.Pointer = srsp;
5430Sstevel@tonic-gate 	rsb.Length = 2 * sizeof (*srsp);
5440Sstevel@tonic-gate 	status = AcpiSetCurrentResources(lnkobj, &rsb);
5450Sstevel@tonic-gate 	kmem_free(srsp, 2 * sizeof (*srsp));
5460Sstevel@tonic-gate 	if (status != AE_OK) {
5470Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: set_irq: _SRS failed");
5480Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (acpica_eval_int(lnkobj, "_STA", &status) == AE_OK) {
5520Sstevel@tonic-gate 		acpipsmlnkp->device_status = (uchar_t)status;
5530Sstevel@tonic-gate 		return (ACPI_PSM_SUCCESS);
5540Sstevel@tonic-gate 	} else
5550Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate  *
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate static int
5630Sstevel@tonic-gate psm_acpi_edgelevel(UINT32 el)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	switch (el) {
5660Sstevel@tonic-gate 	case ACPI_EDGE_SENSITIVE:
5670Sstevel@tonic-gate 		return (INTR_EL_EDGE);
5680Sstevel@tonic-gate 	case ACPI_LEVEL_SENSITIVE:
5690Sstevel@tonic-gate 		return (INTR_EL_LEVEL);
5700Sstevel@tonic-gate 	default:
5710Sstevel@tonic-gate 		/* el is a single bit; should never reach here */
5720Sstevel@tonic-gate 		return (INTR_EL_CONFORM);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  *
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate static int
5810Sstevel@tonic-gate psm_acpi_po(UINT32 po)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	switch (po) {
5840Sstevel@tonic-gate 	case ACPI_ACTIVE_HIGH:
5850Sstevel@tonic-gate 		return (INTR_PO_ACTIVE_HIGH);
5860Sstevel@tonic-gate 	case ACPI_ACTIVE_LOW:
5870Sstevel@tonic-gate 		return (INTR_PO_ACTIVE_LOW);
5880Sstevel@tonic-gate 	default:
5890Sstevel@tonic-gate 		/* po is a single bit; should never reach here */
5900Sstevel@tonic-gate 		return (INTR_PO_CONFORM);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * Retrieves the current irq setting for the interrrupt link device.
5970Sstevel@tonic-gate  *
5980Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by
5990Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.
6000Sstevel@tonic-gate  *
6010Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success, ACPI_PSM_FAILURE upon failure.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate int
6040Sstevel@tonic-gate acpi_get_current_irq_resource(acpi_psm_lnk_t *acpipsmlnkp, int *pci_irqp,
6050Sstevel@tonic-gate     iflag_t *intr_flagp)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
6080Sstevel@tonic-gate 	ACPI_BUFFER rb;
6090Sstevel@tonic-gate 	ACPI_RESOURCE *rp;
6100Sstevel@tonic-gate 	int irq;
6110Sstevel@tonic-gate 	int status = ACPI_PSM_FAILURE;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
6140Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (!(acpipsmlnkp->device_status & STA_PRESENT) ||
6170Sstevel@tonic-gate 	    !(acpipsmlnkp->device_status & STA_ENABLE)) {
6180Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_WARN, "!psm: crs device either not "
6190Sstevel@tonic-gate 		    "present or disabled, status 0x%x",
6200Sstevel@tonic-gate 		    acpipsmlnkp->device_status));
6210Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	rb.Pointer = NULL;
6250Sstevel@tonic-gate 	rb.Length = ACPI_ALLOCATE_BUFFER;
6260Sstevel@tonic-gate 	if (AcpiGetCurrentResources(lnkobj, &rb) != AE_OK) {
6270Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_WARN, "!psm: no crs object found or"
6280Sstevel@tonic-gate 		" evaluation failed"));
6290Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	irq = -1;
6330Sstevel@tonic-gate 	for (rp = rb.Pointer; rp->Length != 0; rp = ACPI_NEXT_RESOURCE(rp)) {
6340Sstevel@tonic-gate 		if (rp->Id == ACPI_RSTYPE_IRQ) {
6350Sstevel@tonic-gate 			if (irq > 0) {
6360Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: multiple IRQ"
6370Sstevel@tonic-gate 				" from _CRS "));
6380Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6390Sstevel@tonic-gate 				break;
6400Sstevel@tonic-gate 			}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 			if (rp->Data.Irq.NumberOfInterrupts != 1) {
6430Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: <>1 interrupt"
6440Sstevel@tonic-gate 				" from _CRS "));
6450Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6460Sstevel@tonic-gate 				break;
6470Sstevel@tonic-gate 			}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 			intr_flagp->intr_el = psm_acpi_edgelevel(
6500Sstevel@tonic-gate 					rp->Data.Irq.EdgeLevel);
6510Sstevel@tonic-gate 			intr_flagp->intr_po = psm_acpi_po(
6520Sstevel@tonic-gate 					rp->Data.Irq.ActiveHighLow);
6530Sstevel@tonic-gate 			irq = rp->Data.Irq.Interrupts[0];
6540Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
6550Sstevel@tonic-gate 		} else if (rp->Id == ACPI_RSTYPE_EXT_IRQ) {
6560Sstevel@tonic-gate 			if (irq > 0) {
6570Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: multiple IRQ"
6580Sstevel@tonic-gate 				" from _CRS "));
6590Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6600Sstevel@tonic-gate 				break;
6610Sstevel@tonic-gate 			}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 			if (rp->Data.ExtendedIrq.NumberOfInterrupts != 1) {
6640Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: <>1 interrupt"
6650Sstevel@tonic-gate 				" from _CRS "));
6660Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6670Sstevel@tonic-gate 				break;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 			intr_flagp->intr_el = psm_acpi_edgelevel(
6710Sstevel@tonic-gate 					rp->Data.ExtendedIrq.EdgeLevel);
6720Sstevel@tonic-gate 			intr_flagp->intr_po = psm_acpi_po(
6730Sstevel@tonic-gate 					rp->Data.ExtendedIrq.ActiveHighLow);
6740Sstevel@tonic-gate 			irq = rp->Data.ExtendedIrq.Interrupts[0];
6750Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	AcpiOsFree(rb.Pointer);
6800Sstevel@tonic-gate 	if (status == ACPI_PSM_SUCCESS) {
6810Sstevel@tonic-gate 		*pci_irqp =  irq;
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	return (status);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate  * Searches for the given IRQ in the irqlist passed in.
6890Sstevel@tonic-gate  *
6900Sstevel@tonic-gate  * If multiple matches exist, this returns true on the first match.
6910Sstevel@tonic-gate  * Returns the interrupt flags, if a match was found, in `intr_flagp' if
6920Sstevel@tonic-gate  * it's passed in non-NULL
6930Sstevel@tonic-gate  */
6940Sstevel@tonic-gate int
6950Sstevel@tonic-gate acpi_irqlist_find_irq(acpi_irqlist_t *irqlistp, int irq, iflag_t *intr_flagp)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	int found = 0;
6980Sstevel@tonic-gate 	int i;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	while (irqlistp != NULL && !found) {
7010Sstevel@tonic-gate 		for (i = 0; i < irqlistp->num_irqs; i++) {
7020Sstevel@tonic-gate 			if (irqlistp->irqs[i] == irq) {
7030Sstevel@tonic-gate 				if (intr_flagp)
7040Sstevel@tonic-gate 					*intr_flagp = irqlistp->intr_flags;
7050Sstevel@tonic-gate 				found = 1;
7060Sstevel@tonic-gate 				break;	/* out of for() */
7070Sstevel@tonic-gate 			}
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	return (found ? ACPI_PSM_SUCCESS : ACPI_PSM_FAILURE);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate  * Frees the irqlist allocated by acpi_get_possible_irq_resource.
7160Sstevel@tonic-gate  * It takes a count of number of entries in the list.
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate void
7190Sstevel@tonic-gate acpi_free_irqlist(acpi_irqlist_t *irqlistp)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	acpi_irqlist_t *freednode;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	while (irqlistp != NULL) {
7240Sstevel@tonic-gate 		/* Free the irq list */
7250Sstevel@tonic-gate 		kmem_free(irqlistp->irqs, irqlistp->num_irqs *
7260Sstevel@tonic-gate 		    sizeof (int32_t));
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		freednode = irqlistp;
7290Sstevel@tonic-gate 		irqlistp = irqlistp->next;
7300Sstevel@tonic-gate 		kmem_free(freednode, sizeof (acpi_irqlist_t));
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate  * Creates a new entry in the given irqlist with the information passed in.
7360Sstevel@tonic-gate  */
7370Sstevel@tonic-gate static void
7380Sstevel@tonic-gate acpi_add_irqlist_entry(acpi_irqlist_t **irqlistp, uint32_t *irqlist,
7390Sstevel@tonic-gate     int irqlist_len, iflag_t *intr_flagp)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	acpi_irqlist_t *newent;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	ASSERT(irqlist != NULL);
7440Sstevel@tonic-gate 	ASSERT(intr_flagp != NULL);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	newent = kmem_alloc(sizeof (acpi_irqlist_t), KM_SLEEP);
7470Sstevel@tonic-gate 	newent->intr_flags = *intr_flagp;
7480Sstevel@tonic-gate 	newent->irqs = irqlist;
7490Sstevel@tonic-gate 	newent->num_irqs = irqlist_len;
7500Sstevel@tonic-gate 	newent->next = *irqlistp;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	*irqlistp = newent;
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * Retrieves a list of possible interrupt settings for the interrupt link
7580Sstevel@tonic-gate  * device.
7590Sstevel@tonic-gate  *
7600Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by intr_flagp.
7610Sstevel@tonic-gate  * Updates value pointed to by irqlistp with the address of a table it
7620Sstevel@tonic-gate  * allocates. where interrupt numbers are stored. Stores the number of entries
7630Sstevel@tonic-gate  * in this table in the value pointed to by num_entriesp;
7640Sstevel@tonic-gate  *
7650Sstevel@tonic-gate  * Each element in this table is of type int32_t. The table should be later
7660Sstevel@tonic-gate  * freed by caller via acpi_free_irq_list().
7670Sstevel@tonic-gate  *
7680Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success and ACPI_PSM_FAILURE upon failure
7690Sstevel@tonic-gate  */
7700Sstevel@tonic-gate int
7710Sstevel@tonic-gate acpi_get_possible_irq_resources(acpi_psm_lnk_t *acpipsmlnkp,
7720Sstevel@tonic-gate     acpi_irqlist_t **irqlistp)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
7750Sstevel@tonic-gate 	ACPI_BUFFER rsb;
7760Sstevel@tonic-gate 	ACPI_RESOURCE *resp;
7770Sstevel@tonic-gate 	int status;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	int i, el, po, irqlist_len;
7800Sstevel@tonic-gate 	uint32_t *irqlist, *tmplist;
7810Sstevel@tonic-gate 	iflag_t intr_flags;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
7840Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	rsb.Pointer = NULL;
7870Sstevel@tonic-gate 	rsb.Length = ACPI_ALLOCATE_BUFFER;
7880Sstevel@tonic-gate 	status = AcpiGetPossibleResources(lnkobj, &rsb);
7890Sstevel@tonic-gate 	if (status != AE_OK) {
7900Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: get_irq: _PRS failed");
7910Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/*
7950Sstevel@tonic-gate 	 * Scan the resources looking for an interrupt resource
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	*irqlistp = 0;
7980Sstevel@tonic-gate 	for (resp = rsb.Pointer; resp->Length != 0;
7990Sstevel@tonic-gate 	    resp = ACPI_NEXT_RESOURCE(resp)) {
8000Sstevel@tonic-gate 		switch (resp->Id) {
8010Sstevel@tonic-gate 		case ACPI_RSTYPE_IRQ:
8020Sstevel@tonic-gate 			irqlist_len = resp->Data.Irq.NumberOfInterrupts;
8030Sstevel@tonic-gate 			tmplist = resp->Data.Irq.Interrupts;
8040Sstevel@tonic-gate 			el = resp->Data.Irq.EdgeLevel;
8050Sstevel@tonic-gate 			po = resp->Data.Irq.ActiveHighLow;
8060Sstevel@tonic-gate 			break;
8070Sstevel@tonic-gate 		case ACPI_RSTYPE_EXT_IRQ:
8080Sstevel@tonic-gate 			irqlist_len = resp->Data.ExtendedIrq.NumberOfInterrupts;
8090Sstevel@tonic-gate 			tmplist = resp->Data.ExtendedIrq.Interrupts;
8100Sstevel@tonic-gate 			el = resp->Data.ExtendedIrq.EdgeLevel;
8110Sstevel@tonic-gate 			po = resp->Data.ExtendedIrq.ActiveHighLow;
8120Sstevel@tonic-gate 			break;
8130Sstevel@tonic-gate 		default:
8140Sstevel@tonic-gate 			continue;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		/* NEEDSWORK: move this into add_irqlist_entry someday */
8170Sstevel@tonic-gate 		irqlist = kmem_zalloc(irqlist_len * sizeof (*irqlist),
8180Sstevel@tonic-gate 					    KM_SLEEP);
8190Sstevel@tonic-gate 		for (i = 0; i < irqlist_len; i++)
8200Sstevel@tonic-gate 			irqlist[i] = tmplist[i];
8210Sstevel@tonic-gate 		intr_flags.intr_el = psm_acpi_edgelevel(el);
8220Sstevel@tonic-gate 		intr_flags.intr_po = psm_acpi_po(po);
8230Sstevel@tonic-gate 		acpi_add_irqlist_entry(irqlistp, irqlist, irqlist_len,
8240Sstevel@tonic-gate 				&intr_flags);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	AcpiOsFree(rsb.Pointer);
8280Sstevel@tonic-gate 	return (irqlistp == NULL ? ACPI_PSM_FAILURE : ACPI_PSM_SUCCESS);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate  * Adds a new cache entry to the irq cache which maps an irq and
8330Sstevel@tonic-gate  * its attributes to PCI bus/dev/ipin and optionally to its associated ACPI
8340Sstevel@tonic-gate  * interrupt link device object.
8350Sstevel@tonic-gate  */
8360Sstevel@tonic-gate void
8370Sstevel@tonic-gate acpi_new_irq_cache_ent(int bus, int dev, int ipin, int pci_irq,
8380Sstevel@tonic-gate     iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate 	int newsize;
8410Sstevel@tonic-gate 	irq_cache_t *new_arr, *ep;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
8440Sstevel@tonic-gate 	if (irq_cache_valid >= irq_cache_len) {
8450Sstevel@tonic-gate 		/* initially, or re-, allocate array */
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 		newsize = (irq_cache_len ?
8480Sstevel@tonic-gate 		    irq_cache_len * 2 : IRQ_CACHE_INITLEN);
8490Sstevel@tonic-gate 		new_arr = kmem_zalloc(newsize * sizeof (irq_cache_t), KM_SLEEP);
8500Sstevel@tonic-gate 		if (irq_cache_len != 0) {
8510Sstevel@tonic-gate 			/* realloc: copy data, free old */
8520Sstevel@tonic-gate 			bcopy(irq_cache_table, new_arr,
8530Sstevel@tonic-gate 			    irq_cache_len * sizeof (irq_cache_t));
8540Sstevel@tonic-gate 			kmem_free(irq_cache_table,
8550Sstevel@tonic-gate 			    irq_cache_len * sizeof (irq_cache_t));
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 		irq_cache_len = newsize;
8580Sstevel@tonic-gate 		irq_cache_table = new_arr;
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	ep = &irq_cache_table[irq_cache_valid++];
8610Sstevel@tonic-gate 	ep->bus = (uchar_t)bus;
8620Sstevel@tonic-gate 	ep->dev = (uchar_t)dev;
8630Sstevel@tonic-gate 	ep->ipin = (uchar_t)ipin;
8640Sstevel@tonic-gate 	ep->flags = *intr_flagp;
8650Sstevel@tonic-gate 	ep->irq = pci_irq;
8660Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
8670Sstevel@tonic-gate 	ep->lnkobj = acpipsmlnkp->lnkobj;
8680Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate /*
8730Sstevel@tonic-gate  * Searches the irq caches for the given bus/dev/ipin.
8740Sstevel@tonic-gate  *
8750Sstevel@tonic-gate  * If info is found, stores polarity and sensitivity in the structure
8760Sstevel@tonic-gate  * pointed to by intr_flagp, and irqno in the value pointed to by pci_irqp,
8770Sstevel@tonic-gate  * and returns ACPI_PSM_SUCCESS.
8780Sstevel@tonic-gate  * Otherwise, ACPI_PSM_FAILURE is returned.
8790Sstevel@tonic-gate  */
8800Sstevel@tonic-gate int
8810Sstevel@tonic-gate acpi_get_irq_cache_ent(uchar_t bus, uchar_t dev, int ipin,
8820Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	irq_cache_t *irqcachep;
8860Sstevel@tonic-gate 	int i;
8870Sstevel@tonic-gate 	int ret = ACPI_PSM_FAILURE;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
8900Sstevel@tonic-gate 	for (irqcachep = irq_cache_table, i = 0; i < irq_cache_valid;
8910Sstevel@tonic-gate 	    irqcachep++, i++)
8920Sstevel@tonic-gate 		if ((irqcachep->bus == bus) &&
8930Sstevel@tonic-gate 		    (irqcachep->dev == dev) &&
8940Sstevel@tonic-gate 		    (irqcachep->ipin == ipin)) {
8950Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL && intr_flagp != NULL);
8960Sstevel@tonic-gate 			*pci_irqp = irqcachep->irq;
8970Sstevel@tonic-gate 			*intr_flagp = irqcachep->flags;
8980Sstevel@tonic-gate 			ret = ACPI_PSM_SUCCESS;
8990Sstevel@tonic-gate 			break;
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
9030Sstevel@tonic-gate 	return (ret);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate  * Searches the irq caches for the given interrupt lnk device object.
9080Sstevel@tonic-gate  *
9090Sstevel@tonic-gate  * If info is found, stores polarity and sensitivity in the structure
9100Sstevel@tonic-gate  * pointed to by intr_flagp, and irqno in the value pointed to by pci_irqp,
9110Sstevel@tonic-gate  * and returns ACPI_PSM_SUCCESS.
9120Sstevel@tonic-gate  * Otherwise, ACPI_PSM_FAILURE is returned.
9130Sstevel@tonic-gate  */
9140Sstevel@tonic-gate int
9150Sstevel@tonic-gate acpi_get_irq_lnk_cache_ent(ACPI_HANDLE lnkobj, int *pci_irqp,
9160Sstevel@tonic-gate     iflag_t *intr_flagp)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	irq_cache_t *irqcachep;
9200Sstevel@tonic-gate 	int i;
9210Sstevel@tonic-gate 	int ret = ACPI_PSM_FAILURE;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if (lnkobj == NULL)
9240Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
9270Sstevel@tonic-gate 	for (irqcachep = irq_cache_table, i = 0; i < irq_cache_valid;
9280Sstevel@tonic-gate 	    irqcachep++, i++)
9290Sstevel@tonic-gate 		if (irqcachep->lnkobj == lnkobj) {
9300Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
9310Sstevel@tonic-gate 			*pci_irqp = irqcachep->irq;
9320Sstevel@tonic-gate 			ASSERT(intr_flagp != NULL);
9330Sstevel@tonic-gate 			*intr_flagp = irqcachep->flags;
9340Sstevel@tonic-gate 			ret = ACPI_PSM_SUCCESS;
9350Sstevel@tonic-gate 			break;
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
9380Sstevel@tonic-gate 	return (ret);
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate int
9420Sstevel@tonic-gate acpi_poweroff(void)
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	PSM_VERBOSE_POWEROFF(("acpi_poweroff: starting poweroff\n"));
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (AcpiEnterSleepStatePrep(5) != AE_OK)
9470Sstevel@tonic-gate 		return (1);
9480Sstevel@tonic-gate 	ACPI_DISABLE_IRQS();
9490Sstevel@tonic-gate 	if (AcpiEnterSleepState(5) != AE_OK) {
9500Sstevel@tonic-gate 		ACPI_ENABLE_IRQS();
9510Sstevel@tonic-gate 		return (1);
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 	ACPI_ENABLE_IRQS();
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/* we should be off; if we get here it's an error */
9560Sstevel@tonic-gate 	PSM_VERBOSE_POWEROFF(("acpi_poweroff: failed to actually power off\n"));
9570Sstevel@tonic-gate 	return (1);
9580Sstevel@tonic-gate }
959*347Smyers 
960*347Smyers 
961*347Smyers /*
962*347Smyers  * psm_set_elcr() sets ELCR bit for specified vector
963*347Smyers  */
964*347Smyers void
965*347Smyers psm_set_elcr(int vecno, int val)
966*347Smyers {
967*347Smyers 	int elcr_port = ELCR_PORT1 + (vecno >> 3);
968*347Smyers 	int elcr_bit = 1 << (vecno & 0x07);
969*347Smyers 
970*347Smyers 	ASSERT((vecno >= 0) && (vecno < 16));
971*347Smyers 
972*347Smyers 	if (val) {
973*347Smyers 		/* set bit to force level-triggered mode */
974*347Smyers 		outb(elcr_port, inb(elcr_port) | elcr_bit);
975*347Smyers 	} else {
976*347Smyers 		/* clear bit to force edge-triggered mode */
977*347Smyers 		outb(elcr_port, inb(elcr_port) & ~elcr_bit);
978*347Smyers 	}
979*347Smyers }
980*347Smyers 
981*347Smyers /*
982*347Smyers  * psm_get_elcr() returns status of ELCR bit for specific vector
983*347Smyers  */
984*347Smyers int
985*347Smyers psm_get_elcr(int vecno)
986*347Smyers {
987*347Smyers 	int elcr_port = ELCR_PORT1 + (vecno >> 3);
988*347Smyers 	int elcr_bit = 1 << (vecno & 0x07);
989*347Smyers 
990*347Smyers 	ASSERT((vecno >= 0) && (vecno < 16));
991*347Smyers 
992*347Smyers 	return ((inb(elcr_port) & elcr_bit) ? 1 : 0);
993*347Smyers }
994