Ordering and Deadlock in an Interconnect

Two replies are ready and the manager is ready, yet nothing moves, for ever. A rule about order and one small store have locked each other. This page shows how to see that coming.

You Will Be Able To

  • State which replies must come back in the order of their requests, and which need not.
  • Draw a "waits for" graph, and use it to tell a deadlock from a slow wait.
  • Choose a way to keep the loop from forming, and say what it costs.
Lesson
S4.9
Time
26 min
Runs on
Your browser
Check
5 questions

A handler reads a switch, and then a word of memory. Both devices have their answer ready. The handler is ready to take it. A thousand clock cycles later, nothing has moved.

Before You Start

This is a senior page. Start with outstanding transactions. A request stays outstanding until its reply returns. The rule of order used here is taught there in one ID, one order.

Five questions this page answers
Question
1Which replies must reach a manager in the order of its requests?
2What is a "waits for" graph, and what does a closed loop in it mean?
3How do you tell a deadlock from a device that is merely slow?
4When must a loop be prevented: before it forms, or after?
5Why may a sender never wait for the receiver before it offers its data?

The Idea: Order, a Finite Store, and a Loop

A manager on AXI may have two reads outstanding. If both carry the same ID, their replies must reach it in the order of the requests. Call the first read the older one, and the second the younger one. The promise is useful. The manager needs no table to match replies to requests.

The promise has a price. Subordinates differ in speed, so the younger reply may be ready first. It must then wait somewhere until the older reply has gone past.

"Somewhere" is a store inside the interconnect, and every store is finite. Suppose the younger reply takes the last free place, and the older reply needs that same place to get home. Now each waits for the other. No block is broken and no device is slow. Nothing will ever move again. That is a deadlock.

The tool for seeing a deadlock is a picture with one kind of arrow. The arrow reads "waits for". Follow the arrows. If they bring you back to where you started, no amount of time will help.

New Words

New words on this page
WordMeaning
DeadlockA state in which every party waits for another, in a closed loop. It never ends by itself.
Waits-for graphA picture with a box for each event, and an arrow from each event to the event it waits for.
Return slotA register on the way back. It holds one reply until the manager has taken it.
AdmissionThe decision to let a request into the interconnect, or to hold it at the entrance.

The lab on this page prints five names at each clock edge. Here is what each one means.

Lab names
In the labMeaning when it is 1
olderthe older reply is ready at its subordinate, GPIO, and has not entered the slot
youngerthe younger reply is ready at its subordinate, SRAM, and has not entered the slot
fullthe return slot holds a reply
youngthe reply in the slot is the younger one
deliverthe slot hands its reply to the manager at this edge

One Picture: Who Waits for What

Who Waits for WhatStep through the picture. Every arrow reads "waits for".
  • Memory
  • Peripheral
  • Bus
Who Waits for WhatStep through the picture. Every arrow reads "waits for". component Younger reply isdelivered the reply from SRAM,which sits in thereturn slot component Older reply isdelivered the reply from GPIO,which is still at itssubordinate component Older replyenters the slot every reply must passthrough the returnslot component The slot becomesfree the slot holds onereply at a time waits for: same ID, sosame orderwaits for: the onlyway homewaits for: roomwaits for: onlydelivery empties it
  1. component Younger reply is deliveredthe reply from SRAM, which sits in the return slot
    • waits for: same ID, so same order → Older reply is delivered
  2. component Older reply is deliveredthe reply from GPIO, which is still at its subordinate
    • waits for: the only way home → Older reply enters the slot
  3. component Older reply enters the slotevery reply must pass through the return slot
    • waits for: room → The slot becomes free
  4. component The slot becomes freethe slot holds one reply at a time
    • waits for: only delivery empties it → Younger reply is delivered
  1. The rule of order

    Both reads carry the same ID. The younger reply may not reach the manager before the older one. So its delivery waits for the delivery of the older reply.

  2. The only way home

    The older reply is ready at its subordinate. To reach the manager it must first enter the return slot.

  3. Room

    The slot holds one reply, and it is full. The older reply can enter only when the slot is free.

  4. The loop closes

    The slot is emptied only by delivering what it holds. It holds the younger reply. You are back at the first box.

  5. Cut one arrow

    Give the two reads different IDs. The first arrow is gone, because no order is owed. The younger reply leaves, the slot is free, and the older reply follows.

