Copyright © 2005-2006 Thomas Gleixner
Copyright © 2005-2006 Ingo Molnar
This documentation is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more details see the file COPYING in the source distribution of Linux.
Table of Contents
The generic interrupt handling layer is designed to provide a complete abstraction of interrupt handling for device drivers. It is able to handle all the different types of interrupt controller hardware. Device drivers use generic API functions to request, enable, disable and free interrupts. The drivers do not have to know anything about interrupt hardware details, so they can be used on different platforms without code changes.
This documentation is provided to developers who want to implement an interrupt subsystem based for their architecture, with the help of the generic IRQ handling layer.
The original implementation of interrupt handling in Linux is using the __do_IRQ() super-handler, which is able to deal with every type of interrupt logic.
Originally, Russell King identified different types of handlers to build a quite universal set for the ARM interrupt handler implementation in Linux 2.5/2.6. He distinguished between:
Level type
Edge type
Simple type
In the SMP world of the __do_IRQ() super-handler another type was identified:
Per CPU type
This split implementation of highlevel IRQ handlers allows us to optimize the flow of the interrupt handling for each specific interrupt type. This reduces complexity in that particular codepath and allows the optimized handling of a given type.
The original general IRQ implementation used hw_interrupt_type structures and their ->ack(), ->end() [etc.] callbacks to differentiate the flow control in the super-handler. This leads to a mix of flow logic and lowlevel hardware logic, and it also leads to unnecessary code duplication: for example in i386, there is a ioapic_level_irq and a ioapic_edge_irq irq-type which share many of the lowlevel details but have different flow handling.
A more natural abstraction is the clean separation of the 'irq flow' and the 'chip details'.
Analysing a couple of architecture's IRQ subsystem implementations reveals that most of them can use a generic set of 'irq flow' methods and only need to add the chip level specific code. The separation is also valuable for (sub)architectures which need specific quirks in the irq flow itself but not in the chip-details - and thus provides a more transparent IRQ subsystem design.
Each interrupt descriptor is assigned its own highlevel flow handler, which is normally one of the generic implementations. (This highlevel flow handler implementation also makes it simple to provide demultiplexing handlers which can be found in embedded platforms on various architectures.)
The separation makes the generic interrupt handling layer more flexible and extensible. For example, an (sub)architecture can use a generic irq-flow implementation for 'level type' interrupts and add a (sub)architecture specific 'edge type' implementation.
To make the transition to the new model easier and prevent the breakage of existing implementations, the __do_IRQ() super-handler is still available. This leads to a kind of duality for the time being. Over time the new model should be used in more and more architectures, as it enables smaller and cleaner IRQ subsystems.
Table of Contents
There are three main levels of abstraction in the interrupt code:
Highlevel driver API
Highlevel IRQ flow handlers
Chiplevel hardware encapsulation
Each interrupt is described by an interrupt descriptor structure irq_desc. The interrupt is referenced by an 'unsigned int' numeric value which selects the corresponding interrupt decription structure in the descriptor structures array. The descriptor structure contains status information and pointers to the interrupt flow method and the interrupt chip structure which are assigned to this interrupt.
Whenever an interrupt triggers, the lowlevel arch code calls into the generic interrupt code by calling desc->handle_irq(). This highlevel IRQ handling function only uses desc->chip primitives referenced by the assigned chip descriptor structure.
The highlevel Driver API consists of following functions:
request_irq()
free_irq()
disable_irq()
enable_irq()
disable_irq_nosync() (SMP only)
synchronize_irq() (SMP only)
set_irq_type()
set_irq_wake()
set_irq_data()
set_irq_chip()
set_irq_chip_data()
See the autogenerated function documentation for details.
The generic layer provides a set of pre-defined irq-flow methods:
handle_level_irq
handle_edge_irq
handle_simple_irq
handle_percpu_irq
The interrupt flow handlers (either predefined or architecture specific) are assigned to specific interrupts by the architecture either during bootup or during device initialization.
The helper functions call the chip primitives and are used by the default flow implementations. The following helper functions are implemented (simplified excerpt):
default_enable(irq)
{
desc->chip->unmask(irq);
}
default_disable(irq)
{
if (!delay_disable(irq))
desc->chip->mask(irq);
}
default_ack(irq)
{
chip->ack(irq);
}
default_mask_ack(irq)
{
if (chip->mask_ack) {
chip->mask_ack(irq);
} else {
chip->mask(irq);
chip->ack(irq);
}
}
noop(irq)
{
}
handle_level_irq provides a generic implementation for level-triggered interrupts.
The following control flow is implemented (simplified excerpt):
desc->chip->start(); handle_IRQ_event(desc->action); desc->chip->end();
handle_edge_irq provides a generic implementation for edge-triggered interrupts.
The following control flow is implemented (simplified excerpt):
if (desc->status & running) {
desc->chip->hold();
desc->status |= pending | masked;
return;
}
desc->chip->start();
desc->status |= running;
do {
if (desc->status & masked)
desc->chip->enable();
desc->status &= ~pending;
handle_IRQ_event(desc->action);
} while (status & pending);
desc->status &= ~running;
desc->chip->end();
handle_simple_irq provides a generic implementation for simple interrupts.
Note: The simple flow handler does not call any handler/chip primitives.
The following control flow is implemented (simplified excerpt):
handle_IRQ_event(desc->action);
handle_percpu_irq provides a generic implementation for per CPU interrupts.
Per CPU interrupts are only available on SMP and the handler provides a simplified version without locking.
The following control flow is implemented (simplified excerpt):
desc->chip->start(); handle_IRQ_event(desc->action); desc->chip->end();
The generic functions are intended for 'clean' architectures and chips, which have no platform-specific IRQ handling quirks. If an architecture needs to implement quirks on the 'flow' level then it can do so by overriding the highlevel irq-flow handler.
This per interrupt selectable feature, which was introduced by Russell King in the ARM interrupt implementation, does not mask an interrupt at the hardware level when disable_irq() is called. The interrupt is kept enabled and is masked in the flow handler when an interrupt event happens. This prevents losing edge interrupts on hardware which does not store an edge interrupt event while the interrupt is disabled at the hardware level. When an interrupt arrives while the IRQ_DISABLED flag is set, then the interrupt is masked at the hardware level and the IRQ_PENDING bit is set. When the interrupt is re-enabled by enable_irq() the pending bit is checked and if it is set, the interrupt is resent either via hardware or by a software resend mechanism. (It's necessary to enable CONFIG_HARDIRQS_SW_RESEND when you want to use the delayed interrupt disable feature and your hardware is not capable of retriggering an interrupt.) The delayed interrupt disable can be runtime enabled, per interrupt, by setting the IRQ_DELAYED_DISABLE flag in the irq_desc status field.
The chip level hardware descriptor structure irq_chip contains all the direct chip relevant functions, which can be utilized by the irq flow implementations.
ack()
mask_ack() - Optional, recommended for performance
mask()
unmask()
retrigger() - Optional
set_type() - Optional
set_wake() - Optional
These primitives are strictly intended to mean what they say: ack means ACK, masking means masking of an IRQ line, etc. It is up to the flow handler(s) to use these basic units of lowlevel functionality.
The original implementation __do_IRQ() is an alternative entry point for all types of interrupts.
This handler turned out to be not suitable for all interrupt hardware and was therefore reimplemented with split functionality for egde/level/simple/percpu interrupts. This is not only a functional optimization. It also shortens code paths for interrupts.
To make use of the split implementation, replace the call to __do_IRQ by a call to desc->chip->handle_irq() and associate the appropriate handler function to desc->chip->handle_irq(). In most cases the generic handler implementations should be sufficient.
The locking of chip registers is up to the architecture that defines the chip primitives. There is a chip->lock field that can be used for serialization, but the generic layer does not touch it. The per-irq structure is protected via desc->lock, by the generic layer.
Table of Contents
This chapter contains the autogenerated documentation of the structures which are used in the generic IRQ layer.
struct irq_chip — hardware interrupt chip descriptor
struct irq_chip {
const char * name;
unsigned int (* startup) (unsigned int irq);
void (* shutdown) (unsigned int irq);
void (* enable) (unsigned int irq);
void (* disable) (unsigned int irq);
void (* ack) (unsigned int irq);
void (* mask) (unsigned int irq);
void (* mask_ack) (unsigned int irq);
void (* unmask) (unsigned int irq);
void (* eoi) (unsigned int irq);
void (* end) (unsigned int irq);
int (* set_affinity) (unsigned int irq,const struct cpumask *dest);
int (* retrigger) (unsigned int irq);
int (* set_type) (unsigned int irq, unsigned int flow_type);
int (* set_wake) (unsigned int irq, unsigned int on);
void (* bus_lock) (unsigned int irq);
void (* bus_sync_unlock) (unsigned int irq);
#ifdef CONFIG_IRQ_RELEASE_METHOD
void (* release) (unsigned int irq, void *dev_id);
#endif
const char * typename;
}; name for /proc/interrupts
start up the interrupt (defaults to ->enable if NULL)
shut down the interrupt (defaults to ->disable if NULL)
enable the interrupt (defaults to chip->unmask if NULL)
disable the interrupt (defaults to chip->mask if NULL)
start of a new interrupt
mask an interrupt source
ack and mask an interrupt source
unmask an interrupt source
end of interrupt - chip level
end of interrupt - flow level
set the CPU affinity on SMP machines
resend an IRQ to the CPU
set the flow type (IRQ_TYPE_LEVEL/etc.) of an IRQ
enable/disable power-management wake-on of an IRQ
function to lock access to slow bus (i2c) chips
function to sync and unlock slow bus (i2c) chips
release function solely used by UML
obsoleted by name, kept as migration helper
struct irq_desc — interrupt descriptor
struct irq_desc {
unsigned int irq;
struct timer_rand_state * timer_rand_state;
unsigned int * kstat_irqs;
#ifdef CONFIG_INTR_REMAP
struct irq_2_iommu * irq_2_iommu;
#endif
irq_flow_handler_t handle_irq;
struct irq_chip * chip;
struct msi_desc * msi_desc;
void * handler_data;
void * chip_data;
struct irqaction * action;
unsigned int status;
unsigned int depth;
unsigned int wake_depth;
unsigned int irq_count;
unsigned long last_unhandled;
unsigned int irqs_unhandled;
spinlock_t lock;
#ifdef CONFIG_SMP
cpumask_var_t affinity;
unsigned int node;
#ifdef CONFIG_GENERIC_PENDING_IRQ
cpumask_var_t pending_mask;
#endif
#endif
atomic_t threads_active;
wait_queue_head_t wait_for_threads;
#ifdef CONFIG_PROC_FS
struct proc_dir_entry * dir;
#endif
const char * name;
}; interrupt number for this descriptor
pointer to timer rand state struct
irq stats per cpu
iommu with this irq
highlevel irq-events handler [if NULL, __do_IRQ]
low level interrupt hardware access
MSI descriptor
per-IRQ data for the irq_chip methods
platform-specific per-chip private data for the chip methods, to allow shared chip implementations
the irq action chain
status information
disable-depth, for nested irq_disable calls
enable depth, for multiple set_irq_wake callers
stats field to detect stalled irqs
aging timer for unhandled count
stats field for spurious unhandled interrupts
locking for SMP
IRQ affinity on SMP
node index useful for balancing
pending rebalanced interrupts
number of irqaction threads currently running
wait queue for sync_irq to wait for threaded handlers
/proc/irq/ procfs entry
flow handler name for /proc/interrupts output
alloc_desc_masks — allocate cpumasks for irq_desc
bool alloc_desc_masks ( | desc, | |
| node, | ||
boot); |
struct irq_desc * | desc; |
int | node; |
bool | boot; |
init_copy_desc_masks — copy cpumasks for irq_desc
void init_copy_desc_masks ( | old_desc, | |
new_desc); |
struct irq_desc * | old_desc; |
struct irq_desc * | new_desc; |
struct irqaction — per interrupt action descriptor
struct irqaction {
irq_handler_t handler;
unsigned long flags;
const char * name;
void * dev_id;
struct irqaction * next;
int irq;
struct proc_dir_entry * dir;
irq_handler_t thread_fn;
struct task_struct * thread;
unsigned long thread_flags;
}; interrupt handler function
flags (see IRQF_* above)
name of the device
cookie to identify the device
pointer to the next irqaction for shared interrupts
interrupt number
pointer to the proc/irq/NN/name entry
interupt handler function for threaded interrupts
thread pointer for threaded interrupts
flags related to thread
Table of Contents
This chapter contains the autogenerated documentation of the kernel API functions which are exported.
synchronize_irq — wait for pending IRQ handlers (on other CPUs)
void synchronize_irq ( | irq); |
unsigned int | irq; |
disable_irq_nosync — disable an irq without waiting
void disable_irq_nosync ( | irq); |
unsigned int | irq; |
disable_irq — disable an irq and wait for completion
void disable_irq ( | irq); |
unsigned int | irq; |
Disable the selected interrupt line. Enables and Disables are nested. This function waits for any pending IRQ handlers for this interrupt to complete before returning. If you use this function while holding a resource the IRQ handler may need you will deadlock.
This function may be called - with care - from IRQ context.
set_irq_wake — control irq power management wakeup
int set_irq_wake ( | irq, | |
on); |
unsigned int | irq; |
unsigned int | on; |
setup_irq — setup an interrupt
int setup_irq ( | irq, | |
act); |
unsigned int | irq; |
struct irqaction * | act; |
remove_irq — free an interrupt
void remove_irq ( | irq, | |
act); |
unsigned int | irq; |
struct irqaction * | act; |
free_irq — free an interrupt allocated with request_irq
void free_irq ( | irq, | |
dev_id); |
unsigned int | irq; |
void * | dev_id; |
Remove an interrupt handler. The handler is removed and if the interrupt line is no longer in use by any driver it is disabled. On a shared IRQ the caller must ensure the interrupt is disabled on the card it drives before calling this function. The function does not return until any executing interrupts for this IRQ have completed.
This function must not be called from interrupt context.
request_threaded_irq — allocate an interrupt line
int request_threaded_irq ( | irq, | |
| handler, | ||
| thread_fn, | ||
| irqflags, | ||
| devname, | ||
dev_id); |
unsigned int | irq; |
irq_handler_t | handler; |
irq_handler_t | thread_fn; |
unsigned long | irqflags; |
const char * | devname; |
void * | dev_id; |
irqInterrupt line to allocate
handlerFunction to be called when the IRQ occurs. Primary handler for threaded interrupts If NULL and thread_fn != NULL the default primary handler is installed
thread_fnFunction called from the irq handler thread If NULL, no irq thread is created
irqflagsInterrupt type flags
devnameAn ascii name for the claiming device
dev_idA cookie passed back to the handler function
This call allocates interrupt resources and enables the interrupt line and IRQ handling. From the point this call is made your handler function may be invoked. Since your handler function must clear any interrupt the board raises, you must take care both to initialise your hardware and to set up the interrupt handler in the right order.
If you want to set up a threaded irq handler for your device
then you need to supply handler and thread_fn. handler ist
still called in hard interrupt context and has to check
whether the interrupt originates from the device. If yes it
needs to disable the interrupt on the device and return
IRQ_WAKE_THREAD which will wake up the handler thread and run
thread_fn. This split handler design is necessary to support
shared interrupts.
Dev_id must be globally unique. Normally the address of the device data structure is used as the cookie. Since the handler receives this value it makes sense to use it.
If your interrupt is shared you must pass a non NULL dev_id as this is required when freeing the interrupt.
set_irq_chip — set the irq chip for an irq
int set_irq_chip ( | irq, | |
chip); |
unsigned int | irq; |
struct irq_chip * | chip; |
set_irq_type — set the irq trigger type for an irq
int set_irq_type ( | irq, | |
type); |
unsigned int | irq; |
unsigned int | type; |
set_irq_data — set irq type data for an irq
int set_irq_data ( | irq, | |
data); |
unsigned int | irq; |
void * | data; |
set_irq_chip_data — set irq chip data for an irq
int set_irq_chip_data ( | irq, | |
data); |
unsigned int | irq; |
void * | data; |
set_irq_nested_thread — Set/Reset the IRQ_NESTED_THREAD flag of an irq
void set_irq_nested_thread ( | irq, | |
nest); |
unsigned int | irq; |
int | nest; |
Table of Contents
This chapter contains the autogenerated documentation of the internal functions.
handle_bad_irq — handle spurious and unhandled irqs
void handle_bad_irq ( | irq, | |
desc); |
unsigned int | irq; |
struct irq_desc * | desc; |
handle_IRQ_event — irq action chain handler
irqreturn_t handle_IRQ_event ( | irq, | |
action); |
unsigned int | irq; |
struct irqaction * | action; |
__do_IRQ — original all in one highlevel IRQ handler
unsigned int __do_IRQ ( | irq); |
unsigned int | irq; |
dynamic_irq_init — initialize a dynamically allocated irq
void dynamic_irq_init ( | irq); |
unsigned int | irq; |
dynamic_irq_cleanup — cleanup a dynamically allocated irq
void dynamic_irq_cleanup ( | irq); |
unsigned int | irq; |
set_irq_msi — set irq type data for an irq
int set_irq_msi ( | irq, | |
entry); |
unsigned int | irq; |
struct msi_desc * | entry; |
handle_simple_irq — Simple and software-decoded IRQs.
void handle_simple_irq ( | irq, | |
desc); |
unsigned int | irq; |
struct irq_desc * | desc; |
handle_fasteoi_irq — irq handler for transparent controllers
void handle_fasteoi_irq ( | irq, | |
desc); |
unsigned int | irq; |
struct irq_desc * | desc; |
handle_edge_irq — edge type IRQ handler
void handle_edge_irq ( | irq, | |
desc); |
unsigned int | irq; |
struct irq_desc * | desc; |
Interrupt occures on the falling and/or rising edge of a hardware signal. The occurence is latched into the irq controller hardware and must be acked in order to be reenabled. After the ack another interrupt can happen on the same source even before the first one is handled by the assosiacted event handler. If this happens it might be necessary to disable (mask) the interrupt depending on the controller hardware. This requires to reenable the interrupt inside of the loop which handles the interrupts which have arrived while the handler was running. If all pending interrupts are handled, the loop is left.
The following people have contributed to this document:
Thomas Gleixner<tglx@linutronix.de>
Ingo Molnar<mingo@elte.hu>