Many Managers: Arbitration and the Bus Matrix

A chip with one manager never argues over the bus. Add a second, and two blocks want the same memory at the same clock edge. Something must choose, and choose fairly.

You Will Be Able To

  • Say which requests a bus matrix can serve in the same cycle, and which it cannot.
  • Tell a granted request from a served one.
  • Compare fixed priority with round robin, and explain starvation.
Lesson
S4.4
Time
26 min
Runs on
Your browser
Check
5 questions

A program reads a word from memory in two cycles all day. Then a second block starts copying data, and the same read sometimes takes four. Nothing is faulty. The read had to wait its turn.

Before You Start

You need the manager, the subordinate and the wait state of S0.3. You need the decoder of Address Decode and Aliases, and the way back from One Manager, Many Subordinates.

Five questions this page answers
Question
1Why does a second manager make an arbiter necessary?
2What can two managers do on a bus matrix that they cannot do on one shared bus?
3What is the difference between a request that is granted and one that is served?
4How do fixed priority and round robin choose the winner of a tie?
5What is starvation, and which rule prevents it?

The Idea: Where Requests Meet, Something Must Choose

Until now, one manager started every transfer. Real chips have more. The usual second one is a DMA block, for direct memory access. It copies data between memory and a device by itself, so the processor need not move every word.

Two managers can ask for the same thing in the same cycle. A subordinate serves one transfer at a time, so one request must wait. Choosing is called arbitration, and the block that chooses is an arbiter.

The first question is where to choose. A shared bus has one set of wires for everybody. One arbiter gives the whole bus to one manager, and the other waits even when it wants a different block. A bus matrix gives each manager its own path to every destination. Requests then meet only at a destination that both want.

The second question is whom to choose. If the same manager always wins, the other may never be served. That is starvation, and a fair rule prevents it.

New Words

New words on this page
WordMeaning
DMADirect memory access: a block that copies data by itself. It is a manager, like the processor.
ArbiterThe block that chooses which request goes first. What it does is called arbitration.
GrantThe answer of an arbiter to one manager: you may go.
Shared busOne set of bus wires for all managers. One manager uses it in a cycle.
Bus matrixA path from every manager to every destination, with an arbiter at each destination.
TieTwo requests for the same destination in the same cycle.
Fixed priorityA rule for ties: the same manager always wins.
Round robinA rule for ties: the manager that was served last waits.
StarvationA request that is never served, because another manager always wins.

One Picture: Two Managers, Two Destinations

Two Managers, Two DestinationsStep through the picture. It starts with one manager, adds a second, and then lets both ask for the same memory.
  • Processor
  • Peripheral
  • Bus
  • Memory
Two Managers, Two DestinationsStep through the picture. It starts with one manager, adds a second, and then lets both ask for the same memory. component Processor the first manager component DMA a second manager: itcopies data by itself component Arbiter at ROM chooses among therequests for ROM component Arbiter at SRAM chooses among therequests for SRAM component Boot ROM serves one transferin a cycle component SRAM serves one transferin a cycle requestrequestrequestrequestthe winnerthe winner
  1. component Processorthe first manager
    • request → Arbiter at ROM
    • request → Arbiter at SRAM
  2. component Arbiter at ROMchooses among the requests for ROM
    • the winner → Boot ROM
  3. component Arbiter at SRAMchooses among the requests for SRAM
    • the winner → SRAM
  4. component Boot ROMserves one transfer in a cycle
  5. component SRAMserves one transfer in a cycle
  6. component DMAa second manager: it copies data by itself
    • request → Arbiter at ROM
    • request → Arbiter at SRAM
  1. One manager

    With the processor alone, no choice is ever needed. Each destination has at most one request.

  2. A second manager

    DMA starts transfers of its own. Now two requests can arrive in the same cycle.

  3. Different destinations

    The processor reads ROM while DMA reads SRAM. Each request has a path of its own, so both are served in the same cycle.

  4. The same destination

    Both ask for SRAM. SRAM can serve one of them. Its arbiter grants one request and makes the other wait.

  5. The next cycle

    The request that lost is still there. The arbiter now has to be fair to it, or it may wait for ever.

