APB Versions and Compatibility

Connect an APB requester to a completer of another version. Pick each tie-off from the specification's tables, and predict what the missing signals cost.

You Will Be Able To

  • Name the signals APB2, APB3, APB4 and APB5 each add, and the default of each optional one.
  • Choose the tie-off for a missing PSTRB, PPROT or PNSE from the compatibility tables.
  • Predict what a byte write does to a completer without PSTRB.
  • Explain why tying PPROT to its default can open a security hole.
Lesson
P3.11
Time
30 min
Runs on
Your browser
Check
5 questions

A team moved a key store from an old APB3 bridge to a new APB4 one, and kept the old wiring. Its PPROT input stayed tied to 000, the specification's default. But 000 means a Secure access. From then on, every Non-secure write to the Secure key register was accepted.

One Non-Secure Write, Two APB4 CompletersEdges 3 to 6 of a measured run. The requester drives PPROT 010 both times. The first completer has its PPROT tied to 000; the second has it wired.
  • Manager
  • Subordinate
PSEL APSEL BPSLVERRAPB4 requesterNon-secure, PPROT 010Key store APPROT tied to 000Key store BPPROT wired
PSEL A→ Key store APSEL B→ Key store BPSLVERR→ APB4 requesterAPB4 requesterNon-secure, PPROT 010Key store APPROT tied to 000Key store BPPROT wired

PPROT[1] = 1 is Non-secure. pprot_seen is what each completer receives.

0123clkpsel_tiedpsel_wiredpenablepprot010pprot_seen000010preadypslverr
Scroll horizontally ↔
  1. Cycle 0

    Setup of a Non-secure write to the Secure register of key store A. A sees PPROT 000: a Secure access.

  2. Cycle 1

    A completes it with OKAY. The key is overwritten.

  3. Cycle 3

    The same write to key store B, whose PPROT is wired. B sees 010 and refuses with PSLVERR.

A tie-off is a claim about every access that will ever arrive.

Four Versions, One Transfer

Every version keeps the setup and access phases of the one before. Each adds optional signals, and Appendix B gives each optional signal a default for an input nobody drives.

VersionAddsDefault of a missing input
APB2 (AMBA 2)PSEL, PENABLE, PADDR, PWRITE, PWDATA, PRDATA—
APB3PREADY, PSLVERRPREADY 1, PSLVERR 0
APB4PPROT, PSTRBPPROT 000; PSTRB has a table instead
APB5PWAKEUP, user signals, parity check signals, PNSE (issue E)PNSE 0

IHI 0024E Preface and Table B-1. An APB2 completer looks like an APB3 completer that is always ready and never fails.

The example runs one APB4 requester against an APB2 completer, an APB3 completer with one wait state, and two APB4 completers. Each gets the same byte write, an unmapped offset and a Non-secure write to its Secure register SREG.

A Byte Write to the APB3 CompleterEdges 6 to 11 of a measured run on the APB3 completer. The word at 0x04 holds 11111111; the write carries aa in lane 1 with PSTRB 0010.
  • Manager
  • Subordinate
PENABLEPSTRBPRDATAAPB4 requesterbyte write, lane 1APB3 completerno PSTRB input
PENABLE→ APB3 completerPSTRB→ APB3 completerPRDATA→ APB4 requesterAPB4 requesterbyte write, lane 1APB3 completerno PSTRB input

The APB3 completer has no strobe input, so the strobes stop at its edge.

012345clkpselpenablepwritepstrb00100000preadyprdata111111110000aa00
Scroll horizontally ↔
  1. Cycle 0

    Setup of the byte write. PSTRB says only lane 1 is valid.

  2. Cycle 2

    After one wait state the write completes. The completer writes all four lanes.

  3. Cycle 5

    The read back returns 0000aa00: the three 11 bytes are gone.

Compatible, says Table 3-1, but sparse writes are not supported.

The Compatibility Tables

Three tables in the specification decide what a missing signal means. Read each row as requester on the left, completer on the top.

SignalRequester has it, completer does notCompleter has it, requester does not
PSTRBCompatible. Sparse writes are not supported, and the documentation must say so.Compatible. Tie the completer's PSTRB inputs to the requester's PWRITE.
PPROTCompatible. The completer has no protection, so PPROT is ignored.Not compatible, unless one fixed protection value is functionally correct.
PNSENot compatible.Compatible. Tie the completer's PNSE input LOW.

