root/kernel/irq.c

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

DEFINITIONS

This source file includes following definitions.
  1. disable_irq
  2. enable_irq
  3. do_bottom_half
  4. do_IRQ
  5. do_fast_IRQ
  6. irqaction
  7. request_irq
  8. free_irq
  9. math_error_irq
  10. no_action
  11. 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 char cache_21 = 0xff;
  38 static unsigned char cache_A1 = 0xff;
  39 
  40 unsigned long intr_count = 0;
  41 unsigned long bh_active = 0;
  42 unsigned long bh_mask = 0xFFFFFFFF;
  43 struct bh_struct bh_base[32]; 
  44 
  45 void disable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47         unsigned long flags;
  48         unsigned char mask;
  49 
  50         mask = 1 << (irq_nr & 7);
  51         save_flags(flags);
  52         if (irq_nr < 8) {
  53                 cli();
  54                 cache_21 |= mask;
  55                 outb(cache_21,0x21);
  56                 restore_flags(flags);
  57                 return;
  58         }
  59         cli();
  60         cache_A1 |= mask;
  61         outb(cache_A1,0xA1);
  62         restore_flags(flags);
  63 }
  64 
  65 void enable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67         unsigned long flags;
  68         unsigned char mask;
  69 
  70         mask = ~(1 << (irq_nr & 7));
  71         save_flags(flags);
  72         if (irq_nr < 8) {
  73                 cli();
  74                 cache_21 &= mask;
  75                 outb(cache_21,0x21);
  76                 restore_flags(flags);
  77                 return;
  78         }
  79         cli();
  80         cache_A1 &= mask;
  81         outb(cache_A1,0xA1);
  82         restore_flags(flags);
  83 }
  84 
  85 /*
  86  * do_bottom_half() runs at normal kernel priority: all interrupts
  87  * enabled.  do_bottom_half() is atomic with respect to itself: a
  88  * bottom_half handler need not be re-entrant.
  89  */
  90 asmlinkage void do_bottom_half(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         unsigned long active;
  93         unsigned long mask, left;
  94         struct bh_struct *bh;
  95 
  96         bh = bh_base;
  97         active = bh_active & bh_mask;
  98         for (mask = 1, left = ~0 ; left & active ; bh++,mask += mask,left += left) {
  99                 if (mask & active) {
 100                         void (*fn)(void *);
 101                         bh_active &= ~mask;
 102                         fn = bh->routine;
 103                         if (!fn)
 104                                 goto bad_bh;
 105                         fn(bh->data);
 106                 }
 107         }
 108         return;
 109 bad_bh:
 110         printk ("irq.c:bad bottom half entry\n");
 111 }
 112 
 113 /*
 114  * This builds up the IRQ handler stubs using some ugly macros in irq.h
 115  *
 116  * These macros create the low-level assembly IRQ routines that do all
 117  * the operations that are needed to keep the AT interrupt-controller
 118  * happy. They are also written to be fast - and to disable interrupts
 119  * as little as humanly possible.
 120  *
 121  * NOTE! These macros expand to three different handlers for each line: one
 122  * complete handler that does all the fancy stuff (including signal handling),
 123  * and one fast handler that is meant for simple IRQ's that want to be
 124  * atomic. The specific handler is chosen depending on the SA_INTERRUPT
 125  * flag when installing a handler. Finally, one "bad interrupt" handler, that
 126  * is used when no handler is present.
 127  */
 128 BUILD_IRQ(FIRST,0,0x01)
 129 BUILD_IRQ(FIRST,1,0x02)
 130 BUILD_IRQ(FIRST,2,0x04)
 131 BUILD_IRQ(FIRST,3,0x08)
 132 BUILD_IRQ(FIRST,4,0x10)
 133 BUILD_IRQ(FIRST,5,0x20)
 134 BUILD_IRQ(FIRST,6,0x40)
 135 BUILD_IRQ(FIRST,7,0x80)
 136 BUILD_IRQ(SECOND,8,0x01)
 137 BUILD_IRQ(SECOND,9,0x02)
 138 BUILD_IRQ(SECOND,10,0x04)
 139 BUILD_IRQ(SECOND,11,0x08)
 140 BUILD_IRQ(SECOND,12,0x10)
 141 BUILD_IRQ(SECOND,13,0x20)
 142 BUILD_IRQ(SECOND,14,0x40)
 143 BUILD_IRQ(SECOND,15,0x80)
 144 
 145 /*
 146  * Pointers to the low-level handlers: first the general ones, then the
 147  * fast ones, then the bad ones.
 148  */
 149 static void (*interrupt[16])(void) = {
 150         IRQ0_interrupt, IRQ1_interrupt, IRQ2_interrupt, IRQ3_interrupt,
 151         IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
 152         IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
 153         IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, IRQ15_interrupt
 154 };
 155 
 156 static void (*fast_interrupt[16])(void) = {
 157         fast_IRQ0_interrupt, fast_IRQ1_interrupt,
 158         fast_IRQ2_interrupt, fast_IRQ3_interrupt,
 159         fast_IRQ4_interrupt, fast_IRQ5_interrupt,
 160         fast_IRQ6_interrupt, fast_IRQ7_interrupt,
 161         fast_IRQ8_interrupt, fast_IRQ9_interrupt,
 162         fast_IRQ10_interrupt, fast_IRQ11_interrupt,
 163         fast_IRQ12_interrupt, fast_IRQ13_interrupt,
 164         fast_IRQ14_interrupt, fast_IRQ15_interrupt
 165 };
 166 
 167 static void (*bad_interrupt[16])(void) = {
 168         bad_IRQ0_interrupt, bad_IRQ1_interrupt,
 169         bad_IRQ2_interrupt, bad_IRQ3_interrupt,
 170         bad_IRQ4_interrupt, bad_IRQ5_interrupt,
 171         bad_IRQ6_interrupt, bad_IRQ7_interrupt,
 172         bad_IRQ8_interrupt, bad_IRQ9_interrupt,
 173         bad_IRQ10_interrupt, bad_IRQ11_interrupt,
 174         bad_IRQ12_interrupt, bad_IRQ13_interrupt,
 175         bad_IRQ14_interrupt, bad_IRQ15_interrupt
 176 };
 177 
 178 /*
 179  * Initial irq handlers.
 180  */
 181 static struct sigaction irq_sigaction[16] = {
 182         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 183         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 184         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 185         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 186         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 187         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 188         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 189         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL }
 190 };
 191 
 192 /*
 193  * do_IRQ handles IRQ's that have been installed without the
 194  * SA_INTERRUPT flag: it uses the full signal-handling return
 195  * and runs with other interrupts enabled. All relatively slow
 196  * IRQ's should use this format: notably the keyboard/timer
 197  * routines.
 198  */
 199 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         struct sigaction * sa = irq + irq_sigaction;
 202 
 203         sa->sa_handler((int) regs);
 204 }
 205 
 206 /*
 207  * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
 208  * stuff - the handler is also running with interrupts disabled unless
 209  * it explicitly enables them later.
 210  */
 211 asmlinkage void do_fast_IRQ(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {
 213         struct sigaction * sa = irq + irq_sigaction;
 214 
 215         sa->sa_handler(irq);
 216 }
 217 
 218 int irqaction(unsigned int irq, struct sigaction * new_sa)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220         struct sigaction * sa;
 221         unsigned long flags;
 222 
 223         if (irq > 15)
 224                 return -EINVAL;
 225         sa = irq + irq_sigaction;
 226         if (sa->sa_mask)
 227                 return -EBUSY;
 228         if (!new_sa->sa_handler)
 229                 return -EINVAL;
 230         save_flags(flags);
 231         cli();
 232         *sa = *new_sa;
 233         sa->sa_mask = 1;
 234         if (sa->sa_flags & SA_INTERRUPT)
 235                 set_intr_gate(0x20+irq,fast_interrupt[irq]);
 236         else
 237                 set_intr_gate(0x20+irq,interrupt[irq]);
 238         if (irq < 8) {
 239                 cache_21 &= ~(1<<irq);
 240                 outb(cache_21,0x21);
 241         } else {
 242                 cache_21 &= ~(1<<2);
 243                 cache_A1 &= ~(1<<(irq-8));
 244                 outb(cache_21,0x21);
 245                 outb(cache_A1,0xA1);
 246         }
 247         restore_flags(flags);
 248         return 0;
 249 }
 250                 
 251 int request_irq(unsigned int irq, void (*handler)(int))
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253         struct sigaction sa;
 254 
 255         sa.sa_handler = handler;
 256         sa.sa_flags = 0;
 257         sa.sa_mask = 0;
 258         sa.sa_restorer = NULL;
 259         return irqaction(irq,&sa);
 260 }
 261 
 262 void free_irq(unsigned int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         struct sigaction * sa = irq + irq_sigaction;
 265         unsigned long flags;
 266 
 267         if (irq > 15) {
 268                 printk("Trying to free IRQ%d\n",irq);
 269                 return;
 270         }
 271         if (!sa->sa_mask) {
 272                 printk("Trying to free free IRQ%d\n",irq);
 273                 return;
 274         }
 275         save_flags(flags);
 276         cli();
 277         if (irq < 8) {
 278                 cache_21 |= 1 << irq;
 279                 outb(cache_21,0x21);
 280         } else {
 281                 cache_A1 |= 1 << (irq-8);
 282                 outb(cache_A1,0xA1);
 283         }
 284         set_intr_gate(0x20+irq,bad_interrupt[irq]);
 285         sa->sa_handler = NULL;
 286         sa->sa_flags = 0;
 287         sa->sa_mask = 0;
 288         sa->sa_restorer = NULL;
 289         restore_flags(flags);
 290 }
 291 
 292 /*
 293  * Note that on a 486, we don't want to do a SIGFPE on a irq13
 294  * as the irq is unreliable, and exception 16 works correctly
 295  * (ie as explained in the intel litterature). On a 386, you
 296  * can't use exception 16 due to bad IBM design, so we have to
 297  * rely on the less exact irq13.
 298  *
 299  * Careful.. Not only is IRQ13 unreliable, but it is also
 300  * leads to races. IBM designers who came up with it should
 301  * be shot.
 302  */
 303 static void math_error_irq(int cpl)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305         outb(0,0xF0);
 306         if (ignore_irq13)
 307                 return;
 308         math_error();
 309 }
 310 
 311 static void no_action(int cpl) { }
     /* [previous][next][first][last][top][bottom][index][help] */
 312 
 313 static struct sigaction ignore_IRQ = {
 314         no_action,
 315         0,
 316         SA_INTERRUPT,
 317         NULL
 318 };
 319 
 320 void init_IRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         int i;
 323 
 324         for (i = 0; i < 16 ; i++)
 325                 set_intr_gate(0x20+i,bad_interrupt[i]);
 326         if (irqaction(2,&ignore_IRQ))
 327                 printk("Unable to get IRQ2 for cascade\n");
 328         if (request_irq(13,math_error_irq))
 329                 printk("Unable to get IRQ13 for math-error handler\n");
 330 
 331         /* intialize the bottom half routines. */
 332         for (i = 0; i < 32; i++) {
 333                 bh_base[i].routine = NULL;
 334                 bh_base[i].data = NULL;
 335         }
 336         bh_active = 0;
 337         intr_count = 0;
 338 }

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