File: /usr/src/linux/arch/mips/kernel/irq.c
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Code to handle x86 style IRQs plus some generic interrupt stuff.
7 *
8 * Copyright (C) 1992 Linus Torvalds
9 * Copyright (C) 1994 - 2000 Ralf Baechle
10 */
11 #include <linux/kernel.h>
12 #include <linux/irq.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/slab.h>
17 #include <linux/mm.h>
18 #include <linux/random.h>
19 #include <linux/sched.h>
20
21 #include <asm/system.h>
22
23 /*
24 * Controller mappings for all interrupt sources:
25 */
26 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
27 { [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
28
29 /*
30 * Special irq handlers.
31 */
32
33 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
34
35 /*
36 * Generic no controller code
37 */
38
39 static void enable_none(unsigned int irq) { }
40 static unsigned int startup_none(unsigned int irq) { return 0; }
41 static void disable_none(unsigned int irq) { }
42 static void ack_none(unsigned int irq)
43 {
44 /*
45 * 'what should we do if we get a hw irq event on an illegal vector'.
46 * each architecture has to answer this themselves, it doesnt deserve
47 * a generic callback i think.
48 */
49 printk("unexpected interrupt %d\n", irq);
50 }
51
52 /* startup is the same as "enable", shutdown is same as "disable" */
53 #define shutdown_none disable_none
54 #define end_none enable_none
55
56 struct hw_interrupt_type no_irq_type = {
57 "none",
58 startup_none,
59 shutdown_none,
60 enable_none,
61 disable_none,
62 ack_none,
63 end_none
64 };
65
66 volatile unsigned long irq_err_count, spurious_count;
67
68 /*
69 * Generic, controller-independent functions:
70 */
71
72 int get_irq_list(char *buf)
73 {
74 struct irqaction * action;
75 char *p = buf;
76 int i;
77
78 p += sprintf(p, " ");
79 for (i=0; i < 1 /*smp_num_cpus*/; i++)
80 p += sprintf(p, "CPU%d ", i);
81 *p++ = '\n';
82
83 for (i = 0 ; i < NR_IRQS ; i++) {
84 action = irq_desc[i].action;
85 if (!action)
86 continue;
87 p += sprintf(p, "%3d: ",i);
88 p += sprintf(p, "%10u ", kstat_irqs(i));
89 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
90 p += sprintf(p, " %s", action->name);
91
92 for (action=action->next; action; action = action->next)
93 p += sprintf(p, ", %s", action->name);
94 *p++ = '\n';
95 }
96 p += sprintf(p, "ERR: %10lu\n", irq_err_count);
97 return p - buf;
98 }
99
100 /*
101 * This should really return information about whether
102 * we should do bottom half handling etc. Right now we
103 * end up _always_ checking the bottom half, which is a
104 * waste of time and is not what some drivers would
105 * prefer.
106 */
107 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
108 {
109 int status;
110 int cpu = smp_processor_id();
111
112 irq_enter(cpu, irq);
113
114 status = 1; /* Force the "do bottom halves" bit */
115
116 if (!(action->flags & SA_INTERRUPT))
117 __sti();
118
119 do {
120 status |= action->flags;
121 action->handler(irq, action->dev_id, regs);
122 action = action->next;
123 } while (action);
124 if (status & SA_SAMPLE_RANDOM)
125 add_interrupt_randomness(irq);
126 __cli();
127
128 irq_exit(cpu, irq);
129
130 return status;
131 }
132
133 /*
134 * Generic enable/disable code: this just calls
135 * down into the PIC-specific version for the actual
136 * hardware disable after having gotten the irq
137 * controller lock.
138 */
139
140 /**
141 * disable_irq_nosync - disable an irq without waiting
142 * @irq: Interrupt to disable
143 *
144 * Disable the selected interrupt line. Disables of an interrupt
145 * stack. Unlike disable_irq(), this function does not ensure existing
146 * instances of the IRQ handler have completed before returning.
147 *
148 * This function may be called from IRQ context.
149 */
150
151 void inline disable_irq_nosync(unsigned int irq)
152 {
153 irq_desc_t *desc = irq_desc + irq;
154 unsigned long flags;
155
156 spin_lock_irqsave(&desc->lock, flags);
157 if (!desc->depth++) {
158 desc->status |= IRQ_DISABLED;
159 desc->handler->disable(irq);
160 }
161 spin_unlock_irqrestore(&desc->lock, flags);
162 }
163
164 /**
165 * disable_irq - disable an irq and wait for completion
166 * @irq: Interrupt to disable
167 *
168 * Disable the selected interrupt line. Disables of an interrupt
169 * stack. That is for two disables you need two enables. This
170 * function waits for any pending IRQ handlers for this interrupt
171 * to complete before returning. If you use this function while
172 * holding a resource the IRQ handler may need you will deadlock.
173 *
174 * This function may be called - with care - from IRQ context.
175 */
176
177 void disable_irq(unsigned int irq)
178 {
179 disable_irq_nosync(irq);
180
181 if (!local_irq_count(smp_processor_id())) {
182 do {
183 barrier();
184 } while (irq_desc[irq].status & IRQ_INPROGRESS);
185 }
186 }
187
188 /**
189 * enable_irq - enable interrupt handling on an irq
190 * @irq: Interrupt to enable
191 *
192 * Re-enables the processing of interrupts on this IRQ line
193 * providing no disable_irq calls are now in effect.
194 *
195 * This function may be called from IRQ context.
196 */
197
198 void enable_irq(unsigned int irq)
199 {
200 irq_desc_t *desc = irq_desc + irq;
201 unsigned long flags;
202
203 spin_lock_irqsave(&desc->lock, flags);
204 switch (desc->depth) {
205 case 1: {
206 unsigned int status = desc->status & ~IRQ_DISABLED;
207 desc->status = status;
208 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
209 desc->status = status | IRQ_REPLAY;
210 hw_resend_irq(desc->handler,irq);
211 }
212 desc->handler->enable(irq);
213 /* fall-through */
214 }
215 default:
216 desc->depth--;
217 break;
218 case 0:
219 printk("enable_irq(%u) unbalanced from %p\n", irq,
220 __builtin_return_address(0));
221 }
222 spin_unlock_irqrestore(&desc->lock, flags);
223 }
224
225 /*
226 * do_IRQ handles all normal device IRQ's (the special
227 * SMP cross-CPU interrupts have their own specific
228 * handlers).
229 */
230 asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
231 {
232 /*
233 * We ack quickly, we don't want the irq controller
234 * thinking we're snobs just because some other CPU has
235 * disabled global interrupts (we have already done the
236 * INT_ACK cycles, it's too late to try to pretend to the
237 * controller that we aren't taking the interrupt).
238 *
239 * 0 return value means that this irq is already being
240 * handled by some other CPU. (or is disabled)
241 */
242 int cpu = smp_processor_id();
243 irq_desc_t *desc = irq_desc + irq;
244 struct irqaction * action;
245 unsigned int status;
246
247 kstat.irqs[cpu][irq]++;
248 spin_lock(&desc->lock);
249 desc->handler->ack(irq);
250 /*
251 REPLAY is when Linux resends an IRQ that was dropped earlier
252 WAITING is used by probe to mark irqs that are being tested
253 */
254 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
255 status |= IRQ_PENDING; /* we _want_ to handle it */
256
257 /*
258 * If the IRQ is disabled for whatever reason, we cannot
259 * use the action we have.
260 */
261 action = NULL;
262 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
263 action = desc->action;
264 status &= ~IRQ_PENDING; /* we commit to handling */
265 status |= IRQ_INPROGRESS; /* we are handling it */
266 }
267 desc->status = status;
268
269 /*
270 * If there is no IRQ handler or it was disabled, exit early.
271 Since we set PENDING, if another processor is handling
272 a different instance of this same irq, the other processor
273 will take care of it.
274 */
275 if (!action)
276 goto out;
277
278 /*
279 * Edge triggered interrupts need to remember
280 * pending events.
281 * This applies to any hw interrupts that allow a second
282 * instance of the same irq to arrive while we are in do_IRQ
283 * or in the handler. But the code here only handles the _second_
284 * instance of the irq, not the third or fourth. So it is mostly
285 * useful for irq hardware that does not mask cleanly in an
286 * SMP environment.
287 */
288 for (;;) {
289 spin_unlock(&desc->lock);
290 handle_IRQ_event(irq, regs, action);
291 spin_lock(&desc->lock);
292
293 if (!(desc->status & IRQ_PENDING))
294 break;
295 desc->status &= ~IRQ_PENDING;
296 }
297 desc->status &= ~IRQ_INPROGRESS;
298 out:
299 /*
300 * The ->end() handler has to deal with interrupts which got
301 * disabled while the handler was running.
302 */
303 desc->handler->end(irq);
304 spin_unlock(&desc->lock);
305
306 if (softirq_pending(cpu))
307 do_softirq();
308 return 1;
309 }
310
311 /**
312 * request_irq - allocate an interrupt line
313 * @irq: Interrupt line to allocate
314 * @handler: Function to be called when the IRQ occurs
315 * @irqflags: Interrupt type flags
316 * @devname: An ascii name for the claiming device
317 * @dev_id: A cookie passed back to the handler function
318 *
319 * This call allocates interrupt resources and enables the
320 * interrupt line and IRQ handling. From the point this
321 * call is made your handler function may be invoked. Since
322 * your handler function must clear any interrupt the board
323 * raises, you must take care both to initialise your hardware
324 * and to set up the interrupt handler in the right order.
325 *
326 * Dev_id must be globally unique. Normally the address of the
327 * device data structure is used as the cookie. Since the handler
328 * receives this value it makes sense to use it.
329 *
330 * If your interrupt is shared you must pass a non NULL dev_id
331 * as this is required when freeing the interrupt.
332 *
333 * Flags:
334 *
335 * SA_SHIRQ Interrupt is shared
336 *
337 * SA_INTERRUPT Disable local interrupts while processing
338 *
339 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
340 *
341 */
342
343 int request_irq(unsigned int irq,
344 void (*handler)(int, void *, struct pt_regs *),
345 unsigned long irqflags,
346 const char * devname,
347 void *dev_id)
348 {
349 int retval;
350 struct irqaction * action;
351
352 #if 1
353 /*
354 * Sanity-check: shared interrupts should REALLY pass in
355 * a real dev-ID, otherwise we'll have trouble later trying
356 * to figure out which interrupt is which (messes up the
357 * interrupt freeing logic etc).
358 */
359 if (irqflags & SA_SHIRQ) {
360 if (!dev_id)
361 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
362 }
363 #endif
364
365 if (irq >= NR_IRQS)
366 return -EINVAL;
367 if (!handler)
368 return -EINVAL;
369
370 action = (struct irqaction *)
371 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
372 if (!action)
373 return -ENOMEM;
374
375 action->handler = handler;
376 action->flags = irqflags;
377 action->mask = 0;
378 action->name = devname;
379 action->next = NULL;
380 action->dev_id = dev_id;
381
382 retval = setup_irq(irq, action);
383 if (retval)
384 kfree(action);
385 return retval;
386 }
387
388 /**
389 * free_irq - free an interrupt
390 * @irq: Interrupt line to free
391 * @dev_id: Device identity to free
392 *
393 * Remove an interrupt handler. The handler is removed and if the
394 * interrupt line is no longer in use by any driver it is disabled.
395 * On a shared IRQ the caller must ensure the interrupt is disabled
396 * on the card it drives before calling this function. The function
397 * does not return until any executing interrupts for this IRQ
398 * have completed.
399 *
400 * This function may be called from interrupt context.
401 *
402 * Bugs: Attempting to free an irq in a handler for the same irq hangs
403 * the machine.
404 */
405
406 void free_irq(unsigned int irq, void *dev_id)
407 {
408 irq_desc_t *desc;
409 struct irqaction **p;
410 unsigned long flags;
411
412 if (irq >= NR_IRQS)
413 return;
414
415 desc = irq_desc + irq;
416 spin_lock_irqsave(&desc->lock,flags);
417 p = &desc->action;
418 for (;;) {
419 struct irqaction * action = *p;
420 if (action) {
421 struct irqaction **pp = p;
422 p = &action->next;
423 if (action->dev_id != dev_id)
424 continue;
425
426 /* Found it - now remove it from the list of entries */
427 *pp = action->next;
428 if (!desc->action) {
429 desc->status |= IRQ_DISABLED;
430 desc->handler->shutdown(irq);
431 }
432 spin_unlock_irqrestore(&desc->lock,flags);
433
434 #ifdef CONFIG_SMP
435 /* Wait to make sure it's not being used on another CPU */
436 while (desc->status & IRQ_INPROGRESS)
437 barrier();
438 #endif
439 kfree(action);
440 return;
441 }
442 printk("Trying to free free IRQ%d\n",irq);
443 spin_unlock_irqrestore(&desc->lock,flags);
444 return;
445 }
446 }
447
448 /*
449 * IRQ autodetection code..
450 *
451 * This depends on the fact that any interrupt that
452 * comes in on to an unassigned handler will get stuck
453 * with "IRQ_WAITING" cleared and the interrupt
454 * disabled.
455 */
456
457 static DECLARE_MUTEX(probe_sem);
458
459 /**
460 * probe_irq_on - begin an interrupt autodetect
461 *
462 * Commence probing for an interrupt. The interrupts are scanned
463 * and a mask of potential interrupt lines is returned.
464 *
465 */
466
467 unsigned long probe_irq_on(void)
468 {
469 unsigned int i;
470 irq_desc_t *desc;
471 unsigned long val;
472 unsigned long delay;
473
474 down(&probe_sem);
475 /*
476 * something may have generated an irq long ago and we want to
477 * flush such a longstanding irq before considering it as spurious.
478 */
479 for (i = NR_IRQS-1; i > 0; i--) {
480 desc = irq_desc + i;
481
482 spin_lock_irq(&desc->lock);
483 if (!irq_desc[i].action)
484 irq_desc[i].handler->startup(i);
485 spin_unlock_irq(&desc->lock);
486 }
487
488 /* Wait for longstanding interrupts to trigger. */
489 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
490 /* about 20ms delay */ synchronize_irq();
491
492 /*
493 * enable any unassigned irqs
494 * (we must startup again here because if a longstanding irq
495 * happened in the previous stage, it may have masked itself)
496 */
497 for (i = NR_IRQS-1; i > 0; i--) {
498 desc = irq_desc + i;
499
500 spin_lock_irq(&desc->lock);
501 if (!desc->action) {
502 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
503 if (desc->handler->startup(i))
504 desc->status |= IRQ_PENDING;
505 }
506 spin_unlock_irq(&desc->lock);
507 }
508
509 /*
510 * Wait for spurious interrupts to trigger
511 */
512 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
513 /* about 100ms delay */ synchronize_irq();
514
515 /*
516 * Now filter out any obviously spurious interrupts
517 */
518 val = 0;
519 for (i = 0; i < NR_IRQS; i++) {
520 irq_desc_t *desc = irq_desc + i;
521 unsigned int status;
522
523 spin_lock_irq(&desc->lock);
524 status = desc->status;
525
526 if (status & IRQ_AUTODETECT) {
527 /* It triggered already - consider it spurious. */
528 if (!(status & IRQ_WAITING)) {
529 desc->status = status & ~IRQ_AUTODETECT;
530 desc->handler->shutdown(i);
531 } else
532 if (i < 32)
533 val |= 1 << i;
534 }
535 spin_unlock_irq(&desc->lock);
536 }
537
538 return val;
539 }
540
541 /*
542 * Return a mask of triggered interrupts (this
543 * can handle only legacy ISA interrupts).
544 */
545
546 /**
547 * probe_irq_mask - scan a bitmap of interrupt lines
548 * @val: mask of interrupts to consider
549 *
550 * Scan the ISA bus interrupt lines and return a bitmap of
551 * active interrupts. The interrupt probe logic state is then
552 * returned to its previous value.
553 *
554 * Note: we need to scan all the irq's even though we will
555 * only return ISA irq numbers - just so that we reset them
556 * all to a known state.
557 */
558 unsigned int probe_irq_mask(unsigned long val)
559 {
560 int i;
561 unsigned int mask;
562
563 mask = 0;
564 for (i = 0; i < NR_IRQS; i++) {
565 irq_desc_t *desc = irq_desc + i;
566 unsigned int status;
567
568 spin_lock_irq(&desc->lock);
569 status = desc->status;
570
571 if (status & IRQ_AUTODETECT) {
572 if (i < 16 && !(status & IRQ_WAITING))
573 mask |= 1 << i;
574
575 desc->status = status & ~IRQ_AUTODETECT;
576 desc->handler->shutdown(i);
577 }
578 spin_unlock_irq(&desc->lock);
579 }
580 up(&probe_sem);
581
582 return mask & val;
583 }
584
585 /*
586 * Return the one interrupt that triggered (this can
587 * handle any interrupt source).
588 */
589
590 /**
591 * probe_irq_off - end an interrupt autodetect
592 * @val: mask of potential interrupts (unused)
593 *
594 * Scans the unused interrupt lines and returns the line which
595 * appears to have triggered the interrupt. If no interrupt was
596 * found then zero is returned. If more than one interrupt is
597 * found then minus the first candidate is returned to indicate
598 * their is doubt.
599 *
600 * The interrupt probe logic state is returned to its previous
601 * value.
602 *
603 * BUGS: When used in a module (which arguably shouldnt happen)
604 * nothing prevents two IRQ probe callers from overlapping. The
605 * results of this are non-optimal.
606 */
607
608 int probe_irq_off(unsigned long val)
609 {
610 int i, irq_found, nr_irqs;
611
612 nr_irqs = 0;
613 irq_found = 0;
614 for (i = 0; i < NR_IRQS; i++) {
615 irq_desc_t *desc = irq_desc + i;
616 unsigned int status;
617
618 spin_lock_irq(&desc->lock);
619 status = desc->status;
620
621 if (status & IRQ_AUTODETECT) {
622 if (!(status & IRQ_WAITING)) {
623 if (!nr_irqs)
624 irq_found = i;
625 nr_irqs++;
626 }
627 desc->status = status & ~IRQ_AUTODETECT;
628 desc->handler->shutdown(i);
629 }
630 spin_unlock_irq(&desc->lock);
631 }
632 up(&probe_sem);
633
634 if (nr_irqs > 1)
635 irq_found = -irq_found;
636 return irq_found;
637 }
638
639 /* this was setup_x86_irq but it seems pretty generic */
640 int setup_irq(unsigned int irq, struct irqaction * new)
641 {
642 int shared = 0;
643 unsigned long flags;
644 struct irqaction *old, **p;
645 irq_desc_t *desc = irq_desc + irq;
646
647 /*
648 * Some drivers like serial.c use request_irq() heavily,
649 * so we have to be careful not to interfere with a
650 * running system.
651 */
652 if (new->flags & SA_SAMPLE_RANDOM) {
653 /*
654 * This function might sleep, we want to call it first,
655 * outside of the atomic block.
656 * Yes, this might clear the entropy pool if the wrong
657 * driver is attempted to be loaded, without actually
658 * installing a new handler, but is this really a problem,
659 * only the sysadmin is able to do this.
660 */
661 rand_initialize_irq(irq);
662 }
663
664 /*
665 * The following block of code has to be executed atomically
666 */
667 spin_lock_irqsave(&desc->lock,flags);
668 p = &desc->action;
669 if ((old = *p) != NULL) {
670 /* Can't share interrupts unless both agree to */
671 if (!(old->flags & new->flags & SA_SHIRQ)) {
672 spin_unlock_irqrestore(&desc->lock,flags);
673 return -EBUSY;
674 }
675
676 /* add new interrupt at end of irq queue */
677 do {
678 p = &old->next;
679 old = *p;
680 } while (old);
681 shared = 1;
682 }
683
684 *p = new;
685
686 if (!shared) {
687 desc->depth = 0;
688 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
689 desc->handler->startup(irq);
690 }
691 spin_unlock_irqrestore(&desc->lock,flags);
692
693 /* register_irq_proc(irq); */
694 return 0;
695 }
696
697 void __init init_generic_irq(void)
698 {
699 int i;
700
701 for (i = 0; i < NR_IRQS; i++) {
702 irq_desc[i].status = IRQ_DISABLED;
703 irq_desc[i].action = NULL;
704 irq_desc[i].depth = 1;
705 irq_desc[i].handler = &no_irq_type;
706 }
707 }
708