Read the diagram as text
  • Younger reply is delivered: the reply from SRAM, which sits in the return slot
  • Older reply is delivered: the reply from GPIO, which is still at its subordinate
  • Older reply enters the slot: every reply must pass through the return slot
  • The slot becomes free: the slot holds one reply at a time
  • Younger reply is delivered → Older reply is delivered: waits for: same ID, so same order
  • Older reply is delivered → Older reply enters the slot: waits for: the only way home
  • Older reply enters the slot → The slot becomes free: waits for: room
  • The slot becomes free → Younger reply is delivered: waits for: only delivery empties it

A "waits for" graph of the lab on this page, at the moment it stops. Four arrows, and none of them leaves the picture.

Every box in this picture is an event that has not happened yet. Every arrow names the one thing it waits for. A waits-for graph is nothing more than that.

The Smallest Example, Worked by Hand: Two Tables of Waiting

Write the picture as a table. For each event that has not happened, name the one thing it waits for.

A deadlock
EventWaits for
The younger reply is deliveredthe older reply being delivered
The older reply is deliveredthe slot becoming free
The slot becomes freethe younger reply being delivered

Start in any row. Look up its right-hand entry in the left-hand column, and go on. After three steps you are in the row where you began. That is a closed loop.

Now a different case. The slot is empty, and GPIO has simply not finished its read.

A slow wait
EventWaits for
The older reply is deliveredGPIO finishing its read
GPIO finishes its readtwo more clock cycles: nothing else

This chain ends at the clock, which ticks by itself. On the wires the two cases look the same for a while: a manager that waits. The tables are what tell them apart.

The Rules

  1. Rule 1: Replies with one ID reach a manager in the order of its requests.

    This holds even when the requests went to different subordinates. The interconnect must see to it.

  2. Rule 2: No order is owed between different IDs, or between a read and a write.

    A manager that needs such an order waits for the first reply before it sends the second request.

  3. Rule 3: A chain of waiting ends at something that happens by itself, or it comes back to its start.

    If it ends at a clock or a running counter, the wait is slow. If it comes back, it is a deadlock.

  4. Rule 4: A loop must be prevented before it forms.

    Once every place is taken, nobody can move to make room. So the decision falls at admission.

  5. Rule 5: A sender offers its data without waiting for the receiver.

    On AXI, VALID may not wait for READY. A receiver may wait for VALID. If both waited, two arrows would close the smallest loop there is.

Sources for These Rules

Arm AMBA AXI Protocol Specification, document IHI 0022, issue L. Order within one ID, and none between different IDs: section A5.1. No order between different managers, or between reads and writes, and the duty of a manager that needs one: section A5.3. Replies with one ID from different subordinates in request order: sections A5.5 and A5.6. VALID may not wait for READY: sections A2.3 and A2.3.2.

Rules 3 and 4 are engineering practice. The specification fixes the order, and leaves the stores of an interconnect to its designer.

The one return slot of the lab is deliberately too small. It is a teaching design, not a crossbar anybody ships.

Now Run It

The lab at the end of this page has one manager, which is test code. It sends two reads with the same ID, 2. The first goes to GPIO, which needs two cycles. The second goes to SRAM, which answers at once. One return slot leads back to the manager, which always takes what it is offered.

