1 /* $OpenBSD: intr.h,v 1.29 2015/09/13 11:48:17 kettenis Exp $ */ 2 /* $NetBSD: intr.h,v 1.2 2003/05/04 22:01:56 fvdl Exp $ */ 3 4 /*- 5 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum, and 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 #ifndef _MACHINE_INTR_H_ 34 #define _MACHINE_INTR_H_ 35 36 #include <machine/intrdefs.h> 37 38 #ifndef _LOCORE 39 #include <machine/cpu.h> 40 41 #include <sys/evcount.h> 42 43 /* 44 * Struct describing an interrupt source for a CPU. struct cpu_info 45 * has an array of MAX_INTR_SOURCES of these. The index in the array 46 * is equal to the stub number of the stubcode as present in vector.s 47 * 48 * The primary CPU's array of interrupt sources has its first 16 49 * entries reserved for legacy ISA irq handlers. This means that 50 * they have a 1:1 mapping for arrayindex:irq_num. This is not 51 * true for interrupts that come in through IO APICs, to find 52 * their source, go through ci->ci_isources[index].is_pic 53 * 54 * It's possible to always maintain a 1:1 mapping, but that means 55 * limiting the total number of interrupt sources to MAX_INTR_SOURCES 56 * (32), instead of 32 per CPU. It also would mean that having multiple 57 * IO APICs which deliver interrupts from an equal pin number would 58 * overlap if they were to be sent to the same CPU. 59 */ 60 61 struct intrstub { 62 void *ist_entry; 63 void *ist_recurse; 64 void *ist_resume; 65 }; 66 67 struct intrsource { 68 int is_maxlevel; /* max. IPL for this source */ 69 int is_pin; /* IRQ for legacy; pin for IO APIC */ 70 struct intrhand *is_handlers; /* handler chain */ 71 struct pic *is_pic; /* originating PIC */ 72 void *is_recurse; /* entry for spllower */ 73 void *is_resume; /* entry for doreti */ 74 char is_evname[32]; /* event counter name */ 75 int is_flags; /* see below */ 76 int is_type; /* level, edge */ 77 int is_idtvec; 78 int is_minlevel; 79 }; 80 81 #define IS_LEGACY 0x0001 /* legacy ISA irq source */ 82 #define IS_IPI 0x0002 83 #define IS_LOG 0x0004 84 85 86 /* 87 * Interrupt handler chains. *_intr_establish() insert a handler into 88 * the list. The handler is called with its (single) argument. 89 */ 90 91 struct intrhand { 92 int (*ih_fun)(void *); 93 void *ih_arg; 94 int ih_level; 95 int ih_flags; 96 struct intrhand *ih_next; 97 int ih_pin; 98 int ih_slot; 99 struct cpu_info *ih_cpu; 100 int ih_irq; 101 struct evcount ih_count; 102 }; 103 104 #define IMASK(ci,level) (ci)->ci_imask[(level)] 105 #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)] 106 107 extern void Xspllower(int); 108 109 int splraise(int); 110 int spllower(int); 111 void softintr(int); 112 113 /* 114 * Convert spl level to local APIC level 115 */ 116 #define APIC_LEVEL(l) ((l) << 4) 117 118 /* 119 * compiler barrier: prevent reordering of instructions. 120 * XXX something similar will move to <sys/cdefs.h> 121 * or thereabouts. 122 * This prevents the compiler from reordering code around 123 * this "instruction", acting as a sequence point for code generation. 124 */ 125 126 #define __splbarrier() __asm volatile("":::"memory") 127 128 /* 129 * Hardware interrupt masks 130 */ 131 #define splbio() splraise(IPL_BIO) 132 #define splnet() splraise(IPL_NET) 133 #define spltty() splraise(IPL_TTY) 134 #define splaudio() splraise(IPL_AUDIO) 135 #define splclock() splraise(IPL_CLOCK) 136 #define splstatclock() splclock() 137 #define splipi() splraise(IPL_IPI) 138 139 /* 140 * Software interrupt masks 141 */ 142 #define splsoftclock() splraise(IPL_SOFTCLOCK) 143 #define splsoftnet() splraise(IPL_SOFTNET) 144 #define splsofttty() splraise(IPL_SOFTTTY) 145 146 /* 147 * Miscellaneous 148 */ 149 #define splvm() splraise(IPL_VM) 150 #define splhigh() splraise(IPL_HIGH) 151 #define spl0() spllower(IPL_NONE) 152 #define splsched() splraise(IPL_SCHED) 153 #define spllock() splhigh() 154 #define splx(x) spllower(x) 155 156 /* SPL asserts */ 157 #ifdef DIAGNOSTIC 158 /* 159 * Although this function is implemented in MI code, it must be in this MD 160 * header because we don't want this header to include MI includes. 161 */ 162 void splassert_fail(int, int, const char *); 163 extern int splassert_ctl; 164 void splassert_check(int, const char *); 165 #define splassert(__wantipl) do { \ 166 if (splassert_ctl > 0) { \ 167 splassert_check(__wantipl, __func__); \ 168 } \ 169 } while (0) 170 #define splsoftassert(wantipl) splassert(wantipl) 171 #else 172 #define splassert(wantipl) do { /* nada */ } while (0) 173 #define splsoftassert(wantipl) do { /* nada */ } while (0) 174 #endif 175 176 #define IPLSHIFT 4 /* The upper nibble of vectors is the IPL. */ 177 #define IPL(level) ((level) >> IPLSHIFT) /* Extract the IPL. */ 178 179 #include <machine/pic.h> 180 181 /* 182 * Stub declarations. 183 */ 184 185 extern void Xsoftclock(void); 186 extern void Xsoftnet(void); 187 extern void Xsofttty(void); 188 189 extern struct intrstub i8259_stubs[]; 190 extern struct intrstub ioapic_edge_stubs[]; 191 extern struct intrstub ioapic_level_stubs[]; 192 193 struct cpu_info; 194 195 extern int intr_shared_edge; 196 197 extern char idt_allocmap[]; 198 199 void intr_default_setup(void); 200 int x86_nmi(void); 201 void intr_calculatemasks(struct cpu_info *); 202 int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *); 203 int intr_allocate_slot(struct pic *, int, int, int, struct cpu_info **, int *, 204 int *); 205 void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), 206 void *, const char *); 207 void intr_disestablish(struct intrhand *); 208 int intr_handler(struct intrframe *, struct intrhand *); 209 void cpu_intr_init(struct cpu_info *); 210 int intr_find_mpmapping(int bus, int pin, int *handle); 211 void intr_printconfig(void); 212 void intr_barrier(void *); 213 214 #ifdef MULTIPROCESSOR 215 void x86_send_ipi(struct cpu_info *, int); 216 int x86_fast_ipi(struct cpu_info *, int); 217 void x86_broadcast_ipi(int); 218 void x86_ipi_handler(void); 219 void x86_setperf_ipi(struct cpu_info *); 220 221 extern void (*ipifunc[X86_NIPI])(struct cpu_info *); 222 #endif 223 224 #endif /* !_LOCORE */ 225 226 /* 227 * Generic software interrupt support. 228 */ 229 230 #define X86_SOFTINTR_SOFTCLOCK 0 231 #define X86_SOFTINTR_SOFTNET 1 232 #define X86_SOFTINTR_SOFTTTY 2 233 #define X86_NSOFTINTR 3 234 235 #ifndef _LOCORE 236 #include <sys/queue.h> 237 238 struct x86_soft_intrhand { 239 TAILQ_ENTRY(x86_soft_intrhand) 240 sih_q; 241 struct x86_soft_intr *sih_intrhead; 242 void (*sih_fn)(void *); 243 void *sih_arg; 244 int sih_pending; 245 }; 246 247 struct x86_soft_intr { 248 TAILQ_HEAD(, x86_soft_intrhand) 249 softintr_q; 250 int softintr_ssir; 251 struct mutex softintr_lock; 252 }; 253 254 void *softintr_establish(int, void (*)(void *), void *); 255 void softintr_disestablish(void *); 256 void softintr_init(void); 257 void softintr_dispatch(int); 258 259 #define softintr_schedule(arg) \ 260 do { \ 261 struct x86_soft_intrhand *__sih = (arg); \ 262 struct x86_soft_intr *__si = __sih->sih_intrhead; \ 263 \ 264 mtx_enter(&__si->softintr_lock); \ 265 if (__sih->sih_pending == 0) { \ 266 TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q); \ 267 __sih->sih_pending = 1; \ 268 softintr(__si->softintr_ssir); \ 269 } \ 270 mtx_leave(&__si->softintr_lock); \ 271 } while (/*CONSTCOND*/ 0) 272 #endif /* _LOCORE */ 273 274 #endif /* !_MACHINE_INTR_H_ */ 275