Skip to content

Commit f8fd54c

Browse files
committed
[rtl] Flush pipe on all CSR modifications
This fixes #2193, an issue that meant bit clears in PMP related CSRs didn't immediately apply to an instruction already in the fetch stage due to a lack of a pipeline flush. With this change the pipeline will flush in that scenario, fixing the issue. It now flushes the pipeline on all CSR modifications as this makes the pipeline more resliant against similar issues in the future (where the list of CSRs to flush on should have been updated but wasn't).
1 parent 53888bc commit f8fd54c

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

rtl/ibex_id_stage.sv

+7-30
Original file line numberDiff line numberDiff line change
@@ -509,36 +509,13 @@ module ibex_id_stage #(
509509
.branch_in_dec_o(branch_in_dec)
510510
);
511511

512-
/////////////////////////////////
513-
// CSR-related pipeline flushes //
514-
/////////////////////////////////
515-
always_comb begin : csr_pipeline_flushes
516-
csr_pipe_flush = 1'b0;
517-
518-
// A pipeline flush is needed to let the controller react after modifying certain CSRs:
519-
// - When enabling interrupts, pending IRQs become visible to the controller only during
520-
// the next cycle. If during that cycle the core disables interrupts again, it does not
521-
// see any pending IRQs and consequently does not start to handle interrupts.
522-
// - When modifying any PMP CSR, PMP check of the next instruction might get invalidated.
523-
// Hence, a pipeline flush is needed to instantiate another PMP check with the updated CSRs.
524-
// - When modifying debug CSRs.
525-
if (csr_op_en_o == 1'b1 && (csr_op_o == CSR_OP_WRITE || csr_op_o == CSR_OP_SET)) begin
526-
if (csr_num_e'(instr_rdata_i[31:20]) == CSR_MSTATUS ||
527-
csr_num_e'(instr_rdata_i[31:20]) == CSR_MIE ||
528-
csr_num_e'(instr_rdata_i[31:20]) == CSR_MSECCFG ||
529-
// To catch all PMPCFG/PMPADDR registers, get the shared top most 7 bits.
530-
instr_rdata_i[31:25] == 7'h1D) begin
531-
csr_pipe_flush = 1'b1;
532-
end
533-
end else if (csr_op_en_o == 1'b1 && csr_op_o != CSR_OP_READ) begin
534-
if (csr_num_e'(instr_rdata_i[31:20]) == CSR_DCSR ||
535-
csr_num_e'(instr_rdata_i[31:20]) == CSR_DPC ||
536-
csr_num_e'(instr_rdata_i[31:20]) == CSR_DSCRATCH0 ||
537-
csr_num_e'(instr_rdata_i[31:20]) == CSR_DSCRATCH1) begin
538-
csr_pipe_flush = 1'b1;
539-
end
540-
end
541-
end
512+
// Flush pipe on any CSR modification. Some CSR modifications alter how instructions execute (e.g.
513+
// the PMP CSRs) so this ensures all instructions always see the latest architectural state when
514+
// entering the fetch stage. This causes some needless flushes but performance impact is
515+
// limited. We have a single fetch stage to flush not many stages of a deep pipeline and CSR
516+
// instructions are in general rare and not part of performance critical parts of the code.
517+
assign csr_pipe_flush =
518+
(csr_op_en_o == 1) && (csr_op_o inside {CSR_OP_WRITE, CSR_OP_SET, CSR_OP_CLEAR});
542519

543520
////////////////
544521
// Controller //

0 commit comments

Comments
 (0)