Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion ci/testcases/isa/amo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defaults:
configs: "-DVX_CFG_EXT_A_ENABLE"
xlen: [32, 64]
tier: smoke
touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, sim/simx/amo, sim/simx/mem]
touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, hw/rtl/core/VX_uop_amo.sv, hw/rtl/core/VX_alu_int.sv, sim/simx/amo, sim/simx/mem, sim/simx/lsu_unit.cpp, sim/simx/alu_unit.cpp]
tests:
# RISC-V 'A' ISA conformance (rv{XLEN}ua-p-{amo*,lrsc}); shares the A-built sim.
- id: isa-conformance
Expand Down Expand Up @@ -62,6 +62,27 @@ tests:
app: amo
xlen: [32]
args: "-n32 -t amoadd_scatter"
# The misaligned-atomic trap in cycles: the atomic's alignment check (an ALU
# branch-port uop) resolves its warp's fetch stall and traps; the LSU drops
# the atomic. SimX must trap and release the warp at the RTL's point.
- id: parity-amo-misaligned
check: model_parity
via: blackbox
app: amo
xlen: [32]
configs+: "-DVX_CFG_EXT_ZACAS_ENABLE"
args: "-n32 -t amo_misaligned"
# Two SIMD slices of two ALU and two LSU packets each (8 threads, SIMD 4,
# 2 lanes): the check holds a misaligned lane of an earlier packet or slice
# until the last, and the LSU drops every packet from the misaligned one on
# while the ones ahead of it perform.
- id: misaligned-simd-packets
app: amo
drivers: [simx, rtlsim]
xlen: [32]
configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_SIMD_WIDTH=4 -DVX_CFG_NUM_ALU_LANES=2 -DVX_CFG_NUM_LSU_LANES=2"
shape: {threads: 8}
args: "-n8 -t amo_misaligned"
# Four operand collectors and two SIMD packets per warp (32 warps x 8 threads,
# SIMD 4): a three-operand compare-and-swap stalls between its packets on
# bank conflicts, and the operand arbiter must keep an instruction's packets
Expand Down
22 changes: 22 additions & 0 deletions docs/designs/atomic_memory_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ so the operation field stays four bits wide.
is the `funct5` bits and is otherwise unused — so the comparand reaches the
LSU as `rs3_data` with no new operand path.

A misaligned atomic (`.W` not on 4 bytes, `.D` not on 8) is never split: it
raises the RISC-V address-misaligned exception, load (`mcause` 4) for `LR`,
store/AMO (6) for every other atomic, with `mepc` the atomic. `mtval` stays
read-only zero. The trap is precise: no instruction after the atomic runs
before the handler, and the handler may return to `mepc + 4`.

- **The check.** The uop sequencer (`VX_uop_amo`) puts an alignment check
ahead of every atomic: a branch-port uop on `rs1` (`INST_BR_AMOCHK`,
`INST_BR_LRCHK`) that the ALU resolves like a branch, not taken when every
active lane is aligned, a trap otherwise. The ALU holds a misaligned lane of
an earlier SIMD slice or packet until the instruction's last, which resolves
the check. The atomic stalls its warp's fetch at decode, as a branch does,
until the check resolves.
- **The drop.** The LSU decides the same from `rs1` on its own: from the packet
holding the first misaligned lane on, the atomic's packets perform no access
and retire through the no-response path writing nothing. Packets ahead of it
have gone to memory, so with several LSU packets per warp their lanes
perform their atomics.

The stall costs an atomic a branch's fetch bubble.

---

## 3. The LSU sideband
Expand Down Expand Up @@ -429,6 +450,7 @@ is the LLC.
| 15, 16 | `cas_ladder`, `cas_lmem` | every hart races compare-and-swap for each slot; compiled only with Zacas |
| 17, 18 | `cas_counter`, `cas_counter_lmem` | compare-and-swap increment from a nonzero base |
| `amoadd_scatter`, `amo_line_burst` | atomics into more lines than the dcache holds; groups of harts adding into distinct words of cold lines | the counts, with missing atomics replaying back to back into the LLC |
| last | `amo_misaligned` | `amoadd.w`, `lr.w`, `sc.w`, `amocas.w` at misaligned addresses; a warp whose lane 1 alone is misaligned; a misaligned lane opening the second LSU packet, and the second SIMD slice: `mcause`, `mepc` (and `mtval` 0), the instruction after the atomic running once and only after the handler, `rd` and every word of memory |

