| 1 | /* |
| 2 | * Copyright (c) 1997, Stefan Esser <se@freebsd.org> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice unmodified, this list of conditions, and the following |
| 10 | * disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | * |
| 26 | * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $ |
| 27 | * $DragonFly: src/sys/kern/kern_intr.c,v 1.2 2003/06/17 04:28:41 dillon Exp $ |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/malloc.h> |
| 35 | #include <sys/kernel.h> |
| 36 | #include <sys/sysctl.h> |
| 37 | |
| 38 | #include <machine/ipl.h> |
| 39 | |
| 40 | #include <sys/interrupt.h> |
| 41 | |
| 42 | struct swilist { |
| 43 | swihand_t *sl_handler; |
| 44 | struct swilist *sl_next; |
| 45 | }; |
| 46 | |
| 47 | static struct swilist swilists[NSWI]; |
| 48 | |
| 49 | void |
| 50 | register_swi(intr, handler) |
| 51 | int intr; |
| 52 | swihand_t *handler; |
| 53 | { |
| 54 | struct swilist *slp, *slq; |
| 55 | int s; |
| 56 | |
| 57 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 58 | panic("register_swi: bad intr %d", intr); |
| 59 | if (handler == swi_generic || handler == swi_null) |
| 60 | panic("register_swi: bad handler %p", (void *)handler); |
| 61 | slp = &swilists[intr - NHWI]; |
| 62 | s = splhigh(); |
| 63 | if (ihandlers[intr] == swi_null) |
| 64 | ihandlers[intr] = handler; |
| 65 | else { |
| 66 | if (slp->sl_next == NULL) { |
| 67 | slp->sl_handler = ihandlers[intr]; |
| 68 | ihandlers[intr] = swi_generic; |
| 69 | } |
| 70 | slq = malloc(sizeof(*slq), M_DEVBUF, M_NOWAIT); |
| 71 | if (slq == NULL) |
| 72 | panic("register_swi: malloc failed"); |
| 73 | slq->sl_handler = handler; |
| 74 | slq->sl_next = NULL; |
| 75 | while (slp->sl_next != NULL) |
| 76 | slp = slp->sl_next; |
| 77 | slp->sl_next = slq; |
| 78 | } |
| 79 | splx(s); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | swi_dispatcher(intr) |
| 84 | int intr; |
| 85 | { |
| 86 | struct swilist *slp; |
| 87 | |
| 88 | slp = &swilists[intr - NHWI]; |
| 89 | do { |
| 90 | (*slp->sl_handler)(); |
| 91 | slp = slp->sl_next; |
| 92 | } while (slp != NULL); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | unregister_swi(intr, handler) |
| 97 | int intr; |
| 98 | swihand_t *handler; |
| 99 | { |
| 100 | struct swilist *slfoundpred, *slp, *slq; |
| 101 | int s; |
| 102 | |
| 103 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 104 | panic("unregister_swi: bad intr %d", intr); |
| 105 | if (handler == swi_generic || handler == swi_null) |
| 106 | panic("unregister_swi: bad handler %p", (void *)handler); |
| 107 | slp = &swilists[intr - NHWI]; |
| 108 | s = splhigh(); |
| 109 | if (ihandlers[intr] == handler) |
| 110 | ihandlers[intr] = swi_null; |
| 111 | else if (slp->sl_next != NULL) { |
| 112 | slfoundpred = NULL; |
| 113 | for (slq = slp->sl_next; slq != NULL; |
| 114 | slp = slq, slq = slp->sl_next) |
| 115 | if (slq->sl_handler == handler) |
| 116 | slfoundpred = slp; |
| 117 | slp = &swilists[intr - NHWI]; |
| 118 | if (slfoundpred != NULL) { |
| 119 | slq = slfoundpred->sl_next; |
| 120 | slfoundpred->sl_next = slq->sl_next; |
| 121 | free(slq, M_DEVBUF); |
| 122 | } else if (slp->sl_handler == handler) { |
| 123 | slq = slp->sl_next; |
| 124 | slp->sl_next = slq->sl_next; |
| 125 | slp->sl_handler = slq->sl_handler; |
| 126 | free(slq, M_DEVBUF); |
| 127 | } |
| 128 | if (slp->sl_next == NULL) |
| 129 | ihandlers[intr] = slp->sl_handler; |
| 130 | } |
| 131 | splx(s); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. |
| 136 | * The data for this machine dependent, and the declarations are in machine |
| 137 | * dependent code. The layout of intrnames and intrcnt however is machine |
| 138 | * independent. |
| 139 | * |
| 140 | * We do not know the length of intrcnt and intrnames at compile time, so |
| 141 | * calculate things at run time. |
| 142 | */ |
| 143 | static int |
| 144 | sysctl_intrnames(SYSCTL_HANDLER_ARGS) |
| 145 | { |
| 146 | return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, |
| 147 | req)); |
| 148 | } |
| 149 | |
| 150 | SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 151 | NULL, 0, sysctl_intrnames, "", "Interrupt Names"); |
| 152 | |
| 153 | static int |
| 154 | sysctl_intrcnt(SYSCTL_HANDLER_ARGS) |
| 155 | { |
| 156 | return (sysctl_handle_opaque(oidp, intrcnt, |
| 157 | (char *)eintrcnt - (char *)intrcnt, req)); |
| 158 | } |
| 159 | |
| 160 | SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 161 | NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); |