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 condition:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
24 #include <kern/kernel.h>
28 void InterruptController::request_int(InterruptSlot *slot, Interrupt *irq)
30 AutoSpinLockIRQ autolock(lock);
32 u32 irqnum = get_irqnum(slot);
33 Interrupt *list = slot->first_int;
34 Interrupt **insert = &slot->first_int;
38 throw_idl(ResourceBusy, -1, countarray("IRQ in use"));
46 irq->controller = this;
48 if (insert == &slot->first_int && slot->mask_count == 0) {
49 // There were no existing handlers, so it's currently masked
54 void InterruptController::free_int(Interrupt *irq)
56 InterruptSlot *slot = irq->slot;
57 u32 irqnum = get_irqnum(slot);
59 DroppableAutoSpinLockIRQ autolock(lock);
61 Interrupt *list = slot->first_int;
62 Interrupt **remove = &slot->first_int;
68 if (remove == &slot->first_int && !list->next &&
69 slot->mask_count == 0)
71 // It was the last handler for this IRQ num.
84 throw_idl(InvalidReference, 0, nullarray);
87 bool InterruptController::handle_irq(int irq)
89 DroppableAutoSpinLock autolock(lock);
90 bool handled_one = false;
91 bool irq_specified = irq >= 0;
97 irq = get_pending_irq();
105 InterruptSlot *slot = get_slot(irq);
107 if (slot->mask_count > 0 || slot->flags & InterruptSlot::Running) {
108 slot->flags |= InterruptSlot::Pending;
112 slot->flags |= InterruptSlot::Running;
113 assert(!(slot->flags & InterruptSlot::Pending));
117 for (Interrupt *i = slot->first_int; i; i = i->next)
118 handled_one |= i->action();
122 slot->flags &= ~InterruptSlot::Running;
124 if (slot->mask_count == 0)
126 } while (!irq_specified);
132 void InterruptController::rec_mask_nowait(InterruptSlot *slot)
134 AutoSpinLockIRQ autolock(lock);
135 u32 irq = get_irqnum(slot);
137 if (slot->mask_count++ == 0)
141 void InterruptController::rec_mask(InterruptSlot *slot)
143 rec_mask_nowait(slot);
147 void InterruptController::rec_unmask(InterruptSlot *slot)
149 bool was_pending = false;
150 DroppableAutoSpinLockRecIRQ autolock(lock);
152 if (--slot->mask_count == 0) {
153 unmask(get_irqnum(slot));
155 if (slot->flags & InterruptSlot::Pending) {
157 slot->flags &= ~InterruptSlot::Pending;
158 slot->flags |= InterruptSlot::Running;
165 // Only really necessary for edge-triggered interrupts
167 for (Interrupt *i = slot->first_int; i; i = i->next)
171 slot->flags &= ~InterruptSlot::Running;
176 void InterruptController::wait_for_irq(InterruptSlot *slot)
178 while (slot->flags & InterruptSlot::Running)
185 //#include "irq-server/footer.cc"