Read the diagram as text
  • Processor: the first manager
  • DMA: a second manager: it copies data by itself
  • Arbiter at ROM: chooses among the requests for ROM
  • Arbiter at SRAM: chooses among the requests for SRAM
  • Boot ROM: serves one transfer in a cycle
  • SRAM: serves one transfer in a cycle
  • Processor → Arbiter at ROM: request
  • Processor → Arbiter at SRAM: request
  • DMA → Arbiter at ROM: request
  • DMA → Arbiter at SRAM: request
  • Arbiter at ROM → Boot ROM: the winner
  • Arbiter at SRAM → SRAM: the winner

This arrangement, with a path from every manager to every destination, is a bus matrix. The way back is not drawn.

A destination here is a subordinate seen from the matrix. Each one keeps the single port it always had. The matrix and its arbiters sit in front of that port.

The Smallest Example, Worked by Hand: Four Requests

The processor wants ROM and then SRAM. DMA wants SRAM twice. Every destination is ready, and every transfer takes one cycle. Count the cycles on a shared bus, and then on a bus matrix.

The same four requests, two ways
Cycle 1Cycle 2Cycle 3Cycle 4
Shared busprocessor to ROMDMA to SRAMprocessor to SRAMDMA to SRAM
Bus matrixprocessor to ROM, and DMA to SRAMprocessor to SRAMDMA to SRAM

The shared bus needs four cycles, one for each transfer. The matrix needs three. In cycle 1 the two requests go to different destinations, so both are served.

Cycle 2 on the matrix is a tie at SRAM. Suppose it goes on: both managers ask for SRAM in every cycle. Who is served?

Six ties in a row at SRAM
RuleTie 1Tie 2Tie 3Tie 4Tie 5Tie 6
Fixed priority, processor firstprocessorprocessorprocessorprocessorprocessorprocessor
Round robinprocessorDMAprocessorDMAprocessorDMA

Under fixed priority DMA is never served while the processor keeps asking. Under round robin it waits one turn at most.

The Rules

  1. Rule 1: A destination serves one transfer in a cycle.

    Two managers are served in the same cycle only when they want different destinations, and each has a path of its own.

  2. Rule 2: Arbitrate where the requests meet.

    In a bus matrix every destination has its own arbiter. It sees only the requests meant for that destination.

  3. Rule 3: Granted is not served.

    A grant says "you may go". The transfer happens only if the destination is also ready. Count progress by transfers served.

  4. Rule 4: Under fixed priority, one manager wins every tie.

    It never waits for the other. The other waits for as long as the first keeps asking, and can starve.

  5. Rule 5: Under round robin, the manager served last loses the next tie.

    A turn is used up only by a transfer that was served. With two managers, nobody loses two ties in a row.

Sources for These Rules

Arm AMBA AHB Protocol Specification, document IHI 0033, issue C, section 4.1. It says that a system with several managers needs an interconnect that arbitrates and routes. It leaves the arbitration rule and the structure to the designer.

The older shared AHB bus had one central arbiter, with a request wire and a grant wire for each manager. Arm AMBA Specification, revision 2.0, document IHI 0011, issue A, section 3.11.

Fixed priority and round robin are common practice, not rules of AHB. The arbiter in the lab is our own.

Now Run It

The lab at the end of this page has two managers, ROM and SRAM, and an arbiter at each destination. The managers are test code. Each has a list of requests and asks for one at a time, until it is served. In this run the tie rule is round robin, and SRAM is busy for one cycle. The lab calls the processor cpu and the DMA block dma.

