Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Open Source

Interrupt Management Under Linux


Interrupt handling is a fundamental part of the Linux kernel. Most of the kernel's functionality, in particular the parts of interest to embedded developers, in some way involve interrupt handling.

This article describes the most important concepts related to the Linux kernel's interrupt handling mechanisms. These concepts include the relevant code and data structures. Sample code from Linux kernel version 2.6.12 is also provided.

struct irqdesc and do_IRQ
Each interrupt source available to the system has allocated to it a single struct irqdesc structure. This structure stores important information for the interrupt controller, handler and others:

The handle field points to a high-level "handler" for the interrupt line asserting the interrupt request. In ARM architectures, the handler is called do_level_IRQ:

           void do_level_IRQ (unsigned int irq,

Do_level_IRQ acknowledges the interrupt request, then invokes the list of device interrupt handlers registered with that interrupt source by calling __do_irq. We will come back to this function in a moment.

The chip field refers to the functions that manage the interrupt controller hardware. It is not uncommon for hardware that supports Linux to contain several different interrupt controllers, each having its own procedure for enabling, disabling and acknowledging interrupts. Most PCs have two interrupt controllers; some microcontrollers used in embedded applications have one or two built-in controllers, with one or more "external" interrupt controllers implemented using programmable logic devices.

The struct irqchip structure looks like this:

The action field of struct irqdesc maintains a list of struct irqaction structures, each of which represents a device interrupt handler that has registered with the interrupt request line using request_irq. The __do_irq function "walks" this list each time the interrupt request line is serviced:


The pend field is an ARM-specific field, used to make sure that pending interrupt requests are fully serviced during interrupt handling.

The chipdata field is another ARM-specific field, used by interrupt controller handlers as a private data pointer. Some ARM implementations like SA1111 use this field to store the physical address of the interrupt controller that manages the request line. Others, like AT91RM920, use this pointer to store the physical address of the GPIO controller that manages the line.

The data field is another ARM-specific field, used by device interrupt handlers as a private data pointer. Many device drivers uses this field to store a pointer to per device data structures.

The disable_depth field keeps interrupt enable and disable requests balanced. An interrupt request line is not truly disabled until the number of disable requests matches the number of enable requests.

Handling an interrupt request
When the host microcontroller responds to an interrupt request, control first goes to a bit of assembly language code that knows how to store register values and other information critical to restoring the machine state after the interrupt is serviced. In ARM machines, the function is called __irq_svc:

With the current processor state saved away, ARM machines then invoke asm_do_IRQ:

The desc->handle() invocation calls do_level_IRQ or do_edge_IRQ, depending on the type of interrupt request line being serviced.

A basic interrupt handler
The Cogent Computer Systems, Inc. CSB637 single board computer has a pushbutton connected to GPIO PB29. When pressed, this pushbutton sends an edge-triggered interrupt to the GPIO controller, which forwards the request to the interrupt controller.

(A bit of magic further demultiplexes the interrupt request to a unique irqdesc. This magic is necessary because the AT91RM9200's GPIO controllers each have only one line leading to the chip's interrupt controller. This magic is well hidden from device interrupt handlers.)

A simple "device interrupt handler" for this pushbutton might look like the following:

This code would be registered with the Linux kernel's request_irq function:

Probe_irq_on and probe_irq_off
The Linux kernel's interrupt management system can help you determine which physical interrupt request line is assigned to your device. This feature is also useful for confirming that the interrupt request line is actually functioning before your system commits to using it.

The following code shows how to "probe" for an interrupt request line:

"Virtual" interrupt descriptors
A multifunction chip might use one interrupt request line to signal interrupt requests from all of its onboard functions. Many multichannel UART chips have only a single interrupt request line leading back to the host microcontroller, for example.

Device drivers for multifunction chips can often be made more reusable and flexible if they can focus on only one feature of the target chip. The SM501 graphics processor has built-in AC97, UART and USBH, but only one interrupt request line back to the host processor.

Rather than writing a combined video-plus-audio-plus-USB that would only be useful for that chip, with some effort it is possible to re-use the Linux kernel's standard framebuffer, audio and USB device drivers instead.

To do so, you must "demultiplex" the interrupt request line for the multifunction chip so that the signaling sub-component can be serviced by the right driver. Under Linux, this is done by creating "virtual interrupt descriptors" that look like unique interrupt sources for the purposes of device drivers:

#if defined(CONFIG_MY_MULTIFUNCTION_CHIP)

The device interrupt handler for the physical interrupt request line gets the interrupt request first. It then reads the interrupt status from the chip, and redirects the interrupt request to the descriptor associated with the function requesting service:

Conclusion
A clear understanding of the Linux kernel's interrupt handling mechanism is essential if you are to write solid, reusable device interrupt handlers. It is also mandatory if you are to successfully port Linux to custom hardware.

Bill Gatliff is a freelance embedded developer and training consultant with 10 years of experience using GNU and other tools for building embedded systems targeting automotive, aerospace, and medical instrumentation applications. He is a contributing editor for Embedded Systems Design, author of the Embedded GNU Jumpstart and Embedded Linux Jumpstart series of training materials. He con be contacted at [email protected].

This article is excerpted from a paper of the same name presented at the Embedded Systems Conference Boston 2006. Used with permission of the Embedded Systems Conference. For more information, please visit www.embedded.com/esc/boston/.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.