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?
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):
| function | cycles | share |
|---|---|---|
read_adc | 64 | 2.9% |
fir_block | 2,048 | 92.0% |
control_update | 82 | 3.7% |
write_dac | 32 | 1.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).
| offset | register | meaning |
|---|---|---|
| 0x00 | CTRL | bit 0 START (begins a job), bit 1 IE (interrupt when done) |
| 0x04 | STATUS | bit 0 BUSY, bit 1 DONE (write 1 to clear) |
| 0x08 | SRC | address of the input samples, here 0x2000_0100 |
| 0x0C | DST | address of the results, here 0x2000_0200 |
| 0x10 | LEN | number of samples |
One call of fir_accel (Demo: offload to the accelerator, 300 cycles) goes like this:
- Setup: the CPU writes
SRC,DST,LENand thenCTRL = START | IE. Each AXI4-Lite access takes 5 cycles: 20 cycles. - The CPU sleeps in
__WFI(). It is free: it could run other code instead. - 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.) - Compute: 80 cycles in the pipeline.
- DMA out: 4 write bursts to
y[ ], 16 beats + 1 cycle for the write response each: 68 cycles. - Interrupt:
STATUSgetsDONEand the IRQ line goes high. Exception entry takes 12 cycles, the handler readsSTATUS(5) and writes 1 to clearDONE(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.
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.
| kernel | software | accelerator | kernel | whole pass | program |
|---|---|---|---|---|---|
| 64 taps, 64 samples | 8,192 | 20 + 100 + 128 + 68 + 32 = 348 | 23.5× ✓ | 8,370 → 526 | 15.9× ✓ |
| 16 taps, 64 samples | 2,048 | 20 + 100 + 80 + 68 + 32 = 300 | 6.83× ✓ | 2,226 → 478 | 4.66× ✓ |
| 2 taps, 64 samples | 256 | 20 + 100 + 66 + 68 + 32 = 286 | 0.90× ✗ | 434 → 464 | 0.94× ✗ |
| 16 taps, one call per sample | 2,048 | 64 × (20 + 10 + 17 + 2 + 32) = 5,184 | 0.40× ✗ | 2,226 → 5,362 | 0.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.
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.