IHI 0024E Tables 3-1, 3-3 and 3-6.

The PPROT row is the opening story. Tying PPROT to 000 does not leave protection out: it claims every access is normal, Secure and data. That is functionally correct only if Secure software is the only thing that can reach the completer.

With RME, the address space comes from PNSE and PPROT[1] together: Secure, Non-secure, Root or Realm. Lesson P3.12 builds on that.

Data Width

PRDATA and PWDATA can each be 8, 16 or 32 bits wide, but both must have the same width. PADDR is always a byte address. A 32-bit register on an 8-bit APB therefore takes four transfers, one byte each.

Two interfaces of different widths do not connect directly. A bridge between them splits or joins the transfers, the way an AXI width converter does.

Asked in interviews: APB versions
  1. What does an APB2 completer drive on PREADY?

    Nothing: APB2 has no PREADY. The requester's input takes the default, 1, so every transfer ends after one access cycle.

  2. An APB4 completer meets an APB3 requester. What about PPROT?

    Not compatible, unless one fixed value is correct for every access. Tying it to 000 makes every access Secure.

  3. Can you mix an 8-bit and a 32-bit APB interface?

    Not directly. Read and write data are the same width on one interface; a bridge must convert.

Try It

APB2 and APB3 have no PSTRB, so the byte write replaces the whole word. APB2 has no PSLVERR, so the unmapped write ends OKAY and its error is lost. Neither has PPROT, so neither can protect SREG.

