1 /* $OpenBSD: softintr.c,v 1.9 2015/08/28 00:03:53 deraadt 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 mtx_init(&si->softintr_lock, IPL_HIGH); 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 cpu_info *ci = curcpu(); 80 struct x86_soft_intr *si = &x86_soft_intrs[which]; 81 struct x86_soft_intrhand *sih; 82 int floor; 83 84 floor = ci->ci_handled_intr_level; 85 ci->ci_handled_intr_level = ci->ci_ilevel; 86 87 KERNEL_LOCK(); 88 for (;;) { 89 mtx_enter(&si->softintr_lock); 90 sih = TAILQ_FIRST(&si->softintr_q); 91 if (sih == NULL) { 92 mtx_leave(&si->softintr_lock); 93 break; 94 } 95 TAILQ_REMOVE(&si->softintr_q, sih, sih_q); 96 sih->sih_pending = 0; 97 98 uvmexp.softs++; 99 100 mtx_leave(&si->softintr_lock); 101 102 (*sih->sih_fn)(sih->sih_arg); 103 } 104 KERNEL_UNLOCK(); 105 106 ci->ci_handled_intr_level = floor; 107 } 108 109 /* 110 * softintr_establish: [interface] 111 * 112 * Register a software interrupt handler. 113 */ 114 void * 115 softintr_establish(int ipl, void (*func)(void *), void *arg) 116 { 117 struct x86_soft_intr *si; 118 struct x86_soft_intrhand *sih; 119 int which; 120 121 switch (ipl) { 122 case IPL_SOFTCLOCK: 123 which = X86_SOFTINTR_SOFTCLOCK; 124 break; 125 126 case IPL_SOFTNET: 127 which = X86_SOFTINTR_SOFTNET; 128 break; 129 130 case IPL_TTY: 131 case IPL_SOFTTTY: 132 which = X86_SOFTINTR_SOFTTTY; 133 break; 134 135 default: 136 panic("softintr_establish"); 137 } 138 139 si = &x86_soft_intrs[which]; 140 141 sih = malloc(sizeof(*sih), M_DEVBUF, M_NOWAIT); 142 if (__predict_true(sih != NULL)) { 143 sih->sih_intrhead = si; 144 sih->sih_fn = func; 145 sih->sih_arg = arg; 146 sih->sih_pending = 0; 147 } 148 return (sih); 149 } 150 151 /* 152 * softintr_disestablish: [interface] 153 * 154 * Unregister a software interrupt handler. 155 */ 156 void 157 softintr_disestablish(void *arg) 158 { 159 struct x86_soft_intrhand *sih = arg; 160 struct x86_soft_intr *si = sih->sih_intrhead; 161 162 mtx_enter(&si->softintr_lock); 163 if (sih->sih_pending) { 164 TAILQ_REMOVE(&si->softintr_q, sih, sih_q); 165 sih->sih_pending = 0; 166 } 167 mtx_leave(&si->softintr_lock); 168 169 free(sih, M_DEVBUF, sizeof(*sih)); 170 } 171