Printed by the lab at each rising clock edge. The last line counts the transfers served, and the longest run of edges that each manager waited.
e0 cpu wants nothing, dma wants nothing | granted: nobody | served: nobody
e1 cpu wants ROM, dma wants SRAM | granted: cpu and dma | served: cpu and dma
e2 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu
e3 cpu wants SRAM, dma wants SRAM | granted: dma | served: nobody
e4 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e5 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu
e6 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma
e7 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu
e8 cpu wants nothing, dma wants SRAM | granted: dma | served: dma
e9 cpu wants nothing, dma wants nothing | granted: nobody | served: nobody
cpu: served 4, longest wait 2 | dma: served 4, longest wait 2

Rule 1 at work: e1 serves both managers, one at ROM and one at SRAM. Rule 3 at work: at e3 DMA is granted and nobody is served, because SRAM is busy. At e4 the grant is still DMA's. It did not move while nothing was served.

Rule 5 at work: from e4 to e7 the served manager alternates: DMA, processor, DMA, processor. Neither waits more than two edges, and one of those two was SRAM's own busy cycle.

Now change the two constants at the top of the lab, one at a time, and compare the last lines.

Three runs of the same requests
RunLongest wait, processorLongest wait, DMALast edge that serves a transfer
Bus matrix, round robin22e8
Bus matrix, fixed priority14e8
Shared bus, round robin22e9

Recorded from the lab with ROTATE and MATRIX changed. Waits are counted in clock edges.

Rule 4 at work: fixed priority cuts the processor's longest wait to one edge, SRAM's busy cycle. DMA pays for it with a wait of four. On the shared bus the same work ends one edge later, because the first two requests could not go together.

This lab leaves things out. Its managers hand over a request and get a "served" back, with no address phase and no data phase. Every transfer is a single one. The next lesson adds transfers that must stay together.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A bus matrixOne shared busTransfers to different destinations do not wait for each other. It costs an arbiter and a multiplexer at every destination, and many more wires.
Round robinFixed priorityEvery manager is served within a known number of turns. No manager can be made faster than the others.
Fixed priorityRound robinOne urgent manager never waits for the others. The others can starve.
A turn counts only when a transfer is servedMove the turn at every grantA busy destination does not use up a manager's turn. The arbiter must look at "ready".

Real arbiters often mix the two rules: a few priority levels, and round robin among the managers of one level.

The Trap: Fast on Average, Starved in Fact

A team gives the processor fixed priority at SRAM, since the processor is what the user feels. Every test passes, and the average wait at SRAM is short.

Then one program runs a tight loop that reads SRAM in every cycle. DMA was filling a buffer for a display. It stops, for as long as the loop runs, and the picture freezes. The second recorded run above shows it in small: DMA waits four edges while the processor waits one.

An average hides this. Ask for the longest wait, and ask what bounds it. Under fixed priority the answer is "nothing, unless the winner stops asking".

At Scale

This page2 managers, 2 destinations
A large chipdozens of managers and destinations

This section looks ahead. The lab has two managers and two destinations. ap_soc has three managers, and a large chip has dozens.

  1. Transfers that must stay together

    Some transfers come in runs, and some pairs must not be split. Then the arbiter may not change its grant at every cycle. The next lesson, on transfers that belong together, covers both.

  2. A bound in time

    Round robin bounds the number of turns a manager waits. A bound in clock cycles also needs a limit on how long one turn can last.

  3. The circuit

    Another track of this site builds both arbiters gate by gate, in Round-robin and priority arbiters. The Protocols track shows the manager's side on AHB, in Many managers.

Try It

Run the lab. For each row, say who is granted and who is served before you read the right-hand side.

Set ROTATE to 0. Predict the last line, then run.

Set ROTATE back to 1 and MATRIX to 0. Find the first row that differs from the first run, and say why.

