The idea: measure, move the hot loop into hardware, keep the rest in software

Hardware/software co-design means deciding, for one product, which parts of the work run as software on a CPU and which parts get their own circuit. A circuit made for one job is called an accelerator. It can be much faster than the CPU at that job, but it costs chip area and design time, and the CPU still has to give it data and collect the results. So the questions are: which part is worth moving, how does the CPU talk to it, and how much faster does the whole program get?

A Cortex-M4 CPU, a FIR accelerator and SRAM sit on an AXI interconnect. The CPU writes the accelerator's registers CTRL, STATUS, SRC, DST and LEN at 0x4002_0000 over AXI4-Lite. The accelerator's DMA master reads x[] from SRAM at 0x2000_0100 and writes y[] to 0x2000_0200 with AXI4 bursts. A 17-stage pipeline does the filtering, and an IRQ line goes back to the CPU's NVIC
The CPU only writes five registers and handles one interrupt; the accelerator moves the data itself with DMA and filters it in a pipeline.

The example is a FIR filter (finite impulse response): each output is a weighted sum of the last 16 input samples, y[n] = Σ h[j] · x[n − j] for j = 0 … 15. The program runs on a Cortex-M4 and handles the samples in blocks of 64. All times are in CPU clock cycles, and the accelerator runs on the same clock.

Profile first

Never guess where the time goes; measure it. The Cortex-M4 has a cycle counter, DWT_CYCCNT, that the program can read before and after each call. A sampling profiler gives the same shares. One pass of the main loop takes 2,226 cycles (Demo: profile the program, 92% is in fir_block):

functioncyclesshare
read_adc642.9%
fir_block2,04892.0%
control_update823.7%
write_dac321.4%

A good candidate for hardware is a small kernel that takes most of the time, does a lot of arithmetic on each piece of data, and has a simple, regular data flow. fir_block is all three. control_update is full of decisions and stays in software.

Software against a pipelined datapath

In software the CPU does one multiply-accumulate (MAC) at a time: load x[n − j] and h[j], multiply, add to acc. That is about 2 cycles per MAC, so a block costs 64 samples × 16 taps × 2 cycles = 2,048 cycles (Demo: the FIR in software, 2,048 cycles).

The accelerator has 16 multipliers, one per tap, arranged as a pipeline of 17 stages (16 tap stages and an output register). A new sample enters every cycle. Once the pipeline is full, one finished result leaves every cycle. N samples through D stages take N + D − 1 cycles: 64 + 17 − 1 = 80. The work is the same 1,024 MACs; they just happen side by side. How SIMD lanes and extra cores do the same on a general CPU is on parallel hardware.

The driver: memory-mapped registers, DMA and an interrupt

The CPU sees the accelerator as five 32-bit memory-mapped registers at 0x4002_0000. A store to one of these addresses is not a memory write: the interconnect sends it to the accelerator's AXI4-Lite slave port (AMBA buses shows the channels; on-chip interconnects shows how the request finds its slave).

offsetregistermeaning
0x00CTRLbit 0 START (begins a job), bit 1 IE (interrupt when done)
0x04STATUSbit 0 BUSY, bit 1 DONE (write 1 to clear)
0x08SRCaddress of the input samples, here 0x2000_0100
0x0CDSTaddress of the results, here 0x2000_0200
0x10LENnumber of samples

One call of fir_accel (Demo: offload to the accelerator, 300 cycles) goes like this:

  1. Setup: the CPU writes SRC, DST, LEN and then CTRL = START | IE. Each AXI4-Lite access takes 5 cycles: 20 cycles.
  2. The CPU sleeps in __WFI(). It is free: it could run other code instead.
  3. DMA in: the accelerator's own AXI4 master port reads x[ ] in 4 bursts of 16 words. Each burst waits 9 cycles for the first word, then gets one word per cycle: 4 × 25 = 100 cycles. The CPU does not copy anything. (How a network card does DMA with descriptor rings and an IOMMU is on DMA.)
  4. Compute: 80 cycles in the pipeline.
  5. DMA out: 4 write bursts to y[ ], 16 beats + 1 cycle for the write response each: 68 cycles.
  6. Interrupt: STATUS gets DONE and the IRQ line goes high. Exception entry takes 12 cycles, the handler reads STATUS (5) and writes 1 to clear DONE (5), and the exception return takes 10: 32 cycles. How the NVIC takes the exception is on Cortex-M exceptions; how it competes with other interrupts is on interrupt priorities and nesting.
