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>
46 #include <internal/hal/mps.h>
50 #include <internal/debug.h>
52 /* GLOBALS *****************************************************************/
57 * FIXME: This does not work if we have more than 24 IRQs (ie. more than one
60 #define VECTOR2IRQ(vector) (((vector) - 0x31) / 8)
61 #define VECTOR2IRQL(vector) (4 + VECTOR2IRQ(vector))
63 #define IRQ_BASE FIRST_DEVICE_VECTOR
64 #define NR_IRQS 0x100 - 0x30
67 #define STR(x) __STR(x)
69 #define INT_NAME(intnum) _KiUnexpectedInterrupt##intnum
70 #define INT_NAME2(intnum) KiUnexpectedInterrupt##intnum
72 #define BUILD_COMMON_INTERRUPT_HANDLER() \
74 "_KiCommonInterrupt:\n\t" \
80 "movl $0xceafbeef,%eax\n\t" \
82 "movl $" STR(KERNEL_DS) ",%eax\n\t" \
85 "movl $" STR(PCR_SELECTOR) ",%eax\n\t" \
89 "call _KiInterruptDispatch\n\t" \
100 #define BUILD_INTERRUPT_HANDLER(intnum) \
101 VOID INT_NAME2(intnum)(VOID); \
103 STR(INT_NAME(intnum)) ":\n\t" \
105 "movl $0x" STR(intnum) ",%ebx\n\t" \
106 "jmp _KiCommonInterrupt");
109 /* Interrupt handlers and declarations */
112 BUILD_INTERRUPT_HANDLER(x##y)
115 B(x,0) B(x,1) B(x,2) B(x,3) \
116 B(x,4) B(x,5) B(x,6) B(x,7) \
117 B(x,8) B(x,9) B(x,A) B(x,B) \
118 B(x,C) B(x,D) B(x,E) B(x,F)
121 BUILD_COMMON_INTERRUPT_HANDLER()
122 B16(3) B16(4) B16(5) B16(6)
123 B16(7) B16(8) B16(9) B16(A)
124 B16(B) B16(C) B16(D) B16(E)
131 /* Interrupt handler list */
134 (ULONG)& INT_NAME2(x##y)
137 L(x,0), L(x,1), L(x,2), L(x,3), \
138 L(x,4), L(x,5), L(x,6), L(x,7), \
139 L(x,8), L(x,9), L(x,A), L(x,B), \
140 L(x,C), L(x,D), L(x,E), L(x,F)
142 static ULONG irq_handler[NR_IRQS] = {
143 L16(3), L16(4), L16(5), L16(6),
144 L16(7), L16(8), L16(9), L16(A),
145 L16(B), L16(C), L16(D), L16(E),
155 #define IRQ_BASE (0x40)
157 void irq_handler_0(void);
158 void irq_handler_1(void);
159 void irq_handler_2(void);
160 void irq_handler_3(void);
161 void irq_handler_4(void);
162 void irq_handler_5(void);
163 void irq_handler_6(void);
164 void irq_handler_7(void);
165 void irq_handler_8(void);
166 void irq_handler_9(void);
167 void irq_handler_10(void);
168 void irq_handler_11(void);
169 void irq_handler_12(void);
170 void irq_handler_13(void);
171 void irq_handler_14(void);
172 void irq_handler_15(void);
174 static unsigned int irq_handler[NR_IRQS]=
186 (int)&irq_handler_10,
187 (int)&irq_handler_11,
188 (int)&irq_handler_12,
189 (int)&irq_handler_13,
190 (int)&irq_handler_14,
191 (int)&irq_handler_15,
197 * PURPOSE: Object describing each isr
198 * NOTE: The data in this table is only modified at passsive level but can
199 * be accessed at any irq level.
202 static LIST_ENTRY isr_table[NR_IRQS]={{NULL,NULL},};
203 static PKSPIN_LOCK isr_lock[NR_IRQS] = {NULL,};
204 static KSPIN_LOCK isr_table_lock = {0,};
206 #define TAG_ISR_LOCK TAG('I', 'S', 'R', 'L')
207 #define TAG_KINTERRUPT TAG('K', 'I', 'S', 'R')
209 /* FUNCTIONS ****************************************************************/
211 #define PRESENT (0x8000)
212 #define I486_INTERRUPT_GATE (0xe00)
214 VOID KeInitInterrupts (VOID)
221 * Setup the IDT entries to point to the interrupt handlers
223 for (i=0;i<NR_IRQS;i++)
225 KiIdt[0x30+i].a=(irq_handler[i]&0xffff)+(KERNEL_CS<<16);
226 KiIdt[0x30+i].b=(irq_handler[i]&0xffff0000)+PRESENT+
228 InitializeListHead(&isr_table[i]);
234 * Setup the IDT entries to point to the interrupt handlers
236 for (i=0;i<NR_IRQS;i++)
238 KiIdt[IRQ_BASE+i].a=(irq_handler[i]&0xffff)+(KERNEL_CS<<16);
239 KiIdt[IRQ_BASE+i].b=(irq_handler[i]&0xffff0000)+PRESENT+
241 InitializeListHead(&isr_table[i]);
248 typedef struct _KIRQ_TRAPFRAME
265 } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
270 KeIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
271 PKTRAP_FRAME TrapFrame)
273 TrapFrame->Fs = IrqTrapFrame->Fs;
274 TrapFrame->Fs = IrqTrapFrame->Es;
275 TrapFrame->Ds = IrqTrapFrame->Ds;
276 TrapFrame->Eax = IrqTrapFrame->Eax;
277 TrapFrame->Ecx = IrqTrapFrame->Ecx;
278 TrapFrame->Edx = IrqTrapFrame->Edx;
279 TrapFrame->Ebx = IrqTrapFrame->Ebx;
280 TrapFrame->Esp = IrqTrapFrame->Esp;
281 TrapFrame->Ebp = IrqTrapFrame->Ebp;
282 TrapFrame->Esi = IrqTrapFrame->Esi;
283 TrapFrame->Edi = IrqTrapFrame->Edi;
284 TrapFrame->Eip = IrqTrapFrame->Eip;
285 TrapFrame->Cs = IrqTrapFrame->Cs;
286 TrapFrame->Eflags = IrqTrapFrame->Eflags;
294 KiInterruptDispatch (ULONG Vector, PKIRQ_TRAPFRAME Trapframe)
296 * FUNCTION: Calls the irq specific handler for an irq
298 * Vector = Interrupt vector
299 * Trapframe = CPU context
309 KTRAP_FRAME KernelTrapFrame;
311 KeIRQTrapFrameToTrapFrame(Trapframe, &KernelTrapFrame);
312 KeGetCurrentThread()->TrapFrame = &KernelTrapFrame;
316 DPRINT("I(%d) ", Vector);
319 * Notify the rest of the kernel of the raised irq level
321 HalBeginSystemInterrupt (Vector,
325 irq = VECTOR2IRQ(Vector);
329 * NOTE: Only higher priority interrupts will get through
335 if (KeGetCurrentProcessorNumber() == 0)
337 KiUpdateSystemTime(old_level, Trapframe->Eip);
342 DPRINT("KiInterruptDispatch(Vector %d)\n", Vector);
344 * Iterate the list until one of the isr tells us its device interrupted
346 current = isr_table[irq].Flink;
347 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
348 //DPRINT("current %x isr %x\n",current,isr);
349 while (current!=(&isr_table[irq]) &&
350 !isr->ServiceRoutine(isr,isr->ServiceContext))
352 current = current->Flink;
353 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
354 //DPRINT("current %x isr %x\n",current,isr);
363 * Unmask the related irq
365 HalEnableSystemInterrupt (Vector, 0, 0);
368 * If the processor level will drop below dispatch level on return then
369 * issue a DPC queue drain interrupt
374 if (old_level < DISPATCH_LEVEL)
377 HalEndSystemInterrupt (DISPATCH_LEVEL, 0);
379 if (KeGetCurrentThread() != NULL)
381 KeGetCurrentThread()->LastEip = Trapframe->Eip;
383 KiDispatchInterrupt();
384 if (KeGetCurrentThread() != NULL &&
385 KeGetCurrentThread()->Alerted[1] != 0 &&
386 Trapframe->Cs != KERNEL_CS)
388 HalEndSystemInterrupt (APC_LEVEL, 0);
389 KiDeliverNormalApc();
393 HalEndSystemInterrupt (old_level, 0);
399 KiInterruptDispatch2 (ULONG Irq, KIRQL old_level)
401 * FUNCTION: Calls all the interrupt handlers for a given irq.
403 * Irq - The number of the irq to call handlers for.
404 * old_level - The irql of the processor when the irq took place.
405 * NOTES: Must be called at DIRQL.
413 KiUpdateSystemTime(old_level, 0);
418 * Iterate the list until one of the isr tells us its device interrupted
420 current = isr_table[Irq].Flink;
421 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
422 while (current != &isr_table[Irq] &&
423 !isr->ServiceRoutine(isr, isr->ServiceContext))
425 current = current->Flink;
426 isr = CONTAINING_RECORD(current,KINTERRUPT,Entry);
432 KiInterruptDispatch (ULONG irq, PKIRQ_TRAPFRAME Trapframe)
434 * FUNCTION: Calls the irq specific handler for an irq
436 * irq = IRQ that has interrupted
442 * At this point we have interrupts disabled, nothing has been done to
447 * Notify the rest of the kernel of the raised irq level. For the
448 * default HAL this will send an EOI to the PIC and alter the IRQL.
450 if (!HalBeginSystemInterrupt (irq + IRQ_BASE,
459 * NOTE: Only higher priority interrupts will get through
464 * Actually call the ISR.
466 KiInterruptDispatch2(irq, old_level);
469 * End the system interrupt.
471 HalEndSystemInterrupt (old_level, 0);
474 * Maybe do a reschedule as well.
476 if (old_level < DISPATCH_LEVEL && irq == 0)
478 PsDispatchThread(THREAD_STATE_READY);
488 PLIST_ENTRY current_entry;
491 for (i=0;i<NR_IRQS;i++)
493 DPRINT("For irq %x ",i);
494 current_entry = isr_table[i].Flink;
495 current = CONTAINING_RECORD(current_entry,KINTERRUPT,Entry);
496 while (current_entry!=(&isr_table[i]))
498 DPRINT("Isr %x ",current);
499 current_entry = current_entry->Flink;
500 current = CONTAINING_RECORD(current_entry,KINTERRUPT,Entry);
507 KeConnectInterrupt(PKINTERRUPT InterruptObject)
511 PKINTERRUPT ListHead;
514 DPRINT("KeConnectInterrupt()\n");
516 Vector = InterruptObject->Vector;
519 * Acquire the table spinlock
521 KeAcquireSpinLock(&isr_table_lock,&oldlvl);
524 * Check if the vector is already in use that we can share it
526 ListHead = CONTAINING_RECORD(isr_table[Vector].Flink,KINTERRUPT,Entry);
527 if (!IsListEmpty(&isr_table[Vector]) &&
528 (InterruptObject->Shareable == FALSE || ListHead->Shareable==FALSE))
530 KeReleaseSpinLock(&isr_table_lock,oldlvl);
531 return(STATUS_INVALID_PARAMETER);
536 ExAllocatePoolWithTag(NonPagedPool, sizeof(KSPIN_LOCK),
538 KeInitializeSpinLock(isr_lock[Vector]);
541 InterruptObject->IrqLock = isr_lock[Vector];
543 KeRaiseIrql(InterruptObject->SynchLevel,&synch_oldlvl);
544 KeAcquireSpinLockAtDpcLevel(InterruptObject->IrqLock);
545 DPRINT("%x %x\n",isr_table[Vector].Flink,isr_table[Vector].Blink);
546 InsertTailList(&isr_table[Vector],&InterruptObject->Entry);
547 DPRINT("%x %x\n",InterruptObject->Entry.Flink,
548 InterruptObject->Entry.Blink);
549 KeReleaseSpinLockFromDpcLevel(InterruptObject->IrqLock);
550 KeLowerIrql(synch_oldlvl);
553 * Release the table spinlock
555 KeReleaseSpinLock(&isr_table_lock,oldlvl);
559 return STATUS_SUCCESS;
564 KeDisconnectInterrupt(PKINTERRUPT InterruptObject)
566 * FUNCTION: Releases a drivers isr
568 * InterruptObject = isr to release
573 KeRaiseIrql(InterruptObject->SynchLevel,&oldlvl);
574 KeAcquireSpinLockAtDpcLevel(InterruptObject->IrqLock);
575 RemoveEntryList(&InterruptObject->Entry);
576 KeReleaseSpinLockFromDpcLevel(InterruptObject->IrqLock);
583 KeInitializeInterrupt(PKINTERRUPT InterruptObject,
584 PKSERVICE_ROUTINE ServiceRoutine,
585 PVOID ServiceContext,
586 PKSPIN_LOCK SpinLock,
589 KIRQL SynchronizeIrql,
590 KINTERRUPT_MODE InterruptMode,
592 KAFFINITY ProcessorEnableMask,
593 BOOLEAN FloatingSave)
595 InterruptObject->ServiceContext = ServiceContext;
596 InterruptObject->ServiceRoutine = ServiceRoutine;
597 InterruptObject->Vector = Vector;
598 InterruptObject->ProcessorEnableMask = ProcessorEnableMask;
599 InterruptObject->SynchLevel = SynchronizeIrql;
600 InterruptObject->Shareable = ShareVector;
601 InterruptObject->FloatingSave = FALSE;
603 return STATUS_SUCCESS;
608 IoConnectInterrupt(PKINTERRUPT* InterruptObject,
609 PKSERVICE_ROUTINE ServiceRoutine,
610 PVOID ServiceContext,
611 PKSPIN_LOCK SpinLock,
614 KIRQL SynchronizeIrql,
615 KINTERRUPT_MODE InterruptMode,
617 KAFFINITY ProcessorEnableMask,
618 BOOLEAN FloatingSave)
620 * FUNCTION: Registers a driver's isr to be called when its device interrupts
622 * InterruptObject (OUT) = Points to the interrupt object created on
624 * ServiceRoutine = Routine to be called when the device interrupts
625 * ServiceContext = Parameter to be passed to ServiceRoutine
626 * SpinLock = Initalized spinlock that will be used to synchronize
627 * access between the isr and other driver routines. This is
628 * required if the isr handles more than one vector or the
629 * driver has more than one isr
630 * Vector = Interrupt vector to allocate
631 * (returned from HalGetInterruptVector)
632 * Irql = DIRQL returned from HalGetInterruptVector
633 * SynchronizeIrql = DIRQL at which the isr will execute. This must
634 * be the highest of all the DIRQLs returned from
635 * HalGetInterruptVector if the driver has multiple
637 * InterruptMode = Specifies if the interrupt is LevelSensitive or
639 * ShareVector = Specifies if the vector can be shared
640 * ProcessorEnableMask = Processors on the isr can run
641 * FloatingSave = TRUE if the floating point stack should be saved when
642 * the isr runs. Must be false for x86 drivers
644 * IRQL: PASSIVE_LEVEL
647 PKINTERRUPT Interrupt;
648 NTSTATUS Status = STATUS_SUCCESS;
650 ASSERT_IRQL(PASSIVE_LEVEL);
652 DPRINT("IoConnectInterrupt(Vector %x)\n",Vector);
655 * Check the parameters
657 if (Vector >= NR_IRQS)
659 return(STATUS_INVALID_PARAMETER);
661 if (FloatingSave == TRUE)
663 return(STATUS_INVALID_PARAMETER);
667 * Initialize interrupt object
669 Interrupt=ExAllocatePoolWithTag(NonPagedPool,sizeof(KINTERRUPT),
673 return(STATUS_INSUFFICIENT_RESOURCES);
676 Status = KeInitializeInterrupt(Interrupt,
687 if (!NT_SUCCESS(Status))
689 ExFreePool(Interrupt);
693 Status = KeConnectInterrupt(Interrupt);
694 if (!NT_SUCCESS(Status))
696 ExFreePool(Interrupt);
700 *InterruptObject = Interrupt;
702 return(STATUS_SUCCESS);
707 IoDisconnectInterrupt(PKINTERRUPT InterruptObject)
709 * FUNCTION: Releases a drivers isr
711 * InterruptObject = isr to release
714 KeDisconnectInterrupt(InterruptObject);
715 ExFreePool(InterruptObject);