The `amo` suite in [`ci/testcases/isa/amo.yaml`](../../ci/testcases/isa/amo.yaml)
runs them on both simulators at both `XLEN`s, with and without Zacas, on a
Expand Down
8 changes: 6 additions & 2 deletions docs/designs/lsu_pipeline_design.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ level needs to know the aperture exists.
| request direction | store, unless the instruction is an atomic — atomics always travel as `rw = 0` |

**Misaligned access is not supported.** An address that is not a multiple of
the access size is a runtime assertion, not a handled fault.
the access size is a runtime assertion, not a handled fault -- except for an
atomic: its warp traps through the atomic's alignment check, and the block
drops it from the packet holding its first misaligned lane on, retiring those
packets through the no-response path with an empty mask
(atomic_memory_operations.md).

### 3.3 Fence ordering

Expand Down Expand Up @@ -458,7 +462,7 @@ into a failure at the slot that lost it.
## 10. Not implemented

- **Misaligned access.** No splitting; the software contract requires
aligned addresses.
aligned addresses (a misaligned atomic traps instead).
- **Prefetching.** Every request is demand-driven.
- **Address-ranged fences.** A fence is a total barrier on its slice; there
is no ordering between specific address ranges.
Expand Down
1 change: 1 addition & 0 deletions docs/designs/trap_and_exception_foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ misaligned, so only there does `ma_fetch` reach the trap.
| 2 illegal instruction | decoder (trap uop) | the instruction | 0 |
| 3 breakpoint (`EBREAK`) | branch unit | the instruction | 0 |
| 11 `ECALL` from M | branch unit | the instruction | 0 |
| 4 load address misaligned (`LR`), 6 store/AMO address misaligned | branch unit, the atomic's alignment check uop | the atomic | 0 |

**Misaligned fetch.** With C disabled (IALIGN=32), a JAL, a JALR or a
*taken* conditional branch whose target has bit 1 set traps instead of
Expand Down
6 changes: 6 additions & 0 deletions hw/rtl/VX_gpu_pkg.sv
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ package VX_gpu_pkg;
///////////////////////////////////////////////////////////////////////////

