xref: /onnv-gate/usr/src/uts/sun4v/io/vnex.c (revision 3156:30109e935ec8)
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
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * 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 /*
221276Smf13430  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4 5.0 */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
320Sstevel@tonic-gate #include <sys/autoconf.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
380Sstevel@tonic-gate #include <sys/promif.h>
390Sstevel@tonic-gate #include <sys/machsystm.h>
400Sstevel@tonic-gate #include <sys/ddi_intr_impl.h>
410Sstevel@tonic-gate #include <sys/hypervisor_api.h>
420Sstevel@tonic-gate #include <sys/intr.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	SUN4V_REG_SPEC2CFG_HDL(x)	((x >> 32) & ~(0xfull << 28))
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static kmutex_t vnex_id_lock;
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Vnex name  to pil map
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate typedef struct vnex_regspec {
510Sstevel@tonic-gate 	uint64_t physaddr;
520Sstevel@tonic-gate 	uint64_t size;
530Sstevel@tonic-gate } vnex_regspec_t;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate struct vnex_pil_map {
560Sstevel@tonic-gate 	caddr_t	name;
570Sstevel@tonic-gate 	uint32_t pil;
580Sstevel@tonic-gate };
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* vnex interrupt descriptor */
610Sstevel@tonic-gate typedef struct vnex_id {
620Sstevel@tonic-gate 	dev_info_t *vid_dip;
630Sstevel@tonic-gate 	uint32_t vid_ino;
640Sstevel@tonic-gate 	uint64_t vid_ihdl;
650Sstevel@tonic-gate 	uint_t	(*vid_handler)();
660Sstevel@tonic-gate 	caddr_t	vid_arg1;
670Sstevel@tonic-gate 	caddr_t	vid_arg2;
680Sstevel@tonic-gate 	ddi_intr_handle_impl_t *vid_ddi_hdlp;
690Sstevel@tonic-gate 	uint64_t vid_cfg_hdl;
700Sstevel@tonic-gate 	struct vnex_id *vid_next;
710Sstevel@tonic-gate } vnex_id_t;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* vnex interrupt descriptor list */
740Sstevel@tonic-gate static vnex_id_t *vnex_id_list;
750Sstevel@tonic-gate 
76444Sarao hrtime_t vnex_pending_timeout = 2ull * NANOSEC; /* 2 seconds in nanoseconds */
77444Sarao 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * vnex interrupt descriptor list manipulation functions
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static vnex_id_t *vnex_locate_id(dev_info_t *dip, uint32_t ino);
830Sstevel@tonic-gate static vnex_id_t *vnex_alloc_id(dev_info_t *dip, uint32_t ino,
840Sstevel@tonic-gate 	uint64_t dhdl);
850Sstevel@tonic-gate static void vnex_add_id(vnex_id_t *vid_p);
860Sstevel@tonic-gate static void vnex_rem_id(vnex_id_t *vid_p);
870Sstevel@tonic-gate static void vnex_free_id(vnex_id_t *vid_p);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate uint_t vnex_intr_wrapper(caddr_t arg);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static struct vnex_pil_map vnex_name_to_pil[] = {
920Sstevel@tonic-gate 	{"console", 	PIL_12},
930Sstevel@tonic-gate 	{"fma",		PIL_5},
940Sstevel@tonic-gate 	{"echo", 	PIL_3},
950Sstevel@tonic-gate 	{"loop", 	PIL_3},
960Sstevel@tonic-gate 	{"sunmc", 	PIL_3},
970Sstevel@tonic-gate 	{"sunvts", 	PIL_3},
981991Sheppo 	{"explorer", 	PIL_3},
99*3156Sgirish 	{"ncp", 	PIL_8},
100*3156Sgirish 	{"crypto", 	PIL_8}
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	VNEX_MAX_DEVS	(sizeof (vnex_name_to_pil) /	\
1040Sstevel@tonic-gate 			    sizeof (struct vnex_pil_map))
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Config information
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static int vnex_intr_ops(dev_info_t *dip, dev_info_t *rdip,
1100Sstevel@tonic-gate     ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static int
1130Sstevel@tonic-gate vnex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static struct bus_ops vnex_bus_ops = {
1160Sstevel@tonic-gate 	BUSO_REV,
1170Sstevel@tonic-gate 	nullbusmap,
1180Sstevel@tonic-gate 	NULL,	/* NO OP */
1190Sstevel@tonic-gate 	NULL,	/* NO OP */
1200Sstevel@tonic-gate 	NULL,	/* NO OP */
1210Sstevel@tonic-gate 	i_ddi_map_fault,
1220Sstevel@tonic-gate 	ddi_no_dma_map,
1230Sstevel@tonic-gate 	ddi_no_dma_allochdl,
1240Sstevel@tonic-gate 	NULL,
1250Sstevel@tonic-gate 	NULL,
1260Sstevel@tonic-gate 	NULL,
1270Sstevel@tonic-gate 	NULL,
1280Sstevel@tonic-gate 	NULL,
1290Sstevel@tonic-gate 	NULL,
1300Sstevel@tonic-gate 	vnex_ctl,
1310Sstevel@tonic-gate 	ddi_bus_prop_op,
1320Sstevel@tonic-gate 	NULL,	/* (*bus_get_eventcookie)();    */
1330Sstevel@tonic-gate 	NULL,	/* (*bus_add_eventcall)();	*/
1340Sstevel@tonic-gate 	NULL,	/* (*bus_remove_eventcall)();   */
1350Sstevel@tonic-gate 	NULL,	/* (*bus_post_event)();		*/
1360Sstevel@tonic-gate 	NULL,	/* (*bus_intr_ctl)();		*/
1370Sstevel@tonic-gate 	NULL,	/* (*bus_config)();		*/
1380Sstevel@tonic-gate 	NULL,	/* (*bus_unconfig)();		*/
1390Sstevel@tonic-gate 	NULL,	/* (*bus_fm_init)();		*/
1400Sstevel@tonic-gate 	NULL,	/* (*bus_fm_fini)();		*/
1410Sstevel@tonic-gate 	NULL,	/* (*bus_fm_access_enter)();	*/
1420Sstevel@tonic-gate 	NULL,	/* (*bus_fm_access_fini)();	*/
1430Sstevel@tonic-gate 	NULL,	/* (*bus_power)();		*/
1440Sstevel@tonic-gate 	vnex_intr_ops	/* (*bus_intr_op)();	*/
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
147444Sarao static int vnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
148444Sarao static int vnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static struct dev_ops pseudo_ops = {
1510Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1520Sstevel@tonic-gate 	0,			/* refcnt  */
1530Sstevel@tonic-gate 	ddi_no_info,		/* info */
1540Sstevel@tonic-gate 	nulldev,		/* identify */
1550Sstevel@tonic-gate 	nulldev,		/* probe */
1560Sstevel@tonic-gate 	vnex_attach,		/* attach */
1570Sstevel@tonic-gate 	vnex_detach,		/* detach */
1580Sstevel@tonic-gate 	nodev,			/* reset */
1590Sstevel@tonic-gate 	(struct cb_ops *)0,	/* driver operations */
1600Sstevel@tonic-gate 	&vnex_bus_ops,	/* bus operations */
1610Sstevel@tonic-gate 	nulldev			/* power */
1620Sstevel@tonic-gate };
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Module linkage information for the kernel.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static struct modldrv modldrv = {
1690Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
1700Sstevel@tonic-gate 	"sun4v virtual-devices nexus driver v%I%",
1710Sstevel@tonic-gate 	&pseudo_ops,	/* driver ops */
1720Sstevel@tonic-gate };
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct modlinkage modlinkage = {
1750Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1760Sstevel@tonic-gate };
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate int
1790Sstevel@tonic-gate _init(void)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate int
1850Sstevel@tonic-gate _fini(void)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
1910Sstevel@tonic-gate _info(struct modinfo *modinfop)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*ARGSUSED*/
197444Sarao void
198444Sarao vnex_intr_dist(void *arg)
199444Sarao {
200444Sarao 	vnex_id_t *vid_p;
201444Sarao 	uint32_t cpuid;
202444Sarao 	int	intr_state;
203444Sarao 	hrtime_t start;
204444Sarao 
205444Sarao 	mutex_enter(&vnex_id_lock);
206444Sarao 
207444Sarao 	for (vid_p = vnex_id_list; vid_p != NULL;
208444Sarao 	    vid_p = vid_p->vid_next) {
209444Sarao 		/*
210444Sarao 		 * Don't do anything for disabled interrupts.
211444Sarao 		 * vnex_enable_intr takes care of redistributing interrupts.
212444Sarao 		 */
213444Sarao 		if ((hvio_intr_getvalid(vid_p->vid_ihdl,
214444Sarao 		    &intr_state) == H_EOK) && (intr_state == HV_INTR_NOTVALID))
215444Sarao 				continue;
216444Sarao 
217444Sarao 		cpuid = intr_dist_cpuid();
218444Sarao 
219444Sarao 		(void) hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_NOTVALID);
220444Sarao 		/*
221444Sarao 		 * Make a best effort to wait for pending interrupts to finish.
222444Sarao 		 * There is not much we can do if we timeout.
223444Sarao 		 */
224444Sarao 		start = gethrtime();
225444Sarao 		while (!panicstr &&
226444Sarao 		    (hvio_intr_getstate(vid_p->vid_ihdl, &intr_state) ==
227444Sarao 		    H_EOK) && (intr_state == HV_INTR_DELIVERED_STATE)) {
228444Sarao 			if (gethrtime() - start > vnex_pending_timeout) {
229444Sarao 				cmn_err(CE_WARN, "vnex_intr_dist: %s%d "
230444Sarao 				    "ino 0x%x pending: timedout\n",
231444Sarao 				    ddi_driver_name(vid_p->vid_dip),
232444Sarao 				    ddi_get_instance(vid_p->vid_dip),
233444Sarao 				    vid_p->vid_ino);
234444Sarao 				break;
235444Sarao 			}
236444Sarao 		}
237444Sarao 		(void) hvio_intr_settarget(vid_p->vid_ihdl, cpuid);
238444Sarao 		(void) hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_VALID);
239444Sarao 	}
240444Sarao 	mutex_exit(&vnex_id_lock);
241444Sarao }
242444Sarao 
2430Sstevel@tonic-gate static int
244444Sarao vnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	switch (cmd) {
2470Sstevel@tonic-gate 	case DDI_ATTACH:
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * Intitialize interrupt descriptor list
2500Sstevel@tonic-gate 		 * and mutex.
2510Sstevel@tonic-gate 		 */
2520Sstevel@tonic-gate 		vnex_id_list = NULL;
2530Sstevel@tonic-gate 		mutex_init(&vnex_id_lock, NULL, MUTEX_DRIVER, NULL);
254444Sarao 		/*
255444Sarao 		 * Add interrupt redistribution callback.
256444Sarao 		 */
257444Sarao 		intr_dist_add(vnex_intr_dist, dip);
2580Sstevel@tonic-gate 		return (DDI_SUCCESS);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	case DDI_RESUME:
2610Sstevel@tonic-gate 		return (DDI_SUCCESS);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	default:
2640Sstevel@tonic-gate 		return (DDI_FAILURE);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*ARGSUSED*/
2690Sstevel@tonic-gate static int
270444Sarao vnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	switch (cmd) {
2730Sstevel@tonic-gate 	case DDI_DETACH:
2740Sstevel@tonic-gate 		return (DDI_FAILURE);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	case DDI_SUSPEND:
2770Sstevel@tonic-gate 		return (DDI_SUCCESS);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	default:
2800Sstevel@tonic-gate 		return (DDI_FAILURE);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static int
2850Sstevel@tonic-gate vnex_ctl(dev_info_t *dip, dev_info_t *rdip,
2860Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	char	name[12];	/* enough for a decimal integer */
2890Sstevel@tonic-gate 	int		reglen;
2900Sstevel@tonic-gate 	uint32_t	*vnex_regspec;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	switch (ctlop) {
2930Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
2940Sstevel@tonic-gate 		if (rdip == NULL)
2950Sstevel@tonic-gate 			return (DDI_FAILURE);
2960Sstevel@tonic-gate 		cmn_err(CE_CONT, "?virtual-device: %s%d\n",
2970Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
2980Sstevel@tonic-gate 		return (DDI_SUCCESS);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
3010Sstevel@tonic-gate 	{
3020Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
3030Sstevel@tonic-gate 
304506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
3050Sstevel@tonic-gate 		    "reg", (caddr_t)&vnex_regspec, &reglen) != DDI_SUCCESS)
3060Sstevel@tonic-gate 			return (DDI_FAILURE);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		(void) sprintf(name, "%x", *vnex_regspec);
3090Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
3100Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
3110Sstevel@tonic-gate 		kmem_free((caddr_t)vnex_regspec, reglen);
3120Sstevel@tonic-gate 		return (DDI_SUCCESS);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
3170Sstevel@tonic-gate 	{
3180Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
3210Sstevel@tonic-gate 		ddi_remove_minor_node(arg, NULL);
3220Sstevel@tonic-gate 		return (DDI_SUCCESS);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * These ops correspond to functions that "shouldn't" be called
3270Sstevel@tonic-gate 	 * by a pseudo driver.  So we whinge when we're called.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
3300Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
3310Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
3320Sstevel@tonic-gate 	{
3330Sstevel@tonic-gate 		*((off_t *)result) = 0;
3340Sstevel@tonic-gate 		return (DDI_SUCCESS);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
3370Sstevel@tonic-gate 	{
3380Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
339506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
3400Sstevel@tonic-gate 		    "reg", (caddr_t)&vnex_regspec, &reglen) != DDI_SUCCESS)
3410Sstevel@tonic-gate 			return (DDI_FAILURE);
3420Sstevel@tonic-gate 		*((uint_t *)result) = reglen / sizeof (uint32_t);
3430Sstevel@tonic-gate 		kmem_free((caddr_t)vnex_regspec, reglen);
3440Sstevel@tonic-gate 		return (DDI_SUCCESS);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
3470Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
3480Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
3490Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
3500Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
3510Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
3520Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n",
3530Sstevel@tonic-gate 			ddi_get_name(dip), ddi_get_instance(dip),
3540Sstevel@tonic-gate 			ctlop, ddi_get_name(rdip), ddi_get_instance(rdip));
3550Sstevel@tonic-gate 		return (DDI_FAILURE);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	default:
3610Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate static int
3660Sstevel@tonic-gate vnex_get_pil(dev_info_t *rdip)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	int i;
3690Sstevel@tonic-gate 	caddr_t	name;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	name = ddi_node_name(rdip);
3720Sstevel@tonic-gate 	for (i = 0; i < VNEX_MAX_DEVS; i++) {
3730Sstevel@tonic-gate 		if (strcmp(vnex_name_to_pil[i].name,
3740Sstevel@tonic-gate 		    name) == 0) {
3750Sstevel@tonic-gate 			return (vnex_name_to_pil[i].pil);
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * if not found pil is 0
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	return (0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate static int
3850Sstevel@tonic-gate vnex_enable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	vnex_id_t *vid_p;
3880Sstevel@tonic-gate 	uint32_t cpuid;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	vid_p = vnex_locate_id(rdip, hdlp->ih_vector);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	ASSERT(vid_p != NULL);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	cpuid = intr_dist_cpuid();
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if ((hvio_intr_settarget(vid_p->vid_ihdl, cpuid)) != H_EOK) {
3970Sstevel@tonic-gate 		return (DDI_FAILURE);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (hvio_intr_setstate(vid_p->vid_ihdl, HV_INTR_IDLE_STATE) != H_EOK) {
4010Sstevel@tonic-gate 		return (DDI_FAILURE);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if ((hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_VALID)) != H_EOK) {
4050Sstevel@tonic-gate 		return (DDI_FAILURE);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (DDI_SUCCESS);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static int
4120Sstevel@tonic-gate vnex_disable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	vnex_id_t *vid_p;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	vid_p = vnex_locate_id(rdip, hdlp->ih_vector);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ASSERT(vid_p != NULL);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_NOTVALID) != H_EOK) {
4210Sstevel@tonic-gate 		return (DDI_FAILURE);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	return (DDI_SUCCESS);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4271991Sheppo int
4281991Sheppo vnex_ino_to_inum(dev_info_t *dip, uint32_t ino)
4291991Sheppo {
4301991Sheppo 	vnex_id_t		*vid_p;
4311991Sheppo 	ddi_intr_handle_impl_t	*hdlp;
4321991Sheppo 
4331991Sheppo 	if ((vid_p = vnex_locate_id(dip, ino)) == NULL)
4341991Sheppo 		return (-1);
4351991Sheppo 	else if ((hdlp = vid_p->vid_ddi_hdlp) == NULL)
4361991Sheppo 		return (-1);
4371991Sheppo 	else
4381991Sheppo 		return (hdlp->ih_inum);
4391991Sheppo }
4401991Sheppo 
4410Sstevel@tonic-gate static int
4420Sstevel@tonic-gate vnex_add_intr(dev_info_t *dip, dev_info_t *rdip,
4430Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	int reglen, ret = DDI_SUCCESS;
4460Sstevel@tonic-gate 	vnex_id_t	*vid_p;
4470Sstevel@tonic-gate 	uint64_t cfg;
4480Sstevel@tonic-gate 	uint32_t ino;
4490Sstevel@tonic-gate 	uint64_t ihdl;
4500Sstevel@tonic-gate 	vnex_regspec_t *reg_p;
4510Sstevel@tonic-gate 
452506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
4530Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "reg", (caddr_t)&reg_p,
4540Sstevel@tonic-gate 	    &reglen) != DDI_SUCCESS) {
4550Sstevel@tonic-gate 		return (DDI_FAILURE);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4590Sstevel@tonic-gate 	 * get the sun4v config handle for this device
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	cfg = SUN4V_REG_SPEC2CFG_HDL(reg_p->physaddr);
463288Sarao 	kmem_free(reg_p, reglen);
4640Sstevel@tonic-gate 	ino = hdlp->ih_vector;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * call hv to get vihdl
4680Sstevel@tonic-gate 	 */
4690Sstevel@tonic-gate 	if (hvio_intr_devino_to_sysino(cfg, ino, &ihdl) != H_EOK)
4700Sstevel@tonic-gate 		return (DDI_FAILURE);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	hdlp->ih_vector = ihdl;
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * Allocate a interrupt descriptor (id) with the
4750Sstevel@tonic-gate 	 * the interrupt handler and append it to
4760Sstevel@tonic-gate 	 * the id list.
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	vid_p = vnex_alloc_id(rdip, ino, cfg);
4800Sstevel@tonic-gate 	vid_p->vid_ihdl = ihdl;
4810Sstevel@tonic-gate 	vid_p->vid_handler =  hdlp->ih_cb_func;
4820Sstevel@tonic-gate 	vid_p->vid_arg1 =  hdlp->ih_cb_arg1;
4830Sstevel@tonic-gate 	vid_p->vid_arg2 =  hdlp->ih_cb_arg2;
4840Sstevel@tonic-gate 	vid_p->vid_ddi_hdlp =  hdlp;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
4870Sstevel@tonic-gate 	    (ddi_intr_handler_t *)vnex_intr_wrapper, (caddr_t)vid_p, NULL);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (hdlp->ih_pri == 0) {
4900Sstevel@tonic-gate 		hdlp->ih_pri = vnex_get_pil(rdip);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
4940Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
4950Sstevel@tonic-gate 		return (ret);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, vid_p->vid_handler,
4990Sstevel@tonic-gate 	    vid_p->vid_arg1, vid_p->vid_arg2);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	return (ret);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate static int
5050Sstevel@tonic-gate vnex_remove_intr(dev_info_t *rdip,
5060Sstevel@tonic-gate 	ddi_intr_handle_impl_t *hdlp)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	vnex_id_t *vid_p;
5090Sstevel@tonic-gate 	uint32_t ino;
5100Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	ino = hdlp->ih_vector;
5130Sstevel@tonic-gate 	vid_p = vnex_locate_id(rdip, ino);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	hdlp->ih_vector = vid_p->vid_ihdl;
5160Sstevel@tonic-gate 	i_ddi_rem_ivintr(hdlp);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	vnex_free_id(vid_p);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (ret);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate static int
5240Sstevel@tonic-gate vnex_intr_ops(dev_info_t *dip, dev_info_t *rdip,
5250Sstevel@tonic-gate     ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result)
5260Sstevel@tonic-gate {
527693Sgovinda 	int	ret = DDI_SUCCESS;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	switch (intr_op) {
5300Sstevel@tonic-gate 		case DDI_INTROP_GETCAP:
531693Sgovinda 			*(int *)result = DDI_INTR_FLAG_LEVEL;
5320Sstevel@tonic-gate 			break;
5330Sstevel@tonic-gate 		case DDI_INTROP_ALLOC:
5340Sstevel@tonic-gate 			*(int *)result = hdlp->ih_scratch1;
5350Sstevel@tonic-gate 			break;
5360Sstevel@tonic-gate 		case DDI_INTROP_GETPRI:
537693Sgovinda 			*(int *)result = hdlp->ih_pri ?
538693Sgovinda 			    hdlp->ih_pri : vnex_get_pil(rdip);
5390Sstevel@tonic-gate 			break;
5400Sstevel@tonic-gate 		case DDI_INTROP_FREE:
5410Sstevel@tonic-gate 			break;
5420Sstevel@tonic-gate 		case DDI_INTROP_SETPRI:
5430Sstevel@tonic-gate 			break;
5440Sstevel@tonic-gate 		case DDI_INTROP_ADDISR:
5450Sstevel@tonic-gate 			ret = vnex_add_intr(dip, rdip, hdlp);
5460Sstevel@tonic-gate 			break;
5470Sstevel@tonic-gate 		case DDI_INTROP_REMISR:
5480Sstevel@tonic-gate 			ret = vnex_remove_intr(rdip, hdlp);
5490Sstevel@tonic-gate 			break;
5500Sstevel@tonic-gate 		case DDI_INTROP_ENABLE:
5510Sstevel@tonic-gate 			ret = vnex_enable_intr(rdip, hdlp);
5520Sstevel@tonic-gate 			break;
5530Sstevel@tonic-gate 		case DDI_INTROP_DISABLE:
5540Sstevel@tonic-gate 			ret = vnex_disable_intr(rdip, hdlp);
5550Sstevel@tonic-gate 			break;
5560Sstevel@tonic-gate 		case DDI_INTROP_NINTRS:
5570Sstevel@tonic-gate 		case DDI_INTROP_NAVAIL:
5582580Sanish 			*(int *)result = i_ddi_get_intx_nintrs(rdip);
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 		case DDI_INTROP_SUPPORTED_TYPES:
5612580Sanish 			*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
5620Sstevel@tonic-gate 			    DDI_INTR_TYPE_FIXED : 0;
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 		default:
5650Sstevel@tonic-gate 			ret = DDI_ENOTSUP;
5660Sstevel@tonic-gate 			break;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	return (ret);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate vnex_id_t *
5730Sstevel@tonic-gate vnex_alloc_id(dev_info_t *dip, uint32_t ino, uint64_t dhdl)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	vnex_id_t *vid_p = kmem_alloc(sizeof (vnex_id_t), KM_SLEEP);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	vid_p->vid_dip = dip;
5780Sstevel@tonic-gate 	vid_p->vid_ino = ino;
5790Sstevel@tonic-gate 	vid_p->vid_cfg_hdl = dhdl;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	mutex_enter(&vnex_id_lock);
5820Sstevel@tonic-gate 	vnex_add_id(vid_p);
5830Sstevel@tonic-gate 	mutex_exit(&vnex_id_lock);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	return (vid_p);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate vnex_id_t *
5890Sstevel@tonic-gate vnex_locate_id(dev_info_t *dip, uint32_t ino)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	vnex_id_t *vid_p;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	mutex_enter(&vnex_id_lock);
5940Sstevel@tonic-gate 	vid_p = vnex_id_list;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	while (vid_p != NULL) {
5970Sstevel@tonic-gate 		if (vid_p->vid_dip == dip && vid_p->vid_ino == ino) {
5980Sstevel@tonic-gate 			mutex_exit(&vnex_id_lock);
5990Sstevel@tonic-gate 			return (vid_p);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		vid_p = vid_p->vid_next;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 	mutex_exit(&vnex_id_lock);
6040Sstevel@tonic-gate 	return (NULL);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate static void
6080Sstevel@tonic-gate vnex_free_id(vnex_id_t *vid_p)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	mutex_enter(&vnex_id_lock);
6110Sstevel@tonic-gate 	vnex_rem_id(vid_p);
6120Sstevel@tonic-gate 	mutex_exit(&vnex_id_lock);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	kmem_free(vid_p, sizeof (*vid_p));
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static void
6180Sstevel@tonic-gate vnex_rem_id(vnex_id_t *vid_p)
6190Sstevel@tonic-gate {
6201276Smf13430 	vnex_id_t *prev_p = vnex_id_list;
6211276Smf13430 
6221276Smf13430 	if (vnex_id_list == NULL)
6231276Smf13430 		cmn_err(CE_PANIC, "vnex: interrupt list empty");
6241276Smf13430 
6251276Smf13430 	if (vid_p == NULL)
6261276Smf13430 		cmn_err(CE_PANIC, "vnex: no element to remove");
6271276Smf13430 
6281276Smf13430 	if (vnex_id_list == vid_p) {
6290Sstevel@tonic-gate 		vnex_id_list = vid_p->vid_next;
6300Sstevel@tonic-gate 	} else {
6311276Smf13430 		while (prev_p != NULL && prev_p->vid_next != vid_p)
6321276Smf13430 			prev_p = prev_p->vid_next;
6331276Smf13430 
6341276Smf13430 		if (prev_p == NULL)
6351276Smf13430 			cmn_err(CE_PANIC, "vnex: element %p not in list",
6361276Smf13430 			    (void *) vid_p);
6371276Smf13430 
6381276Smf13430 		prev_p->vid_next = vid_p->vid_next;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate static void
6430Sstevel@tonic-gate vnex_add_id(vnex_id_t *vid_p)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	vid_p->vid_next = vnex_id_list;
6460Sstevel@tonic-gate 	vnex_id_list = vid_p;
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate uint_t
6500Sstevel@tonic-gate vnex_intr_wrapper(caddr_t arg)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	vnex_id_t *vid_p = (vnex_id_t *)arg;
6530Sstevel@tonic-gate 	int res;
6540Sstevel@tonic-gate 	uint_t (*handler)();
6550Sstevel@tonic-gate 	caddr_t handler_arg1;
6560Sstevel@tonic-gate 	caddr_t handler_arg2;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	handler = vid_p->vid_handler;
6590Sstevel@tonic-gate 	handler_arg1 = vid_p->vid_arg1;
6600Sstevel@tonic-gate 	handler_arg2 = vid_p->vid_arg2;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	res = (*handler)(handler_arg1, handler_arg2);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	(void) hvio_intr_setstate(vid_p->vid_ihdl, HV_INTR_IDLE_STATE);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	return (res);
6670Sstevel@tonic-gate }
668