vm: util.S not used currently; leave it out.
[minix.git] / kernel / interrupt.c
blob00d0310da06dfefdf65111658041e13d914c91bc
1 /*
2 * The Minix hardware interrupt system.
3 *
4 * This file contains routines for managing the interrupt
5 * controller.
6 *
7 * put_irq_handler: register an interrupt handler.
8 * rm_irq_handler: deregister an interrupt handler.
9 * irq_handle: handle a hardware interrupt.
10 * called by the system dependent part when an
11 * external interrupt occures.
12 * enable_irq: enable hook for IRQ.
13 * disable_irq: disable hook for IRQ.
16 #include <assert.h>
18 #include "kernel.h"
19 #include "proc.h"
20 #include "archconst.h"
21 #include "hw_intr.h"
23 /* number of lists of IRQ hooks, one list per supported line. */
24 PUBLIC irq_hook_t* irq_handlers[NR_IRQ_VECTORS] = {0};
25 /*===========================================================================*
26 * put_irq_handler *
27 *===========================================================================*/
28 /* Register an interrupt handler. */
29 PUBLIC void put_irq_handler( irq_hook_t* hook, int irq,
30 const irq_handler_t handler)
32 int id;
33 irq_hook_t **line;
34 unsigned long bitmap;
36 if( irq < 0 || irq >= NR_IRQ_VECTORS )
37 panic("invalid call to put_irq_handler: %d", irq);
39 line = &irq_handlers[irq];
41 bitmap = 0;
42 while ( *line != NULL ) {
43 if(hook == *line) return; /* extra initialization */
44 bitmap |= (*line)->id; /* mark ids in use */
45 line = &(*line)->next;
48 /* find the lowest id not in use */
49 for (id = 1; id != 0; id <<= 1)
50 if (!(bitmap & id)) break;
52 if(id == 0)
53 panic("Too many handlers for irq: %d", irq);
55 hook->next = NULL;
56 hook->handler = handler;
57 hook->irq = irq;
58 hook->id = id;
59 *line = hook;
60 irq_use |= 1 << irq; /* this does not work for irq >= 32 */
62 /* And as last enable the irq at the hardware.
64 * Internal this activates the line or source of the given interrupt.
66 if((irq_actids[hook->irq] &= ~hook->id) == 0) {
67 hw_intr_unmask(hook->irq);
71 /*===========================================================================*
72 * rm_irq_handler *
73 *===========================================================================*/
74 /* Unregister an interrupt handler. */
75 PUBLIC void rm_irq_handler( const irq_hook_t* hook ) {
76 const int irq = hook->irq;
77 const int id = hook->id;
78 irq_hook_t **line;
80 if( irq < 0 || irq >= NR_IRQ_VECTORS )
81 panic("invalid call to rm_irq_handler: %d", irq);
83 /* remove the hook */
84 line = &irq_handlers[irq];
85 while( (*line) != NULL ) {
86 if((*line)->id == id) {
87 (*line) = (*line)->next;
88 if(!irq_handlers[irq])
89 irq_use &= ~(1 << irq);
90 if (irq_actids[irq] & id)
91 irq_actids[irq] &= ~id;
93 else {
94 line = &(*line)->next;
98 /* Disable the irq if there are no other handlers registered.
99 * If the irq is shared, reenable it if there is no active handler.
101 if (irq_handlers[irq] == NULL) {
102 hw_intr_mask(irq);
104 else if (irq_actids[irq] == 0) {
105 hw_intr_unmask(irq);
109 /*===========================================================================*
110 * irq_handle *
111 *===========================================================================*/
113 * The function first disables interrupt is need be and restores the state at
114 * the end. Before returning, it unmasks the IRQ if and only if all active ID
115 * bits are cleared, and restart a process.
117 PUBLIC void irq_handle(int irq)
119 irq_hook_t * hook;
121 /* here we need not to get this IRQ until all the handlers had a say */
122 assert(irq >= 0 && irq < NR_IRQ_VECTORS);
123 hw_intr_mask(irq);
124 hook = irq_handlers[irq];
126 /* Check for spurious interrupts. */
127 if(hook == NULL) {
128 static int nspurious[NR_IRQ_VECTORS];
129 nspurious[irq]++;
130 if(nspurious[irq] == 1 || !(nspurious[irq] % 1000)) {
131 printf("irq_handle: spurious irq %d (count: %d); keeping masked\n",
132 irq, nspurious[irq]);
134 return;
137 /* Call list of handlers for an IRQ. */
138 while( hook != NULL ) {
139 /* For each handler in the list, mark it active by setting its ID bit,
140 * call the function, and unmark it if the function returns true.
142 irq_actids[irq] |= hook->id;
144 /* Call the hooked function. */
145 if( (*hook->handler)(hook) )
146 irq_actids[hook->irq] &= ~hook->id;
148 /* Next hooked function. */
149 hook = hook->next;
152 /* reenable the IRQ only if there is no active handler */
153 if (irq_actids[irq] == 0)
154 hw_intr_unmask(irq);
157 /* Enable/Disable a interrupt line. */
158 PUBLIC void enable_irq(const irq_hook_t *hook)
160 if((irq_actids[hook->irq] &= ~hook->id) == 0) {
161 hw_intr_unmask(hook->irq);
165 /* Return true if the interrupt was enabled before call. */
166 PUBLIC int disable_irq(const irq_hook_t *hook)
168 if(irq_actids[hook->irq] & hook->id) /* already disabled */
169 return 0;
170 irq_actids[hook->irq] |= hook->id;
171 hw_intr_mask(hook->irq);
172 return TRUE;