localparam INST_BR_BEQ = 4'b0000;
// An atomic's alignment check (VX_uop_amo): a store/AMO (AMOCHK) or load
// (LRCHK) address-misaligned trap when an active lane's rs1 is not aligned
// to the access size. It targets PC + 4, and its br_args.is_rvc bit says
// the access is 8 bytes (an atomic is never compressed).
localparam INST_BR_AMOCHK = 4'b0001;
localparam INST_BR_LRCHK = 4'b0011;
localparam INST_BR_BNE = 4'b0010;
localparam INST_BR_BLTU = 4'b0100;
localparam INST_BR_BGEU = 4'b0110;
Expand Down
2 changes: 2 additions & 0 deletions hw/rtl/VX_trace_pkg.sv
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ package VX_trace_pkg;
ALU_TYPE_BRANCH: begin
case (INST_BR_BITS'(op_type))
INST_BR_BEQ: `TRACE(level, ("BEQ"))
INST_BR_AMOCHK:`TRACE(level, ("AMOCHK"))
INST_BR_LRCHK: `TRACE(level, ("LRCHK"))
INST_BR_BNE: `TRACE(level, ("BNE"))
INST_BR_BLT: `TRACE(level, ("BLT"))
INST_BR_BGE: `TRACE(level, ("BGE"))
Expand Down
49 changes: 42 additions & 7 deletions hw/rtl/core/VX_alu_int.sv
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,30 @@ module VX_alu_int import VX_gpu_pkg::*; #(
// uuid).
wire is_wgather = (execute_if.data.op_args.alu.xtype == ALU_TYPE_OTHER) && alu_op[3];
wire is_jmp_misaligned = br_misaligned && inst_br_is_static(br_op);

`ifdef VX_CFG_EXT_A_ENABLE
// An atomic's alignment check traps when an active lane's address is not
// aligned to the access size. Its SIMD slices and packets reach the block
// back to back, so a misaligned lane of an earlier one is held until the
// last, which resolves the check.
wire is_chk = is_br_op && (br_op[3:2] == 2'b00) && br_op[0];
wire chk_is_d = execute_if.data.op_args.br.is_rvc;
wire [NUM_LANES-1:0] chk_lane_mis;
for (genvar i = 0; i < NUM_LANES; ++i) begin : g_chk_lane_mis
assign chk_lane_mis[i] = execute_if.data.header.tmask[i]
&& ((alu_in1[i][1:0] != 2'b00) || (chk_is_d && alu_in1[i][2]));
end
reg chk_mis_r;
wire chk_mis = chk_mis_r || (| chk_lane_mis);
always @(posedge clk) begin
if (reset) begin
chk_mis_r <= 1'b0;
end else if (execute_if.valid && execute_if.ready && is_chk) begin
chk_mis_r <= chk_mis && ~execute_if.data.header.eop;
end
end
wire chk_trap = is_chk && chk_mis;
`endif
wire [NUM_LANES-1:0] alu_tmask = is_jmp_misaligned ? NUM_LANES'(0)
: is_wgather ? ~wg_src_mask
: execute_if.data.header.tmask;
Expand All @@ -291,21 +315,27 @@ module VX_alu_int import VX_gpu_pkg::*; #(
wire is_rvc_r;
`endif
wire br_misaligned_r;
wire chk_trap_r;

VX_elastic_buffer #(
.DATAW ($bits(header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + 1 + `VX_CFG_EXT_C_ENABLED)
.DATAW ($bits(header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + 1 + `VX_CFG_EXT_C_ENABLED + `VX_CFG_EXT_A_ENABLED)
) rsp_buf (
.clk (clk),
.reset (reset),
.valid_in (execute_if.valid),
.ready_in (execute_if.ready),
.data_in ({alu_hdr_in, alu_result, cbr_dest, is_br_op, br_op, last_tid, br_misaligned
`ifdef VX_CFG_EXT_C_ENABLE , is_rvc_in `endif }),
`ifdef VX_CFG_EXT_C_ENABLE , is_rvc_in `endif
`ifdef VX_CFG_EXT_A_ENABLE , chk_trap `endif }),
.data_out ({result_if.data.header, alu_result_r, cbr_dest_r, is_br_op_r, br_op_r, last_tid_r, br_misaligned_r
`ifdef VX_CFG_EXT_C_ENABLE , is_rvc_r `endif }),
`ifdef VX_CFG_EXT_C_ENABLE , is_rvc_r `endif
`ifdef VX_CFG_EXT_A_ENABLE , chk_trap_r `endif }),
.valid_out (result_if.valid),
.ready_out (result_if.ready)
);
`ifndef VX_CFG_EXT_A_ENABLE
assign chk_trap_r = 1'b0;
`endif

