Skip to content

Commit

Permalink
RVFI - Adding mhpmevents csr reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoann Pruvost committed Apr 5, 2024
1 parent c0bdfab commit 663a17d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
37 changes: 33 additions & 4 deletions bhv/cv32e40p_rvfi.sv
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ module cv32e40p_rvfi

localparam logic [31:0] MSTATUS_WRITE_MASK = 32'h0000_6088;
localparam logic [31:0] MCOUNTINHIBIT_WRITE_MASK = {{(29-NUM_MHPMCOUNTERS){1'b0}}, {(NUM_MHPMCOUNTERS){1'b1}}, 3'b101};
localparam NUM_HPM_EVENTS = 16;
localparam logic [31:0] MHPMEVENT_WRITE_MASK = {{(31-NUM_HPM_EVENTS){1'b0}}, {(NUM_HPM_EVENTS){1'b1}}};

`include "pipe_freeze_trace.sv"

Expand Down Expand Up @@ -958,6 +960,11 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
rvfi_csr_mhpmcounterh_wmask[idx] = new_rvfi_trace.m_csr.mhpmcounter_wmask[idx][63:32];
rvfi_csr_mhpmcounterh_rdata[idx] = new_rvfi_trace.m_csr.mhpmcounter_rdata[idx][63:32];
rvfi_csr_mhpmcounterh_wdata[idx] = new_rvfi_trace.m_csr.mhpmcounter_wdata[idx][63:32];

rvfi_csr_mhpmevent_rmask[idx] = new_rvfi_trace.m_csr.mhpmevent_rmask[idx];
rvfi_csr_mhpmevent_wmask[idx] = new_rvfi_trace.m_csr.mhpmevent_wmask[idx] & MHPMEVENT_WRITE_MASK;
rvfi_csr_mhpmevent_rdata[idx] = new_rvfi_trace.m_csr.mhpmevent_rdata[idx];
rvfi_csr_mhpmevent_wdata[idx] = new_rvfi_trace.m_csr.mhpmevent_wdata[idx];
end
// `SET_RVFI_CSR_FROM_INSN(instreth)
rvfi_csr_instreth_rdata = new_rvfi_trace.m_csr.minstreth_rdata;
Expand Down Expand Up @@ -1029,6 +1036,23 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
sample_perf_counter_to_id(idx);
endfunction

function void sample_perf_event_to_trace(int idx, insn_trace_t m_trace);
m_trace.m_csr.mhpmevent_rdata[idx] = r_pipe_freeze_trace.csr.mhpmevent_q[idx];
m_trace.m_csr.mhpmevent_rmask[idx] = '1;
endfunction

function void perf_event_to_trace(int idx, insn_trace_t m_trace);
if(!m_trace.m_csr.mhpmevent_we[idx]) begin
m_trace.m_csr.mhpmevent_wdata[idx] = r_pipe_freeze_trace.csr.wdata_int;
end
if(r_pipe_freeze_trace.csr.mhpmevent_we[idx]) begin
m_trace.m_csr.mhpmevent_we[idx] = r_pipe_freeze_trace.csr.mhpmevent_we[idx];
m_trace.m_csr.mhpmevent_wdata[idx] = r_pipe_freeze_trace.csr.wdata_int;
m_trace.m_csr.mhpmevent_wmask[idx] = r_pipe_freeze_trace.csr.mhpmevent_we[idx] ? '1 : '0;
end
sample_perf_event_to_trace(idx, m_trace);
endfunction

function void sample_minstret_to_trace(insn_trace_t m_trace);
m_trace.m_csr.minstret_rdata = r_pipe_freeze_trace.csr.mhpmcounter_q[2][31:0];
m_trace.m_csr.minstret_rmask = '1;
Expand Down Expand Up @@ -1092,6 +1116,9 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
sample_minstret_to_trace(m_trace);
sample_minstreth_to_trace(m_trace);
sample_mcycle_to_trace(m_trace);
for(int idx=3; idx<32; idx++)begin
sample_perf_event_to_trace(idx, m_trace); //TO CHANGE
end
endfunction

function void perf_counter_to_trace(insn_trace_t m_trace);
Expand All @@ -1103,10 +1130,13 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
end
for(int idx=3; idx<32; idx++) begin
if(r_pipe_freeze_trace.csr.mhpmcounter_write_lower[idx]) begin
perf_counter_to_id(3);
perf_counter_to_id(idx);
end
if(r_pipe_freeze_trace.csr.mhpmcounter_write_upper[idx]) begin
perf_counter_h_to_id(3);
perf_counter_h_to_id(idx);
end
if(r_pipe_freeze_trace.csr.mhpmevent_we[idx]) begin
perf_event_to_trace(idx, m_trace);
end
end
endfunction
Expand Down Expand Up @@ -1211,8 +1241,6 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
lpcount1_to_id();
lpend1_to_id();
lpstart1_to_id();


endfunction

bit s_was_flush; //debug exception is flagged as trap only if preceed by a flush
Expand Down Expand Up @@ -1708,6 +1736,7 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
for(int idx=3; idx<32; idx++) begin
sample_perf_counter_to_id(idx);
sample_perf_counter_h_to_id(idx);
sample_perf_event_to_trace(idx, trace_id);
end
end
trace_id.m_instret_smaple_trigger = trace_id.m_instret_smaple_trigger + 1;
Expand Down
3 changes: 3 additions & 0 deletions bhv/cv32e40p_tb_wrapper.sv
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ module cv32e40p_tb_wrapper
.csr_mcountinhibit_n_i (cv32e40p_top_i.core_i.cs_registers_i.mcountinhibit_n),
.csr_mcountinhibit_we_i(cv32e40p_top_i.core_i.cs_registers_i.mcountinhibit_we),

.csr_mhpmevent_n_i (cv32e40p_top_i.core_i.cs_registers_i.mhpmevent_n),
.csr_mhpmevent_q_i (cv32e40p_top_i.core_i.cs_registers_i.mhpmevent_q),
.csr_mhpmevent_we_i(cv32e40p_top_i.core_i.cs_registers_i.mhpmevent_we),
.csr_mscratch_q_i(cv32e40p_top_i.core_i.cs_registers_i.mscratch_q),
.csr_mscratch_n_i(cv32e40p_top_i.core_i.cs_registers_i.mscratch_n),
.csr_mepc_q_i(cv32e40p_top_i.core_i.cs_registers_i.mepc_q),
Expand Down
25 changes: 19 additions & 6 deletions bhv/insn_trace.sv
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
logic [31:0][63:0] mhpmcounter_rmask;
logic [31:0][63:0] mhpmcounter_wdata;
logic [31:0][63:0] mhpmcounter_wmask;

logic [31:0] mhpmevent_we;
logic [31:0][31:0] mhpmevent_rdata;
logic [31:0][31:0] mhpmevent_rmask;
logic [31:0][31:0] mhpmevent_wdata;
logic [31:0][31:0] mhpmevent_wmask;
`DEFINE_CSR(mip)
//mnxti
//mintstatus
Expand Down Expand Up @@ -657,12 +663,12 @@
INSTR_CVCMPLEB : this.m_mnemonic = "cv.cmple.b";
INSTR_CVCMPLESCB : this.m_mnemonic = "cv.cmple.sc.b";
INSTR_CVCMPLESCIB : this.m_mnemonic = "cv.cmple.sci.b";
INSTR_CVCMPGTUH : this.m_mnemonic = "cv.cmptu.h";
INSTR_CVCMPGTUSCH : this.m_mnemonic = "cv.cmptu.sc.h";
INSTR_CVCMPGTUSCIH : this.m_mnemonic = "cv.cmptu.sci.h";
INSTR_CVCMPGTUB : this.m_mnemonic = "cv.cmptu.b";
INSTR_CVCMPGTUSCB : this.m_mnemonic = "cv.cmptu.sc.b";
INSTR_CVCMPGTUSCIB : this.m_mnemonic = "cv.cmptu.sci.b";
INSTR_CVCMPGTUH : this.m_mnemonic = "cv.cmpgtu.h";
INSTR_CVCMPGTUSCH : this.m_mnemonic = "cv.cmpgtu.sc.h";
INSTR_CVCMPGTUSCIH : this.m_mnemonic = "cv.cmpgtu.sci.h";
INSTR_CVCMPGTUB : this.m_mnemonic = "cv.cmpgtu.b";
INSTR_CVCMPGTUSCB : this.m_mnemonic = "cv.cmpgtu.sc.b";
INSTR_CVCMPGTUSCIB : this.m_mnemonic = "cv.cmpgtu.sci.b";
INSTR_CVCMPGEUH : this.m_mnemonic = "cv.cmpgeu.h";
INSTR_CVCMPGEUSCH : this.m_mnemonic = "cv.cmpgeu.sc.h";
INSTR_CVCMPGEUSCIH : this.m_mnemonic = "cv.cmpgeu.sci.h";
Expand Down Expand Up @@ -901,6 +907,8 @@
`INIT_CSR(instreth)
this.m_csr.mhpmcounter_we = '0;
this.m_csr.mhpmcounter_wmask = '0;
this.m_csr.mhpmevent_we = '0;
this.m_csr.mhpmevent_wmask = '0;
`INIT_CSR(mip)
`INIT_CSR(tdata1)
`INIT_CSR(tdata2)
Expand Down Expand Up @@ -1078,6 +1086,11 @@
this.m_csr.mhpmcounter_rmask = m_source.m_csr.mhpmcounter_rmask;
this.m_csr.mhpmcounter_wdata = m_source.m_csr.mhpmcounter_wdata;
this.m_csr.mhpmcounter_wmask = m_source.m_csr.mhpmcounter_wmask;
this.m_csr.mhpmevent_we = m_source.m_csr.mhpmevent_we;
this.m_csr.mhpmevent_rdata = m_source.m_csr.mhpmevent_rdata;
this.m_csr.mhpmevent_rmask = m_source.m_csr.mhpmevent_rmask;
this.m_csr.mhpmevent_wdata = m_source.m_csr.mhpmevent_wdata;
this.m_csr.mhpmevent_wmask = m_source.m_csr.mhpmevent_wmask;
`ASSIGN_CSR(mip)
`ASSIGN_CSR(tdata1)
`ASSIGN_CSR(tdata2)
Expand Down
6 changes: 5 additions & 1 deletion bhv/pipe_freeze_trace.sv
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ function compute_csr_we();
r_pipe_freeze_trace.csr.fflags_we = 1'b0;
r_pipe_freeze_trace.csr.frm_we = 1'b0;
r_pipe_freeze_trace.csr.fcsr_we = 1'b0;
r_pipe_freeze_trace.csr.mhpmevent_we = '0;
r_pipe_freeze_trace.csr.dpc_we = csr_dpc_we_i;
if (r_pipe_freeze_trace.csr.we) begin
case (r_pipe_freeze_trace.csr.addr)
Expand Down Expand Up @@ -378,6 +379,10 @@ function compute_csr_we();
CSR_DSCRATCH1: r_pipe_freeze_trace.csr.dscratch1_we = 1'b1;
endcase
end

if(csr_mhpmevent_we_i)begin
r_pipe_freeze_trace.csr.mhpmevent_we[r_pipe_freeze_trace.csr.addr[4:0]] = 1'b1;
end
// CSR_MCAUSE: r_pipe_freeze_trace.csr.mcause_we = r_pipe_freeze_trace.csr.mcause_n != r_pipe_freeze_trace.csr.mcause_q; //for debug purpose
endfunction
/*
Expand Down Expand Up @@ -575,7 +580,6 @@ task monitor_pipeline();
r_pipe_freeze_trace.csr.mcountinhibit_we = csr_mcountinhibit_we_i;
r_pipe_freeze_trace.csr.mhpmevent_n = csr_mhpmevent_n_i;
r_pipe_freeze_trace.csr.mhpmevent_q = csr_mhpmevent_q_i;
r_pipe_freeze_trace.csr.mhpmevent_we = csr_mhpmevent_we_i;
r_pipe_freeze_trace.csr.mscratch_n = csr_mscratch_n_i;
r_pipe_freeze_trace.csr.mscratch_q = csr_mscratch_q_i;
r_pipe_freeze_trace.csr.mepc_n = csr_mepc_n_i;
Expand Down

0 comments on commit 663a17d

Please sign in to comment.