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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4 5.0 */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 317c478bd9Sstevel@tonic-gate #include <sys/conf.h> 327c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 337c478bd9Sstevel@tonic-gate #include <sys/autoconf.h> 347c478bd9Sstevel@tonic-gate #include <sys/systm.h> 357c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 367c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 387c478bd9Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 397c478bd9Sstevel@tonic-gate #include <sys/promif.h> 407c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 417c478bd9Sstevel@tonic-gate #include <sys/ddi_intr_impl.h> 427c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h> 437c478bd9Sstevel@tonic-gate #include <sys/intr.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #define SUN4V_REG_SPEC2CFG_HDL(x) ((x >> 32) & ~(0xfull << 28)) 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static kmutex_t vnex_id_lock; 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Vnex name to pil map 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate typedef struct vnex_regspec { 527c478bd9Sstevel@tonic-gate uint64_t physaddr; 537c478bd9Sstevel@tonic-gate uint64_t size; 547c478bd9Sstevel@tonic-gate } vnex_regspec_t; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate struct vnex_pil_map { 577c478bd9Sstevel@tonic-gate caddr_t name; 587c478bd9Sstevel@tonic-gate uint32_t pil; 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* vnex interrupt descriptor */ 627c478bd9Sstevel@tonic-gate typedef struct vnex_id { 637c478bd9Sstevel@tonic-gate dev_info_t *vid_dip; 647c478bd9Sstevel@tonic-gate uint32_t vid_ino; 657c478bd9Sstevel@tonic-gate uint64_t vid_ihdl; 667c478bd9Sstevel@tonic-gate uint_t (*vid_handler)(); 677c478bd9Sstevel@tonic-gate caddr_t vid_arg1; 687c478bd9Sstevel@tonic-gate caddr_t vid_arg2; 697c478bd9Sstevel@tonic-gate ddi_intr_handle_impl_t *vid_ddi_hdlp; 707c478bd9Sstevel@tonic-gate uint64_t vid_cfg_hdl; 717c478bd9Sstevel@tonic-gate struct vnex_id *vid_next; 727c478bd9Sstevel@tonic-gate struct vnex_id *vid_prev; 737c478bd9Sstevel@tonic-gate } vnex_id_t; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* vnex interrupt descriptor list */ 767c478bd9Sstevel@tonic-gate static vnex_id_t *vnex_id_list; 777c478bd9Sstevel@tonic-gate 78f3307d20Sarao hrtime_t vnex_pending_timeout = 2ull * NANOSEC; /* 2 seconds in nanoseconds */ 79f3307d20Sarao 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * vnex interrupt descriptor list manipulation functions 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static vnex_id_t *vnex_locate_id(dev_info_t *dip, uint32_t ino); 857c478bd9Sstevel@tonic-gate static vnex_id_t *vnex_alloc_id(dev_info_t *dip, uint32_t ino, 867c478bd9Sstevel@tonic-gate uint64_t dhdl); 877c478bd9Sstevel@tonic-gate static void vnex_add_id(vnex_id_t *vid_p); 887c478bd9Sstevel@tonic-gate static void vnex_rem_id(vnex_id_t *vid_p); 897c478bd9Sstevel@tonic-gate static void vnex_free_id(vnex_id_t *vid_p); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate uint_t vnex_intr_wrapper(caddr_t arg); 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static struct vnex_pil_map vnex_name_to_pil[] = { 947c478bd9Sstevel@tonic-gate {"console", PIL_12}, 957c478bd9Sstevel@tonic-gate {"fma", PIL_5}, 967c478bd9Sstevel@tonic-gate {"echo", PIL_3}, 977c478bd9Sstevel@tonic-gate {"loop", PIL_3}, 987c478bd9Sstevel@tonic-gate {"sunmc", PIL_3}, 997c478bd9Sstevel@tonic-gate {"sunvts", PIL_3}, 1007c478bd9Sstevel@tonic-gate {"explorer", PIL_3} 1017c478bd9Sstevel@tonic-gate }; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #define VNEX_MAX_DEVS (sizeof (vnex_name_to_pil) / \ 1047c478bd9Sstevel@tonic-gate sizeof (struct vnex_pil_map)) 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * Config information 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate static int vnex_intr_ops(dev_info_t *dip, dev_info_t *rdip, 1107c478bd9Sstevel@tonic-gate ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate static int 1137c478bd9Sstevel@tonic-gate vnex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate static struct bus_ops vnex_bus_ops = { 1167c478bd9Sstevel@tonic-gate BUSO_REV, 1177c478bd9Sstevel@tonic-gate nullbusmap, 1187c478bd9Sstevel@tonic-gate NULL, /* NO OP */ 1197c478bd9Sstevel@tonic-gate NULL, /* NO OP */ 1207c478bd9Sstevel@tonic-gate NULL, /* NO OP */ 1217c478bd9Sstevel@tonic-gate i_ddi_map_fault, 1227c478bd9Sstevel@tonic-gate ddi_no_dma_map, 1237c478bd9Sstevel@tonic-gate ddi_no_dma_allochdl, 1247c478bd9Sstevel@tonic-gate NULL, 1257c478bd9Sstevel@tonic-gate NULL, 1267c478bd9Sstevel@tonic-gate NULL, 1277c478bd9Sstevel@tonic-gate NULL, 1287c478bd9Sstevel@tonic-gate NULL, 1297c478bd9Sstevel@tonic-gate NULL, 1307c478bd9Sstevel@tonic-gate vnex_ctl, 1317c478bd9Sstevel@tonic-gate ddi_bus_prop_op, 1327c478bd9Sstevel@tonic-gate NULL, /* (*bus_get_eventcookie)(); */ 1337c478bd9Sstevel@tonic-gate NULL, /* (*bus_add_eventcall)(); */ 1347c478bd9Sstevel@tonic-gate NULL, /* (*bus_remove_eventcall)(); */ 1357c478bd9Sstevel@tonic-gate NULL, /* (*bus_post_event)(); */ 1367c478bd9Sstevel@tonic-gate NULL, /* (*bus_intr_ctl)(); */ 1377c478bd9Sstevel@tonic-gate NULL, /* (*bus_config)(); */ 1387c478bd9Sstevel@tonic-gate NULL, /* (*bus_unconfig)(); */ 1397c478bd9Sstevel@tonic-gate NULL, /* (*bus_fm_init)(); */ 1407c478bd9Sstevel@tonic-gate NULL, /* (*bus_fm_fini)(); */ 1417c478bd9Sstevel@tonic-gate NULL, /* (*bus_fm_access_enter)(); */ 1427c478bd9Sstevel@tonic-gate NULL, /* (*bus_fm_access_fini)(); */ 1437c478bd9Sstevel@tonic-gate NULL, /* (*bus_power)(); */ 1447c478bd9Sstevel@tonic-gate vnex_intr_ops /* (*bus_intr_op)(); */ 1457c478bd9Sstevel@tonic-gate }; 1467c478bd9Sstevel@tonic-gate 147f3307d20Sarao static int vnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 148f3307d20Sarao static int vnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate static struct dev_ops pseudo_ops = { 1517c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 1527c478bd9Sstevel@tonic-gate 0, /* refcnt */ 1537c478bd9Sstevel@tonic-gate ddi_no_info, /* info */ 1547c478bd9Sstevel@tonic-gate nulldev, /* identify */ 1557c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1567c478bd9Sstevel@tonic-gate vnex_attach, /* attach */ 1577c478bd9Sstevel@tonic-gate vnex_detach, /* detach */ 1587c478bd9Sstevel@tonic-gate nodev, /* reset */ 1597c478bd9Sstevel@tonic-gate (struct cb_ops *)0, /* driver operations */ 1607c478bd9Sstevel@tonic-gate &vnex_bus_ops, /* bus operations */ 1617c478bd9Sstevel@tonic-gate nulldev /* power */ 1627c478bd9Sstevel@tonic-gate }; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1697c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */ 1707c478bd9Sstevel@tonic-gate "sun4v virtual-devices nexus driver v%I%", 1717c478bd9Sstevel@tonic-gate &pseudo_ops, /* driver ops */ 1727c478bd9Sstevel@tonic-gate }; 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1757c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 1767c478bd9Sstevel@tonic-gate }; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate int 1797c478bd9Sstevel@tonic-gate _init(void) 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate int 1857c478bd9Sstevel@tonic-gate _fini(void) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate int 1917c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 197f3307d20Sarao void 198f3307d20Sarao vnex_intr_dist(void *arg) 199f3307d20Sarao { 200f3307d20Sarao vnex_id_t *vid_p; 201f3307d20Sarao uint32_t cpuid; 202f3307d20Sarao int intr_state; 203f3307d20Sarao hrtime_t start; 204f3307d20Sarao 205f3307d20Sarao mutex_enter(&vnex_id_lock); 206f3307d20Sarao 207f3307d20Sarao for (vid_p = vnex_id_list; vid_p != NULL; 208f3307d20Sarao vid_p = vid_p->vid_next) { 209f3307d20Sarao /* 210f3307d20Sarao * Don't do anything for disabled interrupts. 211f3307d20Sarao * vnex_enable_intr takes care of redistributing interrupts. 212f3307d20Sarao */ 213f3307d20Sarao if ((hvio_intr_getvalid(vid_p->vid_ihdl, 214f3307d20Sarao &intr_state) == H_EOK) && (intr_state == HV_INTR_NOTVALID)) 215f3307d20Sarao continue; 216f3307d20Sarao 217f3307d20Sarao cpuid = intr_dist_cpuid(); 218f3307d20Sarao 219f3307d20Sarao (void) hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_NOTVALID); 220f3307d20Sarao /* 221f3307d20Sarao * Make a best effort to wait for pending interrupts to finish. 222f3307d20Sarao * There is not much we can do if we timeout. 223f3307d20Sarao */ 224f3307d20Sarao start = gethrtime(); 225f3307d20Sarao while (!panicstr && 226f3307d20Sarao (hvio_intr_getstate(vid_p->vid_ihdl, &intr_state) == 227f3307d20Sarao H_EOK) && (intr_state == HV_INTR_DELIVERED_STATE)) { 228f3307d20Sarao if (gethrtime() - start > vnex_pending_timeout) { 229f3307d20Sarao cmn_err(CE_WARN, "vnex_intr_dist: %s%d " 230f3307d20Sarao "ino 0x%x pending: timedout\n", 231f3307d20Sarao ddi_driver_name(vid_p->vid_dip), 232f3307d20Sarao ddi_get_instance(vid_p->vid_dip), 233f3307d20Sarao vid_p->vid_ino); 234f3307d20Sarao break; 235f3307d20Sarao } 236f3307d20Sarao } 237f3307d20Sarao (void) hvio_intr_settarget(vid_p->vid_ihdl, cpuid); 238f3307d20Sarao (void) hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_VALID); 239f3307d20Sarao } 240f3307d20Sarao mutex_exit(&vnex_id_lock); 241f3307d20Sarao } 242f3307d20Sarao 2437c478bd9Sstevel@tonic-gate static int 244f3307d20Sarao vnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate switch (cmd) { 2477c478bd9Sstevel@tonic-gate case DDI_ATTACH: 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * Intitialize interrupt descriptor list 2507c478bd9Sstevel@tonic-gate * and mutex. 2517c478bd9Sstevel@tonic-gate */ 2527c478bd9Sstevel@tonic-gate vnex_id_list = NULL; 2537c478bd9Sstevel@tonic-gate mutex_init(&vnex_id_lock, NULL, MUTEX_DRIVER, NULL); 254f3307d20Sarao /* 255f3307d20Sarao * Add interrupt redistribution callback. 256f3307d20Sarao */ 257f3307d20Sarao intr_dist_add(vnex_intr_dist, dip); 2587c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate case DDI_RESUME: 2617c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate default: 2647c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2697c478bd9Sstevel@tonic-gate static int 270f3307d20Sarao vnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2717c478bd9Sstevel@tonic-gate { 2727c478bd9Sstevel@tonic-gate switch (cmd) { 2737c478bd9Sstevel@tonic-gate case DDI_DETACH: 2747c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 2777c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate default: 2807c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate static int 2857c478bd9Sstevel@tonic-gate vnex_ctl(dev_info_t *dip, dev_info_t *rdip, 2867c478bd9Sstevel@tonic-gate ddi_ctl_enum_t ctlop, void *arg, void *result) 2877c478bd9Sstevel@tonic-gate { 2887c478bd9Sstevel@tonic-gate char name[12]; /* enough for a decimal integer */ 2897c478bd9Sstevel@tonic-gate int reglen; 2907c478bd9Sstevel@tonic-gate uint32_t *vnex_regspec; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate switch (ctlop) { 2937c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV: 2947c478bd9Sstevel@tonic-gate if (rdip == NULL) 2957c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2967c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?virtual-device: %s%d\n", 2977c478bd9Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 2987c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD: 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg; 3037c478bd9Sstevel@tonic-gate 304a3282898Scth if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 3057c478bd9Sstevel@tonic-gate "reg", (caddr_t)&vnex_regspec, ®len) != DDI_SUCCESS) 3067c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate (void) sprintf(name, "%x", *vnex_regspec); 3097c478bd9Sstevel@tonic-gate ddi_set_name_addr(child, name); 3107c478bd9Sstevel@tonic-gate ddi_set_parent_data(child, NULL); 3117c478bd9Sstevel@tonic-gate kmem_free((caddr_t)vnex_regspec, reglen); 3127c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD: 3177c478bd9Sstevel@tonic-gate { 3187c478bd9Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate ddi_set_name_addr(child, NULL); 3217c478bd9Sstevel@tonic-gate ddi_remove_minor_node(arg, NULL); 3227c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * These ops correspond to functions that "shouldn't" be called 3277c478bd9Sstevel@tonic-gate * by a pseudo driver. So we whinge when we're called. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate case DDI_CTLOPS_DMAPMAPC: 3307c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REPORTINT: 3317c478bd9Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE: 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate *((off_t *)result) = 0; 3347c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate case DDI_CTLOPS_NREGS: 3377c478bd9Sstevel@tonic-gate { 3387c478bd9Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg; 339a3282898Scth if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 3407c478bd9Sstevel@tonic-gate "reg", (caddr_t)&vnex_regspec, ®len) != DDI_SUCCESS) 3417c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3427c478bd9Sstevel@tonic-gate *((uint_t *)result) = reglen / sizeof (uint32_t); 3437c478bd9Sstevel@tonic-gate kmem_free((caddr_t)vnex_regspec, reglen); 3447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate case DDI_CTLOPS_SIDDEV: 3477c478bd9Sstevel@tonic-gate case DDI_CTLOPS_SLAVEONLY: 3487c478bd9Sstevel@tonic-gate case DDI_CTLOPS_AFFINITY: 3497c478bd9Sstevel@tonic-gate case DDI_CTLOPS_IOMIN: 3507c478bd9Sstevel@tonic-gate case DDI_CTLOPS_POKE: 3517c478bd9Sstevel@tonic-gate case DDI_CTLOPS_PEEK: 3527c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "%s%d: invalid op (%d) from %s%d\n", 3537c478bd9Sstevel@tonic-gate ddi_get_name(dip), ddi_get_instance(dip), 3547c478bd9Sstevel@tonic-gate ctlop, ddi_get_name(rdip), ddi_get_instance(rdip)); 3557c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate default: 3617c478bd9Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static int 3667c478bd9Sstevel@tonic-gate vnex_get_pil(dev_info_t *rdip) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate int i; 3697c478bd9Sstevel@tonic-gate caddr_t name; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate name = ddi_node_name(rdip); 3727c478bd9Sstevel@tonic-gate for (i = 0; i < VNEX_MAX_DEVS; i++) { 3737c478bd9Sstevel@tonic-gate if (strcmp(vnex_name_to_pil[i].name, 3747c478bd9Sstevel@tonic-gate name) == 0) { 3757c478bd9Sstevel@tonic-gate return (vnex_name_to_pil[i].pil); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate /* 3797c478bd9Sstevel@tonic-gate * if not found pil is 0 3807c478bd9Sstevel@tonic-gate */ 3817c478bd9Sstevel@tonic-gate return (0); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate static int 3857c478bd9Sstevel@tonic-gate vnex_enable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate vnex_id_t *vid_p; 3887c478bd9Sstevel@tonic-gate uint32_t cpuid; 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate vid_p = vnex_locate_id(rdip, hdlp->ih_vector); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate ASSERT(vid_p != NULL); 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate cpuid = intr_dist_cpuid(); 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate if ((hvio_intr_settarget(vid_p->vid_ihdl, cpuid)) != H_EOK) { 3977c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate if (hvio_intr_setstate(vid_p->vid_ihdl, HV_INTR_IDLE_STATE) != H_EOK) { 4017c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate if ((hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_VALID)) != H_EOK) { 4057c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate static int 4127c478bd9Sstevel@tonic-gate vnex_disable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp) 4137c478bd9Sstevel@tonic-gate { 4147c478bd9Sstevel@tonic-gate vnex_id_t *vid_p; 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate vid_p = vnex_locate_id(rdip, hdlp->ih_vector); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate ASSERT(vid_p != NULL); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if (hvio_intr_setvalid(vid_p->vid_ihdl, HV_INTR_NOTVALID) != H_EOK) { 4217c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate static int 4287c478bd9Sstevel@tonic-gate vnex_add_intr(dev_info_t *dip, dev_info_t *rdip, 4297c478bd9Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate int reglen, ret = DDI_SUCCESS; 4327c478bd9Sstevel@tonic-gate vnex_id_t *vid_p; 4337c478bd9Sstevel@tonic-gate uint64_t cfg; 4347c478bd9Sstevel@tonic-gate uint32_t ino; 4357c478bd9Sstevel@tonic-gate uint64_t ihdl; 4367c478bd9Sstevel@tonic-gate vnex_regspec_t *reg_p; 4377c478bd9Sstevel@tonic-gate 438a3282898Scth if (ddi_getlongprop(DDI_DEV_T_ANY, dip, 4397c478bd9Sstevel@tonic-gate DDI_PROP_DONTPASS, "reg", (caddr_t)®_p, 4407c478bd9Sstevel@tonic-gate ®len) != DDI_SUCCESS) { 4417c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate /* 4457c478bd9Sstevel@tonic-gate * get the sun4v config handle for this device 4467c478bd9Sstevel@tonic-gate */ 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate cfg = SUN4V_REG_SPEC2CFG_HDL(reg_p->physaddr); 449ea841a36Sarao kmem_free(reg_p, reglen); 4507c478bd9Sstevel@tonic-gate ino = hdlp->ih_vector; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate /* 4537c478bd9Sstevel@tonic-gate * call hv to get vihdl 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate if (hvio_intr_devino_to_sysino(cfg, ino, &ihdl) != H_EOK) 4567c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate hdlp->ih_vector = ihdl; 4597c478bd9Sstevel@tonic-gate /* 4607c478bd9Sstevel@tonic-gate * Allocate a interrupt descriptor (id) with the 4617c478bd9Sstevel@tonic-gate * the interrupt handler and append it to 4627c478bd9Sstevel@tonic-gate * the id list. 4637c478bd9Sstevel@tonic-gate */ 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate vid_p = vnex_alloc_id(rdip, ino, cfg); 4667c478bd9Sstevel@tonic-gate vid_p->vid_ihdl = ihdl; 4677c478bd9Sstevel@tonic-gate vid_p->vid_handler = hdlp->ih_cb_func; 4687c478bd9Sstevel@tonic-gate vid_p->vid_arg1 = hdlp->ih_cb_arg1; 4697c478bd9Sstevel@tonic-gate vid_p->vid_arg2 = hdlp->ih_cb_arg2; 4707c478bd9Sstevel@tonic-gate vid_p->vid_ddi_hdlp = hdlp; 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, 4737c478bd9Sstevel@tonic-gate (ddi_intr_handler_t *)vnex_intr_wrapper, (caddr_t)vid_p, NULL); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate if (hdlp->ih_pri == 0) { 4767c478bd9Sstevel@tonic-gate hdlp->ih_pri = vnex_get_pil(rdip); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate ret = i_ddi_add_ivintr(hdlp); 4807c478bd9Sstevel@tonic-gate if (ret != DDI_SUCCESS) { 4817c478bd9Sstevel@tonic-gate return (ret); 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, vid_p->vid_handler, 4857c478bd9Sstevel@tonic-gate vid_p->vid_arg1, vid_p->vid_arg2); 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate return (ret); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate static int 4917c478bd9Sstevel@tonic-gate vnex_remove_intr(dev_info_t *rdip, 4927c478bd9Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp) 4937c478bd9Sstevel@tonic-gate { 4947c478bd9Sstevel@tonic-gate vnex_id_t *vid_p; 4957c478bd9Sstevel@tonic-gate uint32_t ino; 4967c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate ino = hdlp->ih_vector; 4997c478bd9Sstevel@tonic-gate vid_p = vnex_locate_id(rdip, ino); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate hdlp->ih_vector = vid_p->vid_ihdl; 5027c478bd9Sstevel@tonic-gate i_ddi_rem_ivintr(hdlp); 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate vnex_free_id(vid_p); 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate return (ret); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate static int 5107c478bd9Sstevel@tonic-gate vnex_intr_ops(dev_info_t *dip, dev_info_t *rdip, 5117c478bd9Sstevel@tonic-gate ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result) 5127c478bd9Sstevel@tonic-gate { 5137c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate switch (intr_op) { 5167c478bd9Sstevel@tonic-gate case DDI_INTROP_GETCAP: 517*a195726fSgovinda *(int *)result = DDI_INTR_FLAG_LEVEL; 5187c478bd9Sstevel@tonic-gate break; 5197c478bd9Sstevel@tonic-gate case DDI_INTROP_ALLOC: 5207c478bd9Sstevel@tonic-gate *(int *)result = hdlp->ih_scratch1; 5217c478bd9Sstevel@tonic-gate break; 5227c478bd9Sstevel@tonic-gate case DDI_INTROP_GETPRI: 523*a195726fSgovinda *(int *)result = hdlp->ih_pri ? 524*a195726fSgovinda hdlp->ih_pri : vnex_get_pil(rdip); 5257c478bd9Sstevel@tonic-gate break; 5267c478bd9Sstevel@tonic-gate case DDI_INTROP_FREE: 5277c478bd9Sstevel@tonic-gate break; 5287c478bd9Sstevel@tonic-gate case DDI_INTROP_SETPRI: 5297c478bd9Sstevel@tonic-gate break; 5307c478bd9Sstevel@tonic-gate case DDI_INTROP_ADDISR: 5317c478bd9Sstevel@tonic-gate ret = vnex_add_intr(dip, rdip, hdlp); 5327c478bd9Sstevel@tonic-gate break; 5337c478bd9Sstevel@tonic-gate case DDI_INTROP_REMISR: 5347c478bd9Sstevel@tonic-gate ret = vnex_remove_intr(rdip, hdlp); 5357c478bd9Sstevel@tonic-gate break; 5367c478bd9Sstevel@tonic-gate case DDI_INTROP_ENABLE: 5377c478bd9Sstevel@tonic-gate ret = vnex_enable_intr(rdip, hdlp); 5387c478bd9Sstevel@tonic-gate break; 5397c478bd9Sstevel@tonic-gate case DDI_INTROP_DISABLE: 5407c478bd9Sstevel@tonic-gate ret = vnex_disable_intr(rdip, hdlp); 5417c478bd9Sstevel@tonic-gate break; 5427c478bd9Sstevel@tonic-gate case DDI_INTROP_NINTRS: 5437c478bd9Sstevel@tonic-gate case DDI_INTROP_NAVAIL: 5447c478bd9Sstevel@tonic-gate *(int *)result = i_ddi_get_nintrs(rdip); 5457c478bd9Sstevel@tonic-gate break; 5467c478bd9Sstevel@tonic-gate case DDI_INTROP_SUPPORTED_TYPES: 5477c478bd9Sstevel@tonic-gate *(int *)result = i_ddi_get_nintrs(rdip) ? 5487c478bd9Sstevel@tonic-gate DDI_INTR_TYPE_FIXED : 0; 5497c478bd9Sstevel@tonic-gate break; 5507c478bd9Sstevel@tonic-gate default: 5517c478bd9Sstevel@tonic-gate ret = DDI_ENOTSUP; 5527c478bd9Sstevel@tonic-gate break; 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate return (ret); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate vnex_id_t * 5597c478bd9Sstevel@tonic-gate vnex_alloc_id(dev_info_t *dip, uint32_t ino, uint64_t dhdl) 5607c478bd9Sstevel@tonic-gate { 5617c478bd9Sstevel@tonic-gate vnex_id_t *vid_p = kmem_alloc(sizeof (vnex_id_t), KM_SLEEP); 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate vid_p->vid_dip = dip; 5647c478bd9Sstevel@tonic-gate vid_p->vid_ino = ino; 5657c478bd9Sstevel@tonic-gate vid_p->vid_cfg_hdl = dhdl; 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate mutex_enter(&vnex_id_lock); 5687c478bd9Sstevel@tonic-gate vnex_add_id(vid_p); 5697c478bd9Sstevel@tonic-gate mutex_exit(&vnex_id_lock); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate return (vid_p); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate vnex_id_t * 5757c478bd9Sstevel@tonic-gate vnex_locate_id(dev_info_t *dip, uint32_t ino) 5767c478bd9Sstevel@tonic-gate { 5777c478bd9Sstevel@tonic-gate vnex_id_t *vid_p; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate mutex_enter(&vnex_id_lock); 5807c478bd9Sstevel@tonic-gate vid_p = vnex_id_list; 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate while (vid_p != NULL) { 5837c478bd9Sstevel@tonic-gate if (vid_p->vid_dip == dip && vid_p->vid_ino == ino) { 5847c478bd9Sstevel@tonic-gate mutex_exit(&vnex_id_lock); 5857c478bd9Sstevel@tonic-gate return (vid_p); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate vid_p = vid_p->vid_next; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate mutex_exit(&vnex_id_lock); 5907c478bd9Sstevel@tonic-gate return (NULL); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate static void 5947c478bd9Sstevel@tonic-gate vnex_free_id(vnex_id_t *vid_p) 5957c478bd9Sstevel@tonic-gate { 5967c478bd9Sstevel@tonic-gate mutex_enter(&vnex_id_lock); 5977c478bd9Sstevel@tonic-gate vnex_rem_id(vid_p); 5987c478bd9Sstevel@tonic-gate mutex_exit(&vnex_id_lock); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate kmem_free(vid_p, sizeof (*vid_p)); 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate static void 6047c478bd9Sstevel@tonic-gate vnex_rem_id(vnex_id_t *vid_p) 6057c478bd9Sstevel@tonic-gate { 6067c478bd9Sstevel@tonic-gate if (vid_p->vid_prev == NULL) { 6077c478bd9Sstevel@tonic-gate vnex_id_list = vid_p->vid_next; 6087c478bd9Sstevel@tonic-gate vid_p->vid_prev = NULL; 6097c478bd9Sstevel@tonic-gate } else if (vid_p->vid_next == NULL) { 6107c478bd9Sstevel@tonic-gate vid_p->vid_prev->vid_next = NULL; 6117c478bd9Sstevel@tonic-gate } else { 6127c478bd9Sstevel@tonic-gate vid_p->vid_prev->vid_next = vid_p->vid_next; 6137c478bd9Sstevel@tonic-gate vid_p->vid_next->vid_prev = vid_p->vid_prev; 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate static void 6187c478bd9Sstevel@tonic-gate vnex_add_id(vnex_id_t *vid_p) 6197c478bd9Sstevel@tonic-gate { 6207c478bd9Sstevel@tonic-gate if (vnex_id_list == NULL) { 6217c478bd9Sstevel@tonic-gate vnex_id_list = vid_p; 6227c478bd9Sstevel@tonic-gate vid_p->vid_next = NULL; 6237c478bd9Sstevel@tonic-gate vid_p->vid_prev = NULL; 6247c478bd9Sstevel@tonic-gate return; 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate /* 6277c478bd9Sstevel@tonic-gate * We always just add to the front of the list 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate vnex_id_list->vid_prev = vid_p; 6307c478bd9Sstevel@tonic-gate vid_p->vid_next = vnex_id_list; 6317c478bd9Sstevel@tonic-gate vid_p->vid_prev = NULL; 6327c478bd9Sstevel@tonic-gate vnex_id_list = vid_p; 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate uint_t 6367c478bd9Sstevel@tonic-gate vnex_intr_wrapper(caddr_t arg) 6377c478bd9Sstevel@tonic-gate { 6387c478bd9Sstevel@tonic-gate vnex_id_t *vid_p = (vnex_id_t *)arg; 6397c478bd9Sstevel@tonic-gate int res; 6407c478bd9Sstevel@tonic-gate uint_t (*handler)(); 6417c478bd9Sstevel@tonic-gate caddr_t handler_arg1; 6427c478bd9Sstevel@tonic-gate caddr_t handler_arg2; 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate handler = vid_p->vid_handler; 6457c478bd9Sstevel@tonic-gate handler_arg1 = vid_p->vid_arg1; 6467c478bd9Sstevel@tonic-gate handler_arg2 = vid_p->vid_arg2; 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate res = (*handler)(handler_arg1, handler_arg2); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate (void) hvio_intr_setstate(vid_p->vid_ihdl, HV_INTR_IDLE_STATE); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate return (res); 6537c478bd9Sstevel@tonic-gate } 654