3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/i386/irq.c
23 * PURPOSE: IRQ handling
24 * PROGRAMMER: David Welch (welch@mcmail.com)
30 * NOTE: In general the PIC interrupt priority facilities are used to
31 * preserve the NT IRQL semantics, global interrupt disables are only used
32 * to keep the PIC in a consistent state
36 /* INCLUDES ****************************************************************/
38 #include <ddk/ntddk.h>
40 #include <internal/ke.h>
41 #include <internal/ps.h>
42 #include <internal/i386/segment.h>
43 #include <internal/pool.h>
45 #include <../dbg/kdb.h>
49 #include <internal/hal/mps.h>
53 #include <internal/debug.h>
55 /* GLOBALS *****************************************************************/
60 * FIXME: This does not work if we have more than 24 IRQs (ie. more than one
63 #define VECTOR2IRQ(vector) (((vector) - FIRST_DEVICE_VECTOR) / 8)
64 #define IRQ2VECTOR(vector) ((vector * 8) + FIRST_DEVICE_VECTOR)
65 #define VECTOR2IRQL(vector) (DISPATCH_LEVEL /* 2 */ + 1 + VECTOR2IRQ(vector))
69 #define NR_IRQS 0x100 - IRQ_BASE
72 #define STR(x) __STR(x)
74 #define INT_NAME(intnum) _KiUnexpectedInterrupt##intnum
75 #define INT_NAME2(intnum) KiUnexpectedInterrupt##intnum
77 #define BUILD_COMMON_INTERRUPT_HANDLER() \
79 "_KiCommonInterrupt:\n\t" \
85 "movl $0xceafbeef,%eax\n\t" \
87 "movl $" STR(KERNEL_DS) ",%eax\n\t" \
90 "movl $" STR(PCR_SELECTOR) ",%eax\n\t" \
94 "call _KiInterruptDispatch\n\t" \
105 #define BUILD_INTERRUPT_HANDLER(intnum) \
106 VOID INT_NAME2(intnum)(VOID); \
108 STR(INT_NAME(intnum)) ":\n\t" \
110 "movl $0x" STR(intnum) ",%ebx\n\t" \
111 "jmp _KiCommonInterrupt");
114 /* Interrupt handlers and declarations */
117 BUILD_INTERRUPT_HANDLER(x##y)
120 B(x,0) B(x,1) B(x,2) B(x,3) \
121 B(x,4) B(x,5) B(x,6) B(x,7) \
122 B(x,8) B(x,9) B(x,A) B(x,B) \
123 B(x,C) B(x,D) B(x,E) B(x,F)
126 BUILD_COMMON_INTERRUPT_HANDLER()
127 B16(3) B16(4) B16(5) B16(6)
128 B16(7) B16(8) B16(9) B16(A)
129 B16(B) B16(C) B16(D) B16(E)
136 /* Interrupt handler list */
139 (ULONG)& INT_NAME2(x##y)
142 L(x,0), L(x,1), L(x,2), L(x,3), \
143 L(x,4), L(x,5), L(x,6), L(x,7), \
144 L(x,8), L(x,9), L(x,A), L(x,B), \
145 L(x,C), L(x,D), L(x,E), L(x,F)
147 static ULONG irq_handler[NR_IRQS] = {
148 L16(3), L16(4), L16(5), L16(6),
149 L16(7), L16(8), L16(9), L16(A),
150 L16(B), L16(C), L16(D), L16(E),
160 #define IRQ_BASE (0x40)
162 void irq_handler_0(void);
163 void irq_handler_1(void);
164 void irq_handler_2(void);
165 void irq_handler_3(void);
166 void irq_handler_4(void);
167 void irq_handler_5(void);
168 void irq_handler_6(void);
169 void irq_handler_7(void);
170 void irq_handler_8(void);
171 void irq_handler_9(void);
172 void irq_handler_10(void);
173 void irq_handler_11(void);
174 void irq_handler_12(void);
175 void irq_handler_13(void);
176 void irq_handler_14(void);
177 void irq_handler_15(void);
179 static unsigned int irq_handler[NR_IRQS]=
191 (int)&irq_handler_10,
192 (int)&irq_handler_11,
193 (int)&irq_handler_12,
194 (int)&irq_handler_13,
195 (int)&irq_handler_14,
196 (int)&irq_handler_15,
202 * PURPOSE: Object describing each isr
203 * NOTE: The data in this table is only modified at passsive level but can
204 * be accessed at any irq level.
207 static LIST_ENTRY isr_table[NR_IRQS]={{NULL,NULL},};
208 static PKSPIN_LOCK isr_lock[NR_IRQS] = {NULL,};
209 static KSPIN_LOCK isr_table_lock = {0,};
211 #define TAG_ISR_LOCK TAG('I', 'S', 'R', 'L')
212 #define TAG_KINTERRUPT TAG('K', 'I', 'S', 'R')
214 /* FUNCTIONS ****************************************************************/
216 #define PRESENT (0x8000)
217 #define I486_INTERRUPT_GATE (0xe00)
219 VOID KeInitInterrupts (VOID)
226 * Setup the IDT entries to point to the interrupt handlers
228 for (i=0;i<NR_IRQS;i++)
230 KiIdt[IRQ_BASE+i].a=(irq_handler[i]&0xffff)+(KERNEL_CS<<16);
231 KiIdt[IRQ_BASE+i].b=(irq_handler[i]&0xffff0000)+PRESENT+
233 InitializeListHead(&isr_table[i]);
239 * Setup the IDT entries to point to the interrupt handlers
241 for (i=0;i<NR_IRQS;i++)
243 KiIdt[IRQ_BASE+i].a=(irq_handler[i]&0xffff)+(KERNEL_CS<<16);
244 KiIdt[IRQ_BASE+i].b=(irq_handler[i]&0xffff0000)+PRESENT+
246 InitializeListHead(&isr_table[i]);
253 typedef struct _KIRQ_TRAPFRAME
270 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
275 KeIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
276 PKTRAP_FRAME TrapFrame)
278 TrapFrame->Fs = IrqTrapFrame->Fs;
279 TrapFrame->Fs = IrqTrapFrame->Es;
280 TrapFrame->Ds = IrqTrapFrame->Ds;
281 TrapFrame->Eax = IrqTrapFrame->Eax;
282 TrapFrame->Ecx = IrqTrapFrame->Ecx;
283 TrapFrame->Edx = IrqTrapFrame->Edx;
284 TrapFrame->Ebx = IrqTrapFrame->Ebx;
285 TrapFrame->Esp = IrqTrapFrame->Esp;
286 TrapFrame->Ebp = IrqTrapFrame->Ebp;
287 TrapFrame->Esi = IrqTrapFrame->Esi;
288 TrapFrame->Edi = IrqTrapFrame->Edi;
289 TrapFrame->Eip = IrqTrapFrame->Eip;
290 TrapFrame->Cs = IrqTrapFrame->Cs;
291 TrapFrame->Eflags = IrqTrapFrame->Eflags;
299 KiInterruptDispatch2 (ULONG vector, KIRQL old_level)
301 * FUNCTION: Calls all the interrupt handlers for a given irq.
303 * vector - The number of the vector to call handlers for.
304 * old_level - The irql of the processor when the irq took place.
305 * NOTES: Must be called at DIRQL.
311 DPRINT1("I(0x%.08x, 0x%.08x)\n", vector, old_level);
315 KiUpdateSystemTime(old_level, 0);
320 * Iterate the list until one of the isr tells us its device interrupted
322 current = isr_table[vector].Flink;
323 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
325 while (current != &isr_table[vector] &&
326 !isr->ServiceRoutine(isr, isr->ServiceContext))
328 current = current->Flink;
329 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
335 KiInterruptDispatch (ULONG Vector, PKIRQ_TRAPFRAME Trapframe)
337 * FUNCTION: Calls the irq specific handler for an irq
339 * Vector = Interrupt vector
340 * Trapframe = CPU context
341 * NOTES: Interrupts are disabled at this point.
348 KTRAP_FRAME KernelTrapFrame;
350 KeIRQTrapFrameToTrapFrame(Trapframe, &KernelTrapFrame);
351 KeGetCurrentThread()->TrapFrame = &KernelTrapFrame;
355 DbgPrint("V(0x%.02x)", Vector);
358 * Notify the rest of the kernel of the raised irq level
360 if (!HalBeginSystemInterrupt (Vector,
368 * Mask the related irq
370 //HalDisableSystemInterrupt (Vector, 0);
374 * NOTE: Only higher priority interrupts will get through
379 * Actually call the ISR.
381 KiInterruptDispatch2(Vector, old_level);
389 * Unmask the related irq
391 //HalEnableSystemInterrupt (Vector, 0, 0);
393 //DbgPrint("E(0x%.02x)\n", Vector);
396 * End the system interrupt.
398 HalEndSystemInterrupt (old_level, 0);
404 KiInterruptDispatch2 (ULONG Irq, KIRQL old_level)
406 * FUNCTION: Calls all the interrupt handlers for a given irq.
408 * Irq - The number of the irq to call handlers for.
409 * old_level - The irql of the processor when the irq took place.
410 * NOTES: Must be called at DIRQL.
418 KiUpdateSystemTime(old_level, 0);
423 * Iterate the list until one of the isr tells us its device interrupted
425 current = isr_table[Irq].Flink;
426 while (current != &isr_table[Irq])
428 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
430 if (isr->ServiceRoutine(isr, isr->ServiceContext))
435 isr->ServiceRoutine(isr, isr->ServiceContext);
437 current = current->Flink;
443 KiInterruptDispatch (ULONG irq, PKIRQ_TRAPFRAME Trapframe)
445 * FUNCTION: Calls the irq specific handler for an irq
447 * irq = IRQ that has interrupted
453 * At this point we have interrupts disabled, nothing has been done to
458 * Notify the rest of the kernel of the raised irq level. For the
459 * default HAL this will send an EOI to the PIC and alter the IRQL.
461 if (!HalBeginSystemInterrupt (irq + IRQ_BASE,
470 * NOTE: Only higher priority interrupts will get through
475 * Actually call the ISR.
477 KiInterruptDispatch2(irq, old_level);
482 KdbProfileInterrupt(Trapframe->Eip);
487 * Maybe do a reschedule as well.
489 if (old_level < DISPATCH_LEVEL && irq == 0)
491 KeLowerIrql(APC_LEVEL);
492 PsDispatchThread(THREAD_STATE_READY);
496 * End the system interrupt.
499 HalEndSystemInterrupt (old_level, 0);
508 PLIST_ENTRY current_entry;
511 for (i=0;i<NR_IRQS;i++)
513 DPRINT("For irq %x ",i);
514 current_entry = isr_table[i].Flink;
515 current = CONTAINING_RECORD(current_entry,KINTERRUPT,Entry);
516 while (current_entry!=(&isr_table[i]))
518 DPRINT("Isr %x ",current);
519 current_entry = current_entry->Flink;
520 current = CONTAINING_RECORD(current_entry,KINTERRUPT,Entry);
527 KeConnectInterrupt(PKINTERRUPT InterruptObject)
531 PKINTERRUPT ListHead;
534 DPRINT("KeConnectInterrupt()\n");
536 Vector = InterruptObject->Vector;
539 * Acquire the table spinlock
541 KeAcquireSpinLock(&isr_table_lock,&oldlvl);
544 * Check if the vector is already in use that we can share it
546 ListHead = CONTAINING_RECORD(isr_table[Vector].Flink,KINTERRUPT,Entry);
547 if (!IsListEmpty(&isr_table[Vector]) &&
548 (InterruptObject->Shareable == FALSE || ListHead->Shareable==FALSE))
550 KeReleaseSpinLock(&isr_table_lock,oldlvl);
551 return(STATUS_INVALID_PARAMETER);
556 ExAllocatePoolWithTag(NonPagedPool, sizeof(KSPIN_LOCK),
558 KeInitializeSpinLock(isr_lock[Vector]);
561 InterruptObject->IrqLock = isr_lock[Vector];
563 KeRaiseIrql(InterruptObject->SynchLevel,&synch_oldlvl);
564 KeAcquireSpinLockAtDpcLevel(InterruptObject->IrqLock);
565 DPRINT("%x %x\n",isr_table[Vector].Flink,isr_table[Vector].Blink);
566 if (IsListEmpty(&isr_table[Vector]))
569 HalEnableSystemInterrupt(Vector, 0, 0);
571 HalEnableSystemInterrupt(Vector + IRQ_BASE, 0, 0);
574 InsertTailList(&isr_table[Vector],&InterruptObject->Entry);
575 DPRINT("%x %x\n",InterruptObject->Entry.Flink,
576 InterruptObject->Entry.Blink);
577 KeReleaseSpinLockFromDpcLevel(InterruptObject->IrqLock);
578 KeLowerIrql(synch_oldlvl);
581 * Release the table spinlock
583 KeReleaseSpinLock(&isr_table_lock,oldlvl);
587 return STATUS_SUCCESS;
592 KeDisconnectInterrupt(PKINTERRUPT InterruptObject)
594 * FUNCTION: Releases a drivers isr
596 * InterruptObject = isr to release
601 KeRaiseIrql(InterruptObject->SynchLevel,&oldlvl);
602 KeAcquireSpinLockAtDpcLevel(InterruptObject->IrqLock);
603 RemoveEntryList(&InterruptObject->Entry);
604 if (IsListEmpty(&isr_table[InterruptObject->Vector]))
607 HalDisableSystemInterrupt(InterruptObject->Vector, 0);
609 HalDisableSystemInterrupt(InterruptObject->Vector + IRQ_BASE, 0);
612 KeReleaseSpinLockFromDpcLevel(InterruptObject->IrqLock);
619 KeInitializeInterrupt(PKINTERRUPT InterruptObject,
620 PKSERVICE_ROUTINE ServiceRoutine,
621 PVOID ServiceContext,
622 PKSPIN_LOCK SpinLock,
625 KIRQL SynchronizeIrql,
626 KINTERRUPT_MODE InterruptMode,
628 KAFFINITY ProcessorEnableMask,
629 BOOLEAN FloatingSave)
631 InterruptObject->ServiceContext = ServiceContext;
632 InterruptObject->ServiceRoutine = ServiceRoutine;
633 InterruptObject->Vector = Vector;
634 InterruptObject->ProcessorEnableMask = ProcessorEnableMask;
635 InterruptObject->SynchLevel = SynchronizeIrql;
636 InterruptObject->Shareable = ShareVector;
637 InterruptObject->FloatingSave = FALSE;
639 return STATUS_SUCCESS;
644 IoConnectInterrupt(PKINTERRUPT* InterruptObject,
645 PKSERVICE_ROUTINE ServiceRoutine,
646 PVOID ServiceContext,
647 PKSPIN_LOCK SpinLock,
650 KIRQL SynchronizeIrql,
651 KINTERRUPT_MODE InterruptMode,
653 KAFFINITY ProcessorEnableMask,
654 BOOLEAN FloatingSave)
656 * FUNCTION: Registers a driver's isr to be called when its device interrupts
658 * InterruptObject (OUT) = Points to the interrupt object created on
660 * ServiceRoutine = Routine to be called when the device interrupts
661 * ServiceContext = Parameter to be passed to ServiceRoutine
662 * SpinLock = Initalized spinlock that will be used to synchronize
663 * access between the isr and other driver routines. This is
664 * required if the isr handles more than one vector or the
665 * driver has more than one isr
666 * Vector = Interrupt vector to allocate
667 * (returned from HalGetInterruptVector)
668 * Irql = DIRQL returned from HalGetInterruptVector
669 * SynchronizeIrql = DIRQL at which the isr will execute. This must
670 * be the highest of all the DIRQLs returned from
671 * HalGetInterruptVector if the driver has multiple
673 * InterruptMode = Specifies if the interrupt is LevelSensitive or
675 * ShareVector = Specifies if the vector can be shared
676 * ProcessorEnableMask = Processors on the isr can run
677 * FloatingSave = TRUE if the floating point stack should be saved when
678 * the isr runs. Must be false for x86 drivers
680 * IRQL: PASSIVE_LEVEL
683 PKINTERRUPT Interrupt;
684 NTSTATUS Status = STATUS_SUCCESS;
686 ASSERT_IRQL(PASSIVE_LEVEL);
688 DPRINT("IoConnectInterrupt(Vector %x)\n",Vector);
691 * Check the parameters
693 if (Vector >= NR_IRQS)
695 return(STATUS_INVALID_PARAMETER);
697 if (FloatingSave == TRUE)
699 return(STATUS_INVALID_PARAMETER);
703 * Initialize interrupt object
705 Interrupt=ExAllocatePoolWithTag(NonPagedPool,sizeof(KINTERRUPT),
709 return(STATUS_INSUFFICIENT_RESOURCES);
712 Status = KeInitializeInterrupt(Interrupt,
723 if (!NT_SUCCESS(Status))
725 ExFreePool(Interrupt);
729 Status = KeConnectInterrupt(Interrupt);
730 if (!NT_SUCCESS(Status))
732 ExFreePool(Interrupt);
736 *InterruptObject = Interrupt;
738 return(STATUS_SUCCESS);
743 IoDisconnectInterrupt(PKINTERRUPT InterruptObject)
745 * FUNCTION: Releases a drivers isr
747 * InterruptObject = isr to release
750 KeDisconnectInterrupt(InterruptObject);
751 ExFreePool(InterruptObject);