xref: /onnv-gate/usr/src/uts/i86pc/io/psm/psm_common.c (revision 12159:8954df95a588)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51456Sdmick  * Common Development and Distribution License (the "License").
61456Sdmick  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12159SSusan.Scheufele@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/promif.h>
290Sstevel@tonic-gate #include <sys/acpi/acpi.h>
300Sstevel@tonic-gate #include <sys/acpica.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
340Sstevel@tonic-gate #include <sys/pci.h>
350Sstevel@tonic-gate #include <sys/debug.h>
360Sstevel@tonic-gate #include <sys/psm_common.h>
370Sstevel@tonic-gate #include <sys/sunndi.h>
380Sstevel@tonic-gate #include <sys/ksynch.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /* Global configurables */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate char *psm_module_name;	/* used to store name of psm module */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * acpi_irq_check_elcr: when set elcr will also be consulted for building
46347Smyers  * the reserved irq list.  When 0 (false), the existing state of the ELCR
47347Smyers  * is ignored when selecting a vector during IRQ translation, and the ELCR
48347Smyers  * is programmed to the proper setting for the type of bus (level-triggered
49347Smyers  * for PCI, edge-triggered for non-PCI).  When non-zero (true), vectors
50347Smyers  * set to edge-mode will not be used when in PIC-mode.  The default value
51347Smyers  * is 0 (false).  Note that ACPI's SCI vector is always set to conform to
52347Smyers  * ACPI-specification regardless of this.
53347Smyers  *
540Sstevel@tonic-gate  */
55347Smyers int acpi_irq_check_elcr = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int psm_verbose = 0;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	PSM_VERBOSE_IRQ(fmt)	\
600Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_IRQ_FLAG) \
610Sstevel@tonic-gate 			cmn_err fmt;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	PSM_VERBOSE_POWEROFF(fmt)  \
640Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_POWEROFF_FLAG || \
650Sstevel@tonic-gate 		    psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) \
660Sstevel@tonic-gate 			prom_printf fmt;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	PSM_VERBOSE_POWEROFF_PAUSE(fmt) \
690Sstevel@tonic-gate 		if (psm_verbose & PSM_VERBOSE_POWEROFF_FLAG || \
700Sstevel@tonic-gate 		    psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) {\
710Sstevel@tonic-gate 			prom_printf fmt; \
720Sstevel@tonic-gate 			if (psm_verbose & PSM_VERBOSE_POWEROFF_PAUSE_FLAG) \
730Sstevel@tonic-gate 				(void) goany(); \
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /* Local storage */
780Sstevel@tonic-gate static ACPI_HANDLE acpi_sbobj = NULL;
790Sstevel@tonic-gate static kmutex_t acpi_irq_cache_mutex;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * irq_cache_table is a list that serves a two-key cache. It is used
830Sstevel@tonic-gate  * as a pci busid/devid/ipin <-> irq cache and also as a acpi
840Sstevel@tonic-gate  * interrupt lnk <-> irq cache.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate static irq_cache_t *irq_cache_table;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	IRQ_CACHE_INITLEN	20
890Sstevel@tonic-gate static int irq_cache_len = 0;
900Sstevel@tonic-gate static int irq_cache_valid = 0;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static int acpi_get_gsiv(dev_info_t *dip, ACPI_HANDLE pciobj, int devno,
930Sstevel@tonic-gate 	int ipin, int *pci_irqp, iflag_t *iflagp,  acpi_psm_lnk_t *acpipsmlnkp);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int acpi_eval_lnk(dev_info_t *dip, char *lnkname,
960Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static int acpi_get_irq_lnk_cache_ent(ACPI_HANDLE lnkobj, int *pci_irqp,
990Sstevel@tonic-gate     iflag_t *intr_flagp);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate extern int goany(void);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #define	NEXT_PRT_ITEM(p)	\
1050Sstevel@tonic-gate 		(ACPI_PCI_ROUTING_TABLE *)(((char *)(p)) + (p)->Length)
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static int
1080Sstevel@tonic-gate acpi_get_gsiv(dev_info_t *dip, ACPI_HANDLE pciobj, int devno, int ipin,
1090Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	ACPI_BUFFER rb;
1120Sstevel@tonic-gate 	ACPI_PCI_ROUTING_TABLE *prtp;
1130Sstevel@tonic-gate 	int status;
1140Sstevel@tonic-gate 	int dev_adr;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * Get the IRQ routing table
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	rb.Pointer = NULL;
1200Sstevel@tonic-gate 	rb.Length = ACPI_ALLOCATE_BUFFER;
1210Sstevel@tonic-gate 	if (AcpiGetIrqRoutingTable(pciobj, &rb) != AE_OK) {
1220Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	status = ACPI_PSM_FAILURE;
1260Sstevel@tonic-gate 	dev_adr = (devno << 16 | 0xffff);
1270Sstevel@tonic-gate 	for (prtp = rb.Pointer; prtp->Length != 0; prtp = NEXT_PRT_ITEM(prtp)) {
1280Sstevel@tonic-gate 		/* look until a matching dev/pin is found */
1290Sstevel@tonic-gate 		if (dev_adr != prtp->Address || ipin != prtp->Pin)
1300Sstevel@tonic-gate 			continue;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		/* NULL Source name means index is GSIV */
1330Sstevel@tonic-gate 		if (*prtp->Source == 0) {
1347851SDana.Myers@Sun.COM 			intr_flagp->intr_el = INTR_EL_LEVEL;
1357851SDana.Myers@Sun.COM 			intr_flagp->intr_po = INTR_PO_ACTIVE_LOW;
1360Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
1370Sstevel@tonic-gate 			*pci_irqp = prtp->SourceIndex;
1380Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
1390Sstevel@tonic-gate 		} else
1400Sstevel@tonic-gate 			status = acpi_eval_lnk(dip, prtp->Source, pci_irqp,
1410Sstevel@tonic-gate 			    intr_flagp, acpipsmlnkp);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		break;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	AcpiOsFree(rb.Pointer);
1480Sstevel@tonic-gate 	return (status);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * If the interrupt link device is already configured,
1540Sstevel@tonic-gate  * stores polarity and sensitivity in the structure pointed to by
1550Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  * Returns:
1580Sstevel@tonic-gate  *	ACPI_PSM_SUCCESS if the interrupt link device is already configured.
1590Sstevel@tonic-gate  *	ACPI_PSM_PARTIAL if configuration is needed.
1600Sstevel@tonic-gate  * 	ACPI_PSM_FAILURE in case of error.
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  * When two devices share the same interrupt link device, and the
1630Sstevel@tonic-gate  * link device is already configured (i.e. found in the irq cache)
1640Sstevel@tonic-gate  * we need to use the already configured irq instead of reconfiguring
1650Sstevel@tonic-gate  * the link device.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate static int
1680Sstevel@tonic-gate acpi_eval_lnk(dev_info_t *dip, char *lnkname, int *pci_irqp,
1690Sstevel@tonic-gate iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	ACPI_HANDLE	tmpobj;
1720Sstevel@tonic-gate 	ACPI_HANDLE	lnkobj;
1730Sstevel@tonic-gate 	int status;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * Convert the passed-in link device name to a handle
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	if (AcpiGetHandle(NULL, lnkname, &lnkobj) != AE_OK) {
1790Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * Assume that the link device is invalid if no _CRS method
1840Sstevel@tonic-gate 	 * exists, since _CRS method is a required method
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	if (AcpiGetHandle(lnkobj, "_CRS", &tmpobj) != AE_OK) {
1870Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
1910Sstevel@tonic-gate 	acpipsmlnkp->lnkobj = lnkobj;
1920Sstevel@tonic-gate 	if ((acpi_get_irq_lnk_cache_ent(lnkobj, pci_irqp, intr_flagp)) ==
1930Sstevel@tonic-gate 	    ACPI_PSM_SUCCESS) {
1940Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_CONT, "!psm: link object found from cache "
1950Sstevel@tonic-gate 		    " for device %s, instance #%d, irq no %d\n",
1960Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip), *pci_irqp));
1970Sstevel@tonic-gate 		return (ACPI_PSM_SUCCESS);
1980Sstevel@tonic-gate 	} else {
1990Sstevel@tonic-gate 		if (acpica_eval_int(lnkobj, "_STA", &status) == AE_OK) {
2000Sstevel@tonic-gate 			acpipsmlnkp->device_status = (uchar_t)status;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		return (ACPI_PSM_PARTIAL);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate int
2080Sstevel@tonic-gate acpi_psm_init(char *module_name, int verbose_flags)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	psm_module_name = module_name;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	psm_verbose = verbose_flags;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (AcpiGetHandle(NULL, "\\_SB", &acpi_sbobj) != AE_OK) {
2150Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: get _SB failed");
2160Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	mutex_init(&acpi_irq_cache_mutex, NULL, MUTEX_DEFAULT, NULL);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	return (ACPI_PSM_SUCCESS);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Return bus/dev/fn for PCI dip (note: not the parent "pci" node).
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate int
2300Sstevel@tonic-gate get_bdf(dev_info_t *dip, int *bus, int *device, int *func)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
2330Sstevel@tonic-gate 	int len;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2360Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, (uint_t *)&len) != DDI_SUCCESS)
2370Sstevel@tonic-gate 		return (-1);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (len < (sizeof (pci_regspec_t) / sizeof (int))) {
2400Sstevel@tonic-gate 		ddi_prop_free(pci_rp);
2410Sstevel@tonic-gate 		return (-1);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	if (bus != NULL)
2440Sstevel@tonic-gate 		*bus = (int)PCI_REG_BUS_G(pci_rp->pci_phys_hi);
2450Sstevel@tonic-gate 	if (device != NULL)
2460Sstevel@tonic-gate 		*device = (int)PCI_REG_DEV_G(pci_rp->pci_phys_hi);
2470Sstevel@tonic-gate 	if (func != NULL)
2480Sstevel@tonic-gate 		*func = (int)PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
2490Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
2500Sstevel@tonic-gate 	return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * Build the reserved ISA irq list, and store it in the table pointed to by
2560Sstevel@tonic-gate  * reserved_irqs_table. The caller is responsible for allocating this table
2570Sstevel@tonic-gate  * with a minimum of MAX_ISA_IRQ + 1 entries.
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  * The routine looks in the device tree at the subtree rooted at /isa
2600Sstevel@tonic-gate  * for each of the devices under that node, if an interrupts property
2610Sstevel@tonic-gate  * is present, its values are used to "reserve" irqs so that later ACPI
2620Sstevel@tonic-gate  * configuration won't choose those irqs.
2630Sstevel@tonic-gate  *
2640Sstevel@tonic-gate  * In addition, if acpi_irq_check_elcr is set, will use ELCR register
2650Sstevel@tonic-gate  * to identify reserved IRQs.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate void
2680Sstevel@tonic-gate build_reserved_irqlist(uchar_t *reserved_irqs_table)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	dev_info_t *isanode = ddi_find_devinfo("isa", -1, 0);
2710Sstevel@tonic-gate 	dev_info_t *isa_child = 0;
2720Sstevel@tonic-gate 	int i;
2730Sstevel@tonic-gate 	uint_t	elcrval;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/* Initialize the reserved ISA IRQs: */
2761456Sdmick 	for (i = 0; i <= MAX_ISA_IRQ; i++)
2770Sstevel@tonic-gate 		reserved_irqs_table[i] = 0;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (acpi_irq_check_elcr) {
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		elcrval = (inb(ELCR_PORT2) << 8) | (inb(ELCR_PORT1));
2820Sstevel@tonic-gate 		if (ELCR_EDGE(elcrval, 0) && ELCR_EDGE(elcrval, 1) &&
2830Sstevel@tonic-gate 		    ELCR_EDGE(elcrval, 2) && ELCR_EDGE(elcrval, 8) &&
2840Sstevel@tonic-gate 		    ELCR_EDGE(elcrval, 13)) {
2850Sstevel@tonic-gate 			/* valid ELCR */
2861456Sdmick 			for (i = 0; i <= MAX_ISA_IRQ; i++)
2870Sstevel@tonic-gate 				if (!ELCR_LEVEL(elcrval, i))
2880Sstevel@tonic-gate 					reserved_irqs_table[i] = 1;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/* always check the isa devinfo nodes */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (isanode != 0) { /* Found ISA */
2950Sstevel@tonic-gate 		uint_t intcnt;		/* Interrupt count */
2960Sstevel@tonic-gate 		int *intrs;		/* Interrupt values */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		/* Load first child: */
2990Sstevel@tonic-gate 		isa_child = ddi_get_child(isanode);
3000Sstevel@tonic-gate 		while (isa_child != 0) { /* Iterate over /isa children */
3010Sstevel@tonic-gate 			/* if child has any interrupts, save them */
3020Sstevel@tonic-gate 			if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, isa_child,
3030Sstevel@tonic-gate 			    DDI_PROP_DONTPASS, "interrupts", &intrs, &intcnt)
3040Sstevel@tonic-gate 			    == DDI_PROP_SUCCESS) {
3050Sstevel@tonic-gate 				/*
3060Sstevel@tonic-gate 				 * iterate over child interrupt list, adding
3070Sstevel@tonic-gate 				 * them to the reserved irq list
3080Sstevel@tonic-gate 				 */
3090Sstevel@tonic-gate 				while (intcnt-- > 0) {
3100Sstevel@tonic-gate 					/*
3110Sstevel@tonic-gate 					 * Each value MUST be <= MAX_ISA_IRQ
3120Sstevel@tonic-gate 					 */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 					if ((intrs[intcnt] > MAX_ISA_IRQ) ||
3150Sstevel@tonic-gate 					    (intrs[intcnt] < 0))
3160Sstevel@tonic-gate 						continue;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 					reserved_irqs_table[intrs[intcnt]] = 1;
3190Sstevel@tonic-gate 				}
3200Sstevel@tonic-gate 				ddi_prop_free(intrs);
3210Sstevel@tonic-gate 			}
3220Sstevel@tonic-gate 			isa_child = ddi_get_next_sibling(isa_child);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		/* The isa node was held by ddi_find_devinfo, so release it */
3250Sstevel@tonic-gate 		ndi_rele_devi(isanode);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Reserve IRQ14 & IRQ15 for IDE.  It shouldn't be hard-coded
3300Sstevel@tonic-gate 	 * here but there's no other way to find the irqs for
3310Sstevel@tonic-gate 	 * legacy-mode ata (since it's hard-coded in pci-ide also).
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	reserved_irqs_table[14] = 1;
3340Sstevel@tonic-gate 	reserved_irqs_table[15] = 1;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * Examine devinfo node to determine if it is a PCI-PCI bridge
3390Sstevel@tonic-gate  *
3400Sstevel@tonic-gate  * Returns:
3410Sstevel@tonic-gate  *	0 if not a bridge or error
3420Sstevel@tonic-gate  *	1 if a bridge
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate static int
3450Sstevel@tonic-gate psm_is_pci_bridge(dev_info_t *dip)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	ddi_acc_handle_t cfg_handle;
3480Sstevel@tonic-gate 	int rv = 0;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_handle) == DDI_SUCCESS) {
3510Sstevel@tonic-gate 		rv = ((pci_config_get8(cfg_handle, PCI_CONF_BASCLASS) ==
3520Sstevel@tonic-gate 		    PCI_CLASS_BRIDGE) && (pci_config_get8(cfg_handle,
3530Sstevel@tonic-gate 		    PCI_CONF_SUBCLASS) == PCI_BRIDGE_PCI));
3540Sstevel@tonic-gate 		pci_config_teardown(&cfg_handle);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	return (rv);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Examines ACPI node for presence of _PRT object
362*12159SSusan.Scheufele@Sun.COM  * Check _STA to make sure node is present and/or enabled
3630Sstevel@tonic-gate  *
3640Sstevel@tonic-gate  * Returns:
3650Sstevel@tonic-gate  *	0 if no _PRT or error
3660Sstevel@tonic-gate  *	1 if _PRT is present
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate static int
3690Sstevel@tonic-gate psm_node_has_prt(ACPI_HANDLE *ah)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	ACPI_HANDLE rh;
372*12159SSusan.Scheufele@Sun.COM 	int sta;
373*12159SSusan.Scheufele@Sun.COM 
374*12159SSusan.Scheufele@Sun.COM 	/*
375*12159SSusan.Scheufele@Sun.COM 	 * Return 0 for "no _PRT" if device does not exist
376*12159SSusan.Scheufele@Sun.COM 	 * According to ACPI Spec,
377*12159SSusan.Scheufele@Sun.COM 	 * 1) setting either bit 0 or bit 3 means that device exists.
378*12159SSusan.Scheufele@Sun.COM 	 * 2) Absence of _STA method means all status bits set.
379*12159SSusan.Scheufele@Sun.COM 	 */
380*12159SSusan.Scheufele@Sun.COM 	if (ACPI_SUCCESS(acpica_eval_int(ah, "_STA", &sta)) &&
381*12159SSusan.Scheufele@Sun.COM 	    !(sta & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_FUNCTIONING)))
382*12159SSusan.Scheufele@Sun.COM 		return (0);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	return (AcpiGetHandle(ah, "_PRT", &rh) == AE_OK);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * Look first for an ACPI PCI bus node matching busid, then for a _PRT on the
3890Sstevel@tonic-gate  * parent node; then drop into the bridge-chasing code (which will also
3900Sstevel@tonic-gate  * look for _PRTs on the way up the tree of bridges)
3910Sstevel@tonic-gate  *
3920Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by
3930Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.  *
3940Sstevel@tonic-gate  * Returns:
3950Sstevel@tonic-gate  *  	ACPI_PSM_SUCCESS on success.
3960Sstevel@tonic-gate  *	ACPI_PSM_PARTIAL to indicate need to configure the interrupt
3970Sstevel@tonic-gate  *	link device.
3980Sstevel@tonic-gate  * 	ACPI_PSM_FAILURE  if an error prevented the system from
3990Sstevel@tonic-gate  *	obtaining irq information for dip.
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate int
4020Sstevel@tonic-gate acpi_translate_pci_irq(dev_info_t *dip, int ipin, int *pci_irqp,
4030Sstevel@tonic-gate     iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	ACPI_HANDLE pciobj;
4060Sstevel@tonic-gate 	int status = AE_ERROR;
4070Sstevel@tonic-gate 	dev_info_t *curdip, *parentdip;
4080Sstevel@tonic-gate 	int curpin, curbus, curdev;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	curpin = ipin;
4120Sstevel@tonic-gate 	curdip = dip;
4130Sstevel@tonic-gate 	while (curdip != ddi_root_node()) {
4140Sstevel@tonic-gate 		parentdip = ddi_get_parent(curdip);
4150Sstevel@tonic-gate 		ASSERT(parentdip != NULL);
4160Sstevel@tonic-gate 
4177851SDana.Myers@Sun.COM 		if (get_bdf(curdip, &curbus, &curdev, NULL) != 0)
4180Sstevel@tonic-gate 			break;
4190Sstevel@tonic-gate 
4204667Smh27603 		status = acpica_get_handle(parentdip, &pciobj);
4210Sstevel@tonic-gate 		if ((status == AE_OK) && psm_node_has_prt(pciobj)) {
4220Sstevel@tonic-gate 			return (acpi_get_gsiv(curdip, pciobj, curdev, curpin,
4230Sstevel@tonic-gate 			    pci_irqp, intr_flagp, acpipsmlnkp));
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		/* if we got here, we need to traverse a bridge upwards */
4270Sstevel@tonic-gate 		if (!psm_is_pci_bridge(parentdip))
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/*
4310Sstevel@tonic-gate 		 * This is the rotating scheme that Compaq is using
4320Sstevel@tonic-gate 		 * and documented in the PCI-PCI spec.  Also, if the
4330Sstevel@tonic-gate 		 * PCI-PCI bridge is behind another PCI-PCI bridge,
4340Sstevel@tonic-gate 		 * then it needs to keep ascending until an interrupt
4350Sstevel@tonic-gate 		 * entry is found or the top is reached
4360Sstevel@tonic-gate 		 */
4370Sstevel@tonic-gate 		curpin = (curdev + curpin) % PCI_INTD;
4380Sstevel@tonic-gate 		curdip = parentdip;
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/*
4420Sstevel@tonic-gate 	 * We should never, ever get here; didn't find a _PRT
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	return (ACPI_PSM_FAILURE);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate  * Sets the irq resource of the lnk object to the requested irq value.
4490Sstevel@tonic-gate  *
4500Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success, ACPI_PSM_FAILURE upon failure.
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate int
4530Sstevel@tonic-gate acpi_set_irq_resource(acpi_psm_lnk_t *acpipsmlnkp, int irq)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	ACPI_BUFFER	rsb;
4560Sstevel@tonic-gate 	ACPI_RESOURCE	*resp;
4570Sstevel@tonic-gate 	ACPI_RESOURCE	*srsp;
4580Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
459941Smyers 	int srs_len, status;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Fetch the possible resources for the link
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	rsb.Pointer = NULL;
4700Sstevel@tonic-gate 	rsb.Length = ACPI_ALLOCATE_BUFFER;
4710Sstevel@tonic-gate 	status = AcpiGetPossibleResources(lnkobj, &rsb);
4720Sstevel@tonic-gate 	if (status != AE_OK) {
4730Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: set_irq: _PRS failed");
4740Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * Find an IRQ resource descriptor to use as template
4790Sstevel@tonic-gate 	 */
4800Sstevel@tonic-gate 	srsp = NULL;
481941Smyers 	for (resp = rsb.Pointer; resp->Type != ACPI_RESOURCE_TYPE_END_TAG;
4820Sstevel@tonic-gate 	    resp = ACPI_NEXT_RESOURCE(resp)) {
483941Smyers 		if ((resp->Type == ACPI_RESOURCE_TYPE_IRQ) ||
484941Smyers 		    (resp->Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ)) {
485941Smyers 			ACPI_RESOURCE *endtag;
4860Sstevel@tonic-gate 			/*
487941Smyers 			 * Allocate enough room for this resource entry
488941Smyers 			 * and one end tag following it
4890Sstevel@tonic-gate 			 */
490941Smyers 			srs_len = resp->Length + sizeof (*endtag);
491941Smyers 			srsp = kmem_zalloc(srs_len, KM_SLEEP);
492941Smyers 			bcopy(resp, srsp, resp->Length);
493941Smyers 			endtag = ACPI_NEXT_RESOURCE(srsp);
494941Smyers 			endtag->Type = ACPI_RESOURCE_TYPE_END_TAG;
495941Smyers 			endtag->Length = 0;
4960Sstevel@tonic-gate 			break;	/* drop out of the loop */
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * We're done with the PRS values, toss 'em lest we forget
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	AcpiOsFree(rsb.Pointer);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (srsp == NULL)
5060Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * The Interrupts[] array is always at least one entry
5100Sstevel@tonic-gate 	 * long; see the definition of ACPI_RESOURCE.
5110Sstevel@tonic-gate 	 */
512941Smyers 	switch (srsp->Type) {
513941Smyers 	case ACPI_RESOURCE_TYPE_IRQ:
514941Smyers 		srsp->Data.Irq.InterruptCount = 1;
5150Sstevel@tonic-gate 		srsp->Data.Irq.Interrupts[0] = irq;
5160Sstevel@tonic-gate 		break;
517941Smyers 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
518941Smyers 		srsp->Data.ExtendedIrq.InterruptCount = 1;
5190Sstevel@tonic-gate 		srsp->Data.ExtendedIrq.Interrupts[0] = irq;
5200Sstevel@tonic-gate 		break;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	rsb.Pointer = srsp;
524941Smyers 	rsb.Length = srs_len;
5250Sstevel@tonic-gate 	status = AcpiSetCurrentResources(lnkobj, &rsb);
526941Smyers 	kmem_free(srsp, srs_len);
5270Sstevel@tonic-gate 	if (status != AE_OK) {
5280Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: set_irq: _SRS failed");
5290Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (acpica_eval_int(lnkobj, "_STA", &status) == AE_OK) {
5330Sstevel@tonic-gate 		acpipsmlnkp->device_status = (uchar_t)status;
5340Sstevel@tonic-gate 		return (ACPI_PSM_SUCCESS);
5350Sstevel@tonic-gate 	} else
5360Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate static int
5440Sstevel@tonic-gate psm_acpi_edgelevel(UINT32 el)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	switch (el) {
5470Sstevel@tonic-gate 	case ACPI_EDGE_SENSITIVE:
5480Sstevel@tonic-gate 		return (INTR_EL_EDGE);
5490Sstevel@tonic-gate 	case ACPI_LEVEL_SENSITIVE:
5500Sstevel@tonic-gate 		return (INTR_EL_LEVEL);
5510Sstevel@tonic-gate 	default:
5520Sstevel@tonic-gate 		/* el is a single bit; should never reach here */
5530Sstevel@tonic-gate 		return (INTR_EL_CONFORM);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate  *
5600Sstevel@tonic-gate  */
5610Sstevel@tonic-gate static int
5620Sstevel@tonic-gate psm_acpi_po(UINT32 po)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	switch (po) {
5650Sstevel@tonic-gate 	case ACPI_ACTIVE_HIGH:
5660Sstevel@tonic-gate 		return (INTR_PO_ACTIVE_HIGH);
5670Sstevel@tonic-gate 	case ACPI_ACTIVE_LOW:
5680Sstevel@tonic-gate 		return (INTR_PO_ACTIVE_LOW);
5690Sstevel@tonic-gate 	default:
5700Sstevel@tonic-gate 		/* po is a single bit; should never reach here */
5710Sstevel@tonic-gate 		return (INTR_PO_CONFORM);
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate  * Retrieves the current irq setting for the interrrupt link device.
5780Sstevel@tonic-gate  *
5790Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by
5800Sstevel@tonic-gate  * intr_flagp, and irqno in the value pointed to by pci_irqp.
5810Sstevel@tonic-gate  *
5820Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success, ACPI_PSM_FAILURE upon failure.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate int
5850Sstevel@tonic-gate acpi_get_current_irq_resource(acpi_psm_lnk_t *acpipsmlnkp, int *pci_irqp,
5860Sstevel@tonic-gate     iflag_t *intr_flagp)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
5890Sstevel@tonic-gate 	ACPI_BUFFER rb;
5900Sstevel@tonic-gate 	ACPI_RESOURCE *rp;
5910Sstevel@tonic-gate 	int irq;
5920Sstevel@tonic-gate 	int status = ACPI_PSM_FAILURE;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
5950Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (!(acpipsmlnkp->device_status & STA_PRESENT) ||
5980Sstevel@tonic-gate 	    !(acpipsmlnkp->device_status & STA_ENABLE)) {
5990Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_WARN, "!psm: crs device either not "
6000Sstevel@tonic-gate 		    "present or disabled, status 0x%x",
6010Sstevel@tonic-gate 		    acpipsmlnkp->device_status));
6020Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	rb.Pointer = NULL;
6060Sstevel@tonic-gate 	rb.Length = ACPI_ALLOCATE_BUFFER;
6070Sstevel@tonic-gate 	if (AcpiGetCurrentResources(lnkobj, &rb) != AE_OK) {
6080Sstevel@tonic-gate 		PSM_VERBOSE_IRQ((CE_WARN, "!psm: no crs object found or"
6090Sstevel@tonic-gate 		" evaluation failed"));
6100Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	irq = -1;
614941Smyers 	for (rp = rb.Pointer; rp->Type != ACPI_RESOURCE_TYPE_END_TAG;
615941Smyers 	    rp = ACPI_NEXT_RESOURCE(rp)) {
616941Smyers 		if (rp->Type == ACPI_RESOURCE_TYPE_IRQ) {
6170Sstevel@tonic-gate 			if (irq > 0) {
6180Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: multiple IRQ"
6190Sstevel@tonic-gate 				" from _CRS "));
6200Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6210Sstevel@tonic-gate 				break;
6220Sstevel@tonic-gate 			}
6230Sstevel@tonic-gate 
624941Smyers 			if (rp->Data.Irq.InterruptCount != 1) {
6250Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: <>1 interrupt"
6260Sstevel@tonic-gate 				" from _CRS "));
6270Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6280Sstevel@tonic-gate 				break;
6290Sstevel@tonic-gate 			}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 			intr_flagp->intr_el = psm_acpi_edgelevel(
6325295Srandyf 			    rp->Data.Irq.Triggering);
6330Sstevel@tonic-gate 			intr_flagp->intr_po = psm_acpi_po(
6345295Srandyf 			    rp->Data.Irq.Polarity);
6350Sstevel@tonic-gate 			irq = rp->Data.Irq.Interrupts[0];
6360Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
637941Smyers 		} else if (rp->Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
6380Sstevel@tonic-gate 			if (irq > 0) {
6390Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: multiple IRQ"
6400Sstevel@tonic-gate 				" from _CRS "));
6410Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6420Sstevel@tonic-gate 				break;
6430Sstevel@tonic-gate 			}
6440Sstevel@tonic-gate 
645941Smyers 			if (rp->Data.ExtendedIrq.InterruptCount != 1) {
6460Sstevel@tonic-gate 				PSM_VERBOSE_IRQ((CE_WARN, "!psm: <>1 interrupt"
6470Sstevel@tonic-gate 				" from _CRS "));
6480Sstevel@tonic-gate 				status = ACPI_PSM_FAILURE;
6490Sstevel@tonic-gate 				break;
6500Sstevel@tonic-gate 			}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 			intr_flagp->intr_el = psm_acpi_edgelevel(
6535295Srandyf 			    rp->Data.ExtendedIrq.Triggering);
6540Sstevel@tonic-gate 			intr_flagp->intr_po = psm_acpi_po(
6555295Srandyf 			    rp->Data.ExtendedIrq.Polarity);
6560Sstevel@tonic-gate 			irq = rp->Data.ExtendedIrq.Interrupts[0];
6570Sstevel@tonic-gate 			status = ACPI_PSM_SUCCESS;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	AcpiOsFree(rb.Pointer);
6620Sstevel@tonic-gate 	if (status == ACPI_PSM_SUCCESS) {
6630Sstevel@tonic-gate 		*pci_irqp =  irq;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	return (status);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate  * Searches for the given IRQ in the irqlist passed in.
6710Sstevel@tonic-gate  *
6720Sstevel@tonic-gate  * If multiple matches exist, this returns true on the first match.
6730Sstevel@tonic-gate  * Returns the interrupt flags, if a match was found, in `intr_flagp' if
6740Sstevel@tonic-gate  * it's passed in non-NULL
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate int
6770Sstevel@tonic-gate acpi_irqlist_find_irq(acpi_irqlist_t *irqlistp, int irq, iflag_t *intr_flagp)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	int found = 0;
6800Sstevel@tonic-gate 	int i;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	while (irqlistp != NULL && !found) {
6830Sstevel@tonic-gate 		for (i = 0; i < irqlistp->num_irqs; i++) {
6840Sstevel@tonic-gate 			if (irqlistp->irqs[i] == irq) {
6850Sstevel@tonic-gate 				if (intr_flagp)
6860Sstevel@tonic-gate 					*intr_flagp = irqlistp->intr_flags;
6870Sstevel@tonic-gate 				found = 1;
6880Sstevel@tonic-gate 				break;	/* out of for() */
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	return (found ? ACPI_PSM_SUCCESS : ACPI_PSM_FAILURE);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate  * Frees the irqlist allocated by acpi_get_possible_irq_resource.
6980Sstevel@tonic-gate  * It takes a count of number of entries in the list.
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate void
7010Sstevel@tonic-gate acpi_free_irqlist(acpi_irqlist_t *irqlistp)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	acpi_irqlist_t *freednode;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	while (irqlistp != NULL) {
7060Sstevel@tonic-gate 		/* Free the irq list */
7070Sstevel@tonic-gate 		kmem_free(irqlistp->irqs, irqlistp->num_irqs *
7080Sstevel@tonic-gate 		    sizeof (int32_t));
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		freednode = irqlistp;
7110Sstevel@tonic-gate 		irqlistp = irqlistp->next;
7120Sstevel@tonic-gate 		kmem_free(freednode, sizeof (acpi_irqlist_t));
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate  * Creates a new entry in the given irqlist with the information passed in.
7180Sstevel@tonic-gate  */
7190Sstevel@tonic-gate static void
7200Sstevel@tonic-gate acpi_add_irqlist_entry(acpi_irqlist_t **irqlistp, uint32_t *irqlist,
7210Sstevel@tonic-gate     int irqlist_len, iflag_t *intr_flagp)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	acpi_irqlist_t *newent;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	ASSERT(irqlist != NULL);
7260Sstevel@tonic-gate 	ASSERT(intr_flagp != NULL);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	newent = kmem_alloc(sizeof (acpi_irqlist_t), KM_SLEEP);
7290Sstevel@tonic-gate 	newent->intr_flags = *intr_flagp;
7300Sstevel@tonic-gate 	newent->irqs = irqlist;
7310Sstevel@tonic-gate 	newent->num_irqs = irqlist_len;
7320Sstevel@tonic-gate 	newent->next = *irqlistp;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	*irqlistp = newent;
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate  * Retrieves a list of possible interrupt settings for the interrupt link
7400Sstevel@tonic-gate  * device.
7410Sstevel@tonic-gate  *
7420Sstevel@tonic-gate  * Stores polarity and sensitivity in the structure pointed to by intr_flagp.
7430Sstevel@tonic-gate  * Updates value pointed to by irqlistp with the address of a table it
7440Sstevel@tonic-gate  * allocates. where interrupt numbers are stored. Stores the number of entries
7450Sstevel@tonic-gate  * in this table in the value pointed to by num_entriesp;
7460Sstevel@tonic-gate  *
7470Sstevel@tonic-gate  * Each element in this table is of type int32_t. The table should be later
7480Sstevel@tonic-gate  * freed by caller via acpi_free_irq_list().
7490Sstevel@tonic-gate  *
7500Sstevel@tonic-gate  * Returns ACPI_PSM_SUCCESS on success and ACPI_PSM_FAILURE upon failure
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate int
7530Sstevel@tonic-gate acpi_get_possible_irq_resources(acpi_psm_lnk_t *acpipsmlnkp,
7540Sstevel@tonic-gate     acpi_irqlist_t **irqlistp)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	ACPI_HANDLE lnkobj;
7570Sstevel@tonic-gate 	ACPI_BUFFER rsb;
7580Sstevel@tonic-gate 	ACPI_RESOURCE *resp;
7590Sstevel@tonic-gate 	int status;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	int i, el, po, irqlist_len;
7621560Smyers 	uint32_t *irqlist;
7631560Smyers 	void *tmplist;
7640Sstevel@tonic-gate 	iflag_t intr_flags;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
7670Sstevel@tonic-gate 	lnkobj = acpipsmlnkp->lnkobj;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	rsb.Pointer = NULL;
7700Sstevel@tonic-gate 	rsb.Length = ACPI_ALLOCATE_BUFFER;
7710Sstevel@tonic-gate 	status = AcpiGetPossibleResources(lnkobj, &rsb);
7720Sstevel@tonic-gate 	if (status != AE_OK) {
7730Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm: get_irq: _PRS failed");
7740Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/*
7780Sstevel@tonic-gate 	 * Scan the resources looking for an interrupt resource
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	*irqlistp = 0;
781941Smyers 	for (resp = rsb.Pointer; resp->Type != ACPI_RESOURCE_TYPE_END_TAG;
7820Sstevel@tonic-gate 	    resp = ACPI_NEXT_RESOURCE(resp)) {
783941Smyers 		switch (resp->Type) {
784941Smyers 		case ACPI_RESOURCE_TYPE_IRQ:
785941Smyers 			irqlist_len = resp->Data.Irq.InterruptCount;
7860Sstevel@tonic-gate 			tmplist = resp->Data.Irq.Interrupts;
787941Smyers 			el = resp->Data.Irq.Triggering;
788941Smyers 			po = resp->Data.Irq.Polarity;
7890Sstevel@tonic-gate 			break;
790941Smyers 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
791941Smyers 			irqlist_len = resp->Data.ExtendedIrq.InterruptCount;
7920Sstevel@tonic-gate 			tmplist = resp->Data.ExtendedIrq.Interrupts;
793941Smyers 			el = resp->Data.ExtendedIrq.Triggering;
794941Smyers 			po = resp->Data.ExtendedIrq.Polarity;
7950Sstevel@tonic-gate 			break;
7960Sstevel@tonic-gate 		default:
7970Sstevel@tonic-gate 			continue;
7980Sstevel@tonic-gate 		}
7991560Smyers 
8001560Smyers 		if (resp->Type != ACPI_RESOURCE_TYPE_IRQ &&
8011560Smyers 		    resp->Type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
8021560Smyers 			cmn_err(CE_WARN, "!psm: get_irq: no IRQ resource");
8031560Smyers 			return (ACPI_PSM_FAILURE);
8041560Smyers 		}
8051560Smyers 
8060Sstevel@tonic-gate 		/* NEEDSWORK: move this into add_irqlist_entry someday */
8070Sstevel@tonic-gate 		irqlist = kmem_zalloc(irqlist_len * sizeof (*irqlist),
8085295Srandyf 		    KM_SLEEP);
8090Sstevel@tonic-gate 		for (i = 0; i < irqlist_len; i++)
8101560Smyers 			if (resp->Type == ACPI_RESOURCE_TYPE_IRQ)
8111560Smyers 				irqlist[i] = ((uint8_t *)tmplist)[i];
8121560Smyers 			else
8131560Smyers 				irqlist[i] = ((uint32_t *)tmplist)[i];
8140Sstevel@tonic-gate 		intr_flags.intr_el = psm_acpi_edgelevel(el);
8150Sstevel@tonic-gate 		intr_flags.intr_po = psm_acpi_po(po);
8160Sstevel@tonic-gate 		acpi_add_irqlist_entry(irqlistp, irqlist, irqlist_len,
8175295Srandyf 		    &intr_flags);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	AcpiOsFree(rsb.Pointer);
8210Sstevel@tonic-gate 	return (irqlistp == NULL ? ACPI_PSM_FAILURE : ACPI_PSM_SUCCESS);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * Adds a new cache entry to the irq cache which maps an irq and
8260Sstevel@tonic-gate  * its attributes to PCI bus/dev/ipin and optionally to its associated ACPI
8270Sstevel@tonic-gate  * interrupt link device object.
8280Sstevel@tonic-gate  */
8290Sstevel@tonic-gate void
8300Sstevel@tonic-gate acpi_new_irq_cache_ent(int bus, int dev, int ipin, int pci_irq,
8310Sstevel@tonic-gate     iflag_t *intr_flagp, acpi_psm_lnk_t *acpipsmlnkp)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	int newsize;
8340Sstevel@tonic-gate 	irq_cache_t *new_arr, *ep;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
8370Sstevel@tonic-gate 	if (irq_cache_valid >= irq_cache_len) {
8380Sstevel@tonic-gate 		/* initially, or re-, allocate array */
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		newsize = (irq_cache_len ?
8410Sstevel@tonic-gate 		    irq_cache_len * 2 : IRQ_CACHE_INITLEN);
8420Sstevel@tonic-gate 		new_arr = kmem_zalloc(newsize * sizeof (irq_cache_t), KM_SLEEP);
8430Sstevel@tonic-gate 		if (irq_cache_len != 0) {
8440Sstevel@tonic-gate 			/* realloc: copy data, free old */
8450Sstevel@tonic-gate 			bcopy(irq_cache_table, new_arr,
8460Sstevel@tonic-gate 			    irq_cache_len * sizeof (irq_cache_t));
8470Sstevel@tonic-gate 			kmem_free(irq_cache_table,
8480Sstevel@tonic-gate 			    irq_cache_len * sizeof (irq_cache_t));
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 		irq_cache_len = newsize;
8510Sstevel@tonic-gate 		irq_cache_table = new_arr;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 	ep = &irq_cache_table[irq_cache_valid++];
8540Sstevel@tonic-gate 	ep->bus = (uchar_t)bus;
8550Sstevel@tonic-gate 	ep->dev = (uchar_t)dev;
8560Sstevel@tonic-gate 	ep->ipin = (uchar_t)ipin;
8570Sstevel@tonic-gate 	ep->flags = *intr_flagp;
8580Sstevel@tonic-gate 	ep->irq = pci_irq;
8590Sstevel@tonic-gate 	ASSERT(acpipsmlnkp != NULL);
8600Sstevel@tonic-gate 	ep->lnkobj = acpipsmlnkp->lnkobj;
8610Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * Searches the irq caches for the given bus/dev/ipin.
8670Sstevel@tonic-gate  *
8680Sstevel@tonic-gate  * If info is found, stores polarity and sensitivity in the structure
8690Sstevel@tonic-gate  * pointed to by intr_flagp, and irqno in the value pointed to by pci_irqp,
8700Sstevel@tonic-gate  * and returns ACPI_PSM_SUCCESS.
8710Sstevel@tonic-gate  * Otherwise, ACPI_PSM_FAILURE is returned.
8720Sstevel@tonic-gate  */
8730Sstevel@tonic-gate int
8740Sstevel@tonic-gate acpi_get_irq_cache_ent(uchar_t bus, uchar_t dev, int ipin,
8750Sstevel@tonic-gate     int *pci_irqp, iflag_t *intr_flagp)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	irq_cache_t *irqcachep;
8790Sstevel@tonic-gate 	int i;
8800Sstevel@tonic-gate 	int ret = ACPI_PSM_FAILURE;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
8830Sstevel@tonic-gate 	for (irqcachep = irq_cache_table, i = 0; i < irq_cache_valid;
8840Sstevel@tonic-gate 	    irqcachep++, i++)
8850Sstevel@tonic-gate 		if ((irqcachep->bus == bus) &&
8860Sstevel@tonic-gate 		    (irqcachep->dev == dev) &&
8870Sstevel@tonic-gate 		    (irqcachep->ipin == ipin)) {
8880Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL && intr_flagp != NULL);
8890Sstevel@tonic-gate 			*pci_irqp = irqcachep->irq;
8900Sstevel@tonic-gate 			*intr_flagp = irqcachep->flags;
8910Sstevel@tonic-gate 			ret = ACPI_PSM_SUCCESS;
8920Sstevel@tonic-gate 			break;
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
8960Sstevel@tonic-gate 	return (ret);
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate /*
9000Sstevel@tonic-gate  * Searches the irq caches for the given interrupt lnk device object.
9010Sstevel@tonic-gate  *
9020Sstevel@tonic-gate  * If info is found, stores polarity and sensitivity in the structure
9030Sstevel@tonic-gate  * pointed to by intr_flagp, and irqno in the value pointed to by pci_irqp,
9040Sstevel@tonic-gate  * and returns ACPI_PSM_SUCCESS.
9050Sstevel@tonic-gate  * Otherwise, ACPI_PSM_FAILURE is returned.
9060Sstevel@tonic-gate  */
9070Sstevel@tonic-gate int
9080Sstevel@tonic-gate acpi_get_irq_lnk_cache_ent(ACPI_HANDLE lnkobj, int *pci_irqp,
9090Sstevel@tonic-gate     iflag_t *intr_flagp)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	irq_cache_t *irqcachep;
9130Sstevel@tonic-gate 	int i;
9140Sstevel@tonic-gate 	int ret = ACPI_PSM_FAILURE;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if (lnkobj == NULL)
9170Sstevel@tonic-gate 		return (ACPI_PSM_FAILURE);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	mutex_enter(&acpi_irq_cache_mutex);
9200Sstevel@tonic-gate 	for (irqcachep = irq_cache_table, i = 0; i < irq_cache_valid;
9210Sstevel@tonic-gate 	    irqcachep++, i++)
9220Sstevel@tonic-gate 		if (irqcachep->lnkobj == lnkobj) {
9230Sstevel@tonic-gate 			ASSERT(pci_irqp != NULL);
9240Sstevel@tonic-gate 			*pci_irqp = irqcachep->irq;
9250Sstevel@tonic-gate 			ASSERT(intr_flagp != NULL);
9260Sstevel@tonic-gate 			*intr_flagp = irqcachep->flags;
9270Sstevel@tonic-gate 			ret = ACPI_PSM_SUCCESS;
9280Sstevel@tonic-gate 			break;
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 	mutex_exit(&acpi_irq_cache_mutex);
9310Sstevel@tonic-gate 	return (ret);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate 
9345295Srandyf /*
9355295Srandyf  * Walk the irq_cache_table and re-configure the link device to
9365295Srandyf  * the saved state.
9375295Srandyf  */
9385295Srandyf void
9395295Srandyf acpi_restore_link_devices(void)
9405295Srandyf {
9415295Srandyf 	irq_cache_t *irqcachep;
9425295Srandyf 	acpi_psm_lnk_t psmlnk;
9435295Srandyf 	int i, status;
9445295Srandyf 
9455295Srandyf 	/* XXX: may not need to hold this mutex */
9465295Srandyf 	mutex_enter(&acpi_irq_cache_mutex);
9475295Srandyf 	for (irqcachep = irq_cache_table, i = 0; i < irq_cache_valid;
9485295Srandyf 	    irqcachep++, i++) {
9496210Sgs150176 		if (irqcachep->lnkobj != NULL) {
9506210Sgs150176 			/* only field used from psmlnk in set_irq is lnkobj */
9516210Sgs150176 			psmlnk.lnkobj = irqcachep->lnkobj;
9526210Sgs150176 			status = acpi_set_irq_resource(&psmlnk, irqcachep->irq);
9536210Sgs150176 			/* warn if set_irq failed; soldier on */
9546210Sgs150176 			if (status != ACPI_PSM_SUCCESS)
9556210Sgs150176 				cmn_err(CE_WARN, "Could not restore interrupt "
9566210Sgs150176 				    "link device for IRQ 0x%x: Devices using "
9576210Sgs150176 				    "this IRQ may no longer function properly."
9586210Sgs150176 				    "\n", irqcachep->irq);
9596210Sgs150176 		}
9605295Srandyf 	}
9615295Srandyf 	mutex_exit(&acpi_irq_cache_mutex);
9625295Srandyf }
9635295Srandyf 
9640Sstevel@tonic-gate int
9650Sstevel@tonic-gate acpi_poweroff(void)
9660Sstevel@tonic-gate {
9678122SKerry.Shu@Sun.COM 	extern int acpica_use_safe_delay;
9686678Sjj204856 	ACPI_STATUS status;
9692025Smyers 
9700Sstevel@tonic-gate 	PSM_VERBOSE_POWEROFF(("acpi_poweroff: starting poweroff\n"));
9710Sstevel@tonic-gate 
9728122SKerry.Shu@Sun.COM 	acpica_use_safe_delay = 1;
9732025Smyers 
9746678Sjj204856 	status = AcpiEnterSleepStatePrep(5);
9756678Sjj204856 	if (status != AE_OK) {
9766678Sjj204856 		PSM_VERBOSE_POWEROFF(("acpi_poweroff: failed to prepare for "
9776678Sjj204856 		    "poweroff, status=0x%x\n", status));
9780Sstevel@tonic-gate 		return (1);
9790Sstevel@tonic-gate 	}
9806678Sjj204856 	ACPI_DISABLE_IRQS();
9816678Sjj204856 	status = AcpiEnterSleepState(5);
9820Sstevel@tonic-gate 	ACPI_ENABLE_IRQS();
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/* we should be off; if we get here it's an error */
9856678Sjj204856 	PSM_VERBOSE_POWEROFF(("acpi_poweroff: failed to actually power "
9866678Sjj204856 	    "off, status=0x%x\n", status));
9870Sstevel@tonic-gate 	return (1);
9880Sstevel@tonic-gate }
989347Smyers 
990347Smyers 
991347Smyers /*
992347Smyers  * psm_set_elcr() sets ELCR bit for specified vector
993347Smyers  */
994347Smyers void
995347Smyers psm_set_elcr(int vecno, int val)
996347Smyers {
997347Smyers 	int elcr_port = ELCR_PORT1 + (vecno >> 3);
998347Smyers 	int elcr_bit = 1 << (vecno & 0x07);
999347Smyers 
1000347Smyers 	ASSERT((vecno >= 0) && (vecno < 16));
1001347Smyers 
1002347Smyers 	if (val) {
1003347Smyers 		/* set bit to force level-triggered mode */
1004347Smyers 		outb(elcr_port, inb(elcr_port) | elcr_bit);
1005347Smyers 	} else {
1006347Smyers 		/* clear bit to force edge-triggered mode */
1007347Smyers 		outb(elcr_port, inb(elcr_port) & ~elcr_bit);
1008347Smyers 	}
1009347Smyers }
1010347Smyers 
1011347Smyers /*
1012347Smyers  * psm_get_elcr() returns status of ELCR bit for specific vector
1013347Smyers  */
1014347Smyers int
1015347Smyers psm_get_elcr(int vecno)
1016347Smyers {
1017347Smyers 	int elcr_port = ELCR_PORT1 + (vecno >> 3);
1018347Smyers 	int elcr_bit = 1 << (vecno & 0x07);
1019347Smyers 
1020347Smyers 	ASSERT((vecno >= 0) && (vecno < 16));
1021347Smyers 
1022347Smyers 	return ((inb(elcr_port) & elcr_bit) ? 1 : 0);
1023347Smyers }
1024