Printed by the lab. The two "issue" lines are the requests. Each later row is what one rising clock edge sees.
issue addr=10001004 id=2
issue addr=80000000 id=2
e0 older=0 younger=1 full=0 young=0 deliver=0
e1 older=1 younger=0 full=1 young=1 deliver=0
e2 older=1 younger=0 full=1 young=1 deliver=0
e3 older=1 younger=0 full=1 young=1 deliver=0
e4 older=1 younger=0 full=1 young=1 deliver=0
e5 older=1 younger=0 full=1 young=1 deliver=0
The Younger Reply Takes the Only Slot, RecordedStep through the clock edges of the recorded run.
012345clkolderyoungerfullyoungdeliver
Scroll horizontally ↔
  1. Cycle 0

    The younger reply is ready first. The slot is empty, and takes it.

  2. Cycle 1

    The older reply is ready now. The slot is full, and it holds the younger reply. Nothing is delivered.

  3. Cycle 5

    Four edges later every wire has the same value. No wire will ever change again.

Recorded from the lab. Each column is what one rising clock edge sees. The names are in the table of lab names.

Rule 1 at work: e1 has full=1 and young=1. The slot holds the younger reply, and deliver stays at 0. Handing it over would break the order.

At the same edge older=1. The older reply is ready, and has nowhere to go. That row is the picture above, with every box still waiting.

Rule 3 at work: the rows from e2 to e5 repeat e1. Five equal rows prove nothing, for a slow device gives equal rows too. The proof is the loop. Every arrow starts and ends inside it.

Now give the second read the ID 3. The lab has a constant for it, YOUNG_ID.

The same lab with YOUNG_ID at 3: the edges e0 to e3.
e0 older=0 younger=1 full=0 young=0 deliver=0
e1 older=1 younger=0 full=1 young=1 deliver=1
e2 older=1 younger=0 full=0 young=1 deliver=0
e3 older=0 younger=0 full=1 young=0 deliver=1

Rule 2 at work: at e1 deliver is 1, and the younger reply leaves first. At e2 the slot is empty. At e3 it holds the older reply and delivers it. The manager got its replies out of order, and that is allowed.

This lab leaves things out. It has one slot and two reads, each a single transfer. A real interconnect has deeper stores and many managers. The loop then needs more traffic to form, and is harder to find.

Why It Is Built This Way

Rule 4 says to prevent the loop at admission. There are three usual ways, and one that only looks like a way.

Way outWhat it doesWhat it costs
One subordinate at a time for each IDA request is held at the entrance while an earlier one with its ID is outstanding elsewhere.Less overlap for that ID.
A reserved place for each replyA request is let in only when a place for its reply is set aside.A place and a tag for every outstanding request.
Different IDs, chosen by the managerNo order is owed, so the early reply simply leaves.The manager must match replies to requests by itself.
A bigger store with no reservationThe loop needs more early replies to form.It only moves the problem. Enough traffic fills any store.

The first three cut an arrow of the graph. The fourth leaves every arrow in place and makes the loop rarer. Rare loops are the ones found by customers.

The Trap: Two Blocks That Each Pass Their Tests

Two bridges face each other across a link. Each has one buffer. It keeps an outgoing request there until the other side accepts it. It needs the same buffer to take an incoming request.

Each bridge passes every test of its own. Then both sides send a request in the same cycle. Each bridge now holds its buffer for the other side, and waits for a free buffer on the other side.

Nobody who looks at one bridge can see this. The loop crosses the boundary between two teams. When you review a design, draw the waits-for graph across every boundary, not inside one block.

At Scale

This page1 store, 2 replies
A large chiphundreds of stores between dozens of managers

This section looks ahead. The lab had one slot and two replies. A large chip has hundreds of stores, owned by different teams, on different clocks and different supplies.

  1. Record who owns a reply

    Capture the manager, the direction, the original ID and the position in the order. An ID alone can name several requests that have nothing to do with each other.

  2. At every full store, name the release

    For the reply at the head of each full queue, write down the event that frees it. Follow that event into the next block.

  3. The rule of order in full

    The Protocols track gives it three lessons: one ID, different IDs and reads against writes.

  4. A device that never answers

    That is not a deadlock, and no graph cures it. It needs a limit on the wait, and a plan for the reply that comes too late. That is the next lesson.

Try It

Run the lab with both IDs at 2. Find the first edge at which the older reply is ready and cannot enter the slot.

Change YOUNG_ID to 3. Predict which reply leaves at e1, then run and compare the row of e3 with the first run.

