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"
21 #include "linux/profile.h"
22 #include "linux/hardirq.h"
24 #include "asm/hw_irq.h"
25 #include "asm/atomic.h"
26 #include "asm/signal.h"
27 #include "asm/system.h"
28 #include "asm/errno.h"
29 #include "asm/uaccess.h"
30 #include "user_util.h"
31 #include "kern_util.h"
35 static void register_irq_proc (unsigned int irq
);
37 irq_desc_t irq_desc
[NR_IRQS
] __cacheline_aligned
= {
39 .handler
= &no_irq_type
,
40 .lock
= SPIN_LOCK_UNLOCKED
45 * Generic no controller code
48 static void enable_none(unsigned int irq
) { }
49 static unsigned int startup_none(unsigned int irq
) { return 0; }
50 static void disable_none(unsigned int irq
) { }
51 static void ack_none(unsigned int irq
)
54 * 'what should we do if we get a hw irq event on an illegal vector'.
55 * each architecture has to answer this themselves, it doesn't deserve
56 * a generic callback i think.
59 printk(KERN_ERR
"unexpected IRQ trap at vector %02x\n", irq
);
60 #ifdef CONFIG_X86_LOCAL_APIC
62 * Currently unexpected vectors happen only on SMP and APIC.
63 * We _must_ ack these because every local APIC has only N
64 * irq slots per priority level, and a 'hanging, unacked' IRQ
65 * holds up an irq slot - in excessive cases (when multiple
66 * unexpected vectors occur) that might lock up the APIC
74 /* startup is the same as "enable", shutdown is same as "disable" */
75 #define shutdown_none disable_none
76 #define end_none enable_none
78 struct hw_interrupt_type no_irq_type
= {
89 * Generic, controller-independent functions:
92 int show_interrupts(struct seq_file
*p
, void *v
)
94 int i
= *(loff_t
*) v
, j
;
95 struct irqaction
* action
;
100 for (j
=0; j
<NR_CPUS
; j
++)
102 seq_printf(p
, "CPU%d ",j
);
107 spin_lock_irqsave(&irq_desc
[i
].lock
, flags
);
108 action
= irq_desc
[i
].action
;
111 seq_printf(p
, "%3d: ",i
);
113 seq_printf(p
, "%10u ", kstat_irqs(i
));
115 for (j
= 0; j
< NR_CPUS
; j
++)
117 seq_printf(p
, "%10u ", kstat_cpu(j
).irqs
[i
]);
119 seq_printf(p
, " %14s", irq_desc
[i
].handler
->typename
);
120 seq_printf(p
, " %s", action
->name
);
122 for (action
=action
->next
; action
; action
= action
->next
)
123 seq_printf(p
, ", %s", action
->name
);
127 spin_unlock_irqrestore(&irq_desc
[i
].lock
, flags
);
128 } else if (i
== NR_IRQS
) {
129 seq_printf(p
, "NMI: ");
130 for (j
= 0; j
< NR_CPUS
; j
++)
132 seq_printf(p
, "%10u ", nmi_count(j
));
140 * This should really return information about whether
141 * we should do bottom half handling etc. Right now we
142 * end up _always_ checking the bottom half, which is a
143 * waste of time and is not what some drivers would
146 int handle_IRQ_event(unsigned int irq
, struct pt_regs
* regs
,
147 struct irqaction
* action
)
149 int status
= 1; /* Force the "do bottom halves" bit */
152 if (!(action
->flags
& SA_INTERRUPT
))
156 ret
= action
->handler(irq
, action
->dev_id
, regs
);
157 if (ret
== IRQ_HANDLED
)
158 status
|= action
->flags
;
160 action
= action
->next
;
162 if (status
& SA_SAMPLE_RANDOM
)
163 add_interrupt_randomness(irq
);
171 * Generic enable/disable code: this just calls
172 * down into the PIC-specific version for the actual
173 * hardware disable after having gotten the irq
178 * disable_irq_nosync - disable an irq without waiting
179 * @irq: Interrupt to disable
181 * Disable the selected interrupt line. Disables of an interrupt
182 * stack. Unlike disable_irq(), this function does not ensure existing
183 * instances of the IRQ handler have completed before returning.
185 * This function may be called from IRQ context.
188 inline void disable_irq_nosync(unsigned int irq
)
190 irq_desc_t
*desc
= irq_desc
+ irq
;
193 spin_lock_irqsave(&desc
->lock
, flags
);
194 if (!desc
->depth
++) {
195 desc
->status
|= IRQ_DISABLED
;
196 desc
->handler
->disable(irq
);
198 spin_unlock_irqrestore(&desc
->lock
, flags
);
202 inline void synchronize_irq(unsigned int irq
)
204 /* is there anything to synchronize with? */
205 if (!irq_desc
[irq
].action
)
208 while (irq_desc
[irq
].status
& IRQ_INPROGRESS
)
214 * disable_irq - disable an irq and wait for completion
215 * @irq: Interrupt to disable
217 * Disable the selected interrupt line. Disables of an interrupt
218 * stack. That is for two disables you need two enables. This
219 * function waits for any pending IRQ handlers for this interrupt
220 * to complete before returning. If you use this function while
221 * holding a resource the IRQ handler may need you will deadlock.
223 * This function may be called - with care - from IRQ context.
226 void disable_irq(unsigned int irq
)
228 disable_irq_nosync(irq
);
229 synchronize_irq(irq
);
233 * enable_irq - enable interrupt handling on an irq
234 * @irq: Interrupt to enable
236 * Re-enables the processing of interrupts on this IRQ line
237 * providing no disable_irq calls are now in effect.
239 * This function may be called from IRQ context.
242 void enable_irq(unsigned int irq
)
244 irq_desc_t
*desc
= irq_desc
+ irq
;
247 spin_lock_irqsave(&desc
->lock
, flags
);
248 switch (desc
->depth
) {
250 unsigned int status
= desc
->status
& ~IRQ_DISABLED
;
251 desc
->status
= status
;
252 if ((status
& (IRQ_PENDING
| IRQ_REPLAY
)) == IRQ_PENDING
) {
253 desc
->status
= status
| IRQ_REPLAY
;
254 hw_resend_irq(desc
->handler
,irq
);
256 desc
->handler
->enable(irq
);
263 printk(KERN_ERR
"enable_irq() unbalanced from %p\n",
264 __builtin_return_address(0));
266 spin_unlock_irqrestore(&desc
->lock
, flags
);
270 * do_IRQ handles all normal device IRQ's (the special
271 * SMP cross-CPU interrupts have their own specific
274 unsigned int do_IRQ(int irq
, union uml_pt_regs
*regs
)
277 * 0 return value means that this irq is already being
278 * handled by some other CPU. (or is disabled)
280 irq_desc_t
*desc
= irq_desc
+ irq
;
281 struct irqaction
* action
;
285 kstat_this_cpu
.irqs
[irq
]++;
286 spin_lock(&desc
->lock
);
287 desc
->handler
->ack(irq
);
289 REPLAY is when Linux resends an IRQ that was dropped earlier
290 WAITING is used by probe to mark irqs that are being tested
292 status
= desc
->status
& ~(IRQ_REPLAY
| IRQ_WAITING
);
293 status
|= IRQ_PENDING
; /* we _want_ to handle it */
296 * If the IRQ is disabled for whatever reason, we cannot
297 * use the action we have.
300 if (!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
))) {
301 action
= desc
->action
;
302 status
&= ~IRQ_PENDING
; /* we commit to handling */
303 status
|= IRQ_INPROGRESS
; /* we are handling it */
305 desc
->status
= status
;
308 * If there is no IRQ handler or it was disabled, exit early.
309 Since we set PENDING, if another processor is handling
310 a different instance of this same irq, the other processor
311 will take care of it.
317 * Edge triggered interrupts need to remember
319 * This applies to any hw interrupts that allow a second
320 * instance of the same irq to arrive while we are in do_IRQ
321 * or in the handler. But the code here only handles the _second_
322 * instance of the irq, not the third or fourth. So it is mostly
323 * useful for irq hardware that does not mask cleanly in an
327 spin_unlock(&desc
->lock
);
328 handle_IRQ_event(irq
, (struct pt_regs
*) regs
, action
);
329 spin_lock(&desc
->lock
);
331 if (!(desc
->status
& IRQ_PENDING
))
333 desc
->status
&= ~IRQ_PENDING
;
335 desc
->status
&= ~IRQ_INPROGRESS
;
338 * The ->end() handler has to deal with interrupts which got
339 * disabled while the handler was running.
341 desc
->handler
->end(irq
);
342 spin_unlock(&desc
->lock
);
350 * request_irq - allocate an interrupt line
351 * @irq: Interrupt line to allocate
352 * @handler: Function to be called when the IRQ occurs
353 * @irqflags: Interrupt type flags
354 * @devname: An ascii name for the claiming device
355 * @dev_id: A cookie passed back to the handler function
357 * This call allocates interrupt resources and enables the
358 * interrupt line and IRQ handling. From the point this
359 * call is made your handler function may be invoked. Since
360 * your handler function must clear any interrupt the board
361 * raises, you must take care both to initialise your hardware
362 * and to set up the interrupt handler in the right order.
364 * Dev_id must be globally unique. Normally the address of the
365 * device data structure is used as the cookie. Since the handler
366 * receives this value it makes sense to use it.
368 * If your interrupt is shared you must pass a non NULL dev_id
369 * as this is required when freeing the interrupt.
373 * SA_SHIRQ Interrupt is shared
375 * SA_INTERRUPT Disable local interrupts while processing
377 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
381 int request_irq(unsigned int irq
,
382 irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
383 unsigned long irqflags
,
384 const char * devname
,
388 struct irqaction
* action
;
392 * Sanity-check: shared interrupts should REALLY pass in
393 * a real dev-ID, otherwise we'll have trouble later trying
394 * to figure out which interrupt is which (messes up the
395 * interrupt freeing logic etc).
397 if (irqflags
& SA_SHIRQ
) {
399 printk(KERN_ERR
"Bad boy: %s (at 0x%x) called us "
400 "without a dev_id!\n", devname
, (&irq
)[-1]);
409 action
= (struct irqaction
*)
410 kmalloc(sizeof(struct irqaction
), GFP_KERNEL
);
414 action
->handler
= handler
;
415 action
->flags
= irqflags
;
416 cpus_clear(action
->mask
);
417 action
->name
= devname
;
419 action
->dev_id
= dev_id
;
421 retval
= setup_irq(irq
, action
);
427 EXPORT_SYMBOL(request_irq
);
429 int um_request_irq(unsigned int irq
, int fd
, int type
,
430 irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
431 unsigned long irqflags
, const char * devname
,
436 err
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
441 err
= activate_fd(irq
, fd
, type
, dev_id
);
444 EXPORT_SYMBOL(um_request_irq
);
445 EXPORT_SYMBOL(reactivate_fd
);
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
);
479 /* Can't share interrupts unless both agree to */
480 if (!(old
->flags
& new->flags
& SA_SHIRQ
)) {
481 spin_unlock_irqrestore(&desc
->lock
,flags
);
485 /* add new interrupt at end of irq queue */
497 desc
->status
&= ~IRQ_DISABLED
;
498 desc
->handler
->startup(irq
);
500 spin_unlock_irqrestore(&desc
->lock
,flags
);
502 register_irq_proc(irq
);
507 * free_irq - free an interrupt
508 * @irq: Interrupt line to free
509 * @dev_id: Device identity to free
511 * Remove an interrupt handler. The handler is removed and if the
512 * interrupt line is no longer in use by any driver it is disabled.
513 * On a shared IRQ the caller must ensure the interrupt is disabled
514 * on the card it drives before calling this function. The function
515 * does not return until any executing interrupts for this IRQ
518 * This function may be called from interrupt context.
520 * Bugs: Attempting to free an irq in a handler for the same irq hangs
524 void free_irq(unsigned int irq
, void *dev_id
)
527 struct irqaction
**p
;
533 desc
= irq_desc
+ irq
;
534 spin_lock_irqsave(&desc
->lock
,flags
);
537 struct irqaction
* action
= *p
;
539 struct irqaction
**pp
= p
;
541 if (action
->dev_id
!= dev_id
)
544 /* Found it - now remove it from the list of entries */
547 desc
->status
|= IRQ_DISABLED
;
548 desc
->handler
->shutdown(irq
);
550 free_irq_by_irq_and_dev(irq
, dev_id
);
551 spin_unlock_irqrestore(&desc
->lock
,flags
);
553 /* Wait to make sure it's not being used on another CPU */
554 synchronize_irq(irq
);
558 printk(KERN_ERR
"Trying to free free IRQ%d\n",irq
);
559 spin_unlock_irqrestore(&desc
->lock
,flags
);
564 EXPORT_SYMBOL(free_irq
);
566 /* These are initialized by sysctl_init, which is called from init/main.c */
567 static struct proc_dir_entry
* root_irq_dir
;
568 static struct proc_dir_entry
* irq_dir
[NR_IRQS
];
569 static struct proc_dir_entry
* smp_affinity_entry
[NR_IRQS
];
571 /* These are read and written as longs, so a read won't see a partial write
572 * even during a race.
574 static cpumask_t irq_affinity
[NR_IRQS
] = { [0 ... NR_IRQS
-1] = CPU_MASK_ALL
};
576 static int irq_affinity_read_proc (char *page
, char **start
, off_t off
,
577 int count
, int *eof
, void *data
)
579 int len
= cpumask_scnprintf(page
, count
, irq_affinity
[(long)data
]);
582 len
+= sprintf(page
+ len
, "\n");
586 static int irq_affinity_write_proc (struct file
*file
, const char *buffer
,
587 unsigned long count
, void *data
)
589 int irq
= (long) data
, full_count
= count
, err
;
592 if (!irq_desc
[irq
].handler
->set_affinity
)
595 err
= cpumask_parse(buffer
, count
, new_value
);
601 * Do not allow disabling IRQs completely - it's a too easy
602 * way to make the system unusable accidentally :-) At least
603 * one online CPU still has to be targeted.
606 cpus_and(tmp
, new_value
, cpu_online_map
);
612 irq_affinity
[irq
] = new_value
;
613 irq_desc
[irq
].handler
->set_affinity(irq
, new_value
);
618 #define MAX_NAMELEN 10
620 static void register_irq_proc (unsigned int irq
)
622 struct proc_dir_entry
*entry
;
623 char name
[MAX_NAMELEN
];
625 if (!root_irq_dir
|| (irq_desc
[irq
].handler
== &no_irq_type
) ||
629 memset(name
, 0, MAX_NAMELEN
);
630 sprintf(name
, "%d", irq
);
632 /* create /proc/irq/1234 */
633 irq_dir
[irq
] = proc_mkdir(name
, root_irq_dir
);
635 /* create /proc/irq/1234/smp_affinity */
636 entry
= create_proc_entry("smp_affinity", 0600, irq_dir
[irq
]);
639 entry
->data
= (void *)(long)irq
;
640 entry
->read_proc
= irq_affinity_read_proc
;
641 entry
->write_proc
= irq_affinity_write_proc
;
643 smp_affinity_entry
[irq
] = entry
;
646 void __init
init_irq_proc (void)
650 /* create /proc/irq */
651 root_irq_dir
= proc_mkdir("irq", 0);
653 /* create /proc/irq/prof_cpu_mask */
654 create_prof_cpu_mask(root_irq_dir
);
657 * Create entries for all existing IRQs.
659 for (i
= 0; i
< NR_IRQS
; i
++)
660 register_irq_proc(i
);
663 static spinlock_t irq_spinlock
= SPIN_LOCK_UNLOCKED
;
665 unsigned long irq_lock(void)
669 spin_lock_irqsave(&irq_spinlock
, flags
);
673 void irq_unlock(unsigned long flags
)
675 spin_unlock_irqrestore(&irq_spinlock
, flags
);
678 unsigned long probe_irq_on(void)
683 EXPORT_SYMBOL(probe_irq_on
);
685 int probe_irq_off(unsigned long val
)
690 EXPORT_SYMBOL(probe_irq_off
);
692 static unsigned int startup_SIGIO_irq(unsigned int irq
)
697 static void shutdown_SIGIO_irq(unsigned int irq
)
701 static void enable_SIGIO_irq(unsigned int irq
)
705 static void disable_SIGIO_irq(unsigned int irq
)
709 static void mask_and_ack_SIGIO(unsigned int irq
)
713 static void end_SIGIO_irq(unsigned int irq
)
717 static unsigned int startup_SIGVTALRM_irq(unsigned int irq
)
722 static void shutdown_SIGVTALRM_irq(unsigned int irq
)
726 static void enable_SIGVTALRM_irq(unsigned int irq
)
730 static void disable_SIGVTALRM_irq(unsigned int irq
)
734 static void mask_and_ack_SIGVTALRM(unsigned int irq
)
738 static void end_SIGVTALRM_irq(unsigned int irq
)
742 static struct hw_interrupt_type SIGIO_irq_type
= {
753 static struct hw_interrupt_type SIGVTALRM_irq_type
= {
755 startup_SIGVTALRM_irq
,
756 shutdown_SIGVTALRM_irq
,
757 enable_SIGVTALRM_irq
,
758 disable_SIGVTALRM_irq
,
759 mask_and_ack_SIGVTALRM
,
764 void __init
init_IRQ(void)
768 irq_desc
[TIMER_IRQ
].status
= IRQ_DISABLED
;
769 irq_desc
[TIMER_IRQ
].action
= 0;
770 irq_desc
[TIMER_IRQ
].depth
= 1;
771 irq_desc
[TIMER_IRQ
].handler
= &SIGVTALRM_irq_type
;
772 enable_irq(TIMER_IRQ
);
773 for(i
=1;i
<NR_IRQS
;i
++){
774 irq_desc
[i
].status
= IRQ_DISABLED
;
775 irq_desc
[i
].action
= 0;
776 irq_desc
[i
].depth
= 1;
777 irq_desc
[i
].handler
= &SIGIO_irq_type
;
784 * Overrides for Emacs so that we follow Linus's tabbing style.
785 * Emacs will notice this stuff at the end of the file and automatically
786 * adjust the settings for this buffer only. This must remain at the end
788 * ---------------------------------------------------------------------------
790 * c-file-style: "linux"