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. get_smp_prof_list
  5. do_IRQ
  6. do_fast_IRQ
  7. request_irq
  8. free_irq
  9. math_error_irq
  10. no_action
  11. probe_irq_on
  12. probe_irq_off
  13. 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/ptrace.h>
  19 #include <linux/errno.h>
  20 #include <linux/kernel_stat.h>
  21 #include <linux/signal.h>
  22 #include <linux/sched.h>
  23 #include <linux/ioport.h>
  24 #include <linux/interrupt.h>
  25 #include <linux/timex.h>
  26 #include <linux/malloc.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 #ifdef __SMP_PROF__
  41 static unsigned int int_count[NR_CPUS][NR_IRQS] = {{0},};
  42 #endif
  43 
  44 void disable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46         unsigned long flags;
  47         unsigned char mask;
  48 
  49         mask = 1 << (irq_nr & 7);
  50         save_flags(flags);
  51         if (irq_nr < 8) {
  52                 cli();
  53                 cache_21 |= mask;
  54                 outb(cache_21,0x21);
  55                 restore_flags(flags);
  56                 return;
  57         }
  58         cli();
  59         cache_A1 |= mask;
  60         outb(cache_A1,0xA1);
  61         restore_flags(flags);
  62 }
  63 
  64 void enable_irq(unsigned int irq_nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66         unsigned long flags;
  67         unsigned char mask;
  68 
  69         mask = ~(1 << (irq_nr & 7));
  70         save_flags(flags);
  71         if (irq_nr < 8) {
  72                 cli();
  73                 cache_21 &= mask;
  74                 outb(cache_21,0x21);
  75                 restore_flags(flags);
  76                 return;
  77         }
  78         cli();
  79         cache_A1 &= mask;
  80         outb(cache_A1,0xA1);
  81         restore_flags(flags);
  82 }
  83 
  84 /*
  85  * This builds up the IRQ handler stubs using some ugly macros in irq.h
  86  *
  87  * These macros create the low-level assembly IRQ routines that do all
  88  * the operations that are needed to keep the AT interrupt-controller
  89  * happy. They are also written to be fast - and to disable interrupts
  90  * as little as humanly possible.
  91  *
  92  * NOTE! These macros expand to three different handlers for each line: one
  93  * complete handler that does all the fancy stuff (including signal handling),
  94  * and one fast handler that is meant for simple IRQ's that want to be
  95  * atomic. The specific handler is chosen depending on the SA_INTERRUPT
  96  * flag when installing a handler. Finally, one "bad interrupt" handler, that
  97  * is used when no handler is present.
  98  *
  99  * The timer interrupt is handled specially to insure that the jiffies
 100  * variable is updated at all times.  Specifically, the timer interrupt is
 101  * just like the complete handlers except that it is invoked with interrupts
 102  * disabled and should never re-enable them.  If other interrupts were
 103  * allowed to be processed while the timer interrupt is active, then the
 104  * other interrupts would have to avoid using the jiffies variable for delay
 105  * and interval timing operations to avoid hanging the system.
 106  */
 107 BUILD_TIMER_IRQ(FIRST,0,0x01)
 108 BUILD_IRQ(FIRST,1,0x02)
 109 BUILD_IRQ(FIRST,2,0x04)
 110 BUILD_IRQ(FIRST,3,0x08)
 111 BUILD_IRQ(FIRST,4,0x10)
 112 BUILD_IRQ(FIRST,5,0x20)
 113 BUILD_IRQ(FIRST,6,0x40)
 114 BUILD_IRQ(FIRST,7,0x80)
 115 BUILD_IRQ(SECOND,8,0x01)
 116 BUILD_IRQ(SECOND,9,0x02)
 117 BUILD_IRQ(SECOND,10,0x04)
 118 BUILD_IRQ(SECOND,11,0x08)
 119 BUILD_IRQ(SECOND,12,0x10)
 120 #ifdef __SMP__
 121 BUILD_MSGIRQ(SECOND,13,0x20)
 122 #else
 123 BUILD_IRQ(SECOND,13,0x20)
 124 #endif
 125 BUILD_IRQ(SECOND,14,0x40)
 126 BUILD_IRQ(SECOND,15,0x80)
 127 #ifdef __SMP__
 128 BUILD_RESCHEDIRQ(16)
 129 #endif
 130 
 131 /*
 132  * Pointers to the low-level handlers: first the general ones, then the
 133  * fast ones, then the bad ones.
 134  */
 135 static void (*interrupt[17])(void) = {
 136         IRQ0_interrupt, IRQ1_interrupt, IRQ2_interrupt, IRQ3_interrupt,
 137         IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
 138         IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
 139         IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, IRQ15_interrupt      
 140 #ifdef __SMP__  
 141         ,IRQ16_interrupt
 142 #endif
 143 };
 144 
 145 static void (*fast_interrupt[16])(void) = {
 146         fast_IRQ0_interrupt, fast_IRQ1_interrupt,
 147         fast_IRQ2_interrupt, fast_IRQ3_interrupt,
 148         fast_IRQ4_interrupt, fast_IRQ5_interrupt,
 149         fast_IRQ6_interrupt, fast_IRQ7_interrupt,
 150         fast_IRQ8_interrupt, fast_IRQ9_interrupt,
 151         fast_IRQ10_interrupt, fast_IRQ11_interrupt,
 152         fast_IRQ12_interrupt, fast_IRQ13_interrupt,
 153         fast_IRQ14_interrupt, fast_IRQ15_interrupt
 154 };
 155 
 156 static void (*bad_interrupt[16])(void) = {
 157         bad_IRQ0_interrupt, bad_IRQ1_interrupt,
 158         bad_IRQ2_interrupt, bad_IRQ3_interrupt,
 159         bad_IRQ4_interrupt, bad_IRQ5_interrupt,
 160         bad_IRQ6_interrupt, bad_IRQ7_interrupt,
 161         bad_IRQ8_interrupt, bad_IRQ9_interrupt,
 162         bad_IRQ10_interrupt, bad_IRQ11_interrupt,
 163         bad_IRQ12_interrupt, bad_IRQ13_interrupt,
 164         bad_IRQ14_interrupt, bad_IRQ15_interrupt
 165 };
 166 
 167 /*
 168  * Initial irq handlers.
 169  */
 170 static struct irqaction timer_irq   = { NULL, 0, 0, NULL, NULL, NULL};
 171 static struct irqaction cascade_irq = { NULL, 0, 0, NULL, NULL, NULL};
 172 static struct irqaction math_irq    = { NULL, 0, 0, NULL, NULL, NULL};
 173 
 174 static struct irqaction *irq_action[16] = {
 175           NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL,
 176           NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL
 177 };
 178 
 179 int get_irq_list(char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         int i, len = 0;
 182         struct irqaction * action;
 183 
 184         for (i = 0 ; i < 16 ; i++) {
 185                 action = *(i + irq_action);
 186                 if (!action) 
 187                         continue;
 188                 len += sprintf(buf+len, "%2d: %8d %c %s",
 189                         i, kstat.interrupts[i],
 190                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
 191                         action->name);
 192                 for (action=action->next; action; action = action->next) {
 193                         len += sprintf(buf+len, ",%s %s",
 194                                 (action->flags & SA_INTERRUPT) ? " +" : "",
 195                                 action->name);
 196                 }
 197                 len += sprintf(buf+len, "\n");
 198         }
 199 /*
 200  *      Linus - should you add NMI counts here ?????
 201  */
 202 #ifdef __SMP_PROF__
 203         len+=sprintf(buf+len, "IPI: %8lu received\n",
 204                 ipi_count);
 205 #endif          
 206         return len;
 207 }
 208 
 209 #ifdef __SMP_PROF__
 210 
 211 int get_smp_prof_list(char *buf) {
     /* [previous][next][first][last][top][bottom][index][help] */
 212         int i,j, len = 0;
 213         struct irqaction * action;
 214         unsigned long sum_spins = 0;
 215         unsigned long sum_spins_syscall = 0;
 216         unsigned long sum_spins_sys_idle = 0;
 217         unsigned long sum_smp_idle_count = 0;
 218 
 219         for (i=0;i<=smp_num_cpus;i++) {
 220           sum_spins+=smp_spins[i];
 221           sum_spins_syscall+=smp_spins_syscall[i];
 222           sum_spins_sys_idle+=smp_spins_sys_idle[i];
 223           sum_smp_idle_count+=smp_idle_count[i];
 224         }
 225 
 226         len += sprintf(buf+len,"CPUS: %10i \n", 
 227                 0==smp_num_cpus?1:smp_num_cpus);
 228         len += sprintf(buf+len,"            SUM ");
 229         for (i=0;i<smp_num_cpus;i++)
 230           len += sprintf(buf+len,"        P%1d ",i);
 231         len += sprintf(buf+len,"\n");
 232         for (i = 0 ; i < NR_IRQS ; i++) {
 233                 action = *(i + irq_action);
 234                 if (!action->handler)
 235                         continue;
 236                 len += sprintf(buf+len, "%3d: %10d ",
 237                         i, kstat.interrupts[i]);
 238                 for (j=0;j<smp_num_cpus;j++)
 239                   len+=sprintf(buf+len, "%10d ",int_count[j][i]);
 240                 len += sprintf(buf+len, "%c %s\n",
 241                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
 242                         action->name);
 243                 for (action=action->next; action; action = action->next) {
 244                         len += sprintf(buf+len, ",%s %s",
 245                                 (action->flags & SA_INTERRUPT) ? " +" : "",
 246                                 action->name);
 247                 }
 248         }
 249         len+=sprintf(buf+len, "LCK: %10lu",
 250                 sum_spins);
 251         for (i=0;i<smp_num_cpus;i++)
 252           len+=sprintf(buf+len," %10lu",smp_spins[i]);
 253         len +=sprintf(buf+len,"   spins from int\n");
 254 
 255         len+=sprintf(buf+len, "LCK: %10lu",
 256                 sum_spins_syscall);
 257         for (i=0;i<smp_num_cpus;i++)
 258           len+=sprintf(buf+len," %10lu",smp_spins_syscall[i]);
 259         len +=sprintf(buf+len,"   spins from syscall\n");
 260 
 261         len+=sprintf(buf+len, "LCK: %10lu",
 262                 sum_spins_sys_idle);
 263         for (i=0;i<smp_num_cpus;i++)
 264           len+=sprintf(buf+len," %10lu",smp_spins_sys_idle[i]);
 265         len +=sprintf(buf+len,"   spins from sysidle\n");
 266         len+=sprintf(buf+len,"IDLE %10lu",sum_smp_idle_count);
 267         for (i=0;i<smp_num_cpus;i++)
 268           len+=sprintf(buf+len," %10lu",smp_idle_count[i]);
 269         len +=sprintf(buf+len,"   idle ticks\n");
 270 
 271         len+=sprintf(buf+len, "IPI: %10lu   received\n",
 272                 ipi_count);
 273 
 274         return len;
 275 }
 276 #endif 
 277 
 278 
 279 
 280 /*
 281  * do_IRQ handles IRQ's that have been installed without the
 282  * SA_INTERRUPT flag: it uses the full signal-handling return
 283  * and runs with other interrupts enabled. All relatively slow
 284  * IRQ's should use this format: notably the keyboard/timer
 285  * routines.
 286  */
 287 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         struct irqaction * action = *(irq + irq_action);
 290 
 291 #ifdef __SMP__
 292         if(smp_threads_ready && active_kernel_processor!=smp_processor_id())
 293                 panic("IRQ %d: active processor set wrongly(%d not %d).\n", irq, active_kernel_processor, smp_processor_id());
 294 #endif
 295 
 296         kstat.interrupts[irq]++;
 297 #ifdef __SMP_PROF__
 298         int_count[smp_processor_id()][irq]++;
 299 #endif
 300         while (action) {
 301             if (action->flags & SA_SAMPLE_RANDOM) {
 302                 add_interrupt_randomness(irq);
 303             }
 304             action->handler(irq, action->dev_id, regs);
 305             action = action->next;
 306         }
 307 }
 308 
 309 /*
 310  * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
 311  * stuff - the handler is also running with interrupts disabled unless
 312  * it explicitly enables them later.
 313  */
 314 asmlinkage void do_fast_IRQ(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 315 {
 316         struct irqaction * action = *(irq + irq_action);
 317 #ifdef __SMP__
 318         /* IRQ 13 is allowed - thats an invalidate */
 319         if(smp_threads_ready && active_kernel_processor!=smp_processor_id() && irq!=13)
 320                 panic("fast_IRQ %d: active processor set wrongly(%d not %d).\n", irq, active_kernel_processor, smp_processor_id());
 321 #endif
 322 
 323         kstat.interrupts[irq]++;
 324 #ifdef __SMP_PROF__
 325         int_count[smp_processor_id()][irq]++;
 326 #endif
 327         while (action) {
 328             if (action->flags & SA_SAMPLE_RANDOM)
 329                 add_interrupt_randomness(irq);
 330             action->handler(irq, action->dev_id, NULL);
 331             action = action->next;
 332         }
 333 }
 334 
 335 #define SA_PROBE SA_ONESHOT
 336 
 337 int request_irq(unsigned int irq, 
     /* [previous][next][first][last][top][bottom][index][help] */
 338                 void (*handler)(int, void *, struct pt_regs *),
 339                 unsigned long irqflags, 
 340                 const char * devname,
 341                 void *dev_id)
 342 {
 343         struct irqaction * action, *tmp = NULL;
 344         unsigned long flags;
 345 
 346         if (irq > 15)
 347             return -EINVAL;
 348         if (!handler)
 349             return -EINVAL;
 350         action = *(irq + irq_action);
 351         if (action) {
 352             if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
 353                 for (tmp = action; tmp->next; tmp = tmp->next);
 354             } else {
 355                 return -EBUSY;
 356             }
 357             if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
 358               printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
 359               return -EBUSY;
 360             }   
 361         }
 362         if (irqflags & SA_SAMPLE_RANDOM)
 363                 rand_initialize_irq(irq);
 364         save_flags(flags);
 365         cli();
 366         if (irq == 2)
 367             action = &cascade_irq;
 368         else if (irq == 13)
 369           action = &math_irq;
 370         else if (irq == TIMER_IRQ)
 371           action = &timer_irq;
 372         else
 373           action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
 374 
 375         if (!action) { 
 376             restore_flags(flags);
 377             return -ENOMEM;
 378         }
 379 
 380         action->handler = handler;
 381         action->flags = irqflags;
 382         action->mask = 0;
 383         action->name = devname;
 384         action->next = NULL;
 385         action->dev_id = dev_id;
 386 
 387         if (tmp) {
 388             tmp->next = action;
 389         } else {
 390             *(irq + irq_action) = action;
 391             if (!(action->flags & SA_PROBE)) {/* SA_ONESHOT used by probing */
 392                 if (action->flags & SA_INTERRUPT)
 393                   set_intr_gate(0x20+irq,fast_interrupt[irq]);
 394                 else
 395                   set_intr_gate(0x20+irq,interrupt[irq]);
 396             }
 397             if (irq < 8) {
 398                 cache_21 &= ~(1<<irq);
 399                 outb(cache_21,0x21);
 400             } else {
 401                 cache_21 &= ~(1<<2);
 402                 cache_A1 &= ~(1<<(irq-8));
 403                 outb(cache_21,0x21);
 404                 outb(cache_A1,0xA1);
 405             }
 406         }
 407 
 408         restore_flags(flags);
 409         return 0;
 410 }
 411                 
 412 void free_irq(unsigned int irq, void *dev_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414         struct irqaction * action = *(irq + irq_action);
 415         struct irqaction * tmp = NULL;
 416         unsigned long flags;
 417 
 418         if (irq > 15) {
 419                 printk("Trying to free IRQ%d\n",irq);
 420                 return;
 421         }
 422         if (!action->handler) {
 423                 printk("Trying to free free IRQ%d\n",irq);
 424                 return;
 425         }
 426         if (dev_id) {
 427             for (; action; action = action->next) {
 428                 if (action->dev_id == dev_id) break;
 429                 tmp = action;
 430             }
 431             if (!action) {
 432                 printk("Trying to free free shared IRQ%d\n",irq);
 433                 return;
 434             }
 435         } else if (action->flags & SA_SHIRQ) {
 436             printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
 437             return;
 438         }
 439         save_flags(flags);
 440         cli();
 441         if (action && tmp) {
 442             tmp->next = action->next;
 443         } else {
 444             *(irq + irq_action) = action->next;
 445         }
 446 
 447         if ((irq == 2) || (irq == 13) | (irq == TIMER_IRQ))
 448           memset(action, 0, sizeof(struct irqaction));
 449         else 
 450           kfree_s(action, sizeof(struct irqaction));
 451         
 452         if (!(*(irq + irq_action))) {
 453             if (irq < 8) {
 454                 cache_21 |= 1 << irq;
 455                 outb(cache_21,0x21);
 456             } else {
 457                 cache_A1 |= 1 << (irq-8);
 458                 outb(cache_A1,0xA1);
 459             }
 460             set_intr_gate(0x20+irq,bad_interrupt[irq]);
 461         }
 462 
 463         restore_flags(flags);
 464 }
 465 
 466 #ifndef __SMP__
 467 
 468 /*
 469  * Note that on a 486, we don't want to do a SIGFPE on a irq13
 470  * as the irq is unreliable, and exception 16 works correctly
 471  * (ie as explained in the intel literature). On a 386, you
 472  * can't use exception 16 due to bad IBM design, so we have to
 473  * rely on the less exact irq13.
 474  *
 475  * Careful.. Not only is IRQ13 unreliable, but it is also
 476  * leads to races. IBM designers who came up with it should
 477  * be shot.
 478  */
 479  
 480 
 481 static void math_error_irq(int cpl, void *dev_id, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 482 {
 483         outb(0,0xF0);
 484         if (ignore_irq13 || !hard_math)
 485                 return;
 486         math_error();
 487 }
 488 
 489 #endif
 490 
 491 static void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
     /* [previous][next][first][last][top][bottom][index][help] */
 492 
 493 unsigned long probe_irq_on (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495         unsigned int i, irqs = 0, irqmask;
 496         unsigned long delay;
 497 
 498         /* first, snaffle up any unassigned irqs */
 499         for (i = 15; i > 0; i--) {
 500                 if (!request_irq(i, no_action, SA_PROBE, "probe", NULL)) {
 501                         enable_irq(i);
 502                         irqs |= (1 << i);
 503                 }
 504         }
 505 
 506         /* wait for spurious interrupts to mask themselves out again */
 507         for (delay = jiffies + 2; delay > jiffies; );   /* min 10ms delay */
 508 
 509         /* now filter out any obviously spurious interrupts */
 510         irqmask = (((unsigned int)cache_A1)<<8) | (unsigned int)cache_21;
 511         for (i = 15; i > 0; i--) {
 512                 if (irqs & (1 << i) & irqmask) {
 513                         irqs ^= (1 << i);
 514                         free_irq(i, NULL);
 515                 }
 516         }
 517 #ifdef DEBUG
 518         printk("probe_irq_on:  irqs=0x%04x irqmask=0x%04x\n", irqs, irqmask);
 519 #endif
 520         return irqs;
 521 }
 522 
 523 int probe_irq_off (unsigned long irqs)
     /* [previous][next][first][last][top][bottom][index][help] */
 524 {
 525         unsigned int i, irqmask;
 526 
 527         irqmask = (((unsigned int)cache_A1)<<8) | (unsigned int)cache_21;
 528         for (i = 15; i > 0; i--) {
 529                 if (irqs & (1 << i)) {
 530                         free_irq(i, NULL);
 531                 }
 532         }
 533 #ifdef DEBUG
 534         printk("probe_irq_off: irqs=0x%04x irqmask=0x%04x\n", irqs, irqmask);
 535 #endif
 536         irqs &= irqmask;
 537         if (!irqs)
 538                 return 0;
 539         i = ffz(~irqs);
 540         if (irqs != (irqs & (1 << i)))
 541                 i = -i;
 542         return i;
 543 }
 544 
 545 void init_IRQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 546 {
 547         int i;
 548         static unsigned char smptrap=0;
 549         if(smptrap)
 550                 return;
 551         smptrap=1;
 552 
 553         /* set the clock to 100 Hz */
 554         outb_p(0x34,0x43);              /* binary, mode 2, LSB/MSB, ch 0 */
 555         outb_p(LATCH & 0xff , 0x40);    /* LSB */
 556         outb(LATCH >> 8 , 0x40);        /* MSB */
 557         for (i = 0; i < 16 ; i++)
 558                 set_intr_gate(0x20+i,bad_interrupt[i]);
 559         /* This bit is a hack because we don't send timer messages to all processors yet */
 560         /* It has to here .. it doesnt work if you put it down the bottom - assembler explodes 8) */
 561 #ifdef __SMP__  
 562         set_intr_gate(0x20+i, interrupt[i]);    /* IRQ '16' - IPI for rescheduling */
 563 #endif  
 564         if (request_irq(2, no_action, SA_INTERRUPT, "cascade", NULL))
 565                 printk("Unable to get IRQ2 for cascade.\n");
 566 #ifndef __SMP__         
 567         if (request_irq(13, math_error_irq, 0, "math error", NULL))
 568                 printk("Unable to get IRQ13 for math-error handler.\n");
 569 #else
 570         if (request_irq(13, smp_message_irq, SA_INTERRUPT, "IPI", NULL))
 571                 printk("Unable to get IRQ13 for IPI.\n");
 572 #endif                          
 573         request_region(0x20,0x20,"pic1");
 574         request_region(0xa0,0x20,"pic2");
 575 } 

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