`UNUSED_VAR (br_op_r)
wire is_br_neg = inst_br_is_neg(br_op_r);
Expand All @@ -320,16 +350,17 @@ module VX_alu_int import VX_gpu_pkg::*; #(
wire br_enable = result_fire && is_br_op_r && result_if.data.header.eop;
wire br_taken = ((is_br_less ? is_less : is_equal) ^ is_br_neg) | is_br_static;

// Synchronous traps: ECALL/EBREAK, an illegal instruction and a taken
// transfer to a misaligned target enter a trap (redirect to mtvec);
// Synchronous traps: ECALL/EBREAK, an illegal instruction, a taken
// transfer to a misaligned target and a misaligned atomic's check enter a
// trap (redirect to mtvec);
// MRET/SRET/URET return (restore PC from mepc). The scheduler acts on
// these via branch_ctl_if.{is_trap,is_mret,trap_cause}.
wire is_trap_op = is_br_op_r && (br_op_r == INST_BR_ECALL || br_op_r == INST_BR_EBREAK || br_op_r == INST_BR_ILLEGAL);
wire is_misaligned = br_misaligned_r && br_taken;
wire is_trap_entry = is_trap_op || is_misaligned;
wire is_trap_entry = is_trap_op || is_misaligned || chk_trap_r;
// A trap's `dest` is its own PC (saved into mepc). The select does not wait
// for the compare: a not-taken branch with a misaligned target ignores dest.
wire [PC_BITS-1:0] br_dest = (is_trap_op || br_misaligned_r) ? result_if.data.header.PC
wire [PC_BITS-1:0] br_dest = (is_trap_op || br_misaligned_r || chk_trap_r) ? result_if.data.header.PC
: is_br_static ? from_fullPC({br_result[`VX_CFG_XLEN-1:1], 1'b0})
: cbr_dest_r;
wire is_mret_op = is_br_op_r && (br_op_r == INST_BR_MRET || br_op_r == INST_BR_SRET || br_op_r == INST_BR_URET);
Expand All @@ -341,6 +372,10 @@ module VX_alu_int import VX_gpu_pkg::*; #(
case (br_op_r)
INST_BR_EBREAK: br_trap_cause = TRAP_CAUSE_BITS'(3); // breakpoint
INST_BR_ILLEGAL: br_trap_cause = TRAP_CAUSE_BITS'(2); // illegal instruction
`ifdef VX_CFG_EXT_A_ENABLE
INST_BR_LRCHK: br_trap_cause = TRAP_CAUSE_BITS'(4); // load address misaligned
INST_BR_AMOCHK: br_trap_cause = TRAP_CAUSE_BITS'(6); // store/AMO address misaligned
`endif
default: br_trap_cause = TRAP_CAUSE_BITS'(11); // ECALL from M-mode
endcase
end
Expand Down
3 changes: 3 additions & 0 deletions hw/rtl/core/VX_decode.sv
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ module VX_decode import
op_args.lsu.amo_unsigned = amo_unsigned;
op_args.lsu.amo_aq = instr[26];
op_args.lsu.amo_rl = instr[25];
// The warp fetches on once the atomic's alignment check
// (VX_uop_amo) resolves through the branch port.
is_wstall = 1;
`USED_IREG (rd);
`USED_IREG (rs1);
// LR has rs2=x0; the LSU just sees rs2_data=0 as rhs.
Expand Down
66 changes: 59 additions & 7 deletions hw/rtl/core/VX_lsu_block.sv
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,37 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
wire no_rsp_buf_valid, no_rsp_buf_ready;

wire [LSUQ_SIZEW-1:0] pkt_waddr, pkt_raddr;
// requests of the packet still being sent (its slot not yet acquired) are
// out at memory
wire pkt_open;

// misaligned atomics

