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 /* 23*f8047eabSsudheer * 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" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 327c478bd9Sstevel@tonic-gate #include <sys/systm.h> 337c478bd9Sstevel@tonic-gate #include <sys/spl.h> 347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 357c478bd9Sstevel@tonic-gate #include <sys/debug.h> 367c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * Handle software interrupts through 'softcall' mechanism 40*f8047eabSsudheer * 41*f8047eabSsudheer * At present softcall mechanism uses a global list headed by softhead. 42*f8047eabSsudheer * Entries are added to tail and removed from head so as to preserve FIFO 43*f8047eabSsudheer * nature of entries in the softcall list. softcall() takes care of adding 44*f8047eabSsudheer * entries to the softtail. 45*f8047eabSsudheer * 46*f8047eabSsudheer * softint must take care of executing the entries in the FIFO 47*f8047eabSsudheer * order. It could be called simultaneously from multiple cpus, however only 48*f8047eabSsudheer * one instance of softint should process the softcall list, this is 49*f8047eabSsudheer * ensured by 50*f8047eabSsudheer * - the state the variable softcall_state will be at time to time. 51*f8047eabSsudheer * (IDLE->PEND->DRAIN->IDLE) 52*f8047eabSsudheer * 53*f8047eabSsudheer * These states are needed for softcall mechanism since Solaris has only 54*f8047eabSsudheer * one interface(ie. siron ) as of now for 55*f8047eabSsudheer * - raising a soft interrupt architecture independently(ie not through 56*f8047eabSsudheer * setsoftint(..) ) 57*f8047eabSsudheer * - to process the softcall queue. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define NSOFTCALLS 200 61*f8047eabSsudheer /* 62*f8047eabSsudheer * Defined states for softcall processing. 63*f8047eabSsudheer */ 64*f8047eabSsudheer #define SOFT_IDLE 0x01 /* no processing is needed */ 65*f8047eabSsudheer #define SOFT_PEND 0x02 /* softcall list needs processing */ 66*f8047eabSsudheer #define SOFT_DRAIN 0x04 /* the list is being processed */ 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate typedef struct softcall { 697c478bd9Sstevel@tonic-gate void (*sc_func)(void *); /* function to call */ 707c478bd9Sstevel@tonic-gate void *sc_arg; /* arg to pass to func */ 717c478bd9Sstevel@tonic-gate struct softcall *sc_next; /* next in list */ 727c478bd9Sstevel@tonic-gate } softcall_t; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate static softcall_t softcalls[NSOFTCALLS], *softhead, *softtail, *softfree; 75*f8047eabSsudheer static uint_t softcall_state; 767c478bd9Sstevel@tonic-gate 77*f8047eabSsudheer /* 78*f8047eabSsudheer * protects softcall lists and control variable softcall_state. 79*f8047eabSsudheer */ 80*f8047eabSsudheer static kmutex_t softcall_lock; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static void (*kdi_softcall_func)(void); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate extern void siron(void); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate void 877c478bd9Sstevel@tonic-gate softcall_init(void) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate softcall_t *sc; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate for (sc = softcalls; sc < &softcalls[NSOFTCALLS]; sc++) { 927c478bd9Sstevel@tonic-gate sc->sc_next = softfree; 937c478bd9Sstevel@tonic-gate softfree = sc; 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate mutex_init(&softcall_lock, NULL, MUTEX_SPIN, (void *)ipltospl(SPL8)); 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * Call function func with argument arg 1007c478bd9Sstevel@tonic-gate * at some later time at software interrupt priority 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate void 1037c478bd9Sstevel@tonic-gate softcall(void (*func)(void *), void *arg) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate softcall_t *sc; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * protect against cross-calls 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate mutex_enter(&softcall_lock); 1117c478bd9Sstevel@tonic-gate /* coalesce identical softcalls */ 1127c478bd9Sstevel@tonic-gate for (sc = softhead; sc != 0; sc = sc->sc_next) { 1137c478bd9Sstevel@tonic-gate if (sc->sc_func == func && sc->sc_arg == arg) { 1147c478bd9Sstevel@tonic-gate mutex_exit(&softcall_lock); 1157c478bd9Sstevel@tonic-gate return; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if ((sc = softfree) == 0) 1207c478bd9Sstevel@tonic-gate panic("too many softcalls"); 1217c478bd9Sstevel@tonic-gate softfree = sc->sc_next; 1227c478bd9Sstevel@tonic-gate sc->sc_func = func; 1237c478bd9Sstevel@tonic-gate sc->sc_arg = arg; 1247c478bd9Sstevel@tonic-gate sc->sc_next = 0; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (softhead) { 1277c478bd9Sstevel@tonic-gate softtail->sc_next = sc; 1287c478bd9Sstevel@tonic-gate softtail = sc; 1297c478bd9Sstevel@tonic-gate mutex_exit(&softcall_lock); 1307c478bd9Sstevel@tonic-gate } else { 1317c478bd9Sstevel@tonic-gate softhead = softtail = sc; 132*f8047eabSsudheer if (softcall_state == SOFT_DRAIN) 133*f8047eabSsudheer /* 134*f8047eabSsudheer * softint is already running; no need to 135*f8047eabSsudheer * raise a siron. Due to lock protection of 136*f8047eabSsudheer * softhead / softcall state, we know 137*f8047eabSsudheer * that softint() will see the new addition to 138*f8047eabSsudheer * the softhead queue. 139*f8047eabSsudheer */ 140*f8047eabSsudheer mutex_exit(&softcall_lock); 141*f8047eabSsudheer else { 142*f8047eabSsudheer softcall_state = SOFT_PEND; 1437c478bd9Sstevel@tonic-gate mutex_exit(&softcall_lock); 1447c478bd9Sstevel@tonic-gate siron(); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate } 147*f8047eabSsudheer } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate void 1507c478bd9Sstevel@tonic-gate kdi_softcall(void (*func)(void)) 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate kdi_softcall_func = func; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate if (softhead == NULL) 1557c478bd9Sstevel@tonic-gate siron(); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 159*f8047eabSsudheer * Called to process software interrupts take one off queue, call it, 160*f8047eabSsudheer * repeat. 161*f8047eabSsudheer * 162*f8047eabSsudheer * Note queue may change during call; softcall_lock and state variables 163*f8047eabSsudheer * softcall_state ensures that 164*f8047eabSsudheer * -we don't have multiple cpus pulling from the list (thus causing 165*f8047eabSsudheer * a violation of FIFO order). 166*f8047eabSsudheer * -we don't miss a new entry having been added to the head. 167*f8047eabSsudheer * -we don't miss a wakeup. 1687c478bd9Sstevel@tonic-gate */ 169*f8047eabSsudheer 1707c478bd9Sstevel@tonic-gate void 1717c478bd9Sstevel@tonic-gate softint(void) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate softcall_t *sc; 1747c478bd9Sstevel@tonic-gate void (*func)(); 1757c478bd9Sstevel@tonic-gate caddr_t arg; 1767c478bd9Sstevel@tonic-gate 177*f8047eabSsudheer /* 178*f8047eabSsudheer * Check if we are asked to process the softcall list. 179*f8047eabSsudheer */ 1807c478bd9Sstevel@tonic-gate mutex_enter(&softcall_lock); 181*f8047eabSsudheer if (softcall_state != SOFT_PEND) { 182*f8047eabSsudheer mutex_exit(&softcall_lock); 183*f8047eabSsudheer goto out; 184*f8047eabSsudheer } 185*f8047eabSsudheer softcall_state = SOFT_DRAIN; 186*f8047eabSsudheer 187*f8047eabSsudheer for (;;) { 1887c478bd9Sstevel@tonic-gate if ((sc = softhead) != NULL) { 1897c478bd9Sstevel@tonic-gate func = sc->sc_func; 1907c478bd9Sstevel@tonic-gate arg = sc->sc_arg; 1917c478bd9Sstevel@tonic-gate softhead = sc->sc_next; 1927c478bd9Sstevel@tonic-gate sc->sc_next = softfree; 1937c478bd9Sstevel@tonic-gate softfree = sc; 1947c478bd9Sstevel@tonic-gate } 195*f8047eabSsudheer if (sc == NULL) { 196*f8047eabSsudheer softcall_state = SOFT_IDLE; 1977c478bd9Sstevel@tonic-gate mutex_exit(&softcall_lock); 1987c478bd9Sstevel@tonic-gate break; 1997c478bd9Sstevel@tonic-gate } 200*f8047eabSsudheer mutex_exit(&softcall_lock); 201*f8047eabSsudheer func(arg); 202*f8047eabSsudheer mutex_enter(&softcall_lock); 203*f8047eabSsudheer } 204*f8047eabSsudheer out: 2057c478bd9Sstevel@tonic-gate if ((func = kdi_softcall_func) != NULL) { 2067c478bd9Sstevel@tonic-gate kdi_softcall_func = NULL; 2077c478bd9Sstevel@tonic-gate func(); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate } 210