Give DMA a ROM request in place of its first SRAM request. Predict what e1 shows.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 cpu wants nothing, dma wants nothing | granted: nobody | served: nobody e1 cpu wants ROM, dma wants SRAM | granted: cpu and dma | served: cpu and dma e2 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu e3 cpu wants SRAM, dma wants SRAM | granted: dma | served: nobody e4 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma e5 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu e6 cpu wants SRAM, dma wants SRAM | granted: dma | served: dma e7 cpu wants SRAM, dma wants SRAM | granted: cpu | served: cpu e8 cpu wants nothing, dma wants SRAM | granted: dma | served: dma e9 cpu wants nothing, dma wants nothing | granted: nobody | served: nobody cpu: served 4, longest wait 2 | dma: served 4, longest wait 2

Check Yourself

Predict the outputThe tie rule is fixed priority, with the processor first. The processor asks for SRAM five times in a row, and DMA asks for it twice, from the same edge. SRAM is always ready. What is the last line the lab prints?
// Two managers and two destinations. Each manager works through a list of requests, one at a time.
// An arbiter chooses when both managers ask for the same thing in the same cycle.
module arbiter #(parameter ROTATE = 1) (
  input  logic clk,
  input  logic req_cpu, req_dma,       // who asks now
  input  logic ready,                  // 1: one transfer can be served in this cycle
  output logic grant_cpu, grant_dma    // who may go
);
  logic last_dma = 1;                  // who was served last. At the start, the cpu wins a tie
  wire  tie       = req_cpu && req_dma;
  wire  cpu_first = !ROTATE || last_dma;       // fixed priority: always the cpu. Round robin: whoever did not go last
  assign grant_cpu = req_cpu && (!tie ||  cpu_first);
  assign grant_dma = req_dma && (!tie || !cpu_first);
  // A turn counts only when it is used: "last" changes when a transfer is really served.
  always @(posedge clk) if (ready && (grant_cpu || grant_dma)) last_dma <= grant_dma;
endmodule

