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"
34 static void register_irq_proc (unsigned int irq);
36 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
38 .handler = &no_irq_type,
39 .lock = SPIN_LOCK_UNLOCKED
44 * Generic no controller code
47 static void enable_none(unsigned int irq) { }
48 static unsigned int startup_none(unsigned int irq) { return 0; }
49 static void disable_none(unsigned int irq) { }
50 static void ack_none(unsigned int irq)
53 * 'what should we do if we get a hw irq event on an illegal vector'.
54 * each architecture has to answer this themselves, it doesn't deserve
55 * a generic callback i think.
58 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
59 #ifdef CONFIG_X86_LOCAL_APIC
61 * Currently unexpected vectors happen only on SMP and APIC.
62 * We _must_ ack these because every local APIC has only N
63 * irq slots per priority level, and a 'hanging, unacked' IRQ
64 * holds up an irq slot - in excessive cases (when multiple
65 * unexpected vectors occur) that might lock up the APIC
73 /* startup is the same as "enable", shutdown is same as "disable" */
74 #define shutdown_none disable_none
75 #define end_none enable_none
77 struct hw_interrupt_type no_irq_type = {
88 * Generic, controller-independent functions:
91 int show_interrupts(struct seq_file *p, void *v)
93 int i = *(loff_t *) v, j;
94 struct irqaction * action;
99 for (j=0; j<NR_CPUS; j++)
101 seq_printf(p, "CPU%d ",j);
106 spin_lock_irqsave(&irq_desc[i].lock, flags);
107 action = irq_desc[i].action;
110 seq_printf(p, "%3d: ",i);
112 seq_printf(p, "%10u ", kstat_irqs(i));
114 for (j = 0; j < NR_CPUS; j++)
116 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
118 seq_printf(p, " %14s", irq_desc[i].handler->typename);
119 seq_printf(p, " %s", action->name);
121 for (action=action->next; action; action = action->next)
122 seq_printf(p, ", %s", action->name);
126 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
127 } else if (i == NR_IRQS) {
128 seq_printf(p, "NMI: ");
129 for (j = 0; j < NR_CPUS; j++)
131 seq_printf(p, "%10u ", nmi_count(j));
139 * This should really return information about whether
140 * we should do bottom half handling etc. Right now we
141 * end up _always_ checking the bottom half, which is a
142 * waste of time and is not what some drivers would
145 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs,
146 struct irqaction * action)
148 int status = 1; /* Force the "do bottom halves" bit */
150 if (!(action->flags & SA_INTERRUPT))
154 status |= action->flags;
155 action->handler(irq, action->dev_id, regs);
156 action = action->next;
158 if (status & SA_SAMPLE_RANDOM)
159 add_interrupt_randomness(irq);
167 * Generic enable/disable code: this just calls
168 * down into the PIC-specific version for the actual
169 * hardware disable after having gotten the irq
174 * disable_irq_nosync - disable an irq without waiting
175 * @irq: Interrupt to disable
177 * Disable the selected interrupt line. Disables of an interrupt
178 * stack. Unlike disable_irq(), this function does not ensure existing
179 * instances of the IRQ handler have completed before returning.
181 * This function may be called from IRQ context.
184 inline void disable_irq_nosync(unsigned int irq)
186 irq_desc_t *desc = irq_desc + irq;
189 spin_lock_irqsave(&desc->lock, flags);
190 if (!desc->depth++) {
191 desc->status |= IRQ_DISABLED;
192 desc->handler->disable(irq);
194 spin_unlock_irqrestore(&desc->lock, flags);
198 inline void synchronize_irq(unsigned int irq)
200 /* is there anything to synchronize with? */
201 if (!irq_desc[irq].action)
204 while (irq_desc[irq].status & IRQ_INPROGRESS)
210 * disable_irq - disable an irq and wait for completion
211 * @irq: Interrupt to disable
213 * Disable the selected interrupt line. Disables of an interrupt
214 * stack. That is for two disables you need two enables. This
215 * function waits for any pending IRQ handlers for this interrupt
216 * to complete before returning. If you use this function while
217 * holding a resource the IRQ handler may need you will deadlock.
219 * This function may be called - with care - from IRQ context.
222 void disable_irq(unsigned int irq)
224 disable_irq_nosync(irq);
225 synchronize_irq(irq);
229 * enable_irq - enable interrupt handling on an irq
230 * @irq: Interrupt to enable
232 * Re-enables the processing of interrupts on this IRQ line
233 * providing no disable_irq calls are now in effect.
235 * This function may be called from IRQ context.
238 void enable_irq(unsigned int irq)
240 irq_desc_t *desc = irq_desc + irq;
243 spin_lock_irqsave(&desc->lock, flags);
244 switch (desc->depth) {
246 unsigned int status = desc->status & ~IRQ_DISABLED;
247 desc->status = status;
248 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
249 desc->status = status | IRQ_REPLAY;
250 hw_resend_irq(desc->handler,irq);
252 desc->handler->enable(irq);
259 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
260 __builtin_return_address(0));
262 spin_unlock_irqrestore(&desc->lock, flags);
266 * do_IRQ handles all normal device IRQ's (the special
267 * SMP cross-CPU interrupts have their own specific
270 unsigned int do_IRQ(int irq, union uml_pt_regs *regs)
273 * 0 return value means that this irq is already being
274 * handled by some other CPU. (or is disabled)
276 irq_desc_t *desc = irq_desc + irq;
277 struct irqaction * action;
281 kstat_this_cpu.irqs[irq]++;
282 spin_lock(&desc->lock);
283 desc->handler->ack(irq);
285 REPLAY is when Linux resends an IRQ that was dropped earlier
286 WAITING is used by probe to mark irqs that are being tested
288 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
289 status |= IRQ_PENDING; /* we _want_ to handle it */
292 * If the IRQ is disabled for whatever reason, we cannot
293 * use the action we have.
296 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
297 action = desc->action;
298 status &= ~IRQ_PENDING; /* we commit to handling */
299 status |= IRQ_INPROGRESS; /* we are handling it */
301 desc->status = status;
304 * If there is no IRQ handler or it was disabled, exit early.
305 Since we set PENDING, if another processor is handling
306 a different instance of this same irq, the other processor
307 will take care of it.
313 * Edge triggered interrupts need to remember
315 * This applies to any hw interrupts that allow a second
316 * instance of the same irq to arrive while we are in do_IRQ
317 * or in the handler. But the code here only handles the _second_
318 * instance of the irq, not the third or fourth. So it is mostly
319 * useful for irq hardware that does not mask cleanly in an
323 spin_unlock(&desc->lock);
324 handle_IRQ_event(irq, (struct pt_regs *) regs, action);
325 spin_lock(&desc->lock);
327 if (!(desc->status & IRQ_PENDING))
329 desc->status &= ~IRQ_PENDING;
331 desc->status &= ~IRQ_INPROGRESS;
334 * The ->end() handler has to deal with interrupts which got
335 * disabled while the handler was running.
337 desc->handler->end(irq);
338 spin_unlock(&desc->lock);
346 * request_irq - allocate an interrupt line
347 * @irq: Interrupt line to allocate
348 * @handler: Function to be called when the IRQ occurs
349 * @irqflags: Interrupt type flags
350 * @devname: An ascii name for the claiming device
351 * @dev_id: A cookie passed back to the handler function
353 * This call allocates interrupt resources and enables the
354 * interrupt line and IRQ handling. From the point this
355 * call is made your handler function may be invoked. Since
356 * your handler function must clear any interrupt the board
357 * raises, you must take care both to initialise your hardware
358 * and to set up the interrupt handler in the right order.
360 * Dev_id must be globally unique. Normally the address of the
361 * device data structure is used as the cookie. Since the handler
362 * receives this value it makes sense to use it.
364 * If your interrupt is shared you must pass a non NULL dev_id
365 * as this is required when freeing the interrupt.
369 * SA_SHIRQ Interrupt is shared
371 * SA_INTERRUPT Disable local interrupts while processing
373 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
377 int request_irq(unsigned int irq,
378 irqreturn_t (*handler)(int, void *, struct pt_regs *),
379 unsigned long irqflags,
380 const char * devname,
384 struct irqaction * action;
388 * Sanity-check: shared interrupts should REALLY pass in
389 * a real dev-ID, otherwise we'll have trouble later trying
390 * to figure out which interrupt is which (messes up the
391 * interrupt freeing logic etc).
393 if (irqflags & SA_SHIRQ) {
395 printk(KERN_ERR "Bad boy: %s (at 0x%x) called us "
396 "without a dev_id!\n", devname, (&irq)[-1]);
405 action = (struct irqaction *)
406 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
410 action->handler = handler;
411 action->flags = irqflags;
412 cpus_clear(action->mask);
413 action->name = devname;
415 action->dev_id = dev_id;
417 retval = setup_irq(irq, action);
423 EXPORT_SYMBOL(request_irq);
425 int um_request_irq(unsigned int irq, int fd, int type,
426 irqreturn_t (*handler)(int, void *, struct pt_regs *),
427 unsigned long irqflags, const char * devname,
432 err = request_irq(irq, handler, irqflags, devname, dev_id);
437 err = activate_fd(irq, fd, type, dev_id);
441 /* this was setup_x86_irq but it seems pretty generic */
442 int setup_irq(unsigned int irq, struct irqaction * new)
446 struct irqaction *old, **p;
447 irq_desc_t *desc = irq_desc + irq;
450 * Some drivers like serial.c use request_irq() heavily,
451 * so we have to be careful not to interfere with a
454 if (new->flags & SA_SAMPLE_RANDOM) {
456 * This function might sleep, we want to call it first,
457 * outside of the atomic block.
458 * Yes, this might clear the entropy pool if the wrong
459 * driver is attempted to be loaded, without actually
460 * installing a new handler, but is this really a problem,
461 * only the sysadmin is able to do this.
463 rand_initialize_irq(irq);
467 * The following block of code has to be executed atomically
469 spin_lock_irqsave(&desc->lock,flags);
473 /* Can't share interrupts unless both agree to */
474 if (!(old->flags & new->flags & SA_SHIRQ)) {
475 spin_unlock_irqrestore(&desc->lock,flags);
479 /* add new interrupt at end of irq queue */
491 desc->status &= ~IRQ_DISABLED;
492 desc->handler->startup(irq);
494 spin_unlock_irqrestore(&desc->lock,flags);
496 register_irq_proc(irq);
501 * free_irq - free an interrupt
502 * @irq: Interrupt line to free
503 * @dev_id: Device identity to free
505 * Remove an interrupt handler. The handler is removed and if the
506 * interrupt line is no longer in use by any driver it is disabled.
507 * On a shared IRQ the caller must ensure the interrupt is disabled
508 * on the card it drives before calling this function. The function
509 * does not return until any executing interrupts for this IRQ
512 * This function may be called from interrupt context.
514 * Bugs: Attempting to free an irq in a handler for the same irq hangs
518 void free_irq(unsigned int irq, void *dev_id)
521 struct irqaction **p;
527 desc = irq_desc + irq;
528 spin_lock_irqsave(&desc->lock,flags);
531 struct irqaction * action = *p;
533 struct irqaction **pp = p;
535 if (action->dev_id != dev_id)
538 /* Found it - now remove it from the list of entries */
541 desc->status |= IRQ_DISABLED;
542 desc->handler->shutdown(irq);
544 free_irq_by_irq_and_dev(irq, dev_id);
545 spin_unlock_irqrestore(&desc->lock,flags);
547 /* Wait to make sure it's not being used on another CPU */
548 synchronize_irq(irq);
552 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
553 spin_unlock_irqrestore(&desc->lock,flags);
558 EXPORT_SYMBOL(free_irq);
560 /* These are initialized by sysctl_init, which is called from init/main.c */
561 static struct proc_dir_entry * root_irq_dir;
562 static struct proc_dir_entry * irq_dir [NR_IRQS];
563 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
565 /* These are read and written as longs, so a read won't see a partial write
566 * even during a race.
568 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
570 static int irq_affinity_read_proc (char *page, char **start, off_t off,
571 int count, int *eof, void *data)
573 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
576 len += sprintf(page + len, "\n");
580 static int irq_affinity_write_proc (struct file *file, const char *buffer,
581 unsigned long count, void *data)
583 int irq = (long) data, full_count = count, err;
586 if (!irq_desc[irq].handler->set_affinity)
589 err = cpumask_parse(buffer, count, new_value);
595 * Do not allow disabling IRQs completely - it's a too easy
596 * way to make the system unusable accidentally :-) At least
597 * one online CPU still has to be targeted.
600 cpus_and(tmp, new_value, cpu_online_map);
606 irq_affinity[irq] = new_value;
607 irq_desc[irq].handler->set_affinity(irq, new_value);
612 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
613 int count, int *eof, void *data)
615 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
618 len += sprintf(page + len, "\n");
622 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
623 unsigned long count, void *data)
625 cpumask_t *mask = (cpumask_t *)data, new_value;
626 unsigned long full_count = count, err;
628 err = cpumask_parse(buffer, count, new_value);
636 #define MAX_NAMELEN 10
638 static void register_irq_proc (unsigned int irq)
640 struct proc_dir_entry *entry;
641 char name [MAX_NAMELEN];
643 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
647 memset(name, 0, MAX_NAMELEN);
648 sprintf(name, "%d", irq);
650 /* create /proc/irq/1234 */
651 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
653 /* create /proc/irq/1234/smp_affinity */
654 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
657 entry->data = (void *)(long)irq;
658 entry->read_proc = irq_affinity_read_proc;
659 entry->write_proc = irq_affinity_write_proc;
661 smp_affinity_entry[irq] = entry;
664 /* Read and written as a long */
665 cpumask_t prof_cpu_mask = CPU_MASK_ALL;
667 void __init init_irq_proc (void)
669 struct proc_dir_entry *entry;
672 /* create /proc/irq */
673 root_irq_dir = proc_mkdir("irq", 0);
675 /* create /proc/irq/prof_cpu_mask */
676 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
679 entry->data = (void *)&prof_cpu_mask;
680 entry->read_proc = prof_cpu_mask_read_proc;
681 entry->write_proc = prof_cpu_mask_write_proc;
684 * Create entries for all existing IRQs.
686 for (i = 0; i < NR_IRQS; i++)
687 register_irq_proc(i);
690 static spinlock_t irq_spinlock = SPIN_LOCK_UNLOCKED;
692 unsigned long irq_lock(void)
696 spin_lock_irqsave(&irq_spinlock, flags);
700 void irq_unlock(unsigned long flags)
702 spin_unlock_irqrestore(&irq_spinlock, flags);
705 unsigned long probe_irq_on(void)
710 EXPORT_SYMBOL(probe_irq_on);
712 int probe_irq_off(unsigned long val)
717 EXPORT_SYMBOL(probe_irq_off);
719 static unsigned int startup_SIGIO_irq(unsigned int irq)
724 static void shutdown_SIGIO_irq(unsigned int irq)
728 static void enable_SIGIO_irq(unsigned int irq)
732 static void disable_SIGIO_irq(unsigned int irq)
736 static void mask_and_ack_SIGIO(unsigned int irq)
740 static void end_SIGIO_irq(unsigned int irq)
744 static unsigned int startup_SIGVTALRM_irq(unsigned int irq)
749 static void shutdown_SIGVTALRM_irq(unsigned int irq)
753 static void enable_SIGVTALRM_irq(unsigned int irq)
757 static void disable_SIGVTALRM_irq(unsigned int irq)
761 static void mask_and_ack_SIGVTALRM(unsigned int irq)
765 static void end_SIGVTALRM_irq(unsigned int irq)
769 static struct hw_interrupt_type SIGIO_irq_type = {
780 static struct hw_interrupt_type SIGVTALRM_irq_type = {
782 startup_SIGVTALRM_irq,
783 shutdown_SIGVTALRM_irq,
784 enable_SIGVTALRM_irq,
785 disable_SIGVTALRM_irq,
786 mask_and_ack_SIGVTALRM,
791 void __init init_IRQ(void)
795 irq_desc[TIMER_IRQ].status = IRQ_DISABLED;
796 irq_desc[TIMER_IRQ].action = 0;
797 irq_desc[TIMER_IRQ].depth = 1;
798 irq_desc[TIMER_IRQ].handler = &SIGVTALRM_irq_type;
799 enable_irq(TIMER_IRQ);
800 for(i=1;i<NR_IRQS;i++){
801 irq_desc[i].status = IRQ_DISABLED;
802 irq_desc[i].action = 0;
803 irq_desc[i].depth = 1;
804 irq_desc[i].handler = &SIGIO_irq_type;
811 * Overrides for Emacs so that we follow Linus's tabbing style.
812 * Emacs will notice this stuff at the end of the file and automatically
813 * adjust the settings for this buffer only. This must remain at the end
815 * ---------------------------------------------------------------------------
817 * c-file-style: "linux"