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. get_irq_list
  5. do_IRQ
  6. do_fast_IRQ
  7. irqaction
  8. request_irq
  9. free_irq
  10. math_error_irq
  11. no_action
  12. probe_irq_on
  13. probe_irq_off
  14. 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 (0 if no).
  20  * sa_mask is horribly ugly (I won't even mention it)
  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/kernel_stat.h>
  28 #include <linux/signal.h>
  29 #include <linux/sched.h>
  30 #include <linux/interrupt.h>
  31 
  32 #include <asm/system.h>
  33 #include <asm/io.h>
  34 #include <asm/irq.h>
  35 #include <asm/bitops.h>
  36 
  37 #define CR0_NE 32
  38 
  39 static unsigned char cache_21 = 0xff;
  40 static unsigned char cache_A1 = 0xff;
  41 
  42 unsigned long intr_count = 0;
  43 unsigned long bh_active = 0;
  44 unsigned long bh_mask = 0xFFFFFFFF;
  45 struct bh_struct bh_base[32]; 
  46 
  47 void disable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         unsigned long flags;
  50         unsigned char mask;
  51 
  52         mask = 1 << (irq_nr & 7);
  53         save_flags(flags);
  54         if (irq_nr < 8) {
  55                 cli();
  56                 cache_21 |= mask;
  57                 outb(cache_21,0x21);
  58                 restore_flags(flags);
  59                 return;
  60         }
  61         cli();
  62         cache_A1 |= mask;
  63         outb(cache_A1,0xA1);
  64         restore_flags(flags);
  65 }
  66 
  67 void enable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69         unsigned long flags;
  70         unsigned char mask;
  71 
  72         mask = ~(1 << (irq_nr & 7));
  73         save_flags(flags);
  74         if (irq_nr < 8) {
  75                 cli();
  76                 cache_21 &= mask;
  77                 outb(cache_21,0x21);
  78                 restore_flags(flags);
  79                 return;
  80         }
  81         cli();
  82         cache_A1 &= mask;
  83         outb(cache_A1,0xA1);
  84         restore_flags(flags);
  85 }
  86 
  87 /*
  88  * do_bottom_half() runs at normal kernel priority: all interrupts
  89  * enabled.  do_bottom_half() is atomic with respect to itself: a
  90  * bottom_half handler need not be re-entrant.
  91  */
  92 asmlinkage void do_bottom_half(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94         unsigned long active;
  95         unsigned long mask, left;
  96         struct bh_struct *bh;
  97 
  98         bh = bh_base;
  99         active = bh_active & bh_mask;
 100         for (mask = 1, left = ~0 ; left & active ; bh++,mask += mask,left += left) {
 101                 if (mask & active) {
 102                         void (*fn)(void *);
 103                         bh_active &= ~mask;
 104                         fn = bh->routine;
 105                         if (!fn)
 106                                 goto bad_bh;
 107                         fn(bh->data);
 108                 }
 109         }
 110         return;
 111 bad_bh:
 112         printk ("irq.c:bad bottom half entry %08lx\n", mask);
 113 }
 114 
 115 /*
 116  * This builds up the IRQ handler stubs using some ugly macros in irq.h
 117  *
 118  * These macros create the low-level assembly IRQ routines that do all
 119  * the operations that are needed to keep the AT interrupt-controller
 120  * happy. They are also written to be fast - and to disable interrupts
 121  * as little as humanly possible.
 122  *
 123  * NOTE! These macros expand to three different handlers for each line: one
 124  * complete handler that does all the fancy stuff (including signal handling),
 125  * and one fast handler that is meant for simple IRQ's that want to be
 126  * atomic. The specific handler is chosen depending on the SA_INTERRUPT
 127  * flag when installing a handler. Finally, one "bad interrupt" handler, that
 128  * is used when no handler is present.
 129  */
 130 BUILD_IRQ(FIRST,0,0x01)
 131 BUILD_IRQ(FIRST,1,0x02)
 132 BUILD_IRQ(FIRST,2,0x04)
 133 BUILD_IRQ(FIRST,3,0x08)
 134 BUILD_IRQ(FIRST,4,0x10)
 135 BUILD_IRQ(FIRST,5,0x20)
 136 BUILD_IRQ(FIRST,6,0x40)
 137 BUILD_IRQ(FIRST,7,0x80)
 138 BUILD_IRQ(SECOND,8,0x01)
 139 BUILD_IRQ(SECOND,9,0x02)
 140 BUILD_IRQ(SECOND,10,0x04)
 141 BUILD_IRQ(SECOND,11,0x08)
 142 BUILD_IRQ(SECOND,12,0x10)
 143 BUILD_IRQ(SECOND,13,0x20)
 144 BUILD_IRQ(SECOND,14,0x40)
 145 BUILD_IRQ(SECOND,15,0x80)
 146 
 147 /*
 148  * Pointers to the low-level handlers: first the general ones, then the
 149  * fast ones, then the bad ones.
 150  */
 151 static void (*interrupt[16])(void) = {
 152         IRQ0_interrupt, IRQ1_interrupt, IRQ2_interrupt, IRQ3_interrupt,
 153         IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
 154         IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
 155         IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, IRQ15_interrupt
 156 };
 157 
 158 static void (*fast_interrupt[16])(void) = {
 159         fast_IRQ0_interrupt, fast_IRQ1_interrupt,
 160         fast_IRQ2_interrupt, fast_IRQ3_interrupt,
 161         fast_IRQ4_interrupt, fast_IRQ5_interrupt,
 162         fast_IRQ6_interrupt, fast_IRQ7_interrupt,
 163         fast_IRQ8_interrupt, fast_IRQ9_interrupt,
 164         fast_IRQ10_interrupt, fast_IRQ11_interrupt,
 165         fast_IRQ12_interrupt, fast_IRQ13_interrupt,
 166         fast_IRQ14_interrupt, fast_IRQ15_interrupt
 167 };
 168 
 169 static void (*bad_interrupt[16])(void) = {
 170         bad_IRQ0_interrupt, bad_IRQ1_interrupt,
 171         bad_IRQ2_interrupt, bad_IRQ3_interrupt,
 172         bad_IRQ4_interrupt, bad_IRQ5_interrupt,
 173         bad_IRQ6_interrupt, bad_IRQ7_interrupt,
 174         bad_IRQ8_interrupt, bad_IRQ9_interrupt,
 175         bad_IRQ10_interrupt, bad_IRQ11_interrupt,
 176         bad_IRQ12_interrupt, bad_IRQ13_interrupt,
 177         bad_IRQ14_interrupt, bad_IRQ15_interrupt
 178 };
 179 
 180 /*
 181  * Initial irq handlers.
 182  */
 183 static struct sigaction irq_sigaction[16] = {
 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         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
 191         { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL }
 192 };
 193 
 194 int get_irq_list(char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         int i, len = 0;
 197         struct sigaction * sa = irq_sigaction;
 198 
 199         for (i = 0 ; i < 16 ; i++, sa++) {
 200                 if (!sa->sa_handler)
 201                         continue;
 202                 len += sprintf(buf+len, "%2d: %8d %c %s\n",
 203                         i, kstat.interrupts[i],
 204                         (sa->sa_flags & SA_INTERRUPT) ? '+' : ' ',
 205                         (char *) sa->sa_mask);
 206         }
 207         return len;
 208 }
 209 
 210 /*
 211  * do_IRQ handles IRQ's that have been installed without the
 212  * SA_INTERRUPT flag: it uses the full signal-handling return
 213  * and runs with other interrupts enabled. All relatively slow
 214  * IRQ's should use this format: notably the keyboard/timer
 215  * routines.
 216  */
 217 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219         struct sigaction * sa = irq + irq_sigaction;
 220 
 221         kstat.interrupts[irq]++;
 222         sa->sa_handler((int) regs);
 223 }
 224 
 225 /*
 226  * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
 227  * stuff - the handler is also running with interrupts disabled unless
 228  * it explicitly enables them later.
 229  */
 230 asmlinkage void do_fast_IRQ(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232         struct sigaction * sa = irq + irq_sigaction;
 233 
 234         kstat.interrupts[irq]++;
 235         sa->sa_handler(irq);
 236 }
 237 
 238 #define SA_PROBE SA_ONESHOT
 239 
 240 /*
 241  * Using "struct sigaction" is slightly silly, but there
 242  * are historical reasons and it works well, so..
 243  */
 244 static int irqaction(unsigned int irq, struct sigaction * new_sa)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246         struct sigaction * sa;
 247         unsigned long flags;
 248 
 249         if (irq > 15)
 250                 return -EINVAL;
 251         sa = irq + irq_sigaction;
 252         if (sa->sa_handler)
 253                 return -EBUSY;
 254         if (!new_sa->sa_handler)
 255                 return -EINVAL;
 256         save_flags(flags);
 257         cli();
 258         *sa = *new_sa;
 259         if (!(sa->sa_flags & SA_PROBE)) { /* SA_ONESHOT is used by probing */
 260                 if (sa->sa_flags & SA_INTERRUPT)
 261                         set_intr_gate(0x20+irq,fast_interrupt[irq]);
 262                 else
 263                         set_intr_gate(0x20+irq,interrupt[irq]);
 264         }
 265         if (irq < 8) {
 266                 cache_21 &= ~(1<<irq);
 267                 outb(cache_21,0x21);
 268         } else {
 269                 cache_21 &= ~(1<<2);
 270                 cache_A1 &= ~(1<<(irq-8));
 271                 outb(cache_21,0x21);
 272                 outb(cache_A1,0xA1);
 273         }
 274         restore_flags(flags);
 275         return 0;
 276 }
 277                 
 278 int request_irq(unsigned int irq, void (*handler)(int),
     /* [previous][next][first][last][top][bottom][index][help] */
 279         unsigned long flags, const char * devname)
 280 {
 281         struct sigaction sa;
 282 
 283         sa.sa_handler = handler;
 284         sa.sa_flags = flags;
 285         sa.sa_mask = (unsigned long) devname;
 286         sa.sa_restorer = NULL;
 287         return irqaction(irq,&sa);
 288 }
 289 
 290 void free_irq(unsigned int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292         struct sigaction * sa = irq + irq_sigaction;
 293         unsigned long flags;
 294 
 295         if (irq > 15) {
 296                 printk("Trying to free IRQ%d\n",irq);
 297                 return;
 298         }
 299         if (!sa->sa_handler) {
 300                 printk("Trying to free free IRQ%d\n",irq);
 301                 return;
 302         }
 303         save_flags(flags);
 304         cli();
 305         if (irq < 8) {
 306                 cache_21 |= 1 << irq;
 307                 outb(cache_21,0x21);
 308         } else {
 309                 cache_A1 |= 1 << (irq-8);
 310                 outb(cache_A1,0xA1);
 311         }
 312         set_intr_gate(0x20+irq,bad_interrupt[irq]);
 313         sa->sa_handler = NULL;
 314         sa->sa_flags = 0;
 315         sa->sa_mask = 0;
 316         sa->sa_restorer = NULL;
 317         restore_flags(flags);
 318 }
 319 
 320 /*
 321  * Note that on a 486, we don't want to do a SIGFPE on a irq13
 322  * as the irq is unreliable, and exception 16 works correctly
 323  * (ie as explained in the intel literature). On a 386, you
 324  * can't use exception 16 due to bad IBM design, so we have to
 325  * rely on the less exact irq13.
 326  *
 327  * Careful.. Not only is IRQ13 unreliable, but it is also
 328  * leads to races. IBM designers who came up with it should
 329  * be shot.
 330  */
 331 static void math_error_irq(int cpl)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333         outb(0,0xF0);
 334         if (ignore_irq13 || !hard_math)
 335                 return;
 336         math_error();
 337 }
 338 
 339 static void no_action(int cpl) { }
     /* [previous][next][first][last][top][bottom][index][help] */
 340 
 341 unsigned int probe_irq_on (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 342 {
 343         unsigned int i, irqs = 0, irqmask;
 344         unsigned long delay;
 345 
 346         /* first, snaffle up any unassigned irqs */
 347         for (i = 15; i > 0; i--) {
 348                 if (!request_irq(i, no_action, SA_PROBE, "probe")) {
 349                         enable_irq(i);
 350                         irqs |= (1 << i);
 351                 }
 352         }
 353 
 354         /* wait for spurious interrupts to mask themselves out again */
 355         for (delay = jiffies + 2; delay > jiffies; );   /* min 10ms delay */
 356 
 357         /* now filter out any obviously spurious interrupts */
 358         irqmask = (((unsigned int)cache_A1)<<8) | (unsigned int)cache_21;
 359         for (i = 15; i > 0; i--) {
 360                 if (irqs & (1 << i) & irqmask) {
 361                         irqs ^= (1 << i);
 362                         free_irq(i);
 363                 }
 364         }
 365 #ifdef DEBUG
 366         printk("probe_irq_on:  irqs=0x%04x irqmask=0x%04x\n", irqs, irqmask);
 367 #endif
 368         return irqs;
 369 }
 370 
 371 int probe_irq_off (unsigned int irqs)
     /* [previous][next][first][last][top][bottom][index][help] */
 372 {
 373         unsigned int i, irqmask;
 374 
 375         irqmask = (((unsigned int)cache_A1)<<8) | (unsigned int)cache_21;
 376         for (i = 15; i > 0; i--) {
 377                 if (irqs & (1 << i)) {
 378                         free_irq(i);
 379                 }
 380         }
 381 #ifdef DEBUG
 382         printk("probe_irq_off: irqs=0x%04x irqmask=0x%04x\n", irqs, irqmask);
 383 #endif
 384         irqs &= irqmask;
 385         if (!irqs)
 386                 return 0;
 387         i = ffz(~irqs);
 388         if (irqs != (irqs & (1 << i)))
 389                 i = -i;
 390         return i;
 391 }
 392 
 393 void init_IRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 394 {
 395         int i;
 396 
 397         for (i = 0; i < 16 ; i++)
 398                 set_intr_gate(0x20+i,bad_interrupt[i]);
 399         if (request_irq(2, no_action, SA_INTERRUPT, "cascade"))
 400                 printk("Unable to get IRQ2 for cascade\n");
 401         if (request_irq(13,math_error_irq, 0, "math error"))
 402                 printk("Unable to get IRQ13 for math-error handler\n");
 403 
 404         /* initialize the bottom half routines. */
 405         for (i = 0; i < 32; i++) {
 406                 bh_base[i].routine = NULL;
 407                 bh_base[i].data = NULL;
 408         }
 409         bh_active = 0;
 410         intr_count = 0;
 411 }

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