Set YOUNG_ID back to 2. For each of the first three ways out in the table above, say which arrow of the picture it cuts.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: issue addr=10001004 id=2 issue addr=80000000 id=2 e0 older=0 younger=1 full=0 young=0 deliver=0 e1 older=1 younger=0 full=1 young=1 deliver=0 e2 older=1 younger=0 full=1 young=1 deliver=0 e3 older=1 younger=0 full=1 young=1 deliver=0 e4 older=1 younger=0 full=1 young=1 deliver=0 e5 older=1 younger=0 full=1 young=1 deliver=0
Read included file: return_slot.sv
// A deliberately undersized return path. Each input is one completed read.
// The older target retains its reply until take_old. The younger target
// retains its reply until take_young. The manager always accepts eligible data.
module return_slot(input logic clk, reset, old_valid, young_valid, same_id,
                   output logic take_old, take_young, occupied, young, old_done,
                   output logic deliver);
  assign take_old   = !occupied && old_valid;
  assign take_young = !occupied && !old_valid && young_valid;
  assign deliver    = occupied && (!young || !same_id || old_done);

  always_ff @(posedge clk) begin
    if (reset) begin
      occupied <= 0;
      young <= 0;
      old_done <= 0;
    end else if (deliver) begin
      occupied <= 0;
      if (!young) old_done <= 1;
    end else if (take_old || take_young) begin
      occupied <= 1;
      young <= take_young;
    end
  end
endmodule

// Two single-beat reads from one manager: GPIO through the peripheral bridge,
// then SRAM. Each target accepts one request and retains its reply until taken.
// The asymmetric target latencies are an experiment setting.
module two_target_reads(input logic clk, reset, request,
                        input logic [31:0] address, input logic [1:0] id,
                        output wire accepted, output logic old_valid, young_valid,
                        output wire occupied, young, deliver);
  localparam logic [31:0] GPIO_INPUT = 32'h10001004, SRAM_BASE = 32'h80000000;
  wire gpio_request = request && address == GPIO_INPUT;
  wire sram_request = request && address == SRAM_BASE;
  wire take_old, take_young, old_done;
  logic [1:0] gpio_id, sram_id, gpio_delay;
  assign accepted = !reset && (gpio_request || sram_request);
  return_slot path(clk,reset,old_valid,young_valid,gpio_id==sram_id,
                   take_old,take_young,occupied,young,old_done,deliver);

  always_ff @(posedge clk) begin
    if (reset) begin
      gpio_delay <= 0;
      gpio_id <= 0;
      sram_id <= 0;
      old_valid <= 0;
      young_valid <= 0;
    end else begin
      if (gpio_request) begin
        gpio_id <= id;
        gpio_delay <= 2;
      end else if (gpio_delay != 0) begin
        gpio_delay <= gpio_delay - 1'b1;
        if (gpio_delay == 1) old_valid <= 1;
      end
      if (sram_request) begin
        sram_id <= id;
        young_valid <= 1;
      end
      if (take_old) old_valid <= 0;
      if (take_young) young_valid <= 0;
    end
  end
endmodule

Check Yourself

