• What is interrupt or Event in computing system?
The simplest answer is,
CPU Attention to any physical or virtual objects whenever those
objects are in action. Or Forcibly change the current execution.
• Busy POLLING alternative and much more efficient.
• In this presentation we will understand the interrupt
management mechanism in Linux kernelspace by taking an
example of GPIO LKM as well as how we can control GPIO
LKM from userspace.
4. Interrupt handling in MCU
• Before we dive into the Linux kernel lets brush-up our original
concept regarding this term “interrupt” in the context of
• In most microcontroller, for each interrupt there are
predefined memory locations, we called it as vector table. This
vector table is vendor specific.
5. Interrupt handling in MCU (continued…)
MCU’s State changes whenever the interrupt occurred.
• The current Program Counter is saved on the stack.
• Interrupts of the same and lower priority are blocked.
• The corresponding interrupt flag is cleared.
• Program execution transfers to the corresponding interrupt
handler vector address.
• The Interrupt Handler Routine executes.
• After execution, return to paused work and resume it.
6. Interrupt handling in MCU (continued…)
• Execution without interrupt • Execution with interrupt
7. Interrupt handling in Linux kernelspace
• Lets talk about SoC + Linux combination.
• It might be possible that we have more then one hardware
connection with SoC.
• For equal response to each hardware, kernel must have to
communicate with each individual hardware.
• Due to low-speed nature of I/O device as compared to CPU,
the I/O devices sent attention signal to CPU asynchronously.
8. Interrupt handling in Linux kernelspace(continued…)
• To reduces the Pinout on SoC or CPU for controlling each I/O
devices, the vendors are use interrupt multiplexer or interrupt
Programmable Interrupt Controller
9. Interrupt handling in Linux kernelspace(continued…)
• Interrupt Controller will provide unique ID to each I/O device
by assigning a IRQ numbers.
• Each IRQ lines are mapped to interrupt vector location.
• By this way, interrupt from the X I/O device is distinct from Y
and Z I/O devices any many more.
• So async signal from the I/O device will directed via interrupt
controller to the processor with IRQ line number. Upon
receiving interrupt the kernel will stop whatever it doing and
execute the registered interrupt handler.
10. Interrupt handling in Linux kernelspace(continued…)
• How to implement I/O devices’ interrupt handler?
• Lets make our custom GPIO LKM and see how the
implementation is look like.
• In this example code for userspace entry point, we will focus
on /sys directory rather then /dev, because it is easy to
11. Interrupt handling in Linux kernelspace(continued…)
• What actually request_irq() kernel function (which may sleep)
• For the answer of this question we have to understand two
• For this two terms all the further code flow is as per the Linux
3.10 release and i8259A PIC as example.
12. Interrupt handling in Linux kernelspace(continued…)
• Now return to request_irq() kernel function.
- -> request_threaded_irq
- - - > action->handler = handler;
Our interrupt handler
assignment to irqaction
13. Interrupt handling in Linux kernelspace(continued…)
• As we know that each and every data structure of kernel will
be initialize after we apply power supply.
• The same in case of interrupt, the related data structure and
code flow will also initialize during the boot time.
• Everything are inside the start_kernel() kernel function, which
is “main” function of kernel itself.
14. Interrupt handling in Linux kernelspace(continued…)
- > make_8259A_irq
- - > irq_set_chip_and_handler
- - - > irq_set_chip
- - - > __irq_set_handler
- - - - > desc->handle_irq = handle @L668
For i8259 chip the handle object is handle_level_irq() kernel
15. Interrupt handling in Linux kernelspace(continued…)
• As we already see in the MCU interrupt handling slide that
there are predefined location in memory for each interrupt
called the vector table.
• The kernel is doing same things, it will jump to corresponding
location at for every interrupt it will call do_IRQ() kernel
16. Interrupt handling in Linux kernelspace(continued…)
• arch/x86/kernel/entry_32.S(entry_64.S x64) will call do_IRQ.
• arch/x86/kernel/irq.c will hold the definition for do_IRQ
- > do_IRQ
- - > handle_irq
- - - > desc->handle_irq(irq, desc) @L197
- - - > handle_level_irq
- - - - > handle_irq_event
- - - - -> handle_irq_event_percpu
- - - - - - > action->handler(irq, action->dev_id);
Final call to our
do_IRQ() will fetch the IRQ
lines number from the
17. Event handling in userspace
• In userspace we do not have to deal with such low-level
• In certain C/C++ applications, the ultimate goal is to provide
notifications system to end users.
• The term “Event handling” is so vast. Full explanation is
beyond the scope of this presentation.
• There are various signalling mechanism and producer-
consumer models for event handling in Linux.
• Let’s take the GPIO example which we had done in our Intel
Sunset Pass Module.
18. Event handling in userspace
Test case thread
is done by using
used to retrieve