1 // core/irq.cc -- generic IRQ handling
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following conditions:
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimers.
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimers in the
17 // documentation and/or other materials provided with the distribution.
19 // * The names of the Software's authors and/or contributors
20 // may not be used to endorse or promote products derived from
21 // this Software without specific prior written permission.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
32 #include <kern/kernel.h>
36 void InterruptController::request_int(InterruptSlot *slot, Interrupt *irq)
38 AutoSpinLockIRQ autolock(lock);
40 u32 irqnum = get_irqnum(slot);
41 Interrupt *list = slot->first_int;
42 Interrupt **insert = &slot->first_int;
46 throw_idl(ResourceBusy, -1, countarray("IRQ in use"));
54 irq->controller = this;
56 if (insert == &slot->first_int && slot->mask_count == 0) {
57 // There were no existing handlers, so it's currently masked
62 void InterruptController::free_int(Interrupt *irq)
64 InterruptSlot *slot = irq->slot;
65 u32 irqnum = get_irqnum(slot);
67 DroppableAutoSpinLockIRQ autolock(lock);
69 Interrupt *list = slot->first_int;
70 Interrupt **remove = &slot->first_int;
76 if (remove == &slot->first_int && !list->next &&
77 slot->mask_count == 0)
79 // It was the last handler for this IRQ num.
92 throw_idl(InvalidReference, 0, nullarray);
95 bool InterruptController::handle_irq(int irq)
97 DroppableAutoSpinLock autolock(lock);
98 bool handled_one = false;
99 bool irq_specified = irq >= 0;
104 if (!irq_specified) {
105 irq = get_pending_irq();
113 InterruptSlot *slot = get_slot(irq);
115 if (slot->mask_count > 0 || slot->flags & InterruptSlot::Running) {
116 slot->flags |= InterruptSlot::Pending;
120 slot->flags |= InterruptSlot::Running;
121 assert(!(slot->flags & InterruptSlot::Pending));
125 for (Interrupt *i = slot->first_int; i; i = i->next)
126 handled_one |= i->action();
130 slot->flags &= ~InterruptSlot::Running;
132 if (slot->mask_count == 0)
134 } while (!irq_specified);
140 void InterruptController::rec_mask_nowait(InterruptSlot *slot)
142 AutoSpinLockIRQ autolock(lock);
143 u32 irq = get_irqnum(slot);
145 if (slot->mask_count++ == 0)
149 void InterruptController::rec_mask(InterruptSlot *slot)
151 rec_mask_nowait(slot);
155 void InterruptController::rec_unmask(InterruptSlot *slot)
157 bool was_pending = false;
158 DroppableAutoSpinLockRecIRQ autolock(lock);
160 if (--slot->mask_count == 0) {
161 unmask(get_irqnum(slot));
163 if (slot->flags & InterruptSlot::Pending) {
165 slot->flags &= ~InterruptSlot::Pending;
166 slot->flags |= InterruptSlot::Running;
173 // Only really necessary for edge-triggered interrupts
175 for (Interrupt *i = slot->first_int; i; i = i->next)
179 slot->flags &= ~InterruptSlot::Running;
184 void InterruptController::wait_for_irq(InterruptSlot *slot)
186 while (slot->flags & InterruptSlot::Running)
193 //#include "irq-server/footer.cc"