Predict the outputThe second read now carries ID 3 in place of 2. Which row does the lab print for e1?
module tb;
  bit clk, reset=1, request;
  logic [31:0] address;
  logic [1:0] id;
  parameter OLD_ID=2, YOUNG_ID=3;
  wire accepted, old_valid, young_valid, occupied, young, deliver;
  two_target_reads fabric(.*);
  always #5 clk = !clk;
  task automatic read_word(input logic [31:0] addr,input logic [1:0] tag);
    request=1; address=addr; id=tag;
    @(posedge clk);
    if (!accepted) $fatal(1,"Unmapped request");
    $display("issue addr=%08h id=%0d",address,id);
    @(negedge clk);
    request=0;
  endtask
  initial begin
    @(negedge clk); reset=0;
    read_word(32'h10001004, OLD_ID);
    read_word(32'h80000000, YOUNG_ID);
    for (int e=0; e<6; e++) begin
      @(posedge clk);
      $display("e%0d older=%b younger=%b full=%b young=%b deliver=%b",e,
               old_valid,young_valid,occupied,young,deliver);
      @(negedge clk);
    end
    $finish;
  end
endmodule
Read included file: return_slot.sv
// A deliberately undersized return path. Each input is one completed read.
// The older target retains its reply until take_old. The younger target
// retains its reply until take_young. The manager always accepts eligible data.
module return_slot(input logic clk, reset, old_valid, young_valid, same_id,
                   output logic take_old, take_young, occupied, young, old_done,
                   output logic deliver);
  assign take_old   = !occupied && old_valid;
  assign take_young = !occupied && !old_valid && young_valid;
  assign deliver    = occupied && (!young || !same_id || old_done);

  always_ff @(posedge clk) begin
    if (reset) begin
      occupied <= 0;
      young <= 0;
      old_done <= 0;
    end else if (deliver) begin
      occupied <= 0;
      if (!young) old_done <= 1;
    end else if (take_old || take_young) begin
      occupied <= 1;
      young <= take_young;
    end
  end
endmodule

// Two single-beat reads from one manager: GPIO through the peripheral bridge,
// then SRAM. Each target accepts one request and retains its reply until taken.
// The asymmetric target latencies are an experiment setting.
module two_target_reads(input logic clk, reset, request,
                        input logic [31:0] address, input logic [1:0] id,
                        output wire accepted, output logic old_valid, young_valid,
                        output wire occupied, young, deliver);
  localparam logic [31:0] GPIO_INPUT = 32'h10001004, SRAM_BASE = 32'h80000000;
  wire gpio_request = request && address == GPIO_INPUT;
  wire sram_request = request && address == SRAM_BASE;
  wire take_old, take_young, old_done;
  logic [1:0] gpio_id, sram_id, gpio_delay;
  assign accepted = !reset && (gpio_request || sram_request);
  return_slot path(clk,reset,old_valid,young_valid,gpio_id==sram_id,
                   take_old,take_young,occupied,young,old_done,deliver);

  always_ff @(posedge clk) begin
    if (reset) begin
      gpio_delay <= 0;
      gpio_id <= 0;
      sram_id <= 0;
      old_valid <= 0;
      young_valid <= 0;
    end else begin
      if (gpio_request) begin
        gpio_id <= id;
        gpio_delay <= 2;
      end else if (gpio_delay != 0) begin
        gpio_delay <= gpio_delay - 1'b1;
        if (gpio_delay == 1) old_valid <= 1;
      end
      if (sram_request) begin
        sram_id <= id;
        young_valid <= 1;
      end
      if (take_old) old_valid <= 0;
      if (take_young) young_valid <= 0;
    end
  end
endmodule
ConceptWhich two replies must reach a manager in the order of their requests?
ConceptWhat shows a deadlock in a waits-for graph?
ConceptA capture shows six equal rows with a reply ready and not delivered. What does that prove?
ConceptWhen must a deadlock of this kind be prevented?

Common Mistakes

Wrong Take "the subordinate has its answer" for "the manager has its answer".

A reply can be ready at its subordinate and still have no way home.

Fix Watch delivery at the manager, not readiness at the subordinate.

Wrong Call a wait a deadlock because many cycles passed.

A slow device gives the same rows.

Fix Follow the chain of "waits for". Only a closed loop is a deadlock.

Wrong Cure a deadlock with a bigger store.

More traffic fills it, and the same loop forms.

Fix Cut an arrow: hold the request at admission, reserve the place, or use another ID.

Remember

  • Replies with one ID reach a manager in the order of its requests; different IDs, and reads against writes, owe no order.
  • A waits-for graph has an arrow from each event to the one event it waits for.
  • A chain that ends at a clock is a slow wait; a chain that comes back to its start is a deadlock.
  • A loop must be prevented at admission: one subordinate for each ID, a reserved place, or another ID.
  • A sender never waits for the receiver before it offers its data.

Practice

Report a problem with this page