1 /* $OpenBSD: softintr.c,v 1.3 2008/06/26 05:42:09 ray Exp $ */ 2 /* $NetBSD: softintr.c,v 1.1 2003/02/26 21:26:12 fvdl Exp $ */ 3 4 /*- 5 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Generic soft interrupt implementation for NetBSD/x86. 35 */ 36 37 #include <sys/param.h> 38 #include <sys/malloc.h> 39 40 #include <machine/intr.h> 41 42 #include <uvm/uvm_extern.h> 43 44 struct x86_soft_intr x86_soft_intrs[X86_NSOFTINTR]; 45 46 const int x86_soft_intr_to_ssir[X86_NSOFTINTR] = { 47 SIR_CLOCK, 48 SIR_NET, 49 SIR_TTY, 50 }; 51 52 /* 53 * softintr_init: 54 * 55 * Initialize the software interrupt system. 56 */ 57 void 58 softintr_init(void) 59 { 60 struct x86_soft_intr *si; 61 int i; 62 63 for (i = 0; i < X86_NSOFTINTR; i++) { 64 si = &x86_soft_intrs[i]; 65 TAILQ_INIT(&si->softintr_q); 66 simple_lock_init(&si->softintr_slock); 67 si->softintr_ssir = x86_soft_intr_to_ssir[i]; 68 } 69 } 70 71 /* 72 * softintr_dispatch: 73 * 74 * Process pending software interrupts. 75 */ 76 void 77 softintr_dispatch(int which) 78 { 79 struct x86_soft_intr *si = &x86_soft_intrs[which]; 80 struct x86_soft_intrhand *sih; 81 int s; 82 83 for (;;) { 84 x86_softintr_lock(si, s); 85 sih = TAILQ_FIRST(&si->softintr_q); 86 if (sih == NULL) { 87 x86_softintr_unlock(si, s); 88 break; 89 } 90 TAILQ_REMOVE(&si->softintr_q, sih, sih_q); 91 sih->sih_pending = 0; 92 x86_softintr_unlock(si, s); 93 94 uvmexp.softs++; 95 (*sih->sih_fn)(sih->sih_arg); 96 } 97 } 98 99 /* 100 * softintr_establish: [interface] 101 * 102 * Register a software interrupt handler. 103 */ 104 void * 105 softintr_establish(int ipl, void (*func)(void *), void *arg) 106 { 107 struct x86_soft_intr *si; 108 struct x86_soft_intrhand *sih; 109 int which; 110 111 switch (ipl) { 112 case IPL_SOFTCLOCK: 113 which = X86_SOFTINTR_SOFTCLOCK; 114 break; 115 116 case IPL_SOFTNET: 117 which = X86_SOFTINTR_SOFTNET; 118 break; 119 120 case IPL_TTY: 121 case IPL_SOFTTTY: 122 which = X86_SOFTINTR_SOFTTTY; 123 break; 124 125 default: 126 panic("softintr_establish"); 127 } 128 129 si = &x86_soft_intrs[which]; 130 131 sih = malloc(sizeof(*sih), M_DEVBUF, M_NOWAIT); 132 if (__predict_true(sih != NULL)) { 133 sih->sih_intrhead = si; 134 sih->sih_fn = func; 135 sih->sih_arg = arg; 136 sih->sih_pending = 0; 137 } 138 return (sih); 139 } 140 141 /* 142 * softintr_disestablish: [interface] 143 * 144 * Unregister a software interrupt handler. 145 */ 146 void 147 softintr_disestablish(void *arg) 148 { 149 struct x86_soft_intrhand *sih = arg; 150 struct x86_soft_intr *si = sih->sih_intrhead; 151 int s; 152 153 x86_softintr_lock(si, s); 154 if (sih->sih_pending) { 155 TAILQ_REMOVE(&si->softintr_q, sih, sih_q); 156 sih->sih_pending = 0; 157 } 158 x86_softintr_unlock(si, s); 159 160 free(sih, M_DEVBUF); 161 } 162