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