17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d12abe7cSanish  * Common Development and Distribution License (the "License").
6d12abe7cSanish  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225febcb4aSScott Carter, SD IOSW  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/note.h>
277c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
357c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
377c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>	/* include prototypes */
387c478bd9Sstevel@tonic-gate 
39ef643aefSegillett extern uint_t		ddi_msix_alloc_limit;
40ef643aefSegillett 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * New DDI interrupt framework
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate void
457c478bd9Sstevel@tonic-gate i_ddi_intr_devi_init(dev_info_t *dip)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	int	supported_types;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_init: dip %p\n",
507c478bd9Sstevel@tonic-gate 	    (void *)dip));
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_intr_p)
537c478bd9Sstevel@tonic-gate 		return;
547c478bd9Sstevel@tonic-gate 
555febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_intr_p = kmem_zalloc(sizeof (devinfo_intr_t), KM_SLEEP);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	supported_types = i_ddi_intr_get_supported_types(dip);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/* Save supported interrupt types information */
607c478bd9Sstevel@tonic-gate 	i_ddi_intr_set_supported_types(dip, supported_types);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate void
647c478bd9Sstevel@tonic-gate i_ddi_intr_devi_fini(dev_info_t *dip)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
677c478bd9Sstevel@tonic-gate 
68a195726fSgovinda 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_fini: dip %p\n",
69a195726fSgovinda 	    (void *)dip));
70a195726fSgovinda 
715febcb4aSScott Carter, SD IOSW 	if ((intr_p == NULL) || i_ddi_intr_get_current_nintrs(dip))
72a195726fSgovinda 		return;
73a195726fSgovinda 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * devi_intr_handle_p will only be used for devices
767c478bd9Sstevel@tonic-gate 	 * which are using the legacy DDI Interrupt interfaces.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if (intr_p->devi_intr_handle_p) {
797c478bd9Sstevel@tonic-gate 		/* nintrs could be zero; so check for it first */
807c478bd9Sstevel@tonic-gate 		if (intr_p->devi_intr_sup_nintrs) {
817c478bd9Sstevel@tonic-gate 			kmem_free(intr_p->devi_intr_handle_p,
827c478bd9Sstevel@tonic-gate 			    intr_p->devi_intr_sup_nintrs *
837c478bd9Sstevel@tonic-gate 			    sizeof (ddi_intr_handle_t));
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 	}
86a195726fSgovinda 
875febcb4aSScott Carter, SD IOSW 	/*
885febcb4aSScott Carter, SD IOSW 	 * devi_irm_req_p will only be used for devices which
895febcb4aSScott Carter, SD IOSW 	 * are mapped to an Interrupt Resource Management pool.
905febcb4aSScott Carter, SD IOSW 	 */
915febcb4aSScott Carter, SD IOSW 	if (intr_p->devi_irm_req_p)
925febcb4aSScott Carter, SD IOSW 		(void) i_ddi_irm_remove(dip);
935febcb4aSScott Carter, SD IOSW 
947c478bd9Sstevel@tonic-gate 	kmem_free(DEVI(dip)->devi_intr_p, sizeof (devinfo_intr_t));
957c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_intr_p = NULL;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate uint_t
997c478bd9Sstevel@tonic-gate i_ddi_intr_get_supported_types(dev_info_t *dip)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1027c478bd9Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1037c478bd9Sstevel@tonic-gate 	int			ret, intr_types;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((intr_p) && (intr_p->devi_intr_sup_types))
1067c478bd9Sstevel@tonic-gate 		return (intr_p->devi_intr_sup_types);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1097c478bd9Sstevel@tonic-gate 	hdl.ih_dip = dip;
1107c478bd9Sstevel@tonic-gate 
111a195726fSgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
1127c478bd9Sstevel@tonic-gate 	    (void *)&intr_types);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return ((ret == DDI_SUCCESS) ? intr_types : 0);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by i_ddi_dev_init().
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate void
1217c478bd9Sstevel@tonic-gate i_ddi_intr_set_supported_types(dev_info_t *dip, int intr_types)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (intr_p)
1267c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_sup_types = intr_types;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate uint_t
1307c478bd9Sstevel@tonic-gate i_ddi_intr_get_supported_nintrs(dev_info_t *dip, int intr_type)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1337c478bd9Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1347c478bd9Sstevel@tonic-gate 	int			ret, nintrs;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if ((intr_p) && (intr_p->devi_intr_curr_type == intr_type) &&
1377c478bd9Sstevel@tonic-gate 	    (intr_p->devi_intr_sup_nintrs))
1387c478bd9Sstevel@tonic-gate 		return (intr_p->devi_intr_sup_nintrs);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1417c478bd9Sstevel@tonic-gate 	hdl.ih_dip = dip;
1427c478bd9Sstevel@tonic-gate 	hdl.ih_type = intr_type;
1437c478bd9Sstevel@tonic-gate 
144a195726fSgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
1457c478bd9Sstevel@tonic-gate 	    (void *)&nintrs);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return ((ret == DDI_SUCCESS) ? nintrs : 0);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by ddi_intr_alloc().
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate void
1547c478bd9Sstevel@tonic-gate i_ddi_intr_set_supported_nintrs(dev_info_t *dip, int nintrs)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (intr_p)
1597c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_sup_nintrs = nintrs;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate uint_t
1637c478bd9Sstevel@tonic-gate i_ddi_intr_get_current_type(dev_info_t *dip)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_intr_curr_type : 0);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by
1727c478bd9Sstevel@tonic-gate  *       ddi_intr_alloc() and ddi_intr_free().
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate void
1757c478bd9Sstevel@tonic-gate i_ddi_intr_set_current_type(dev_info_t *dip, int intr_type)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (intr_p)
1807c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_curr_type = intr_type;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate uint_t
1847c478bd9Sstevel@tonic-gate i_ddi_intr_get_current_nintrs(dev_info_t *dip)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_intr_curr_nintrs : 0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by
1937c478bd9Sstevel@tonic-gate  *       ddi_intr_alloc() and ddi_intr_free().
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate void
1967c478bd9Sstevel@tonic-gate i_ddi_intr_set_current_nintrs(dev_info_t *dip, int nintrs)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (intr_p)
2017c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_curr_nintrs = nintrs;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2045febcb4aSScott Carter, SD IOSW uint_t
205*2145b8d4SGuoli Shu i_ddi_intr_get_current_nenables(dev_info_t *dip)
206*2145b8d4SGuoli Shu {
207*2145b8d4SGuoli Shu 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
208*2145b8d4SGuoli Shu 
209*2145b8d4SGuoli Shu 	return (intr_p ? intr_p->devi_intr_curr_nenables : 0);
210*2145b8d4SGuoli Shu }
211*2145b8d4SGuoli Shu 
212*2145b8d4SGuoli Shu void
213*2145b8d4SGuoli Shu i_ddi_intr_set_current_nenables(dev_info_t *dip, int nintrs)
214*2145b8d4SGuoli Shu {
215*2145b8d4SGuoli Shu 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
216*2145b8d4SGuoli Shu 
217*2145b8d4SGuoli Shu 	if (intr_p)
218*2145b8d4SGuoli Shu 		intr_p->devi_intr_curr_nenables = nintrs;
219*2145b8d4SGuoli Shu }
220*2145b8d4SGuoli Shu 
221*2145b8d4SGuoli Shu uint_t
2225febcb4aSScott Carter, SD IOSW i_ddi_intr_get_current_navail(dev_info_t *dip, int type)
2235febcb4aSScott Carter, SD IOSW {
2245febcb4aSScott Carter, SD IOSW 	ddi_intr_handle_impl_t	hdl;
2255febcb4aSScott Carter, SD IOSW 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
2265febcb4aSScott Carter, SD IOSW 	ddi_cb_t		*cb_p;
2275febcb4aSScott Carter, SD IOSW 	ddi_irm_pool_t		*pool_p;
2285febcb4aSScott Carter, SD IOSW 	ddi_irm_req_t		*req_p;
2295febcb4aSScott Carter, SD IOSW 	uint_t			navail = 0, nintrs, nreq;
2305febcb4aSScott Carter, SD IOSW 
2315febcb4aSScott Carter, SD IOSW 	/* Get maximum number of supported interrupts */
2325febcb4aSScott Carter, SD IOSW 	nintrs = i_ddi_intr_get_supported_nintrs(dip, type);
2335febcb4aSScott Carter, SD IOSW 
2345febcb4aSScott Carter, SD IOSW 	/* Check for an interrupt pool */
2355febcb4aSScott Carter, SD IOSW 	pool_p = i_ddi_intr_get_pool(dip, type);
2365febcb4aSScott Carter, SD IOSW 
2375febcb4aSScott Carter, SD IOSW 	/*
2385febcb4aSScott Carter, SD IOSW 	 * If a pool exists, then IRM determines the availability.
2395febcb4aSScott Carter, SD IOSW 	 * Otherwise, use the older INTROP method.
2405febcb4aSScott Carter, SD IOSW 	 */
2415febcb4aSScott Carter, SD IOSW 	if (pool_p) {
2425febcb4aSScott Carter, SD IOSW 		if (intr_p && (req_p = intr_p->devi_irm_req_p) &&
2435febcb4aSScott Carter, SD IOSW 		    (type == req_p->ireq_type)) {
2445febcb4aSScott Carter, SD IOSW 			mutex_enter(&pool_p->ipool_navail_lock);
2455febcb4aSScott Carter, SD IOSW 			navail = req_p->ireq_navail;
2465febcb4aSScott Carter, SD IOSW 			mutex_exit(&pool_p->ipool_navail_lock);
2475febcb4aSScott Carter, SD IOSW 			return (navail);
2485febcb4aSScott Carter, SD IOSW 		}
2495febcb4aSScott Carter, SD IOSW 		if ((type == DDI_INTR_TYPE_MSIX) &&
2505febcb4aSScott Carter, SD IOSW 		    (cb_p = DEVI(dip)->devi_cb_p) &&
2515febcb4aSScott Carter, SD IOSW 		    (cb_p->cb_flags & DDI_CB_FLAG_INTR)) {
2525febcb4aSScott Carter, SD IOSW 			return (nintrs);
2535febcb4aSScott Carter, SD IOSW 		}
2545febcb4aSScott Carter, SD IOSW 		navail = pool_p->ipool_defsz;
2555febcb4aSScott Carter, SD IOSW 	} else {
2565febcb4aSScott Carter, SD IOSW 		bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
2575febcb4aSScott Carter, SD IOSW 		hdl.ih_dip = dip;
2585febcb4aSScott Carter, SD IOSW 		hdl.ih_type = type;
2595febcb4aSScott Carter, SD IOSW 
2605febcb4aSScott Carter, SD IOSW 		if (i_ddi_intr_ops(dip, dip, DDI_INTROP_NAVAIL, &hdl,
2615febcb4aSScott Carter, SD IOSW 		    (void *)&navail) != DDI_SUCCESS) {
2625febcb4aSScott Carter, SD IOSW 			return (0);
2635febcb4aSScott Carter, SD IOSW 		}
2645febcb4aSScott Carter, SD IOSW 	}
2655febcb4aSScott Carter, SD IOSW 
2665febcb4aSScott Carter, SD IOSW 	/* Apply MSI-X workarounds */
2675febcb4aSScott Carter, SD IOSW 	if (type == DDI_INTR_TYPE_MSIX) {
2685febcb4aSScott Carter, SD IOSW 		/* Global tunable workaround */
2695febcb4aSScott Carter, SD IOSW 		if (navail < nintrs)
2705febcb4aSScott Carter, SD IOSW 			navail = MIN(nintrs, ddi_msix_alloc_limit);
2715febcb4aSScott Carter, SD IOSW 		/* Device property workaround */
2725febcb4aSScott Carter, SD IOSW 		if ((nreq = i_ddi_get_msix_alloc_limit(dip)) > 0)
2735febcb4aSScott Carter, SD IOSW 			navail = MAX(navail, nreq);
2745febcb4aSScott Carter, SD IOSW 	}
2755febcb4aSScott Carter, SD IOSW 
2765febcb4aSScott Carter, SD IOSW 	/* Always restrict MSI to a precise limit */
2775febcb4aSScott Carter, SD IOSW 	if (type == DDI_INTR_TYPE_MSI)
2785febcb4aSScott Carter, SD IOSW 		navail = MIN(navail, DDI_MAX_MSI_ALLOC);
2795febcb4aSScott Carter, SD IOSW 
2805febcb4aSScott Carter, SD IOSW 	/* Ensure availability doesn't exceed what's supported */
2815febcb4aSScott Carter, SD IOSW 	navail = MIN(navail, nintrs);
2825febcb4aSScott Carter, SD IOSW 
2835febcb4aSScott Carter, SD IOSW 	return (navail);
2845febcb4aSScott Carter, SD IOSW }
2855febcb4aSScott Carter, SD IOSW 
2867c478bd9Sstevel@tonic-gate ddi_intr_msix_t *
2877c478bd9Sstevel@tonic-gate i_ddi_get_msix(dev_info_t *dip)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_msix_p : NULL);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate void
2957c478bd9Sstevel@tonic-gate i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (intr_p)
3007c478bd9Sstevel@tonic-gate 		intr_p->devi_msix_p = msix_p;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3035febcb4aSScott Carter, SD IOSW ddi_intr_handle_t
3047c478bd9Sstevel@tonic-gate i_ddi_get_intr_handle(dev_info_t *dip, int inum)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (intr_p == NULL)
3097c478bd9Sstevel@tonic-gate 		return (NULL);
3107c478bd9Sstevel@tonic-gate 
311e821bd34Sanish 	/*
312e821bd34Sanish 	 * Changed this to a check and return NULL if an invalid inum
313e821bd34Sanish 	 * is passed to retrieve a handle
314e821bd34Sanish 	 */
315e821bd34Sanish 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
316e821bd34Sanish 		return (NULL);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return ((intr_p->devi_intr_handle_p) ?
3197c478bd9Sstevel@tonic-gate 	    intr_p->devi_intr_handle_p[inum] : NULL);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate void
3235febcb4aSScott Carter, SD IOSW i_ddi_set_intr_handle(dev_info_t *dip, int inum, ddi_intr_handle_t intr_hdl)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (intr_p == NULL)
3287c478bd9Sstevel@tonic-gate 		return;
3297c478bd9Sstevel@tonic-gate 
330e821bd34Sanish 	/*
331e821bd34Sanish 	 * Changed this to a check and return if an invalid inum
332e821bd34Sanish 	 * is passed to set a handle
333e821bd34Sanish 	 */
334e821bd34Sanish 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
335e821bd34Sanish 		return;
3367c478bd9Sstevel@tonic-gate 
3375febcb4aSScott Carter, SD IOSW 	if (intr_hdl && (intr_p->devi_intr_handle_p == NULL)) {
3387c478bd9Sstevel@tonic-gate 		/* nintrs could be zero; so check for it first */
3397c478bd9Sstevel@tonic-gate 		if (intr_p->devi_intr_sup_nintrs)
3407c478bd9Sstevel@tonic-gate 			intr_p->devi_intr_handle_p = kmem_zalloc(
3417c478bd9Sstevel@tonic-gate 			    sizeof (ddi_intr_handle_t) *
3427c478bd9Sstevel@tonic-gate 			    intr_p->devi_intr_sup_nintrs, KM_SLEEP);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
345a195726fSgovinda 	if (intr_p->devi_intr_handle_p)
3465febcb4aSScott Carter, SD IOSW 		intr_p->devi_intr_handle_p[inum] = intr_hdl;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * The "ddi-intr-weight" property contains the weight of each interrupt
3517c478bd9Sstevel@tonic-gate  * associated with a dev_info node. For devices with multiple interrupts per
3527c478bd9Sstevel@tonic-gate  * dev_info node, the total load of the device is "devi_intr_weight * nintr",
3537c478bd9Sstevel@tonic-gate  * possibly spread out over multiple CPUs.
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * Maintaining this as a property permits possible tweaking in the product
3567c478bd9Sstevel@tonic-gate  * in response to customer problems via driver.conf property definitions at
3577c478bd9Sstevel@tonic-gate  * the driver or the instance level.  This does not mean that "ddi-intr_weight"
3587c478bd9Sstevel@tonic-gate  * is a formal or committed interface.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate int32_t
3617c478bd9Sstevel@tonic-gate i_ddi_get_intr_weight(dev_info_t *dip)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int32_t	weight;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	weight = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3667c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "ddi-intr-weight", -1);
3677c478bd9Sstevel@tonic-gate 	if (weight < -1)
3687c478bd9Sstevel@tonic-gate 		weight = -1;			/* undefined */
3697c478bd9Sstevel@tonic-gate 	return (weight);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate int32_t
3737c478bd9Sstevel@tonic-gate i_ddi_set_intr_weight(dev_info_t *dip, int32_t weight)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	int32_t oweight;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	oweight = i_ddi_get_intr_weight(dip);
3787c478bd9Sstevel@tonic-gate 	if ((weight > 0) && (oweight != weight))
3797c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3807c478bd9Sstevel@tonic-gate 		    "ddi-intr-weight", weight);
3817c478bd9Sstevel@tonic-gate 	return (oweight);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * Old DDI interrupt framework
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * NOTE:
3887c478bd9Sstevel@tonic-gate  *	The following 4 busops entry points are obsoleted with version
3897c478bd9Sstevel@tonic-gate  *	9 or greater. Use i_ddi_intr_op interface in place of these
3907c478bd9Sstevel@tonic-gate  *	obsolete interfaces.
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  *	Remove these busops entry points and all related data structures
3937c478bd9Sstevel@tonic-gate  *	in future major/minor solaris release.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /* ARGSUSED */
3977c478bd9Sstevel@tonic-gate ddi_intrspec_t
3987c478bd9Sstevel@tonic-gate i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, uint_t inumber)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4037c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
404a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
405a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	return (NULL);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /* ARGSUSED */
4117c478bd9Sstevel@tonic-gate int
4127c478bd9Sstevel@tonic-gate i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, ddi_intrspec_t intrspec,
4137c478bd9Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep,
4147c478bd9Sstevel@tonic-gate     ddi_idevice_cookie_t *idevice_cookiep,
4157c478bd9Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg),
4167c478bd9Sstevel@tonic-gate     caddr_t int_handler_arg, int kind)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4217c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
422a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
423a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (DDI_ENOTSUP);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /* ARGSUSED */
4297c478bd9Sstevel@tonic-gate void
4307c478bd9Sstevel@tonic-gate i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
4317c478bd9Sstevel@tonic-gate     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4367c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
437a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
438a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /* ARGSUSED */
4427c478bd9Sstevel@tonic-gate int
4437c478bd9Sstevel@tonic-gate i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_ctlop_t op,
4447c478bd9Sstevel@tonic-gate     void *arg, void *val)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4497c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
450a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
451a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	return (DDI_ENOTSUP);
4547c478bd9Sstevel@tonic-gate }
455d12abe7cSanish 
456d12abe7cSanish #if defined(__i386) || defined(__amd64)
457d12abe7cSanish ddi_acc_handle_t
458d12abe7cSanish i_ddi_get_pci_config_handle(dev_info_t *dip)
459d12abe7cSanish {
460d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
461d12abe7cSanish 
462d12abe7cSanish 	return (intr_p ? intr_p->devi_cfg_handle : NULL);
463d12abe7cSanish }
464d12abe7cSanish 
465d12abe7cSanish void
466d12abe7cSanish i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle)
467d12abe7cSanish {
468d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
469d12abe7cSanish 
470d12abe7cSanish 	if (intr_p)
471d12abe7cSanish 		intr_p->devi_cfg_handle = handle;
472d12abe7cSanish }
473d12abe7cSanish 
474d12abe7cSanish 
475d12abe7cSanish int
476d12abe7cSanish i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip)
477d12abe7cSanish {
478d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
479d12abe7cSanish 
480d12abe7cSanish 	return (intr_p ? intr_p->devi_cap_ptr : 0);
481d12abe7cSanish }
482d12abe7cSanish 
483d12abe7cSanish void
484d12abe7cSanish i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr)
485d12abe7cSanish {
486d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
487d12abe7cSanish 
488d12abe7cSanish 	if (intr_p)
489d12abe7cSanish 		intr_p->devi_cap_ptr = cap_ptr;
490d12abe7cSanish }
491d12abe7cSanish #endif
492ef643aefSegillett 
493ef643aefSegillett /* ARGSUSED */
494ef643aefSegillett uint_t
495ef643aefSegillett i_ddi_get_msix_alloc_limit(dev_info_t *dip)
496ef643aefSegillett {
497ef643aefSegillett 	uint_t	msix_alloc_limit = ddi_msix_alloc_limit;
498ef643aefSegillett 
499ef643aefSegillett #if defined(__sparc)
500ef643aefSegillett 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_NOTPROM |
501ef643aefSegillett 	    DDI_PROP_DONTPASS, "#msix-request")) {
502ef643aefSegillett 		msix_alloc_limit = MAX(DDI_MAX_MSIX_ALLOC,
503ef643aefSegillett 		    ddi_msix_alloc_limit);
504ef643aefSegillett 	}
505ef643aefSegillett #endif
506ef643aefSegillett 
507ef643aefSegillett 	return (msix_alloc_limit);
508ef643aefSegillett }
509