</>EmbeddedInterview

What are the ways a CPU can communicate with an I/O device (polling, interrupts, DMA)?

EasyOperating System Concepts

Short answer: With polling the CPU repeatedly reads a status register until the device is ready (simple, lowest latency for very fast devices, but wastes CPU time). With interrupts the device signals the CPU when it needs attention, and the CPU does other work meanwhile. With DMA a DMA controller copies whole blocks of data between the device and memory without the CPU, interrupting only when the transfer is done. Device registers are accessed through memory-mapped I/O (ARM) or a separate I/O port space (x86 in/out).

Polling Interrupt-driven DMA
CPU cost per byte high (busy loop) one interrupt per event/byte almost none (one interrupt per block)
Latency lowest if the CPU is already looping interrupt entry latency (µs) setup cost, then hardware speed
Complexity trivial ISR, shared data protection buffer management, cache coherency, descriptors
Good for very short waits, boot code, very high rates (NAPI busy polling) keyboards, UART at low rates, buttons, timers disks, network, audio, displays, high-rate ADC/SPI

Interrupt handling basics: the device asserts an IRQ line → interrupt controller (GIC, NVIC, APIC) → CPU saves state and jumps to the vector → the handler acknowledges the device and does minimal work → defers the rest (bottom half, tasklet, workqueue, threaded IRQ, or an RTOS task) → returns.

Hybrid approaches: Linux network drivers use NAPI: the first packet raises an interrupt, then the driver disables the interrupt and polls while traffic is heavy, switching back to interrupts when it is quiet. This avoids an "interrupt storm" at high packet rates.

DMA caveats: on CPUs without hardware cache coherency for DMA (many Cortex-A/M7 systems), the driver must clean the cache before a device reads memory and invalidate it before the CPU reads what a device wrote. Linux's DMA API (dma_map_single, dma_alloc_coherent) handles this.

Watching interrupts on Linux: /proc/interrupts shows per-CPU counts for every IRQ. Sampling it over a minute and dividing by 60 gives a stable per-second rate, smoothing out bursts that a one-second sample would exaggerate or miss.