root/arch/i386/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. get_irq_list
  4. do_IRQ
  5. do_fast_IRQ
  6. request_irq
  7. free_irq
  8. math_error_irq
  9. no_action
  10. probe_irq_on
  11. probe_irq_off
  12. init_IRQ

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

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