root/kernel/irq.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. do_bottom_half
  2. do_IRQ
  3. do_fast_IRQ
  4. irqaction
  5. request_irq
  6. free_irq
  7. math_error_irq
  8. no_action
  9. init_IRQ

   1 /*
   2  *      linux/kernel/irq.c
   3  *
   4  *      Copyright (C) 1992 Linus Torvalds
   5  *
   6  * This file contains the code used by various IRQ handling routines:
   7  * asking for different IRQ's should be done through these routines
   8  * instead of just grabbing them. Thus setups with different IRQ numbers
   9  * shouldn't result in any weird surprises, and installing new handlers
  10  * should be easier.
  11  */
  12 
  13 /*
  14  * IRQ's are in fact implemented a bit like signal handlers for the kernel.
  15  * The same sigaction struct is used, and with similar semantics (ie there
  16  * is a SA_INTERRUPT flag etc). Naturally it's not a 1:1 relation, but there
  17  * are similarities.
  18  *
  19  * sa_handler(int irq_NR) is the default function called.
  20  * sa_mask is 0 if nothing uses this IRQ
  21  * sa_flags contains various info: SA_INTERRUPT etc
  22  * sa_restorer is the unused
  23  */
  24 
  25 #include <linux/ptrace.h>
  26 #include <linux/errno.h>
  27 #include <linux/signal.h>
  28 #include <linux/sched.h>
  29 #include <linux/interrupt.h>
  30 
  31 #include <asm/system.h>
  32 #include <asm/io.h>
  33 #include <asm/irq.h>
  34 
  35 #define CR0_NE 32
  36 
  37 static unsigned long intr_count=0;
  38 static unsigned char cache_21 = 0xff;
  39 static unsigned char cache_A1 = 0xff;
  40 
  41 unsigned long bh_active = 0;
  42 unsigned long bh_mask = 0xFFFFFFFF;
  43 struct bh_struct bh_base[32]; 
  44 
  45 /*
  46  * do_bottom_half() runs at normal kernel priority: all interrupts
  47  * enabled.  do_bottom_half() is atomic with respect to itself: a
  48  * bottom_half handler need not be re-entrant.  This function is
  49  * called only when bh_active is non-zero and when there aren't any
  50  * nested irq's active.
  51  */
  52 void do_bottom_half(int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54         struct bh_struct *bh;
  55 
  56         bh = bh_base+nr;
  57         if (bh->routine != NULL)
  58                 bh->routine(bh->data);
  59         else
  60                 printk ("irq.c:bad bottom half entry.\n");
  61 }
  62 
  63 /*
  64  * This builds up the IRQ handler stubs using some ugly macros in irq.h
  65  *
  66  * These macros create the low-level assembly IRQ routines that do all
  67  * the operations that are needed to keep the AT interrupt-controller
  68  * happy. They are also written to be fast - and to disable interrupts
  69  * as little as humanly possible.
  70  *
  71  * NOTE! These macros expand to three different handlers for each line: one
  72  * complete handler that does all the fancy stuff (including signal handling),
  73  * and one fast handler that is meant for simple IRQ's that want to be
  74  * atomic. The specific handler is chosen depending on the SA_INTERRUPT
  75  * flag when installing a handler. Finally, one "bad interrupt" handler, that
  76  * is used when no handler is present.
  77  */
  78 BUILD_IRQ(FIRST,0,0x01)
  79 BUILD_IRQ(FIRST,1,0x02)
  80 BUILD_IRQ(FIRST,2,0x04)
  81 BUILD_IRQ(FIRST,3,0x08)
  82 BUILD_IRQ(FIRST,4,0x10)
  83 BUILD_IRQ(FIRST,5,0x20)
  84 BUILD_IRQ(FIRST,6,0x40)
  85 BUILD_IRQ(FIRST,7,0x80)
  86 BUILD_IRQ(SECOND,8,0x01)
  87 BUILD_IRQ(SECOND,9,0x02)
  88 BUILD_IRQ(SECOND,10,0x04)
  89 BUILD_IRQ(SECOND,11,0x08)
  90 BUILD_IRQ(SECOND,12,0x10)
  91 BUILD_IRQ(SECOND,13,0x20)
  92 BUILD_IRQ(SECOND,14,0x40)
  93 BUILD_IRQ(SECOND,15,0x80)
  94 
  95 /*
  96  * Pointers to the low-level handlers: first the general ones, then the
  97  * fast ones, then the bad ones.
  98  */
  99 static void (*interrupt[16])(void) = {
 100         IRQ0_interrupt, IRQ1_interrupt, IRQ2_interrupt, IRQ3_interrupt,
 101         IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
 102         IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
 103         IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, IRQ15_interrupt
 104 };
 105 
 106 static void (*fast_interrupt[16])(void) = {
 107         fast_IRQ0_interrupt, fast_IRQ1_interrupt,
 108         fast_IRQ2_interrupt, fast_IRQ3_interrupt,
 109         fast_IRQ4_interrupt, fast_IRQ5_interrupt,
 110         fast_IRQ6_interrupt, fast_IRQ7_interrupt,
 111         fast_IRQ8_interrupt, fast_IRQ9_interrupt,
 112         fast_IRQ10_interrupt, fast_IRQ11_interrupt,
 113         fast_IRQ12_interrupt, fast_IRQ13_interrupt,
 114         fast_IRQ14_interrupt, fast_IRQ15_interrupt
 115 };
 116 
 117 static void (*bad_interrupt[16])(void) = {
 118         bad_IRQ0_interrupt, bad_IRQ1_interrupt,
 119         bad_IRQ2_interrupt, bad_IRQ3_interrupt,
 120         bad_IRQ4_interrupt, bad_IRQ5_interrupt,
 121         bad_IRQ6_interrupt, bad_IRQ7_interrupt,
 122         bad_IRQ8_interrupt, bad_IRQ9_interrupt,
 123         bad_IRQ10_interrupt, bad_IRQ11_interrupt,
 124         bad_IRQ12_interrupt, bad_IRQ13_interrupt,
 125         bad_IRQ14_interrupt, bad_IRQ15_interrupt
 126 };
 127 
 128 /*
 129  * Initial irq handlers.
 130  */
 131 static struct sigaction irq_sigaction[16] = {
 132         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 133         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 134         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 135         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 136         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 137         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 138         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 139         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL }
 140 };
 141 
 142 /*
 143  * do_IRQ handles IRQ's that have been installed without the
 144  * SA_INTERRUPT flag: it uses the full signal-handling return
 145  * and runs with other interrupts enabled. All relatively slow
 146  * IRQ's should use this format: notably the keyboard/timer
 147  * routines.
 148  */
 149 void do_IRQ(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         struct sigaction * sa = irq + irq_sigaction;
 152 
 153         sa->sa_handler((int) regs);
 154 }
 155 
 156 /*
 157  * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
 158  * stuff - the handler is also running with interrupts disabled unless
 159  * it explicitly enables them later.
 160  */
 161 void do_fast_IRQ(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163         struct sigaction * sa = irq + irq_sigaction;
 164 
 165         sa->sa_handler(irq);
 166 }
 167 
 168 int irqaction(unsigned int irq, struct sigaction * new)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170         struct sigaction * sa;
 171         unsigned long flags;
 172 
 173         if (irq > 15)
 174                 return -EINVAL;
 175         sa = irq + irq_sigaction;
 176         if (sa->sa_mask)
 177                 return -EBUSY;
 178         if (!new->sa_handler)
 179                 return -EINVAL;
 180         save_flags(flags);
 181         cli();
 182         *sa = *new;
 183         sa->sa_mask = 1;
 184         if (sa->sa_flags & SA_INTERRUPT)
 185                 set_intr_gate(0x20+irq,fast_interrupt[irq]);
 186         else
 187                 set_intr_gate(0x20+irq,interrupt[irq]);
 188         if (irq < 8) {
 189                 cache_21 &= ~(1<<irq);
 190                 outb(cache_21,0x21);
 191         } else {
 192                 cache_21 &= ~(1<<2);
 193                 cache_A1 &= ~(1<<(irq-8));
 194                 outb(cache_21,0x21);
 195                 outb(cache_A1,0xA1);
 196         }
 197         restore_flags(flags);
 198         return 0;
 199 }
 200                 
 201 int request_irq(unsigned int irq, void (*handler)(int))
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203         struct sigaction sa;
 204 
 205         sa.sa_handler = handler;
 206         sa.sa_flags = 0;
 207         sa.sa_mask = 0;
 208         sa.sa_restorer = NULL;
 209         return irqaction(irq,&sa);
 210 }
 211 
 212 void free_irq(unsigned int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214         struct sigaction * sa = irq + irq_sigaction;
 215         unsigned long flags;
 216 
 217         if (irq > 15) {
 218                 printk("Trying to free IRQ%d\n",irq);
 219                 return;
 220         }
 221         if (!sa->sa_mask) {
 222                 printk("Trying to free free IRQ%d\n",irq);
 223                 return;
 224         }
 225         save_flags(flags);
 226         cli();
 227         if (irq < 8) {
 228                 cache_21 |= 1 << irq;
 229                 outb(cache_21,0x21);
 230         } else {
 231                 cache_A1 |= 1 << (irq-8);
 232                 outb(cache_A1,0xA1);
 233         }
 234         set_intr_gate(0x20+irq,bad_interrupt[irq]);
 235         sa->sa_handler = NULL;
 236         sa->sa_flags = 0;
 237         sa->sa_mask = 0;
 238         sa->sa_restorer = NULL;
 239         restore_flags(flags);
 240 }
 241 
 242 /*
 243  * Note that on a 486, we don't want to do a SIGFPE on a irq13
 244  * as the irq is unreliable, and exception 16 works correctly
 245  * (ie as explained in the intel litterature). On a 386, you
 246  * can't use exception 16 due to bad IBM design, so we have to
 247  * rely on the less exact irq13.
 248  */
 249 static void math_error_irq(int cpl)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         outb(0,0xF0);
 252         if (ignore_irq13)
 253                 return;
 254         send_sig(SIGFPE, last_task_used_math, 1);
 255         __asm__("fninit");
 256 }
 257 
 258 static void no_action(int cpl) { }
     /* [previous][next][first][last][top][bottom][index][help] */
 259 
 260 static struct sigaction ignore_IRQ = {
 261         no_action,
 262         0,
 263         SA_INTERRUPT,
 264         NULL
 265 };
 266 
 267 void init_IRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int i;
 270 
 271         for (i = 0; i < 16 ; i++)
 272                 set_intr_gate(0x20+i,bad_interrupt[i]);
 273         if (irqaction(2,&ignore_IRQ))
 274                 printk("Unable to get IRQ2 for cascade\n");
 275         if (request_irq(13,math_error_irq))
 276                 printk("Unable to get IRQ13 for math-error handler\n");
 277 
 278         /* intialize the bottom half routines. */
 279         for (i = 0; i < 32; i++) {
 280                 bh_base[i].routine = NULL;
 281                 bh_base[i].data = NULL;
 282         }
 283         bh_active = 0;
 284         intr_count = 0;
 285 }

/* [previous][next][first][last][top][bottom][index][help] */