module tb;
  logic clk = 0;
  always #5 clk = ~clk;
  localparam ROTATE = 0;               // 1: round robin. 0: fixed priority, the cpu first
  localparam MATRIX = 1;               // 1: a bus matrix, an arbiter at each destination. 0: one shared bus

  // ---- The managers: test code. Each has a list of requests, and asks for one at a time ----
  localparam NONE = 0, ROM = 1, SRAM = 2;
  logic [1:0] cpu_list [0:9], dma_list [0:9];
  int         cpu_i = 0, dma_i = 0;    // how far each manager has come in its list
  wire [1:0]  cpu_wants = cpu_list[cpu_i], dma_wants = dma_list[dma_i];

  // ---- The destinations ----
  int   edge_no = 0;                   // counts the rising clock edges
  wire  rom_ready = 1;
  wire  ram_ready = (edge_no != -1);    // in this run SRAM is always ready

  // ---- A bus matrix: each destination has its own arbiter, and sees only the requests meant for it ----
  wire rom_cpu, rom_dma, ram_cpu, ram_dma;       // the four grants
  arbiter #(ROTATE) at_rom (clk, cpu_wants == ROM,  dma_wants == ROM,  rom_ready, rom_cpu, rom_dma);
  arbiter #(ROTATE) at_ram (clk, cpu_wants == SRAM, dma_wants == SRAM, ram_ready, ram_cpu, ram_dma);

  // ---- A shared bus: one arbiter for everything. Only one manager may use the bus in a cycle ----
  wire bus_cpu, bus_dma;
  wire [1:0] bus_wants = bus_cpu ? cpu_wants : dma_wants;        // where the manager with the bus wants to go
  wire bus_ready = (bus_wants == SRAM) ? ram_ready : rom_ready;
  arbiter #(ROTATE) at_bus (clk, cpu_wants != NONE, dma_wants != NONE, bus_ready, bus_cpu, bus_dma);

  // Granted: the manager may go. Served: its destination was also ready, so the transfer happened.
  wire cpu_grant  = MATRIX ? (rom_cpu || ram_cpu) : bus_cpu;
  wire dma_grant  = MATRIX ? (rom_dma || ram_dma) : bus_dma;
  wire cpu_served = MATRIX ? ((rom_cpu && rom_ready) || (ram_cpu && ram_ready)) : (bus_cpu && bus_ready);
  wire dma_served = MATRIX ? ((rom_dma && rom_ready) || (ram_dma && ram_ready)) : (bus_dma && bus_ready);

  // Counting and printing. A function turns bits into a name, for printing only.
  int cpu_count = 0, dma_count = 0;        // transfers served
  int cpu_wait = 0, dma_wait = 0;          // edges waited in a row
  int cpu_longest = 0, dma_longest = 0;    // the longest such wait
  function string place(input logic [1:0] w);
    return (w == ROM) ? "ROM" : (w == SRAM) ? "SRAM" : "nothing";
  endfunction
  function string who(input logic c, input logic d);
    return (c && d) ? "cpu and dma" : c ? "cpu" : d ? "dma" : "nobody";
  endfunction
  always @(posedge clk) begin
    $display("e%0d cpu wants %s, dma wants %s | granted: %s | served: %s", edge_no, place(cpu_wants), place(dma_wants),
             who(cpu_grant, dma_grant), who(cpu_served, dma_served));
    // A manager moves on to its next request when this one is served ("<=" takes effect after the edge).
    if ((cpu_served || cpu_wants == NONE) && cpu_i < 9) cpu_i <= cpu_i + 1;
    if ((dma_served || dma_wants == NONE) && dma_i < 9) dma_i <= dma_i + 1;
    if (cpu_served) cpu_count++;
    if (dma_served) dma_count++;
    cpu_wait = (cpu_wants != NONE && !cpu_served) ? cpu_wait + 1 : 0;
    dma_wait = (dma_wants != NONE && !dma_served) ? dma_wait + 1 : 0;
    if (cpu_wait > cpu_longest) cpu_longest = cpu_wait;
    if (dma_wait > dma_longest) dma_longest = dma_wait;
    edge_no <= edge_no + 1;
  end

  initial begin
    for (int i = 0; i < 10; i++) begin cpu_list[i] = NONE; dma_list[i] = NONE; end
    // Entry 0 is the idle cycle that edge 0 sees. The requests start at entry 1.
    cpu_list[1] = SRAM;
    cpu_list[2] = SRAM;
    cpu_list[3] = SRAM;
    cpu_list[4] = SRAM;
    cpu_list[5] = SRAM;
    dma_list[1] = SRAM;
    dma_list[2] = SRAM;
    repeat (10) @(posedge clk);
    #1 $display("cpu: served %0d, longest wait %0d | dma: served %0d, longest wait %0d", cpu_count, cpu_longest, dma_count, dma_longest);
    $finish;                           // $finish ends the simulation
  end
endmodule
ConceptWhy does a second manager make an arbiter necessary?
ConceptThe processor asks for ROM and DMA asks for SRAM in the same cycle, and both are ready. What happens on a bus matrix?
ConceptAt one edge, DMA is granted SRAM, and SRAM is busy. What happened at that edge?
ConceptWhich rule can starve a manager, and when?

Common Mistakes

Wrong Count every granted cycle as progress.

The destination can be busy while a request holds the grant.

Fix Count the cycles in which a transfer was served.

Wrong Use one arbiter for the whole chip.

Requests for different destinations then wait for each other.

Fix Put an arbiter at each destination, where the requests really meet.

Wrong Judge an arbiter by its average wait.

A starved manager can hide behind a good average.

Fix Find the longest wait, and what bounds it.

Remember

  • A second manager means two requests can meet at one subordinate; an arbiter chooses.
  • A bus matrix serves requests for different destinations in the same cycle; a shared bus cannot.
  • A grant is not a transfer: progress is counted in transfers served.
  • Fixed priority never makes the winner wait, and can starve the loser.
  • Round robin makes the manager served last wait, so every manager gets its turn.

Practice

Report a problem with this page