The wired APB4 completer keeps its neighbours, reports the unmapped offset and refuses the Non-secure write. The tied one does all of that except the last. Change its tie-off to 3'b010 and it refuses every write to SREG, Secure ones included.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: APB2: byte write leaves 0000aa00, unmapped OKAY, Non-secure write to SREG OKAY, SREG = 5a APB3: byte write leaves 0000aa00, unmapped PSLVERR, Non-secure write to SREG OKAY, SREG = 5a APB4: byte write leaves 1111aa11, unmapped PSLVERR, Non-secure write to SREG PSLVERR, SREG = 00 APB4, PPROT tied 000: byte write leaves 1111aa11, unmapped PSLVERR, Non-secure write to SREG OKAY, SREG = 5a
Read included file: apb_ver.sv
`timescale 1ns/1ns
// apb_ver.sv: a small APB completer built as APB2, APB3, APB4 or APB5 (read-only).
// It has 16 bytes of RAM at offset 0x00 to 0x0f and a Secure-only register SREG at 0x10.
// VERSION decides which signals the completer has; its port list keeps them all, so a version that
// lacks a signal simply ignores the input or drives the output to the specification default.
//   APB2  no PREADY (driven 1), no PSLVERR (driven 0), no PSTRB (writes every lane), no PPROT
//   APB3  adds PREADY (WAITS wait states) and PSLVERR (unmapped offsets and refused writes)
//   APB4  adds PSTRB (writes only strobed lanes) and PPROT: SREG refuses a Non-secure write (PPROT[1] = 1)
//   APB5  the same transfer as APB4 here (its additions are optional sideband signals)
// DATA_W is 8, 16 or 32; a transfer moves DATA_W/8 bytes at the aligned address.
// Without PPROT an APB2 or APB3 completer cannot tell Secure from Non-secure, so SREG takes any write.
module apb_ver #(parameter int VERSION = 4, parameter int DATA_W = 32, parameter int WAITS = 0) (
  input  logic                pclk,
  input  logic                presetn,
  input  logic                psel,
  input  logic                penable,
  input  logic                pwrite,
  input  logic [31:0]         paddr,
  input  logic [DATA_W-1:0]   pwdata,
  input  logic [DATA_W/8-1:0] pstrb,
  input  logic [2:0]          pprot,
  output logic                pready,
  output logic [DATA_W-1:0]   prdata,
  output logic                pslverr);
  localparam int B = DATA_W / 8;
  logic [7:0] mem [0:16];                  // bytes 0 to 15 are the RAM, byte 16 is SREG
  int wcnt = 0;
  initial for (int i = 0; i <= 16; i++) mem[i] = 0;
  wire [7:0] off = paddr[7:0];
  wire mapped  = (off < 8'h10) || (off == 8'h10 && B == 1) || (off[7:2] == 6'h04 && B > 1);
  wire refused = (VERSION >= 4) && pwrite && (off >= 8'h10) && pprot[1];
  wire access  = psel && penable;
  assign pready  = (VERSION == 2) ? 1'b1 : (access ? (wcnt >= WAITS) : 1'b0);
  assign pslverr = (VERSION == 2) ? 1'b0 : (access && pready && (!mapped || refused));
  wire [7:0] base = off & ~8'(B - 1);
  for (genvar l = 0; l < B; l++) begin : g_rd
    assign prdata[8*l +: 8] = (base + l <= 16) ? mem[base + l] : 8'h00;
  end
  always @(posedge pclk or negedge presetn) begin
    if (!presetn) wcnt <= 0;
    else begin
      wcnt <= (access && !pready) ? wcnt + 1 : 0;
      if (access && pready && pwrite && mapped && !refused)
        for (int l = 0; l < B; l++)
          if ((VERSION < 4 || pstrb[l]) && base + l <= 16) mem[base + l] <= pwdata[8*l +: 8];
    end
  end
endmodule

Check Yourself

Predict the outputThe APB4 requester writes 11111111 to offset 0x08 of the APB3 completer. Then it writes 00cc0000 with PSTRB 0100 and reads the word. It prints read and the word in hex. What is the line?
`timescale 1ns/1ps
module tb;
  logic pclk = 0, presetn = 0;
  always #5 pclk = ~pclk;                 // rising edges at 5, 15, 25, ...
  logic        penable = 0, pwrite = 0;
  logic [31:0] paddr = 0, pwdata = 0;
  logic [3:0]  pstrb = 0, pselx = 0;
  logic [2:0]  pprot = 0;
  wire  [3:0]  rdy, err;
  wire  [31:0] rd [0:3];
  // One APB4 requester, four completers. Each port list is the integrator's choice.
  apb_ver #(.VERSION(2))            c2  (.pclk(pclk), .presetn(presetn), .psel(pselx[0]), .penable(penable), .pwrite(pwrite), .paddr(paddr),
                                         .pwdata(pwdata), .pstrb(4'b0000), .pprot(3'b000), .pready(rdy[0]), .prdata(rd[0]), .pslverr(err[0]));
  apb_ver #(.VERSION(3), .WAITS(1)) c3  (.pclk(pclk), .presetn(presetn), .psel(pselx[1]), .penable(penable), .pwrite(pwrite), .paddr(paddr),
                                         .pwdata(pwdata), .pstrb(4'b0000), .pprot(3'b000), .pready(rdy[1]), .prdata(rd[1]), .pslverr(err[1]));
  apb_ver #(.VERSION(4))            c4  (.pclk(pclk), .presetn(presetn), .psel(pselx[2]), .penable(penable), .pwrite(pwrite), .paddr(paddr),
                                         .pwdata(pwdata), .pstrb(pstrb), .pprot(pprot), .pready(rdy[2]), .prdata(rd[2]), .pslverr(err[2]));
  apb_ver #(.VERSION(4))            c4t (.pclk(pclk), .presetn(presetn), .psel(pselx[3]), .penable(penable), .pwrite(pwrite), .paddr(paddr),
                                         .pwdata(pwdata), .pstrb(pstrb), .pprot(3'b000), .pready(rdy[3]), .prdata(rd[3]), .pslverr(err[3]));
  int cycle = -1;
  always @(posedge pclk) begin
    cycle++;
    if (0) $display("wave e%0d: psel=%b penable=%b pwrite=%b paddr=%h pstrb=%b pprot=%b pready=%b pslverr=%b", cycle, pselx, penable, pwrite, paddr, pstrb, pprot, rdy, err);
  end
  logic [31:0] rdata;
  bit          rerr;
  // One transfer to completer k: setup, then access until its PREADY.
  task automatic xfer(input int k, input bit wr, input logic [31:0] a, input logic [31:0] d, input logic [3:0] s, input logic [2:0] p);
    paddr = a; pwrite = wr; pwdata = wr ? d : 0; pstrb = wr ? s : 0; pprot = p; pselx = 4'b1 << k; penable = 0;
    @(posedge pclk); #1 penable = 1;
    @(posedge pclk); while (rdy[k] !== 1'b1) @(posedge pclk);
    rdata = rd[k]; rerr = err[k];
    #1 pselx = 0; penable = 0; pwrite = 0; pstrb = 0;
  endtask
  function automatic string name(input int k);
    case (k) 0: return "APB2"; 1: return "APB3"; 2: return "APB4"; default: return "APB4, PPROT tied 000"; endcase
  endfunction
  initial begin
    repeat (2) @(posedge pclk); #1 presetn = 1;   // reset released after edge 1
    @(posedge pclk); #1;
    xfer(1, 1, 32'h08, 32'h11111111, 4'b1111, 3'b010);
    xfer(1, 1, 32'h08, 32'h00cc0000, 4'b0100, 3'b010);
    xfer(1, 0, 32'h08, 0, 0, 3'b010);
    $display("read %h", rdata);
    $finish;
  end
endmodule
Read included file: apb_ver.sv
`timescale 1ns/1ns
// apb_ver.sv: a small APB completer built as APB2, APB3, APB4 or APB5 (read-only).
// It has 16 bytes of RAM at offset 0x00 to 0x0f and a Secure-only register SREG at 0x10.
// VERSION decides which signals the completer has; its port list keeps them all, so a version that
// lacks a signal simply ignores the input or drives the output to the specification default.
//   APB2  no PREADY (driven 1), no PSLVERR (driven 0), no PSTRB (writes every lane), no PPROT
//   APB3  adds PREADY (WAITS wait states) and PSLVERR (unmapped offsets and refused writes)
//   APB4  adds PSTRB (writes only strobed lanes) and PPROT: SREG refuses a Non-secure write (PPROT[1] = 1)
//   APB5  the same transfer as APB4 here (its additions are optional sideband signals)
// DATA_W is 8, 16 or 32; a transfer moves DATA_W/8 bytes at the aligned address.
// Without PPROT an APB2 or APB3 completer cannot tell Secure from Non-secure, so SREG takes any write.
module apb_ver #(parameter int VERSION = 4, parameter int DATA_W = 32, parameter int WAITS = 0) (
  input  logic                pclk,
  input  logic                presetn,
  input  logic                psel,
  input  logic                penable,
  input  logic                pwrite,
  input  logic [31:0]         paddr,
  input  logic [DATA_W-1:0]   pwdata,
  input  logic [DATA_W/8-1:0] pstrb,
  input  logic [2:0]          pprot,
  output logic                pready,
  output logic [DATA_W-1:0]   prdata,
  output logic                pslverr);
  localparam int B = DATA_W / 8;
  logic [7:0] mem [0:16];                  // bytes 0 to 15 are the RAM, byte 16 is SREG
  int wcnt = 0;
  initial for (int i = 0; i <= 16; i++) mem[i] = 0;
  wire [7:0] off = paddr[7:0];
  wire mapped  = (off < 8'h10) || (off == 8'h10 && B == 1) || (off[7:2] == 6'h04 && B > 1);
  wire refused = (VERSION >= 4) && pwrite && (off >= 8'h10) && pprot[1];
  wire access  = psel && penable;
  assign pready  = (VERSION == 2) ? 1'b1 : (access ? (wcnt >= WAITS) : 1'b0);
  assign pslverr = (VERSION == 2) ? 1'b0 : (access && pready && (!mapped || refused));
  wire [7:0] base = off & ~8'(B - 1);
  for (genvar l = 0; l < B; l++) begin : g_rd
    assign prdata[8*l +: 8] = (base + l <= 16) ? mem[base + l] : 8'h00;
  end
  always @(posedge pclk or negedge presetn) begin
    if (!presetn) wcnt <= 0;
    else begin
      wcnt <= (access && !pready) ? wcnt + 1 : 0;
      if (access && pready && pwrite && mapped && !refused)
        for (int l = 0; l < B; l++)
          if ((VERSION < 4 || pstrb[l]) && base + l <= 16) mem[base + l] <= pwdata[8*l +: 8];
    end
  end
endmodule
Spot the bugEach option sends one write of 5a to SREG (offset 0x10). Which one does the wired APB4 completer (k = 2) refuse with PSLVERR?
Read included file: apb_ver.sv
`timescale 1ns/1ns
// apb_ver.sv: a small APB completer built as APB2, APB3, APB4 or APB5 (read-only).
// It has 16 bytes of RAM at offset 0x00 to 0x0f and a Secure-only register SREG at 0x10.
// VERSION decides which signals the completer has; its port list keeps them all, so a version that
// lacks a signal simply ignores the input or drives the output to the specification default.
//   APB2  no PREADY (driven 1), no PSLVERR (driven 0), no PSTRB (writes every lane), no PPROT
//   APB3  adds PREADY (WAITS wait states) and PSLVERR (unmapped offsets and refused writes)
//   APB4  adds PSTRB (writes only strobed lanes) and PPROT: SREG refuses a Non-secure write (PPROT[1] = 1)
//   APB5  the same transfer as APB4 here (its additions are optional sideband signals)
// DATA_W is 8, 16 or 32; a transfer moves DATA_W/8 bytes at the aligned address.
// Without PPROT an APB2 or APB3 completer cannot tell Secure from Non-secure, so SREG takes any write.
module apb_ver #(parameter int VERSION = 4, parameter int DATA_W = 32, parameter int WAITS = 0) (
  input  logic                pclk,
  input  logic                presetn,
  input  logic                psel,
  input  logic                penable,
  input  logic                pwrite,
  input  logic [31:0]         paddr,
  input  logic [DATA_W-1:0]   pwdata,
  input  logic [DATA_W/8-1:0] pstrb,
  input  logic [2:0]          pprot,
  output logic                pready,
  output logic [DATA_W-1:0]   prdata,
  output logic                pslverr);
  localparam int B = DATA_W / 8;
  logic [7:0] mem [0:16];                  // bytes 0 to 15 are the RAM, byte 16 is SREG
  int wcnt = 0;
  initial for (int i = 0; i <= 16; i++) mem[i] = 0;
  wire [7:0] off = paddr[7:0];
  wire mapped  = (off < 8'h10) || (off == 8'h10 && B == 1) || (off[7:2] == 6'h04 && B > 1);
  wire refused = (VERSION >= 4) && pwrite && (off >= 8'h10) && pprot[1];
  wire access  = psel && penable;
  assign pready  = (VERSION == 2) ? 1'b1 : (access ? (wcnt >= WAITS) : 1'b0);
  assign pslverr = (VERSION == 2) ? 1'b0 : (access && pready && (!mapped || refused));
  wire [7:0] base = off & ~8'(B - 1);
  for (genvar l = 0; l < B; l++) begin : g_rd
    assign prdata[8*l +: 8] = (base + l <= 16) ? mem[base + l] : 8'h00;
  end
  always @(posedge pclk or negedge presetn) begin
    if (!presetn) wcnt <= 0;
    else begin
      wcnt <= (access && !pready) ? wcnt + 1 : 0;
      if (access && pready && pwrite && mapped && !refused)
        for (int l = 0; l < B; l++)
          if ((VERSION < 4 || pstrb[l]) && base + l <= 16) mem[base + l] <= pwdata[8*l +: 8];
    end
  end
endmodule
ConceptAn APB5 requester that drives PNSE is connected to an APB5 completer without RME support. What does the specification say?
ConceptAn APB2 completer sits on an APB3 bus. What must the bridge see on its PREADY and PSLVERR inputs for that completer?
ConceptAn APB4 requester issues a byte write to an APB3 peripheral that has no PSTRB. Which statement is right?

Common Mistakes

Wrong Tie a completer's PPROT input to 3'b000 because 000 is the default.

PPROT 000 means normal, Secure and data, so every access gets Secure rights.

Fix Tie PPROT only to a value that is correct for every access, or treat the pair as not compatible.

Before
.pprot(3'b000)   // default, so it must be safe
After
.pprot(3'b010)   // this requester is Non-secure only

Wrong Tie the PSTRB inputs of an APB4 completer high when the requester has no PSTRB.

Reads would then carry strobes, and the protocol requires every strobe low during a read.

Fix Tie every PSTRB bit to the requester's PWRITE.

Before
.pstrb(4'b1111)
After
.pstrb({4{pwrite}})

Wrong Trust byte stores to a register block because the bridge sends PSTRB.

An APB2 or APB3 block has no strobe input and writes every lane of the word.

Fix Check the block's version. Without PSTRB, do read-modify-write in software or put an adapter in front.

Remember

  • APB3 added PREADY and PSLVERR, APB4 PPROT and PSTRB, APB5 wake-up, user, parity and PNSE.
  • A missing PREADY reads as 1 and a missing PSLVERR as 0, so an APB2 completer never waits or fails.
  • A completer without PSTRB writes whole words; a completer with PSTRB behind a requester without it gets PSTRB tied to PWRITE.
  • PPROT tied to 000 claims every access is Secure; PNSE behind a requester without it is tied low.
  • Both data buses of one interface have the same width: 8, 16 or 32 bits.

Practice

Report a problem with this page