Time runs down. The CPU writes SRC, DST, LEN and CTRL (20 cycles), then sleeps in WFI. The accelerator reads x[] with 4 DMA bursts (100 cycles), computes in the pipeline (80 cycles), writes y[] with 4 bursts (68 cycles), and raises the IRQ. The CPU's interrupt handler reads STATUS and clears DONE (32 cycles). Total 300 cycles, of which the CPU is busy 52
Of the 300 cycles of one call only 80 are filtering; 220 are setup, transfer and the interrupt, and the CPU itself is busy for just 52.

Clearing DONE matters: as long as it is set, the interrupt line stays high and the handler would be entered again. Before starting the DMA the driver must also make sure the samples are really in SRAM, not only in a data cache (the Cortex-M4 here has none; see DMA for cache coherency).

Amdahl's law for the whole program

The kernel went from 2,048 to 300 cycles, 6.83× faster. The program did not. The other 178 cycles per pass are unchanged, so a pass now takes 178 + 300 = 478 cycles: 2,226 / 478 = 4.66× (Demo: Amdahl, the program is 4.66× faster, capped at 12.5×). That is Amdahl's law: if a part p of the time is sped up s times, the whole speeds up 1 / ((1 − p) + p / s) = 1 / (0.080 + 0.920 / 6.83) = 4.66×.

Even an accelerator that took no time at all would leave the 178 cycles: 2,226 / 178 = 12.5× is the limit, 1 / (1 − p). To go further you must also speed up the 8% left in software. Speedup curves for many processors, and Gustafson's other view, are on Amdahl and Gustafson.

When the accelerator is not worth it

Every call pays a fixed price: 20 cycles of setup and 32 for the interrupt, 9 cycles of latency per read burst, and a pipeline fill. What matters is how much work there is for each word moved.

kernelsoftwareacceleratorkernelwhole passprogram
64 taps, 64 samples8,19220 + 100 + 128 + 68 + 32 = 34823.5× ✓8,370 → 52615.9× ✓
16 taps, 64 samples2,04820 + 100 + 80 + 68 + 32 = 3006.83× ✓2,226 → 4784.66× ✓
2 taps, 64 samples25620 + 100 + 66 + 68 + 32 = 2860.90× ✗434 → 4640.94× ✗
16 taps, one call per sample2,04864 × (20 + 10 + 17 + 2 + 32) = 5,1840.40× ✗2,226 → 5,3620.42× ✗
  • Demo: 64 taps, more work per byte moved, 15.9×: four times the arithmetic, the same data moved. The transfer cost is the same 168 cycles, so the gain grows.
  • Demo: a 2-tap filter, transfer cost dominates ✗: the CPU needs only 256 cycles, while moving the data in and out alone takes 168. The accelerator loses.
  • Demo: one call per sample, setup and interrupt dominate ✗: the same 16-tap work, but the driver is called 64 times with LEN = 1. Each call pays 52 cycles of setup and interrupt for 32 cycles of software work. Batch the data, then call.
Pairs of bars, software against accelerator, for each kernel: 64 taps 8,192 against 348, 16 taps 2,048 against 300, 2 taps 256 against 286, and 16 taps one call per sample 2,048 against 5,184. The accelerator bars are split into setup, DMA, compute and interrupt; for the last two kernels the overhead makes the accelerator bar the longer one
The accelerator wins when there is a lot of arithmetic for each word moved; with little work per word, or one call per sample, the fixed overhead makes it slower.

Two more ways to win back the overhead: while the accelerator works on one block, the CPU can run control_update for the previous one (double buffering), and the DMA can feed samples straight into the pipeline instead of filling a buffer first, so transfer and compute overlap.

What the page leaves out

The cycle costs are round example values, not the numbers of a real chip: real AXI latencies depend on the interconnect, bridges and memory, and the accelerator often runs on its own clock. The page does not overlap transfer and compute, does not show double buffering, cache maintenance on cores that have a data cache, the HLS or RTL design of the accelerator itself, fixed-point rounding and saturation, the FIR delay line between blocks (the first 15 samples need the end of the previous block), the CMSIS-DSP library and the M4's own SIMD MAC instructions (SMLAD), or the area and power the accelerator costs.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirqOpen the Operating Systems mind mapOperating Systemshardware interrupts the CPUInterrupts and devicesInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirq
This page on the Operating Systems mind map · 78 pages in 9 branches · open it to see the whole map and test yourself in recall mode.