2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
5 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
8 #include "linux/config.h"
9 #include "linux/kernel.h"
10 #include "linux/module.h"
11 #include "linux/smp.h"
12 #include "linux/irq.h"
13 #include "linux/kernel_stat.h"
14 #include "linux/interrupt.h"
15 #include "linux/random.h"
16 #include "linux/slab.h"
17 #include "linux/file.h"
18 #include "linux/proc_fs.h"
19 #include "linux/init.h"
20 #include "linux/seq_file.h"
22 #include "asm/hw_irq.h"
23 #include "asm/hardirq.h"
24 #include "asm/atomic.h"
25 #include "asm/signal.h"
26 #include "asm/system.h"
27 #include "asm/errno.h"
28 #include "asm/uaccess.h"
29 #include "user_util.h"
30 #include "kern_util.h"
33 static void register_irq_proc (unsigned int irq);
35 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
37 .handler = &no_irq_type,
38 .lock = SPIN_LOCK_UNLOCKED
43 * Generic no controller code
46 static void enable_none(unsigned int irq) { }
47 static unsigned int startup_none(unsigned int irq) { return 0; }
48 static void disable_none(unsigned int irq) { }
49 static void ack_none(unsigned int irq)
52 * 'what should we do if we get a hw irq event on an illegal vector'.
53 * each architecture has to answer this themselves, it doesn't deserve
54 * a generic callback i think.
57 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
58 #ifdef CONFIG_X86_LOCAL_APIC
60 * Currently unexpected vectors happen only on SMP and APIC.
61 * We _must_ ack these because every local APIC has only N
62 * irq slots per priority level, and a 'hanging, unacked' IRQ
63 * holds up an irq slot - in excessive cases (when multiple
64 * unexpected vectors occur) that might lock up the APIC
72 /* startup is the same as "enable", shutdown is same as "disable" */
73 #define shutdown_none disable_none
74 #define end_none enable_none
76 struct hw_interrupt_type no_irq_type = {
87 volatile unsigned long irq_err_count;
90 * Generic, controller-independent functions:
93 int get_irq_list(char *buf)
97 struct irqaction * action;
100 p += sprintf(p, " ");
101 for (j=0; j<num_online_cpus(); j++)
102 p += sprintf(p, "CPU%d ",j);
105 for (i = 0 ; i < NR_IRQS ; i++) {
106 spin_lock_irqsave(&irq_desc[i].lock, flags);
107 action = irq_desc[i].action;
110 p += sprintf(p, "%3d: ",i);
112 p += sprintf(p, "%10u ", kstat_irqs(i));
114 for (j = 0; j < num_online_cpus(); j++)
115 p += sprintf(p, "%10u ",
116 kstat_cpu(cpu_logical_map(j)).irqs[i]);
118 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
119 p += sprintf(p, " %s", action->name);
121 for (action=action->next; action; action = action->next)
122 p += sprintf(p, ", %s", action->name);
125 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
127 p += sprintf(p, "\n");
130 p += sprintf(p, "LOC: ");
131 for (j = 0; j < num_online_cpus(); j++)
132 p += sprintf(p, "%10u ",
133 apic_timer_irqs[cpu_logical_map(j)]);
134 p += sprintf(p, "\n");
137 p += sprintf(p, "ERR: %10lu\n", irq_err_count);
142 int show_interrupts(struct seq_file *p, void *v)
148 * This should really return information about whether
149 * we should do bottom half handling etc. Right now we
150 * end up _always_ checking the bottom half, which is a
151 * waste of time and is not what some drivers would
154 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs,
155 struct irqaction * action)
157 int status = 1; /* Force the "do bottom halves" bit */
159 if (!(action->flags & SA_INTERRUPT))
163 status |= action->flags;
164 action->handler(irq, action->dev_id, regs);
165 action = action->next;
167 if (status & SA_SAMPLE_RANDOM)
168 add_interrupt_randomness(irq);
176 * Generic enable/disable code: this just calls
177 * down into the PIC-specific version for the actual
178 * hardware disable after having gotten the irq
183 * disable_irq_nosync - disable an irq without waiting
184 * @irq: Interrupt to disable
186 * Disable the selected interrupt line. Disables of an interrupt
187 * stack. Unlike disable_irq(), this function does not ensure existing
188 * instances of the IRQ handler have completed before returning.
190 * This function may be called from IRQ context.
193 inline void disable_irq_nosync(unsigned int irq)
195 irq_desc_t *desc = irq_desc + irq;
198 spin_lock_irqsave(&desc->lock, flags);
199 if (!desc->depth++) {
200 desc->status |= IRQ_DISABLED;
201 desc->handler->disable(irq);
203 spin_unlock_irqrestore(&desc->lock, flags);
207 inline void synchronize_irq(unsigned int irq)
209 /* is there anything to synchronize with? */
210 if (!irq_desc[irq].action)
213 while (irq_desc[irq].status & IRQ_INPROGRESS)
219 * disable_irq - disable an irq and wait for completion
220 * @irq: Interrupt to disable
222 * Disable the selected interrupt line. Disables of an interrupt
223 * stack. That is for two disables you need two enables. This
224 * function waits for any pending IRQ handlers for this interrupt
225 * to complete before returning. If you use this function while
226 * holding a resource the IRQ handler may need you will deadlock.
228 * This function may be called - with care - from IRQ context.
231 void disable_irq(unsigned int irq)
233 disable_irq_nosync(irq);
234 synchronize_irq(irq);
238 * enable_irq - enable interrupt handling on an irq
239 * @irq: Interrupt to enable
241 * Re-enables the processing of interrupts on this IRQ line
242 * providing no disable_irq calls are now in effect.
244 * This function may be called from IRQ context.
247 void enable_irq(unsigned int irq)
249 irq_desc_t *desc = irq_desc + irq;
252 spin_lock_irqsave(&desc->lock, flags);
253 switch (desc->depth) {
255 unsigned int status = desc->status & ~IRQ_DISABLED;
256 desc->status = status;
257 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
258 desc->status = status | IRQ_REPLAY;
259 hw_resend_irq(desc->handler,irq);
261 desc->handler->enable(irq);
268 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
269 __builtin_return_address(0));
271 spin_unlock_irqrestore(&desc->lock, flags);
275 * do_IRQ handles all normal device IRQ's (the special
276 * SMP cross-CPU interrupts have their own specific
279 unsigned int do_IRQ(int irq, union uml_pt_regs *regs)
282 * 0 return value means that this irq is already being
283 * handled by some other CPU. (or is disabled)
285 int cpu = smp_processor_id();
286 irq_desc_t *desc = irq_desc + irq;
287 struct irqaction * action;
291 kstat_cpu(cpu).irqs[irq]++;
292 spin_lock(&desc->lock);
293 desc->handler->ack(irq);
295 REPLAY is when Linux resends an IRQ that was dropped earlier
296 WAITING is used by probe to mark irqs that are being tested
298 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
299 status |= IRQ_PENDING; /* we _want_ to handle it */
302 * If the IRQ is disabled for whatever reason, we cannot
303 * use the action we have.
306 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
307 action = desc->action;
308 status &= ~IRQ_PENDING; /* we commit to handling */
309 status |= IRQ_INPROGRESS; /* we are handling it */
311 desc->status = status;
314 * If there is no IRQ handler or it was disabled, exit early.
315 Since we set PENDING, if another processor is handling
316 a different instance of this same irq, the other processor
317 will take care of it.
323 * Edge triggered interrupts need to remember
325 * This applies to any hw interrupts that allow a second
326 * instance of the same irq to arrive while we are in do_IRQ
327 * or in the handler. But the code here only handles the _second_
328 * instance of the irq, not the third or fourth. So it is mostly
329 * useful for irq hardware that does not mask cleanly in an
333 spin_unlock(&desc->lock);
334 handle_IRQ_event(irq, (struct pt_regs *) regs, action);
335 spin_lock(&desc->lock);
337 if (!(desc->status & IRQ_PENDING))
339 desc->status &= ~IRQ_PENDING;
341 desc->status &= ~IRQ_INPROGRESS;
344 * The ->end() handler has to deal with interrupts which got
345 * disabled while the handler was running.
347 desc->handler->end(irq);
348 spin_unlock(&desc->lock);
356 * request_irq - allocate an interrupt line
357 * @irq: Interrupt line to allocate
358 * @handler: Function to be called when the IRQ occurs
359 * @irqflags: Interrupt type flags
360 * @devname: An ascii name for the claiming device
361 * @dev_id: A cookie passed back to the handler function
363 * This call allocates interrupt resources and enables the
364 * interrupt line and IRQ handling. From the point this
365 * call is made your handler function may be invoked. Since
366 * your handler function must clear any interrupt the board
367 * raises, you must take care both to initialise your hardware
368 * and to set up the interrupt handler in the right order.
370 * Dev_id must be globally unique. Normally the address of the
371 * device data structure is used as the cookie. Since the handler
372 * receives this value it makes sense to use it.
374 * If your interrupt is shared you must pass a non NULL dev_id
375 * as this is required when freeing the interrupt.
379 * SA_SHIRQ Interrupt is shared
381 * SA_INTERRUPT Disable local interrupts while processing
383 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
387 int request_irq(unsigned int irq,
388 void (*handler)(int, void *, struct pt_regs *),
389 unsigned long irqflags,
390 const char * devname,
394 struct irqaction * action;
398 * Sanity-check: shared interrupts should REALLY pass in
399 * a real dev-ID, otherwise we'll have trouble later trying
400 * to figure out which interrupt is which (messes up the
401 * interrupt freeing logic etc).
403 if (irqflags & SA_SHIRQ) {
405 printk(KERN_ERR "Bad boy: %s (at 0x%x) called us "
406 "without a dev_id!\n", devname, (&irq)[-1]);
415 action = (struct irqaction *)
416 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
420 action->handler = handler;
421 action->flags = irqflags;
423 action->name = devname;
425 action->dev_id = dev_id;
427 retval = setup_irq(irq, action);
433 EXPORT_SYMBOL(request_irq);
435 int um_request_irq(unsigned int irq, int fd, int type,
436 void (*handler)(int, void *, struct pt_regs *),
437 unsigned long irqflags, const char * devname,
442 retval = request_irq(irq, handler, irqflags, devname, dev_id);
443 if(retval) return(retval);
444 return(activate_fd(irq, fd, type, dev_id));
447 /* this was setup_x86_irq but it seems pretty generic */
448 int setup_irq(unsigned int irq, struct irqaction * new)
452 struct irqaction *old, **p;
453 irq_desc_t *desc = irq_desc + irq;
456 * Some drivers like serial.c use request_irq() heavily,
457 * so we have to be careful not to interfere with a
460 if (new->flags & SA_SAMPLE_RANDOM) {
462 * This function might sleep, we want to call it first,
463 * outside of the atomic block.
464 * Yes, this might clear the entropy pool if the wrong
465 * driver is attempted to be loaded, without actually
466 * installing a new handler, but is this really a problem,
467 * only the sysadmin is able to do this.
469 rand_initialize_irq(irq);
473 * The following block of code has to be executed atomically
475 spin_lock_irqsave(&desc->lock,flags);
477 if ((old = *p) != NULL) {
478 /* Can't share interrupts unless both agree to */
479 if (!(old->flags & new->flags & SA_SHIRQ)) {
480 spin_unlock_irqrestore(&desc->lock,flags);
484 /* add new interrupt at end of irq queue */
496 desc->status &= ~IRQ_DISABLED;
497 desc->handler->startup(irq);
499 spin_unlock_irqrestore(&desc->lock,flags);
501 register_irq_proc(irq);
506 * free_irq - free an interrupt
507 * @irq: Interrupt line to free
508 * @dev_id: Device identity to free
510 * Remove an interrupt handler. The handler is removed and if the
511 * interrupt line is no longer in use by any driver it is disabled.
512 * On a shared IRQ the caller must ensure the interrupt is disabled
513 * on the card it drives before calling this function. The function
514 * does not return until any executing interrupts for this IRQ
517 * This function may be called from interrupt context.
519 * Bugs: Attempting to free an irq in a handler for the same irq hangs
523 void free_irq(unsigned int irq, void *dev_id)
526 struct irqaction **p;
532 desc = irq_desc + irq;
533 spin_lock_irqsave(&desc->lock,flags);
536 struct irqaction * action = *p;
538 struct irqaction **pp = p;
540 if (action->dev_id != dev_id)
543 /* Found it - now remove it from the list of entries */
546 desc->status |= IRQ_DISABLED;
547 desc->handler->shutdown(irq);
549 free_irq_by_irq_and_dev(irq, dev_id);
550 spin_unlock_irqrestore(&desc->lock,flags);
552 /* Wait to make sure it's not being used on another CPU */
553 synchronize_irq(irq);
557 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
558 spin_unlock_irqrestore(&desc->lock,flags);
563 EXPORT_SYMBOL(free_irq);
565 /* These are initialized by sysctl_init, which is called from init/main.c */
566 static struct proc_dir_entry * root_irq_dir;
567 static struct proc_dir_entry * irq_dir [NR_IRQS];
568 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
570 /* These are read and written as longs, so a read won't see a partial write
571 * even during a race.
573 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
575 static int irq_affinity_read_proc (char *page, char **start, off_t off,
576 int count, int *eof, void *data)
578 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
581 len += sprintf(page + len, "\n");
585 static int irq_affinity_write_proc (struct file *file, const char *buffer,
586 unsigned long count, void *data)
588 int irq = (long) data, full_count = count, err;
589 cpumask_t new_value, tmp;
591 if (!irq_desc[irq].handler->set_affinity)
594 err = cpumask_parse(buffer, count, new_value);
598 * Do not allow disabling IRQs completely - it's a too easy
599 * way to make the system unusable accidentally :-) At least
600 * one online CPU still has to be targeted.
602 cpus_and(tmp, new_value, cpu_online_map);
607 irq_affinity[irq] = new_value;
608 irq_desc[irq].handler->set_affinity(irq, new_value);
613 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
614 int count, int *eof, void *data)
616 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
619 len += sprintf(page + len, "\n");
623 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
624 unsigned long count, void *data)
626 cpumask_t *mask = (cpumask_t *)data, new_value;
627 unsigned long full_count = count, err;
629 err = cpumask_parse(buffer, count, new_value);
637 #define MAX_NAMELEN 10
639 static void register_irq_proc (unsigned int irq)
641 struct proc_dir_entry *entry;
642 char name [MAX_NAMELEN];
644 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
648 memset(name, 0, MAX_NAMELEN);
649 sprintf(name, "%d", irq);
651 /* create /proc/irq/1234 */
652 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
654 /* create /proc/irq/1234/smp_affinity */
655 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
658 entry->data = (void *)(long)irq;
659 entry->read_proc = irq_affinity_read_proc;
660 entry->write_proc = irq_affinity_write_proc;
662 smp_affinity_entry[irq] = entry;
665 /* Read and written as a long */
666 cpumask_t prof_cpu_mask = CPU_MASK_ALL;
668 void __init init_irq_proc (void)
670 struct proc_dir_entry *entry;
673 /* create /proc/irq */
674 root_irq_dir = proc_mkdir("irq", 0);
676 /* create /proc/irq/prof_cpu_mask */
677 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
680 entry->data = (void *)&prof_cpu_mask;
681 entry->read_proc = prof_cpu_mask_read_proc;
682 entry->write_proc = prof_cpu_mask_write_proc;
685 * Create entries for all existing IRQs.
687 for (i = 0; i < NR_IRQS; i++)
688 register_irq_proc(i);
691 static spinlock_t irq_spinlock = SPIN_LOCK_UNLOCKED;
693 unsigned long irq_lock(void)
697 spin_lock_irqsave(&irq_spinlock, flags);
701 void irq_unlock(unsigned long flags)
703 spin_unlock_irqrestore(&irq_spinlock, flags);
706 unsigned long probe_irq_on(void)
711 EXPORT_SYMBOL(probe_irq_on);
713 int probe_irq_off(unsigned long val)
718 EXPORT_SYMBOL(probe_irq_off);
720 static unsigned int startup_SIGIO_irq(unsigned int irq)
725 static void shutdown_SIGIO_irq(unsigned int irq)
729 static void enable_SIGIO_irq(unsigned int irq)
733 static void disable_SIGIO_irq(unsigned int irq)
737 static void mask_and_ack_SIGIO(unsigned int irq)
741 static void end_SIGIO_irq(unsigned int irq)
745 static unsigned int startup_SIGVTALRM_irq(unsigned int irq)
750 static void shutdown_SIGVTALRM_irq(unsigned int irq)
754 static void enable_SIGVTALRM_irq(unsigned int irq)
758 static void disable_SIGVTALRM_irq(unsigned int irq)
762 static void mask_and_ack_SIGVTALRM(unsigned int irq)
766 static void end_SIGVTALRM_irq(unsigned int irq)
770 static struct hw_interrupt_type SIGIO_irq_type = {
781 static struct hw_interrupt_type SIGVTALRM_irq_type = {
783 startup_SIGVTALRM_irq,
784 shutdown_SIGVTALRM_irq,
785 enable_SIGVTALRM_irq,
786 disable_SIGVTALRM_irq,
787 mask_and_ack_SIGVTALRM,
792 void __init init_IRQ(void)
796 irq_desc[TIMER_IRQ].status = IRQ_DISABLED;
797 irq_desc[TIMER_IRQ].action = 0;
798 irq_desc[TIMER_IRQ].depth = 1;
799 irq_desc[TIMER_IRQ].handler = &SIGVTALRM_irq_type;
800 enable_irq(TIMER_IRQ);
801 for(i=1;i<NR_IRQS;i++){
802 irq_desc[i].status = IRQ_DISABLED;
803 irq_desc[i].action = 0;
804 irq_desc[i].depth = 1;
805 irq_desc[i].handler = &SIGIO_irq_type;
812 * Overrides for Emacs so that we follow Linus's tabbing style.
813 * Emacs will notice this stuff at the end of the file and automatically
814 * adjust the settings for this buffer only. This must remain at the end
816 * ---------------------------------------------------------------------------
818 * c-file-style: "linux"