
Recall first from Interrupt context and threaded handlers
- A hard IRQ handler must finish without sleeping.
- A primary handler can return IRQ_WAKE_THREAD to schedule its threaded handler.
- IRQF_ONESHOT keeps the interrupt masked while the IRQ thread runs.
From the interrupt description to a Linux IRQ
2 min readThe last lesson worked with an IRQ number it was handed. This lesson answers where that number comes from, with platform_get_irq and IRQ domains, and how to enable, disable and synchronize interrupts.
On a board with a GPIO interrupt controller and a CPU-facing interrupt controller, each controller has its own numbering. A hardware interrupt identifier from the GPIO controller can have the same numerical value as an identifier from the other controller and refer to a different source. Copying a number from a controller manual into a handler registration can therefore fail or select an unrelated IRQ. Linux uses the controller identity together with its hardware identifier to resolve the interrupt. Kernel IRQ domain documentation
A an interrupt identifier in the numbering of a particular interrupt controller, often called an hwirq has meaning within that controller. A the kernel-wide interrupt number used by the generic IRQ APIs, also called a virtual IRQ or virq identifies the kernel's IRQ descriptor. The descriptor holds the state Linux uses to manage the interrupt and the registered handlers. Treat the Linux number as a runtime value. Even if it happens to equal the hardware number on one boot, the driver should get it from the kernel every time it probes.
For a platform device, call platform_get_irq(pdev, 0) to get its first interrupt. The index selects an interrupt described for that device. On a Device Tree system, the description identifies an interrupt controller and supplies cells interpreted according to that controller's binding. Depending on the binding, those cells can describe the interrupt class, source and trigger type. The controller's IRQ domain resolves the controller-local interrupt to a Linux IRQ, creating a mapping where needed. A mapping makes the IRQ available to generic IRQ operations. Handler registration is a separate step.
platform_get_irq() returns a positive Linux IRQ number on success and a negative error on failure. Keep the return value in an int until you have checked it. In probe, the essential sequence is irq = platform_get_irq(pdev, 0) followed by if (irq < 0) return irq;. In particular, pass -EPROBE_DEFER back unchanged. The driver core can then retry probe when dependencies become available. Converting the result to an unsigned value before testing it can turn an error into a large apparent IRQ number.
Code that starts with a Device Tree node can use irq_of_parse_and_map(np, index). It parses the indexed interrupt specifier and resolves it to a Linux IRQ. Its return convention is different. It returns an unsigned int, with a positive IRQ on success and zero on failure. Zero alone cannot distinguish an unavailable domain from a bad specifier. For a platform driver looking up its own IRQ, use platform_get_irq() to preserve the error information. For either API, pass only a successfully resolved Linux IRQ to the handler registration call.
When a device is active, you can inspect /proc/interrupts and look for the handler name supplied at registration. Where the kernel exposes the entry, the leading number on its numbered row is the Linux IRQ, followed by per-CPU counts. Controller information elsewhere on the row can vary. WSL2 and virtual machines expose their virtual hardware, so an entry for a board peripheral requires a system where that peripheral is present and its driver has registered the interrupt.
Control delivery and wait for handlers
3 min readDecide what needs to stop before picking an API. A short region of code may need to run without a hard IRQ interrupting it on the current CPU. A device operation may need one Linux IRQ disabled across CPUs. A shutdown sequence may already have stopped the device from raising interrupts and need to wait for handlers that were running when it stopped. Each operation has a different scope.
Use local_irq_save(flags) and local_irq_restore(flags) for a short region that must exclude local hard IRQ delivery. Declare flags as unsigned long. The first call saves the current interrupt state and disables local hard IRQs. The second restores that saved state. If the caller already had interrupts disabled, they stay disabled after the restore. Use a separate saved value for each nested region and restore in reverse order. Keep the region short and use operations that cannot sleep. A handler on another CPU can still run and access shared data, so use the appropriate lock if you also need cross-CPU exclusion.
Use disable_irq(irq) to disable a resolved Linux IRQ and wait for its active handlers to finish, including a running threaded handler. For normal driver code, call it from a context that can sleep. Keep resources that the handlers need available during the wait. For example, if the IRQ thread needs a mutex held by the caller, the thread waits for the mutex while the caller waits inside disable_irq(). Both are stuck. Calling it from the IRQ's own handler can also make the handler wait for itself.
Use enable_irq(irq) to balance your disable_irq(irq) when the device is ready to handle interrupts again. The disable depth is nested. If two callers each disable the IRQ, delivery stays disabled after the first matching enable and can resume after the second. Balance temporary disables on error paths that leave the device running, too. Before enabling, make sure handler-visible state is ready. If the device is still asserting a level-triggered interrupt, a handler can run as soon as delivery resumes. Consult the device's interrupt status and acknowledgement rules when preparing to resume.
A shared IRQ has one Linux IRQ number for multiple registered handlers. Disabling that IRQ affects every handler on it. If you only need to stop your device from generating interrupts, use its own interrupt enable or mask controls when the device provides them. Masking at the device leaves other devices on the shared IRQ able to deliver interrupts. A handler on the shared line can still be invoked because another device asserted it, so keep your handler and its state valid until your registration is removed.
Use synchronize_irq(irq) to wait for pending or running handlers for that IRQ to finish, including threaded handlers. Like disable_irq(), it can sleep and can deadlock if the caller holds a resource a handler needs. It leaves the IRQ's enable state alone. With delivery still enabled, a new interrupt can arrive as soon as the call returns. For example, synchronize_irq() by itself is insufficient before resetting registers a handler might read. For an exclusive IRQ, a temporary disable_irq() can provide both the wait and the disabled interval. If you have already stopped the device's interrupt source and need to wait for in-flight handling, synchronize_irq() provides the wait. Work queued separately by a handler has its own lifetime and may need its own cancellation or flush before you release the driver state it uses.
How controllers cooperate
2 min readAn IRQ domain supplies the controller-specific mapping. An irq_chip supplies operations the IRQ core can call to control delivery, such as masking, unmasking and acknowledging an interrupt. Which callbacks a controller implements depends on the hardware. The device driver uses the generic IRQ operations on its Linux IRQ number. The IRQ core and the interrupt-controller driver handle the controller-specific operations. A device's own status and acknowledgement registers remain the responsibility of its device driver.
In a Linux hierarchical IRQ domain, a child domain has a parent domain. For an IRQ allocated through that hierarchy, the per-level irq_data records can hold different hardware IRQ identifiers and refer to different chips, while the device driver uses one Linux IRQ number. This represents a route that needs configuration at multiple controller levels. A child controller can allocate its upstream interrupt through the parent domain. An ARM Generic Interrupt Controller (GIC) can supply the CPU-facing domain in this arrangement, receiving the upstream interrupt and delivering it to a CPU.
A GPIO expander acting as an interrupt controller illustrates a related design. An interrupt-capable I2C GPIO expander can use a single interrupt output wired to a SoC GPIO interrupt input. The SoC GPIO controller can in turn signal a GIC on a system that uses one. Linux handles the expander's parent IRQ, and a threaded handler can read the expander over I2C to identify the source before dispatching the corresponding child IRQ. Each exposed interrupt input can have its own Linux IRQ even though the expander has one parent output. This is a cascaded controller arrangement with nested threaded handling. A physical parent-child connection by itself does not imply a Linux hierarchical IRQ domain. In this example, the parent IRQ is a separately handled interrupt shared by the expander's child sources. GPIO Driver Interface
When reading a board's interrupt description, ask which controller interprets the device's specifier and which controller receives that controller's output. The first question explains the device's hardware IRQ identifier. The second explains whether handling relies on another IRQ upstream. “Writing a GPIO controller” follows this path from the provider side and explains how a GPIO controller makes its lines available to consumers.
Key takeaways
- An IRQ domain resolves a controller-local hardware IRQ to a Linux IRQ number.
- platform_get_irq returns a positive IRQ or a negative error, including probe deferral.
- local_irq_save and local_irq_restore affect interrupt delivery on the current CPU.
- disable_irq waits for active handlers and prevents new delivery on the IRQ, while synchronize_irq waits without disabling the IRQ.
Reflect
- Explain to a colleague, in two sentences: distinguish a controller-local hardware IRQ from a Linux IRQ number.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationThe irq_domain interrupt number mapping library
- Kernel documentationLinux generic IRQ handling
- Kernel documentationGPIO Driver Interface
- Kernel documentationUnreliable Guide To Locking
Official documentation first. Links checked when the lesson was published.