// An atomic with an active lane whose address (rs1) is not aligned to the
// access size performs no access: its warp traps through the atomic's
// alignment check (VX_uop_amo), and this packet and the instruction's
// later ones retire writing nothing. The instruction's earlier packets
// have gone to memory.
wire req_fault;
`ifdef VX_CFG_EXT_A_ENABLE
wire req_is_d = (inst_lsu_wsize(execute_if.data.op_type) == 2'd3);
wire [NUM_LANES-1:0] req_lane_mis;
for (genvar i = 0; i < NUM_LANES; ++i) begin : g_req_lane_mis
assign req_lane_mis[i] = execute_if.data.header.tmask[i]
&& ((execute_if.data.rs1_data[i][1:0] != 2'b00) || (req_is_d && execute_if.data.rs1_data[i][2]));
end
reg fault_open_r;
assign req_fault = execute_if.data.op_args.lsu.amo_valid && (fault_open_r || (| req_lane_mis));
always @(posedge clk) begin
if (reset) begin
fault_open_r <= 1'b0;
end else if (execute_if.valid && execute_if.ready) begin
fault_open_r <= req_fault && ~execute_if.data.header.eop;
end
end
`else
assign req_fault = 1'b0;
`endif

// fence handling

Expand All @@ -270,20 +301,30 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
end

wire req_skip = req_is_fence && ~execute_if.data.header.eop;
wire no_rsp_buf_enable = (mem_req_rw && ~execute_if.data.header.wb) || req_skip;
// A faulted packet that ends its instruction, while packets before it
// have gone to memory, waits for their responses: they opened the packet
// slot its end-of-packet request would have acquired, so the next
// instruction's packet could otherwise start in a slot still counting
// them, and the instruction's end would retire ahead of their data.
wire fault_hold = req_fault && execute_if.data.header.eop && pkt_open;
// retires through the no-response path without a memory request
wire req_bypass = req_skip || req_fault;
wire no_rsp_buf_enable = (mem_req_rw && ~execute_if.data.header.wb) || req_bypass;

assign mem_req_valid = execute_if.valid
&& ~req_skip
&& ~req_bypass
&& ~(no_rsp_buf_enable && ~no_rsp_buf_ready)
&& ~fence_lock;

assign no_rsp_buf_valid = execute_if.valid
&& no_rsp_buf_enable
&& (req_skip || mem_req_ready)
&& (req_bypass || mem_req_ready)
&& ~fault_hold
&& ~fence_lock;

assign execute_if.ready = (mem_req_ready || req_skip)
assign execute_if.ready = (mem_req_ready || req_bypass)
&& ~(no_rsp_buf_enable && ~no_rsp_buf_ready)
&& ~fault_hold
&& ~fence_lock;

// A fence is sent on every lane, whatever the thread mask: the block's
Expand Down Expand Up @@ -337,10 +378,10 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
assign mem_req_byteen[i] = mem_req_byteen_w;
end

// memory misalignment not supported!
// memory misalignment not supported! (a misaligned atomic traps instead)
for (genvar i = 0; i < NUM_LANES; ++i) begin : g_missalign
wire lsu_req_fire = execute_if.valid && execute_if.ready;
`RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0),
`RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0),
("%t: misaligned memory access, wid=%0d, PC=0x%0h, addr=0x%0h, wsize=%0d! (#%0d)",
$time, execute_if.data.header.wid, to_fullPC(execute_if.data.header.PC), full_addr[i], inst_lsu_wsize(execute_if.data.op_type), execute_if.data.header.uuid))
end
Expand Down Expand Up @@ -423,6 +464,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
end

assign mem_rsp_sop_pkt = pkt_sop[pkt_raddr];
assign pkt_open = (pkt_ctr[pkt_waddr] != 0);
assign mem_rsp_eop_pkt = mem_rsp_eop && pkt_eop[pkt_raddr] && (pkt_ctr[pkt_raddr] == 1);
`RUNTIME_ASSERT(~(mem_req_rd_fire && full), ("allocator full!"))
`RUNTIME_ASSERT(~(mem_req_rd_sop_fire && pkt_ctr[pkt_waddr] != 0), ("oops! broken sop request!"))
Expand All @@ -434,6 +476,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
`UNUSED_VAR (mem_rsp_sop)
end else begin : g_no_pid
assign pkt_waddr = 0;
assign pkt_open = 1'b0;
assign mem_rsp_sop_pkt = mem_rsp_sop;
assign mem_rsp_eop_pkt = mem_rsp_eop;
`UNUSED_VAR (pkt_raddr)
Expand Down Expand Up @@ -580,6 +623,15 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
.ready_out (result_rsp_if.ready)
);

// a faulted atomic retires writing nothing
header_t no_rsp_hdr;
always @(*) begin
no_rsp_hdr = execute_if.data.header;
if (req_fault) begin
no_rsp_hdr.tmask = '0;
end
end

VX_elastic_buffer #(
.DATAW ($bits(header_t)),
.SIZE (2)
Expand All @@ -588,7 +640,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #(
.reset (reset),
.valid_in (no_rsp_buf_valid),
.ready_in (no_rsp_buf_ready),
.data_in (execute_if.data.header),
.data_in (no_rsp_hdr),
.data_out (result_no_rsp_if.data.header),
.valid_out (result_no_rsp_if.valid),
.ready_out (result_no_rsp_if.ready)
Expand Down
Loading
Loading