xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/LoadUnit.scala (revision 33a61811225bc8f42b55b3517efe83d890599d31)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.mem
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils._
23import utility._
24import xiangshan.ExceptionNO._
25import xiangshan._
26import xiangshan.backend.Bundles.{DynInst, MemExuInput, MemExuOutput}
27import xiangshan.backend.fu.PMPRespBundle
28import xiangshan.backend.fu.FuConfig._
29import xiangshan.backend.ctrlblock.{DebugLsInfoBundle, LsTopdownInfo}
30import xiangshan.backend.rob.RobPtr
31import xiangshan.backend.ctrlblock.DebugLsInfoBundle
32import xiangshan.backend.fu.util.SdtrigExt
33
34import xiangshan.cache._
35import xiangshan.cache.wpu.ReplayCarry
36import xiangshan.cache.mmu._
37import xiangshan.mem.mdp._
38
39class LoadToLsqReplayIO(implicit p: Parameters) extends XSBundle
40  with HasDCacheParameters
41  with HasTlbConst
42{
43  // mshr refill index
44  val mshr_id         = UInt(log2Up(cfg.nMissEntries).W)
45  // get full data from store queue and sbuffer
46  val full_fwd        = Bool()
47  // wait for data from store inst's store queue index
48  val data_inv_sq_idx = new SqPtr
49  // wait for address from store queue index
50  val addr_inv_sq_idx = new SqPtr
51  // replay carry
52  val rep_carry       = new ReplayCarry(nWays)
53  // data in last beat
54  val last_beat       = Bool()
55  // replay cause
56  val cause           = Vec(LoadReplayCauses.allCauses, Bool())
57  // performance debug information
58  val debug           = new PerfDebugInfo
59  // tlb hint
60  val tlb_id          = UInt(log2Up(loadfiltersize).W)
61  val tlb_full        = Bool()
62
63  // alias
64  def mem_amb       = cause(LoadReplayCauses.C_MA)
65  def tlb_miss      = cause(LoadReplayCauses.C_TM)
66  def fwd_fail      = cause(LoadReplayCauses.C_FF)
67  def dcache_rep    = cause(LoadReplayCauses.C_DR)
68  def dcache_miss   = cause(LoadReplayCauses.C_DM)
69  def wpu_fail      = cause(LoadReplayCauses.C_WF)
70  def bank_conflict = cause(LoadReplayCauses.C_BC)
71  def rar_nack      = cause(LoadReplayCauses.C_RAR)
72  def raw_nack      = cause(LoadReplayCauses.C_RAW)
73  def nuke          = cause(LoadReplayCauses.C_NK)
74  def need_rep      = cause.asUInt.orR
75}
76
77
78class LoadToLsqIO(implicit p: Parameters) extends XSBundle {
79  val ldin            = DecoupledIO(new LqWriteBundle)
80  val uncache         = Flipped(DecoupledIO(new MemExuOutput))
81  val ld_raw_data     = Input(new LoadDataFromLQBundle)
82  val forward         = new PipeLoadForwardQueryIO
83  val stld_nuke_query = new LoadNukeQueryIO
84  val ldld_nuke_query = new LoadNukeQueryIO
85  val trigger         = Flipped(new LqTriggerIO)
86}
87
88class LoadToLoadIO(implicit p: Parameters) extends XSBundle {
89  val valid      = Bool()
90  val data       = UInt(XLEN.W) // load to load fast path is limited to ld (64 bit) used as vaddr src1 only
91  val dly_ld_err = Bool()
92}
93
94class LoadUnitTriggerIO(implicit p: Parameters) extends XSBundle {
95  val tdata2      = Input(UInt(64.W))
96  val matchType   = Input(UInt(2.W))
97  val tEnable     = Input(Bool()) // timing is calculated before this
98  val addrHit     = Output(Bool())
99}
100
101class LoadUnit(implicit p: Parameters) extends XSModule
102  with HasLoadHelper
103  with HasPerfEvents
104  with HasDCacheParameters
105  with HasCircularQueuePtrHelper
106  with HasVLSUParameters
107  with SdtrigExt
108{
109  val io = IO(new Bundle() {
110    // control
111    val redirect      = Flipped(ValidIO(new Redirect))
112    val csrCtrl       = Flipped(new CustomCSRCtrlIO)
113
114    // int issue path
115    val ldin          = Flipped(Decoupled(new MemExuInput))
116    val ldout         = Decoupled(new MemExuOutput)
117
118    // vec issue path
119    val vecldin = Flipped(Decoupled(new VecPipeBundle))
120    val vecldout = Decoupled(new VecPipelineFeedbackIO(isVStore = false))
121
122    // misalignBuffer issue path
123    val misalign_ldin = Flipped(Decoupled(new LsPipelineBundle))
124    val misalign_ldout = Valid(new LqWriteBundle)
125
126    // data path
127    val tlb           = new TlbRequestIO(2)
128    val pmp           = Flipped(new PMPRespBundle()) // arrive same to tlb now
129    val dcache        = new DCacheLoadIO
130    val sbuffer       = new LoadForwardQueryIO
131    val lsq           = new LoadToLsqIO
132    val tl_d_channel  = Input(new DcacheToLduForwardIO)
133    val forward_mshr  = Flipped(new LduToMissqueueForwardIO)
134   // val refill        = Flipped(ValidIO(new Refill))
135    val l2_hint       = Input(Valid(new L2ToL1Hint))
136    val tlb_hint      = Flipped(new TlbHintReq)
137    // fast wakeup
138    // TODO: implement vector fast wakeup
139    val fast_uop = ValidIO(new DynInst) // early wakeup signal generated in load_s1, send to RS in load_s2
140
141    // trigger
142    val trigger = Vec(TriggerNum, new LoadUnitTriggerIO)
143
144    // prefetch
145    val prefetch_train            = ValidIO(new LdPrefetchTrainBundle()) // provide prefetch info to sms
146    val prefetch_train_l1         = ValidIO(new LdPrefetchTrainBundle()) // provide prefetch info to stream & stride
147    // speculative for gated control
148    val s1_prefetch_spec = Output(Bool())
149    val s2_prefetch_spec = Output(Bool())
150
151    val prefetch_req              = Flipped(ValidIO(new L1PrefetchReq)) // hardware prefetch to l1 cache req
152    val canAcceptLowConfPrefetch  = Output(Bool())
153    val canAcceptHighConfPrefetch = Output(Bool())
154
155    // ifetchPrefetch
156    val ifetchPrefetch = ValidIO(new SoftIfetchPrefetchBundle)
157
158    // load to load fast path
159    val l2l_fwd_in    = Input(new LoadToLoadIO)
160    val l2l_fwd_out   = Output(new LoadToLoadIO)
161
162    val ld_fast_match    = Input(Bool())
163    val ld_fast_fuOpType = Input(UInt())
164    val ld_fast_imm      = Input(UInt(12.W))
165
166    // rs feedback
167    val wakeup = ValidIO(new DynInst)
168    val feedback_fast = ValidIO(new RSFeedback) // stage 2
169    val feedback_slow = ValidIO(new RSFeedback) // stage 3
170    val ldCancel = Output(new LoadCancelIO()) // use to cancel the uops waked by this load, and cancel load
171
172    // load ecc error
173    val s3_dly_ld_err = Output(Bool()) // Note that io.s3_dly_ld_err and io.lsq.s3_dly_ld_err is different
174
175    // schedule error query
176    val stld_nuke_query = Flipped(Vec(StorePipelineWidth, Valid(new StoreNukeQueryIO)))
177
178    // queue-based replay
179    val replay       = Flipped(Decoupled(new LsPipelineBundle))
180    val lq_rep_full  = Input(Bool())
181
182    // misc
183    val s2_ptr_chasing = Output(Bool()) // provide right pc for hw prefetch
184
185    // Load fast replay path
186    val fast_rep_in  = Flipped(Decoupled(new LqWriteBundle))
187    val fast_rep_out = Decoupled(new LqWriteBundle)
188
189    // to misalign buffer
190    val misalign_buf = Valid(new LqWriteBundle)
191
192    // Load RAR rollback
193    val rollback = Valid(new Redirect)
194
195    // perf
196    val debug_ls         = Output(new DebugLsInfoBundle)
197    val lsTopdownInfo    = Output(new LsTopdownInfo)
198    val correctMissTrain = Input(Bool())
199  })
200
201  val s1_ready, s2_ready, s3_ready = WireInit(false.B)
202
203  // Pipeline
204  // --------------------------------------------------------------------------------
205  // stage 0
206  // --------------------------------------------------------------------------------
207  // generate addr, use addr to query DCache and DTLB
208  val s0_valid         = Wire(Bool())
209  val s0_mmio_select   = Wire(Bool())
210  val s0_kill          = Wire(Bool())
211  val s0_can_go        = s1_ready
212  val s0_fire          = s0_valid && s0_can_go
213  val s0_mmio_fire     = s0_mmio_select && s0_can_go
214  val s0_out           = Wire(new LqWriteBundle)
215  val s0_tlb_valid     = Wire(Bool())
216  val s0_tlb_hlv       = Wire(Bool())
217  val s0_tlb_hlvx      = Wire(Bool())
218  val s0_tlb_vaddr     = Wire(UInt(VAddrBits.W))
219  val s0_dcache_vaddr  = Wire(UInt(VAddrBits.W))
220
221  // flow source bundle
222  class FlowSource extends Bundle {
223    val vaddr         = UInt(VAddrBits.W)
224    val mask          = UInt((VLEN/8).W)
225    val uop           = new DynInst
226    val try_l2l       = Bool()
227    val has_rob_entry = Bool()
228    val rep_carry     = new ReplayCarry(nWays)
229    val mshrid        = UInt(log2Up(cfg.nMissEntries).W)
230    val isFirstIssue  = Bool()
231    val fast_rep      = Bool()
232    val ld_rep        = Bool()
233    val l2l_fwd       = Bool()
234    val prf           = Bool()
235    val prf_rd        = Bool()
236    val prf_wr        = Bool()
237    val prf_i         = Bool()
238    val sched_idx     = UInt(log2Up(LoadQueueReplaySize+1).W)
239    // Record the issue port idx of load issue queue. This signal is used by load cancel.
240    val deqPortIdx    = UInt(log2Ceil(LoadPipelineWidth).W)
241    val frm_mabuf     = Bool()
242    // vec only
243    val isvec         = Bool()
244    val is128bit      = Bool()
245    val uop_unit_stride_fof = Bool()
246    val reg_offset    = UInt(vOffsetBits.W)
247    val vecActive     = Bool() // 1: vector active element or scala mem operation, 0: vector not active element
248    val is_first_ele  = Bool()
249    // val flowPtr       = new VlflowPtr
250    val usSecondInv   = Bool()
251    val mbIndex       = UInt(vlmBindexBits.W)
252    val elemIdx       = UInt(elemIdxBits.W)
253    val elemIdxInsideVd = UInt(elemIdxBits.W)
254    val alignedType   = UInt(alignTypeBits.W)
255  }
256  val s0_sel_src = Wire(new FlowSource)
257
258  // load flow select/gen
259  // src0: misalignBuffer load (io.misalign_ldin)
260  // src1: super load replayed by LSQ (cache miss replay) (io.replay)
261  // src2: fast load replay (io.fast_rep_in)
262  // src3: mmio (io.lsq.uncache)
263  // src4: load replayed by LSQ (io.replay)
264  // src5: hardware prefetch from prefetchor (high confidence) (io.prefetch)
265  // NOTE: Now vec/int loads are sent from same RS
266  //       A vec load will be splited into multiple uops,
267  //       so as long as one uop is issued,
268  //       the other uops should have higher priority
269  // src6: vec read from RS (io.vecldin)
270  // src7: int read / software prefetch first issue from RS (io.in)
271  // src8: load try pointchaising when no issued or replayed load (io.fastpath)
272  // src9: hardware prefetch from prefetchor (high confidence) (io.prefetch)
273  // priority: high to low
274  val s0_rep_stall           = io.ldin.valid && isAfter(io.replay.bits.uop.robIdx, io.ldin.bits.uop.robIdx)
275  private val SRC_NUM = 10
276  private val Seq(
277    mab_idx, super_rep_idx, fast_rep_idx, mmio_idx, lsq_rep_idx,
278    high_pf_idx, vec_iss_idx, int_iss_idx, l2l_fwd_idx, low_pf_idx
279  ) = (0 until SRC_NUM).toSeq
280  // load flow source valid
281  val s0_src_valid_vec = WireInit(VecInit(Seq(
282    io.misalign_ldin.valid,
283    io.replay.valid && io.replay.bits.forward_tlDchannel,
284    io.fast_rep_in.valid,
285    io.lsq.uncache.valid,
286    io.replay.valid && !io.replay.bits.forward_tlDchannel && !s0_rep_stall,
287    io.prefetch_req.valid && io.prefetch_req.bits.confidence > 0.U,
288    io.vecldin.valid,
289    io.ldin.valid, // int flow first issue or software prefetch
290    io.l2l_fwd_in.valid,
291    io.prefetch_req.valid && io.prefetch_req.bits.confidence === 0.U,
292  )))
293  // load flow source ready
294  val s0_src_ready_vec = Wire(Vec(SRC_NUM, Bool()))
295  s0_src_ready_vec(0) := true.B
296  for(i <- 1 until SRC_NUM){
297    s0_src_ready_vec(i) := !s0_src_valid_vec.take(i).reduce(_ || _)
298  }
299  // load flow source select (OH)
300  val s0_src_select_vec = WireInit(VecInit((0 until SRC_NUM).map{i => s0_src_valid_vec(i) && s0_src_ready_vec(i)}))
301  val s0_hw_prf_select = s0_src_select_vec(high_pf_idx) || s0_src_select_vec(low_pf_idx)
302  dontTouch(s0_src_valid_vec)
303  dontTouch(s0_src_ready_vec)
304  dontTouch(s0_src_select_vec)
305
306  val s0_tlb_no_query = s0_hw_prf_select || s0_src_select_vec(fast_rep_idx) || s0_src_select_vec(mmio_idx) || s0_sel_src.prf_i
307  s0_valid := (
308    s0_src_valid_vec(mab_idx) ||
309    s0_src_valid_vec(super_rep_idx) ||
310    s0_src_valid_vec(fast_rep_idx) ||
311    s0_src_valid_vec(lsq_rep_idx) ||
312    s0_src_valid_vec(high_pf_idx) ||
313    s0_src_valid_vec(vec_iss_idx) ||
314    s0_src_valid_vec(int_iss_idx) ||
315    s0_src_valid_vec(l2l_fwd_idx) ||
316    s0_src_valid_vec(low_pf_idx)
317  ) && !s0_src_select_vec(mmio_idx) && io.dcache.req.ready && !s0_kill
318
319  s0_mmio_select := s0_src_select_vec(mmio_idx) && !s0_kill
320
321   // if is hardware prefetch or fast replay, don't send valid to tlb
322  s0_tlb_valid := (
323    s0_src_valid_vec(mab_idx) ||
324    s0_src_valid_vec(super_rep_idx) ||
325    s0_src_valid_vec(lsq_rep_idx) ||
326    s0_src_valid_vec(vec_iss_idx) ||
327    s0_src_valid_vec(int_iss_idx) ||
328    s0_src_valid_vec(l2l_fwd_idx)
329  ) && io.dcache.req.ready
330
331  // which is S0's out is ready and dcache is ready
332  val s0_try_ptr_chasing      = s0_src_select_vec(l2l_fwd_idx)
333  val s0_do_try_ptr_chasing   = s0_try_ptr_chasing && s0_can_go && io.dcache.req.ready
334  val s0_ptr_chasing_vaddr    = io.l2l_fwd_in.data(5, 0) +& io.ld_fast_imm(5, 0)
335  val s0_ptr_chasing_canceled = WireInit(false.B)
336  s0_kill := s0_ptr_chasing_canceled
337
338  // prefetch related ctrl signal
339  io.canAcceptLowConfPrefetch  := s0_src_ready_vec(low_pf_idx) && io.dcache.req.ready
340  io.canAcceptHighConfPrefetch := s0_src_ready_vec(high_pf_idx) && io.dcache.req.ready
341
342  // query DTLB
343  io.tlb.req.valid                   := s0_tlb_valid
344  io.tlb.req.bits.cmd                := Mux(s0_sel_src.prf,
345                                         Mux(s0_sel_src.prf_wr, TlbCmd.write, TlbCmd.read),
346                                         TlbCmd.read
347                                       )
348  io.tlb.req.bits.vaddr              := s0_tlb_vaddr
349  io.tlb.req.bits.hyperinst          := s0_tlb_hlv
350  io.tlb.req.bits.hlvx               := s0_tlb_hlvx
351  io.tlb.req.bits.size               := Mux(s0_sel_src.isvec, s0_sel_src.alignedType(2,0), LSUOpType.size(s0_sel_src.uop.fuOpType))
352  io.tlb.req.bits.kill               := s0_kill || s0_tlb_no_query // if does not need to be translated, kill it
353  io.tlb.req.bits.memidx.is_ld       := true.B
354  io.tlb.req.bits.memidx.is_st       := false.B
355  io.tlb.req.bits.memidx.idx         := s0_sel_src.uop.lqIdx.value
356  io.tlb.req.bits.debug.robIdx       := s0_sel_src.uop.robIdx
357  io.tlb.req.bits.no_translate       := s0_tlb_no_query  // hardware prefetch and fast replay does not need to be translated, need this signal for pmp check
358  io.tlb.req.bits.debug.pc           := s0_sel_src.uop.pc
359  io.tlb.req.bits.debug.isFirstIssue := s0_sel_src.isFirstIssue
360
361  // query DCache
362  io.dcache.req.valid             := s0_valid && !s0_sel_src.prf_i
363  io.dcache.req.bits.cmd          := Mux(s0_sel_src.prf_rd,
364                                      MemoryOpConstants.M_PFR,
365                                      Mux(s0_sel_src.prf_wr, MemoryOpConstants.M_PFW, MemoryOpConstants.M_XRD)
366                                    )
367  io.dcache.req.bits.vaddr        := s0_dcache_vaddr
368  io.dcache.req.bits.mask         := s0_sel_src.mask
369  io.dcache.req.bits.data         := DontCare
370  io.dcache.req.bits.isFirstIssue := s0_sel_src.isFirstIssue
371  io.dcache.req.bits.instrtype    := Mux(s0_sel_src.prf, DCACHE_PREFETCH_SOURCE.U, LOAD_SOURCE.U)
372  io.dcache.req.bits.debug_robIdx := s0_sel_src.uop.robIdx.value
373  io.dcache.req.bits.replayCarry  := s0_sel_src.rep_carry
374  io.dcache.req.bits.id           := DontCare // TODO: update cache meta
375  io.dcache.req.bits.lqIdx        := s0_sel_src.uop.lqIdx
376  io.dcache.pf_source             := Mux(s0_hw_prf_select, io.prefetch_req.bits.pf_source.value, L1_HW_PREFETCH_NULL)
377  io.dcache.is128Req              := s0_sel_src.is128bit
378
379  // load flow priority mux
380  def fromNullSource(): FlowSource = {
381    val out = WireInit(0.U.asTypeOf(new FlowSource))
382    out
383  }
384
385  def fromMisAlignBufferSource(src: LsPipelineBundle): FlowSource = {
386    val out = WireInit(0.U.asTypeOf(new FlowSource))
387    out.vaddr         := src.vaddr
388    out.mask          := src.mask
389    out.uop           := src.uop
390    out.try_l2l       := false.B
391    out.has_rob_entry := false.B
392    out.rep_carry     := src.replayCarry
393    out.mshrid        := src.mshrid
394    out.frm_mabuf     := true.B
395    out.isFirstIssue  := false.B
396    out.fast_rep      := false.B
397    out.ld_rep        := false.B
398    out.l2l_fwd       := false.B
399    out.prf           := false.B
400    out.prf_rd        := false.B
401    out.prf_wr        := false.B
402    out.sched_idx     := src.schedIndex
403    out.isvec         := false.B
404    out.is128bit      := src.is128bit
405    out.vecActive     := true.B
406    out
407  }
408
409  def fromFastReplaySource(src: LqWriteBundle): FlowSource = {
410    val out = WireInit(0.U.asTypeOf(new FlowSource))
411    out.mask          := src.mask
412    out.uop           := src.uop
413    out.try_l2l       := false.B
414    out.has_rob_entry := src.hasROBEntry
415    out.rep_carry     := src.rep_info.rep_carry
416    out.mshrid        := src.rep_info.mshr_id
417    out.frm_mabuf     := src.isFrmMisAlignBuf
418    out.isFirstIssue  := false.B
419    out.fast_rep      := true.B
420    out.ld_rep        := src.isLoadReplay
421    out.l2l_fwd       := false.B
422    out.prf           := LSUOpType.isPrefetch(src.uop.fuOpType) && !src.isvec
423    out.prf_rd        := src.uop.fuOpType === LSUOpType.prefetch_r
424    out.prf_wr        := src.uop.fuOpType === LSUOpType.prefetch_w
425    out.prf_i         := false.B
426    out.sched_idx     := src.schedIndex
427    out.isvec         := src.isvec
428    out.is128bit      := src.is128bit
429    out.uop_unit_stride_fof := src.uop_unit_stride_fof
430    out.reg_offset    := src.reg_offset
431    out.vecActive     := src.vecActive
432    out.is_first_ele  := src.is_first_ele
433    out.usSecondInv   := src.usSecondInv
434    out.mbIndex       := src.mbIndex
435    out.elemIdx       := src.elemIdx
436    out.elemIdxInsideVd := src.elemIdxInsideVd
437    out.alignedType   := src.alignedType
438    out
439  }
440
441  // TODO: implement vector mmio
442  def fromMmioSource(src: MemExuOutput) = {
443    val out = WireInit(0.U.asTypeOf(new FlowSource))
444    out.mask          := 0.U
445    out.uop           := src.uop
446    out.try_l2l       := false.B
447    out.has_rob_entry := false.B
448    out.rep_carry     := 0.U.asTypeOf(out.rep_carry)
449    out.mshrid        := 0.U
450    out.frm_mabuf     := false.B
451    out.isFirstIssue  := false.B
452    out.fast_rep      := false.B
453    out.ld_rep        := false.B
454    out.l2l_fwd       := false.B
455    out.prf           := false.B
456    out.prf_rd        := false.B
457    out.prf_wr        := false.B
458    out.prf_i         := false.B
459    out.sched_idx     := 0.U
460    out.vecActive     := true.B
461    out
462  }
463
464  def fromNormalReplaySource(src: LsPipelineBundle): FlowSource = {
465    val out = WireInit(0.U.asTypeOf(new FlowSource))
466    out.mask          := Mux(src.isvec, src.mask, genVWmask(src.vaddr, src.uop.fuOpType(1, 0)))
467    out.uop           := src.uop
468    out.try_l2l       := false.B
469    out.has_rob_entry := true.B
470    out.rep_carry     := src.replayCarry
471    out.mshrid        := src.mshrid
472    out.frm_mabuf     := false.B
473    out.isFirstIssue  := false.B
474    out.fast_rep      := false.B
475    out.ld_rep        := true.B
476    out.l2l_fwd       := false.B
477    out.prf           := LSUOpType.isPrefetch(src.uop.fuOpType) && !src.isvec
478    out.prf_rd        := src.uop.fuOpType === LSUOpType.prefetch_r
479    out.prf_wr        := src.uop.fuOpType === LSUOpType.prefetch_w
480    out.prf_i         := false.B
481    out.sched_idx     := src.schedIndex
482    out.isvec         := src.isvec
483    out.is128bit      := src.is128bit
484    out.uop_unit_stride_fof := src.uop_unit_stride_fof
485    out.reg_offset    := src.reg_offset
486    out.vecActive     := src.vecActive
487    out.is_first_ele  := src.is_first_ele
488    out.usSecondInv   := src.usSecondInv
489    out.mbIndex       := src.mbIndex
490    out.elemIdx       := src.elemIdx
491    out.elemIdxInsideVd := src.elemIdxInsideVd
492    out.alignedType   := src.alignedType
493    out
494  }
495
496  // TODO: implement vector prefetch
497  def fromPrefetchSource(src: L1PrefetchReq): FlowSource = {
498    val out = WireInit(0.U.asTypeOf(new FlowSource))
499    out.mask          := 0.U
500    out.uop           := DontCare
501    out.try_l2l       := false.B
502    out.has_rob_entry := false.B
503    out.rep_carry     := 0.U.asTypeOf(out.rep_carry)
504    out.mshrid        := 0.U
505    out.frm_mabuf     := false.B
506    out.isFirstIssue  := false.B
507    out.fast_rep      := false.B
508    out.ld_rep        := false.B
509    out.l2l_fwd       := false.B
510    out.prf           := true.B
511    out.prf_rd        := !src.is_store
512    out.prf_wr        := src.is_store
513    out.prf_i         := false.B
514    out.sched_idx     := 0.U
515    out
516  }
517
518  def fromVecIssueSource(src: VecPipeBundle): FlowSource = {
519    val out = WireInit(0.U.asTypeOf(new FlowSource))
520    out.mask          := src.mask
521    out.uop           := src.uop
522    out.try_l2l       := false.B
523    out.has_rob_entry := true.B
524    // TODO: VLSU, implement replay carry
525    out.rep_carry     := 0.U.asTypeOf(out.rep_carry)
526    out.mshrid        := 0.U
527    out.frm_mabuf     := false.B
528    // TODO: VLSU, implement first issue
529//    out.isFirstIssue  := src.isFirstIssue
530    out.fast_rep      := false.B
531    out.ld_rep        := false.B
532    out.l2l_fwd       := false.B
533    out.prf           := false.B
534    out.prf_rd        := false.B
535    out.prf_wr        := false.B
536    out.prf_i         := false.B
537    out.sched_idx     := 0.U
538    // Vector load interface
539    out.isvec               := true.B
540    // vector loads only access a single element at a time, so 128-bit path is not used for now
541    out.is128bit            := is128Bit(src.alignedType)
542    out.uop_unit_stride_fof := src.uop_unit_stride_fof
543    // out.rob_idx_valid       := src.rob_idx_valid
544    // out.inner_idx           := src.inner_idx
545    // out.rob_idx             := src.rob_idx
546    out.reg_offset          := src.reg_offset
547    // out.offset              := src.offset
548    out.vecActive           := src.vecActive
549    out.is_first_ele        := src.is_first_ele
550    // out.flowPtr             := src.flowPtr
551    out.usSecondInv         := src.usSecondInv
552    out.mbIndex             := src.mBIndex
553    out.elemIdx             := src.elemIdx
554    out.elemIdxInsideVd     := src.elemIdxInsideVd
555    out.alignedType         := src.alignedType
556    out
557  }
558
559  def fromIntIssueSource(src: MemExuInput): FlowSource = {
560    val out = WireInit(0.U.asTypeOf(new FlowSource))
561    val addr           = io.ldin.bits.src(0) + SignExt(io.ldin.bits.uop.imm(11, 0), VAddrBits)
562    out.mask          := genVWmask(addr, src.uop.fuOpType(1,0))
563    out.uop           := src.uop
564    out.try_l2l       := false.B
565    out.has_rob_entry := true.B
566    out.rep_carry     := 0.U.asTypeOf(out.rep_carry)
567    out.mshrid        := 0.U
568    out.frm_mabuf     := false.B
569    out.isFirstIssue  := true.B
570    out.fast_rep      := false.B
571    out.ld_rep        := false.B
572    out.l2l_fwd       := false.B
573    out.prf           := LSUOpType.isPrefetch(src.uop.fuOpType)
574    out.prf_rd        := src.uop.fuOpType === LSUOpType.prefetch_r
575    out.prf_wr        := src.uop.fuOpType === LSUOpType.prefetch_w
576    out.prf_i         := src.uop.fuOpType === LSUOpType.prefetch_i
577    out.sched_idx     := 0.U
578    out.vecActive     := true.B // true for scala load
579    out
580  }
581
582  // TODO: implement vector l2l
583  def fromLoadToLoadSource(src: LoadToLoadIO): FlowSource = {
584    val out = WireInit(0.U.asTypeOf(new FlowSource))
585    out.mask               := genVWmask(0.U, LSUOpType.ld)
586    // When there's no valid instruction from RS and LSQ, we try the load-to-load forwarding.
587    // Assume the pointer chasing is always ld.
588    out.uop.fuOpType       := LSUOpType.ld
589    out.try_l2l            := true.B
590    // we dont care out.isFirstIssue and out.rsIdx and s0_sqIdx in S0 when trying pointchasing
591    // because these signals will be updated in S1
592    out.has_rob_entry      := false.B
593    out.mshrid             := 0.U
594    out.frm_mabuf          := false.B
595    out.rep_carry          := 0.U.asTypeOf(out.rep_carry)
596    out.isFirstIssue       := true.B
597    out.fast_rep           := false.B
598    out.ld_rep             := false.B
599    out.l2l_fwd            := true.B
600    out.prf                := false.B
601    out.prf_rd             := false.B
602    out.prf_wr             := false.B
603    out.prf_i              := false.B
604    out.sched_idx          := 0.U
605    out
606  }
607
608  // set default
609  val s0_src_selector = WireInit(s0_src_valid_vec)
610  if (!EnableLoadToLoadForward) { s0_src_selector(l2l_fwd_idx) := false.B }
611  val s0_src_format = Seq(
612    fromMisAlignBufferSource(io.misalign_ldin.bits),
613    fromNormalReplaySource(io.replay.bits),
614    fromFastReplaySource(io.fast_rep_in.bits),
615    fromMmioSource(io.lsq.uncache.bits),
616    fromNormalReplaySource(io.replay.bits),
617    fromPrefetchSource(io.prefetch_req.bits),
618    fromVecIssueSource(io.vecldin.bits),
619    fromIntIssueSource(io.ldin.bits),
620    (if (EnableLoadToLoadForward) fromLoadToLoadSource(io.l2l_fwd_in) else fromNullSource()),
621    fromPrefetchSource(io.prefetch_req.bits)
622  )
623  s0_sel_src := ParallelPriorityMux(s0_src_selector, s0_src_format)
624
625  // fast replay and hardware prefetch don't need to query tlb
626  val int_issue_vaddr = io.ldin.bits.src(0) + SignExt(io.ldin.bits.uop.imm(11, 0), VAddrBits)
627  val int_vec_vaddr = Mux(s0_src_valid_vec(vec_iss_idx), io.vecldin.bits.vaddr, int_issue_vaddr)
628  s0_tlb_vaddr := Mux(
629    s0_src_valid_vec(mab_idx),
630    io.misalign_ldin.bits.vaddr,
631    Mux(
632      s0_src_valid_vec(super_rep_idx) || s0_src_valid_vec(lsq_rep_idx),
633      io.replay.bits.vaddr,
634      int_vec_vaddr
635    )
636  )
637  s0_dcache_vaddr := Mux(
638    s0_src_select_vec(fast_rep_idx),
639    io.fast_rep_in.bits.vaddr,
640    Mux(
641      s0_hw_prf_select,
642      io.prefetch_req.bits.getVaddr(),
643      s0_tlb_vaddr
644    )
645  )
646
647  s0_tlb_hlv := Mux(
648    s0_src_valid_vec(mab_idx),
649    LSUOpType.isHlv(io.misalign_ldin.bits.uop.fuOpType),
650    Mux(
651      s0_src_valid_vec(super_rep_idx) || s0_src_valid_vec(lsq_rep_idx),
652      LSUOpType.isHlv(io.replay.bits.uop.fuOpType),
653      Mux(
654        s0_src_valid_vec(int_iss_idx),
655        LSUOpType.isHlv(io.ldin.bits.uop.fuOpType),
656        false.B
657      )
658    )
659  )
660  s0_tlb_hlvx := Mux(
661    s0_src_valid_vec(mab_idx),
662    LSUOpType.isHlvx(io.misalign_ldin.bits.uop.fuOpType),
663    Mux(
664      s0_src_valid_vec(super_rep_idx) || s0_src_valid_vec(lsq_rep_idx),
665      LSUOpType.isHlvx(io.replay.bits.uop.fuOpType),
666      Mux(
667        s0_src_valid_vec(int_iss_idx),
668        LSUOpType.isHlvx(io.ldin.bits.uop.fuOpType),
669        false.B
670      )
671    )
672  )
673
674  // address align check
675  val s0_addr_aligned = LookupTree(Mux(s0_sel_src.isvec, s0_sel_src.alignedType(1,0), s0_sel_src.uop.fuOpType(1, 0)), List(
676    "b00".U   -> true.B,                   //b
677    "b01".U   -> (s0_dcache_vaddr(0)    === 0.U), //h
678    "b10".U   -> (s0_dcache_vaddr(1, 0) === 0.U), //w
679    "b11".U   -> (s0_dcache_vaddr(2, 0) === 0.U)  //d
680  ))
681  XSError(s0_sel_src.isvec && s0_dcache_vaddr(3, 0) =/= 0.U && s0_sel_src.alignedType(2), "unit-stride 128 bit element is not aligned!")
682
683  // accept load flow if dcache ready (tlb is always ready)
684  // TODO: prefetch need writeback to loadQueueFlag
685  s0_out               := DontCare
686  s0_out.vaddr         := s0_dcache_vaddr
687  s0_out.mask          := s0_sel_src.mask
688  s0_out.uop           := s0_sel_src.uop
689  s0_out.isFirstIssue  := s0_sel_src.isFirstIssue
690  s0_out.hasROBEntry   := s0_sel_src.has_rob_entry
691  s0_out.isPrefetch    := s0_sel_src.prf
692  s0_out.isHWPrefetch  := s0_hw_prf_select
693  s0_out.isFastReplay  := s0_sel_src.fast_rep
694  s0_out.isLoadReplay  := s0_sel_src.ld_rep
695  s0_out.isFastPath    := s0_sel_src.l2l_fwd
696  s0_out.mshrid        := s0_sel_src.mshrid
697  s0_out.isvec           := s0_sel_src.isvec
698  s0_out.is128bit        := s0_sel_src.is128bit
699  s0_out.isFrmMisAlignBuf    := s0_sel_src.frm_mabuf
700  s0_out.uop_unit_stride_fof := s0_sel_src.uop_unit_stride_fof
701  s0_out.paddr         := Mux(s0_src_valid_vec(fast_rep_idx), io.fast_rep_in.bits.paddr, io.prefetch_req.bits.paddr) // only for prefetch and fast_rep
702  s0_out.tlbNoQuery    := s0_tlb_no_query
703  // s0_out.rob_idx_valid   := s0_rob_idx_valid
704  // s0_out.inner_idx       := s0_inner_idx
705  // s0_out.rob_idx         := s0_rob_idx
706  s0_out.reg_offset      := s0_sel_src.reg_offset
707  // s0_out.offset          := s0_offset
708  s0_out.vecActive             := s0_sel_src.vecActive
709  s0_out.usSecondInv    := s0_sel_src.usSecondInv
710  s0_out.is_first_ele   := s0_sel_src.is_first_ele
711  s0_out.elemIdx        := s0_sel_src.elemIdx
712  s0_out.elemIdxInsideVd := s0_sel_src.elemIdxInsideVd
713  s0_out.alignedType    := s0_sel_src.alignedType
714  s0_out.mbIndex        := s0_sel_src.mbIndex
715  // s0_out.flowPtr         := s0_sel_src.flowPtr
716  s0_out.uop.exceptionVec(loadAddrMisaligned) := (!s0_addr_aligned || s0_sel_src.uop.exceptionVec(loadAddrMisaligned)) && s0_sel_src.vecActive
717  s0_out.forward_tlDchannel := s0_src_select_vec(super_rep_idx)
718  when(io.tlb.req.valid && s0_sel_src.isFirstIssue) {
719    s0_out.uop.debugInfo.tlbFirstReqTime := GTimer()
720  }.otherwise{
721    s0_out.uop.debugInfo.tlbFirstReqTime := s0_sel_src.uop.debugInfo.tlbFirstReqTime
722  }
723  s0_out.schedIndex     := s0_sel_src.sched_idx
724
725  // load fast replay
726  io.fast_rep_in.ready := (s0_can_go && io.dcache.req.ready && s0_src_ready_vec(fast_rep_idx))
727
728  // mmio
729  io.lsq.uncache.ready := s0_mmio_fire
730
731  // load flow source ready
732  // cache missed load has highest priority
733  // always accept cache missed load flow from load replay queue
734  io.replay.ready := (s0_can_go && io.dcache.req.ready && (s0_src_ready_vec(lsq_rep_idx) && !s0_rep_stall || s0_src_select_vec(super_rep_idx)))
735
736  // accept load flow from rs when:
737  // 1) there is no lsq-replayed load
738  // 2) there is no fast replayed load
739  // 3) there is no high confidence prefetch request
740  io.vecldin.ready := s0_can_go && io.dcache.req.ready && s0_src_ready_vec(vec_iss_idx)
741  io.ldin.ready := s0_can_go && io.dcache.req.ready && s0_src_ready_vec(int_iss_idx)
742  io.misalign_ldin.ready := s0_can_go && io.dcache.req.ready && s0_src_ready_vec(mab_idx)
743
744  // for hw prefetch load flow feedback, to be added later
745  // io.prefetch_in.ready := s0_hw_prf_select
746
747  // dcache replacement extra info
748  // TODO: should prefetch load update replacement?
749  io.dcache.replacementUpdated := Mux(s0_src_select_vec(lsq_rep_idx) || s0_src_select_vec(super_rep_idx), io.replay.bits.replacementUpdated, false.B)
750
751  // load wakeup
752  // TODO: vector load wakeup?
753  val s0_wakeup_selector = Seq(
754    s0_src_valid_vec(super_rep_idx),
755    s0_src_valid_vec(fast_rep_idx),
756    s0_mmio_fire,
757    s0_src_valid_vec(lsq_rep_idx),
758    s0_src_valid_vec(int_iss_idx)
759  )
760  val s0_wakeup_format = Seq(
761    io.replay.bits.uop,
762    io.fast_rep_in.bits.uop,
763    io.lsq.uncache.bits.uop,
764    io.replay.bits.uop,
765    io.ldin.bits.uop,
766  )
767  val s0_wakeup_uop = ParallelPriorityMux(s0_wakeup_selector, s0_wakeup_format)
768  io.wakeup.valid := s0_fire && !s0_sel_src.isvec && !s0_sel_src.frm_mabuf &&
769                    (s0_src_valid_vec(super_rep_idx) || s0_src_valid_vec(fast_rep_idx) || s0_src_valid_vec(lsq_rep_idx) || ((s0_src_valid_vec(int_iss_idx) && !s0_sel_src.prf) && !s0_src_valid_vec(vec_iss_idx) && !s0_src_valid_vec(high_pf_idx))) || s0_mmio_fire
770  io.wakeup.bits := s0_wakeup_uop
771
772  // prefetch.i(Zicbop)
773  io.ifetchPrefetch.valid := RegNext(s0_src_select_vec(int_iss_idx) && s0_sel_src.prf_i)
774  io.ifetchPrefetch.bits.vaddr := RegEnable(s0_out.vaddr, 0.U, s0_src_select_vec(int_iss_idx) && s0_sel_src.prf_i)
775
776  XSDebug(io.dcache.req.fire,
777    p"[DCACHE LOAD REQ] pc ${Hexadecimal(s0_sel_src.uop.pc)}, vaddr ${Hexadecimal(s0_dcache_vaddr)}\n"
778  )
779  XSDebug(s0_valid,
780    p"S0: pc ${Hexadecimal(s0_out.uop.pc)}, lId ${Hexadecimal(s0_out.uop.lqIdx.asUInt)}, " +
781    p"vaddr ${Hexadecimal(s0_out.vaddr)}, mask ${Hexadecimal(s0_out.mask)}\n")
782
783  // Pipeline
784  // --------------------------------------------------------------------------------
785  // stage 1
786  // --------------------------------------------------------------------------------
787  // TLB resp (send paddr to dcache)
788  val s1_valid      = RegInit(false.B)
789  val s1_in         = Wire(new LqWriteBundle)
790  val s1_out        = Wire(new LqWriteBundle)
791  val s1_kill       = Wire(Bool())
792  val s1_can_go     = s2_ready
793  val s1_fire       = s1_valid && !s1_kill && s1_can_go
794  val s1_vecActive        = RegEnable(s0_out.vecActive, true.B, s0_fire)
795
796  s1_ready := !s1_valid || s1_kill || s2_ready
797  when (s0_fire) { s1_valid := true.B }
798  .elsewhen (s1_fire) { s1_valid := false.B }
799  .elsewhen (s1_kill) { s1_valid := false.B }
800  s1_in   := RegEnable(s0_out, s0_fire)
801
802  val s1_fast_rep_dly_kill = RegEnable(io.fast_rep_in.bits.lateKill, io.fast_rep_in.valid) && s1_in.isFastReplay
803  val s1_fast_rep_dly_err =  RegEnable(io.fast_rep_in.bits.delayedLoadError, io.fast_rep_in.valid) && s1_in.isFastReplay
804  val s1_l2l_fwd_dly_err  = RegEnable(io.l2l_fwd_in.dly_ld_err, io.l2l_fwd_in.valid) && s1_in.isFastPath
805  val s1_dly_err          = s1_fast_rep_dly_err || s1_l2l_fwd_dly_err
806  val s1_vaddr_hi         = Wire(UInt())
807  val s1_vaddr_lo         = Wire(UInt())
808  val s1_vaddr            = Wire(UInt())
809  val s1_paddr_dup_lsu    = Wire(UInt())
810  val s1_gpaddr_dup_lsu   = Wire(UInt())
811  val s1_paddr_dup_dcache = Wire(UInt())
812  val s1_exception        = ExceptionNO.selectByFu(s1_out.uop.exceptionVec, LduCfg).asUInt.orR   // af & pf exception were modified below.
813  val s1_tlb_miss         = io.tlb.resp.bits.miss && io.tlb.resp.valid && s1_valid
814  val s1_tlb_fast_miss    = io.tlb.resp.bits.fastMiss && io.tlb.resp.valid && s1_valid
815  val s1_pbmt             = Mux(io.tlb.resp.valid, io.tlb.resp.bits.pbmt(0), 0.U(2.W))
816  val s1_prf              = s1_in.isPrefetch
817  val s1_hw_prf           = s1_in.isHWPrefetch
818  val s1_sw_prf           = s1_prf && !s1_hw_prf
819  val s1_tlb_memidx       = io.tlb.resp.bits.memidx
820
821  s1_vaddr_hi         := s1_in.vaddr(VAddrBits - 1, 6)
822  s1_vaddr_lo         := s1_in.vaddr(5, 0)
823  s1_vaddr            := Cat(s1_vaddr_hi, s1_vaddr_lo)
824  s1_paddr_dup_lsu    := Mux(s1_in.tlbNoQuery, s1_in.paddr, io.tlb.resp.bits.paddr(0))
825  s1_paddr_dup_dcache := Mux(s1_in.tlbNoQuery, s1_in.paddr, io.tlb.resp.bits.paddr(1))
826  s1_gpaddr_dup_lsu   := Mux(s1_in.isFastReplay, s1_in.paddr, io.tlb.resp.bits.gpaddr(0))
827
828  when (s1_tlb_memidx.is_ld && io.tlb.resp.valid && !s1_tlb_miss && s1_tlb_memidx.idx === s1_in.uop.lqIdx.value) {
829    // printf("load idx = %d\n", s1_tlb_memidx.idx)
830    s1_out.uop.debugInfo.tlbRespTime := GTimer()
831  }
832
833  io.tlb.req_kill   := s1_kill || s1_dly_err
834  io.tlb.req.bits.pmp_addr := s1_in.paddr
835  io.tlb.resp.ready := true.B
836
837  io.dcache.s1_paddr_dup_lsu    <> s1_paddr_dup_lsu
838  io.dcache.s1_paddr_dup_dcache <> s1_paddr_dup_dcache
839  io.dcache.s1_kill             := s1_kill || s1_dly_err || s1_tlb_miss || s1_exception
840  io.dcache.s1_kill_data_read   := s1_kill || s1_dly_err || s1_tlb_fast_miss
841
842  // store to load forwarding
843  io.sbuffer.valid := s1_valid && !(s1_exception || s1_tlb_miss || s1_kill || s1_dly_err || s1_prf)
844  io.sbuffer.vaddr := s1_vaddr
845  io.sbuffer.paddr := s1_paddr_dup_lsu
846  io.sbuffer.uop   := s1_in.uop
847  io.sbuffer.sqIdx := s1_in.uop.sqIdx
848  io.sbuffer.mask  := s1_in.mask
849  io.sbuffer.pc    := s1_in.uop.pc // FIXME: remove it
850
851  io.lsq.forward.valid     := s1_valid && !(s1_exception || s1_tlb_miss || s1_kill || s1_dly_err || s1_prf)
852  io.lsq.forward.vaddr     := s1_vaddr
853  io.lsq.forward.paddr     := s1_paddr_dup_lsu
854  io.lsq.forward.uop       := s1_in.uop
855  io.lsq.forward.sqIdx     := s1_in.uop.sqIdx
856  io.lsq.forward.sqIdxMask := 0.U
857  io.lsq.forward.mask      := s1_in.mask
858  io.lsq.forward.pc        := s1_in.uop.pc // FIXME: remove it
859
860  // st-ld violation query
861    // if store unit is 128-bits memory access, need match 128-bit
862  private val s1_isMatch128 = io.stld_nuke_query.map(x => (x.bits.matchLine || (s1_in.isvec && s1_in.is128bit)))
863  val s1_nuke_paddr_match = VecInit((0 until StorePipelineWidth).zip(s1_isMatch128).map{case (w, s) => {Mux(s,
864    s1_paddr_dup_lsu(PAddrBits-1, 4) === io.stld_nuke_query(w).bits.paddr(PAddrBits-1, 4),
865    s1_paddr_dup_lsu(PAddrBits-1, 3) === io.stld_nuke_query(w).bits.paddr(PAddrBits-1, 3))}})
866  val s1_nuke = VecInit((0 until StorePipelineWidth).map(w => {
867                       io.stld_nuke_query(w).valid && // query valid
868                       isAfter(s1_in.uop.robIdx, io.stld_nuke_query(w).bits.robIdx) && // older store
869                       s1_nuke_paddr_match(w) && // paddr match
870                       (s1_in.mask & io.stld_nuke_query(w).bits.mask).orR // data mask contain
871                      })).asUInt.orR && !s1_tlb_miss
872
873  s1_out                   := s1_in
874  s1_out.vaddr             := s1_vaddr
875  s1_out.paddr             := s1_paddr_dup_lsu
876  s1_out.gpaddr            := s1_gpaddr_dup_lsu
877  s1_out.tlbMiss           := s1_tlb_miss
878  s1_out.ptwBack           := io.tlb.resp.bits.ptwBack
879  s1_out.rep_info.debug    := s1_in.uop.debugInfo
880  s1_out.rep_info.nuke     := s1_nuke && !s1_sw_prf
881  s1_out.delayedLoadError  := s1_dly_err
882
883  when (!s1_dly_err) {
884    // current ori test will cause the case of ldest == 0, below will be modifeid in the future.
885    // af & pf exception were modified
886    // if is tlbNoQuery request, don't trigger exception from tlb resp
887    s1_out.uop.exceptionVec(loadPageFault)   := io.tlb.resp.bits.excp(0).pf.ld && s1_vecActive && !s1_tlb_miss && !s1_in.tlbNoQuery
888    s1_out.uop.exceptionVec(loadGuestPageFault)   := io.tlb.resp.bits.excp(0).gpf.ld && !s1_tlb_miss && !s1_in.tlbNoQuery
889    s1_out.uop.exceptionVec(loadAccessFault) := io.tlb.resp.bits.excp(0).af.ld && s1_vecActive && !s1_tlb_miss && !s1_in.tlbNoQuery
890  } .otherwise {
891    s1_out.uop.exceptionVec(loadPageFault)      := false.B
892    s1_out.uop.exceptionVec(loadGuestPageFault) := false.B
893    s1_out.uop.exceptionVec(loadAddrMisaligned) := false.B
894    s1_out.uop.exceptionVec(loadAccessFault)    := s1_dly_err && s1_vecActive
895  }
896
897  // pointer chasing
898  val s1_try_ptr_chasing       = GatedValidRegNext(s0_do_try_ptr_chasing, false.B)
899  val s1_ptr_chasing_vaddr     = RegEnable(s0_ptr_chasing_vaddr, s0_do_try_ptr_chasing)
900  val s1_fu_op_type_not_ld     = WireInit(false.B)
901  val s1_not_fast_match        = WireInit(false.B)
902  val s1_addr_mismatch         = WireInit(false.B)
903  val s1_addr_misaligned       = WireInit(false.B)
904  val s1_fast_mismatch         = WireInit(false.B)
905  val s1_ptr_chasing_canceled  = WireInit(false.B)
906  val s1_cancel_ptr_chasing    = WireInit(false.B)
907
908  val s1_redirect_reg = Wire(Valid(new Redirect))
909  s1_redirect_reg.bits := RegEnable(io.redirect.bits, io.redirect.valid)
910  s1_redirect_reg.valid := GatedValidRegNext(io.redirect.valid)
911
912  s1_kill := s1_fast_rep_dly_kill ||
913             s1_cancel_ptr_chasing ||
914             s1_in.uop.robIdx.needFlush(io.redirect) ||
915            (s1_in.uop.robIdx.needFlush(s1_redirect_reg) && !GatedValidRegNext(s0_try_ptr_chasing)) ||
916             RegEnable(s0_kill, false.B, io.ldin.valid || io.vecldin.valid || io.replay.valid || io.l2l_fwd_in.valid || io.fast_rep_in.valid || io.misalign_ldin.valid)
917
918  if (EnableLoadToLoadForward) {
919    // Sometimes, we need to cancel the load-load forwarding.
920    // These can be put at S0 if timing is bad at S1.
921    // Case 0: CACHE_SET(base + offset) != CACHE_SET(base) (lowest 6-bit addition has an overflow)
922    s1_addr_mismatch     := s1_ptr_chasing_vaddr(6) ||
923                             RegEnable(io.ld_fast_imm(11, 6).orR, s0_do_try_ptr_chasing)
924    // Case 1: the address is not 64-bit aligned or the fuOpType is not LD
925    s1_addr_misaligned := s1_ptr_chasing_vaddr(2, 0).orR
926    s1_fu_op_type_not_ld := io.ldin.bits.uop.fuOpType =/= LSUOpType.ld
927    // Case 2: this load-load uop is cancelled
928    s1_ptr_chasing_canceled := !io.ldin.valid
929    // Case 3: fast mismatch
930    s1_fast_mismatch := RegEnable(!io.ld_fast_match, s0_do_try_ptr_chasing)
931
932    when (s1_try_ptr_chasing) {
933      s1_cancel_ptr_chasing := s1_addr_mismatch ||
934                               s1_addr_misaligned ||
935                               s1_fu_op_type_not_ld ||
936                               s1_ptr_chasing_canceled ||
937                               s1_fast_mismatch
938
939      s1_in.uop           := io.ldin.bits.uop
940      s1_in.isFirstIssue  := io.ldin.bits.isFirstIssue
941      s1_vaddr_lo         := s1_ptr_chasing_vaddr(5, 0)
942      s1_paddr_dup_lsu    := Cat(io.tlb.resp.bits.paddr(0)(PAddrBits - 1, 6), s1_vaddr_lo)
943      s1_paddr_dup_dcache := Cat(io.tlb.resp.bits.paddr(0)(PAddrBits - 1, 6), s1_vaddr_lo)
944
945      // recored tlb time when get the data to ensure the correctness of the latency calculation (although it should not record in here, because it does not use tlb)
946      s1_in.uop.debugInfo.tlbFirstReqTime := GTimer()
947      s1_in.uop.debugInfo.tlbRespTime     := GTimer()
948    }
949    when (!s1_cancel_ptr_chasing) {
950      s0_ptr_chasing_canceled := s1_try_ptr_chasing && !io.replay.fire && !io.fast_rep_in.fire && !(s0_src_valid_vec(high_pf_idx) && io.canAcceptHighConfPrefetch) && !io.misalign_ldin.fire
951      when (s1_try_ptr_chasing) {
952        io.ldin.ready := true.B
953      }
954    }
955  }
956
957  // pre-calcuate sqIdx mask in s0, then send it to lsq in s1 for forwarding
958  val s1_sqIdx_mask = RegEnable(UIntToMask(s0_out.uop.sqIdx.value, StoreQueueSize), s0_fire)
959  // to enable load-load, sqIdxMask must be calculated based on ldin.uop
960  // If the timing here is not OK, load-load forwarding has to be disabled.
961  // Or we calculate sqIdxMask at RS??
962  io.lsq.forward.sqIdxMask := s1_sqIdx_mask
963  if (EnableLoadToLoadForward) {
964    when (s1_try_ptr_chasing) {
965      io.lsq.forward.sqIdxMask := UIntToMask(io.ldin.bits.uop.sqIdx.value, StoreQueueSize)
966    }
967  }
968
969  io.forward_mshr.valid  := s1_valid && s1_out.forward_tlDchannel
970  io.forward_mshr.mshrid := s1_out.mshrid
971  io.forward_mshr.paddr  := s1_out.paddr
972
973  XSDebug(s1_valid,
974    p"S1: pc ${Hexadecimal(s1_out.uop.pc)}, lId ${Hexadecimal(s1_out.uop.lqIdx.asUInt)}, tlb_miss ${io.tlb.resp.bits.miss}, " +
975    p"paddr ${Hexadecimal(s1_out.paddr)}, mmio ${s1_out.mmio}\n")
976
977  // Pipeline
978  // --------------------------------------------------------------------------------
979  // stage 2
980  // --------------------------------------------------------------------------------
981  // s2: DCache resp
982  val s2_valid  = RegInit(false.B)
983  val s2_in     = Wire(new LqWriteBundle)
984  val s2_out    = Wire(new LqWriteBundle)
985  val s2_kill   = Wire(Bool())
986  val s2_can_go = s3_ready
987  val s2_fire   = s2_valid && !s2_kill && s2_can_go
988  val s2_vecActive = RegEnable(s1_out.vecActive, true.B, s1_fire)
989  val s2_isvec  = RegEnable(s1_out.isvec, false.B, s1_fire)
990  val s2_data_select  = genRdataOH(s2_out.uop)
991  val s2_data_select_by_offset = genDataSelectByOffset(s2_out.paddr(2, 0))
992  val s2_frm_mabuf = s2_in.isFrmMisAlignBuf
993  val s2_pbmt = RegEnable(s1_pbmt, s1_fire)
994
995  s2_kill := s2_in.uop.robIdx.needFlush(io.redirect)
996  s2_ready := !s2_valid || s2_kill || s3_ready
997  when (s1_fire) { s2_valid := true.B }
998  .elsewhen (s2_fire) { s2_valid := false.B }
999  .elsewhen (s2_kill) { s2_valid := false.B }
1000  s2_in := RegEnable(s1_out, s1_fire)
1001
1002  val s2_pmp = WireInit(io.pmp)
1003
1004  val s2_prf    = s2_in.isPrefetch
1005  val s2_hw_prf = s2_in.isHWPrefetch
1006
1007  // exception that may cause load addr to be invalid / illegal
1008  // if such exception happen, that inst and its exception info
1009  // will be force writebacked to rob
1010  val s2_exception_vec = WireInit(s2_in.uop.exceptionVec)
1011  when (!s2_in.delayedLoadError) {
1012    s2_exception_vec(loadAccessFault) := (s2_in.uop.exceptionVec(loadAccessFault) ||
1013                                         s2_pmp.ld ||
1014                                         s2_isvec && s2_pmp.mmio && !s2_prf && !s2_in.tlbMiss ||
1015                                         (io.dcache.resp.bits.tag_error && GatedValidRegNext(io.csrCtrl.cache_error_enable))
1016                                         ) && s2_vecActive
1017  }
1018
1019  // soft prefetch will not trigger any exception (but ecc error interrupt may
1020  // be triggered)
1021  when (!s2_in.delayedLoadError && (s2_prf || s2_in.tlbMiss)) {
1022    s2_exception_vec := 0.U.asTypeOf(s2_exception_vec.cloneType)
1023  }
1024  val s2_exception = ExceptionNO.selectByFu(s2_exception_vec, LduCfg).asUInt.orR && s2_vecActive
1025  val s2_mis_align = s2_valid && s2_exception_vec(loadAddrMisaligned) && GatedValidRegNext(io.csrCtrl.hd_misalign_ld_enable) && !s2_in.isvec
1026
1027  val (s2_fwd_frm_d_chan, s2_fwd_data_frm_d_chan) = io.tl_d_channel.forward(s1_valid && s1_out.forward_tlDchannel, s1_out.mshrid, s1_out.paddr)
1028  val (s2_fwd_data_valid, s2_fwd_frm_mshr, s2_fwd_data_frm_mshr) = io.forward_mshr.forward()
1029  val s2_fwd_frm_d_chan_or_mshr = s2_fwd_data_valid && (s2_fwd_frm_d_chan || s2_fwd_frm_mshr)
1030
1031  // writeback access fault caused by ecc error / bus error
1032  // * ecc data error is slow to generate, so we will not use it until load stage 3
1033  // * in load stage 3, an extra signal io.load_error will be used to
1034  val s2_actually_mmio = s2_pmp.mmio || Pbmt.isUncache(s2_pbmt)
1035  val s2_mmio          = !s2_prf &&
1036                          s2_actually_mmio &&
1037                         !s2_exception &&
1038                         !s2_in.tlbMiss
1039
1040  val s2_full_fwd      = Wire(Bool())
1041  val s2_mem_amb       = s2_in.uop.storeSetHit &&
1042                         io.lsq.forward.addrInvalid && RegNext(io.lsq.forward.valid)
1043
1044  val s2_tlb_miss      = s2_in.tlbMiss
1045  val s2_fwd_fail      = io.lsq.forward.dataInvalid && RegNext(io.lsq.forward.valid)
1046  val s2_dcache_miss   = io.dcache.resp.bits.miss &&
1047                         !s2_fwd_frm_d_chan_or_mshr &&
1048                         !s2_full_fwd
1049
1050  val s2_mq_nack       = io.dcache.s2_mq_nack &&
1051                         !s2_fwd_frm_d_chan_or_mshr &&
1052                         !s2_full_fwd
1053
1054  val s2_bank_conflict = io.dcache.s2_bank_conflict &&
1055                         !s2_fwd_frm_d_chan_or_mshr &&
1056                         !s2_full_fwd
1057
1058  val s2_wpu_pred_fail = io.dcache.s2_wpu_pred_fail &&
1059                        !s2_fwd_frm_d_chan_or_mshr &&
1060                        !s2_full_fwd
1061
1062  val s2_rar_nack      = io.lsq.ldld_nuke_query.req.valid &&
1063                         !io.lsq.ldld_nuke_query.req.ready
1064
1065  val s2_raw_nack      = io.lsq.stld_nuke_query.req.valid &&
1066                         !io.lsq.stld_nuke_query.req.ready
1067  // st-ld violation query
1068  //  NeedFastRecovery Valid when
1069  //  1. Fast recovery query request Valid.
1070  //  2. Load instruction is younger than requestors(store instructions).
1071  //  3. Physical address match.
1072  //  4. Data contains.
1073  private val s2_isMatch128 = io.stld_nuke_query.map(x => (x.bits.matchLine || (s2_in.isvec && s2_in.is128bit)))
1074  val s2_nuke_paddr_match = VecInit((0 until StorePipelineWidth).zip(s2_isMatch128).map{case (w, s) => {Mux(s,
1075    s2_in.paddr(PAddrBits-1, 4) === io.stld_nuke_query(w).bits.paddr(PAddrBits-1, 4),
1076    s2_in.paddr(PAddrBits-1, 3) === io.stld_nuke_query(w).bits.paddr(PAddrBits-1, 3))}})
1077  val s2_nuke          = VecInit((0 until StorePipelineWidth).map(w => {
1078                          io.stld_nuke_query(w).valid && // query valid
1079                          isAfter(s2_in.uop.robIdx, io.stld_nuke_query(w).bits.robIdx) && // older store
1080                          s2_nuke_paddr_match(w) && // paddr match
1081                          (s2_in.mask & io.stld_nuke_query(w).bits.mask).orR // data mask contain
1082                        })).asUInt.orR && !s2_tlb_miss || s2_in.rep_info.nuke
1083
1084  val s2_cache_handled   = io.dcache.resp.bits.handled
1085  val s2_cache_tag_error = GatedValidRegNext(io.csrCtrl.cache_error_enable) &&
1086                           io.dcache.resp.bits.tag_error
1087
1088  val s2_troublem        = !s2_exception &&
1089                           !s2_mmio &&
1090                           !s2_prf &&
1091                           !s2_in.delayedLoadError
1092
1093  io.dcache.resp.ready  := true.B
1094  val s2_dcache_should_resp = !(s2_in.tlbMiss || s2_exception || s2_in.delayedLoadError || s2_mmio || s2_prf)
1095  assert(!(s2_valid && (s2_dcache_should_resp && !io.dcache.resp.valid)), "DCache response got lost")
1096
1097  // fast replay require
1098  val s2_dcache_fast_rep = (s2_mq_nack || !s2_dcache_miss && (s2_bank_conflict || s2_wpu_pred_fail))
1099  val s2_nuke_fast_rep   = !s2_mq_nack &&
1100                           !s2_dcache_miss &&
1101                           !s2_bank_conflict &&
1102                           !s2_wpu_pred_fail &&
1103                           !s2_rar_nack &&
1104                           !s2_raw_nack &&
1105                           s2_nuke
1106
1107  val s2_fast_rep = !s2_mem_amb &&
1108                    !s2_tlb_miss &&
1109                    !s2_fwd_fail &&
1110                    (s2_dcache_fast_rep || s2_nuke_fast_rep) &&
1111                    s2_troublem
1112
1113  // need allocate new entry
1114  val s2_can_query = !s2_mem_amb &&
1115                     !s2_tlb_miss &&
1116                     !s2_fwd_fail &&
1117                     !s2_frm_mabuf &&
1118                     s2_troublem
1119
1120  val s2_data_fwded = s2_dcache_miss && (s2_full_fwd || s2_cache_tag_error)
1121
1122  val s2_vp_match_fail = (io.lsq.forward.matchInvalid || io.sbuffer.matchInvalid) && s2_troublem
1123  val s2_safe_wakeup = !s2_out.rep_info.need_rep && !s2_mmio && !s2_mis_align && !s2_exception // don't need to replay and is not a mmio and misalign
1124  val s2_safe_writeback = s2_exception || s2_safe_wakeup || s2_vp_match_fail
1125
1126  // ld-ld violation require
1127  io.lsq.ldld_nuke_query.req.valid           := s2_valid && s2_can_query
1128  io.lsq.ldld_nuke_query.req.bits.uop        := s2_in.uop
1129  io.lsq.ldld_nuke_query.req.bits.mask       := s2_in.mask
1130  io.lsq.ldld_nuke_query.req.bits.paddr      := s2_in.paddr
1131  io.lsq.ldld_nuke_query.req.bits.data_valid := Mux(s2_full_fwd || s2_fwd_data_valid, true.B, !s2_dcache_miss)
1132
1133  // st-ld violation require
1134  io.lsq.stld_nuke_query.req.valid           := s2_valid && s2_can_query
1135  io.lsq.stld_nuke_query.req.bits.uop        := s2_in.uop
1136  io.lsq.stld_nuke_query.req.bits.mask       := s2_in.mask
1137  io.lsq.stld_nuke_query.req.bits.paddr      := s2_in.paddr
1138  io.lsq.stld_nuke_query.req.bits.data_valid := Mux(s2_full_fwd || s2_fwd_data_valid, true.B, !s2_dcache_miss)
1139
1140  // merge forward result
1141  // lsq has higher priority than sbuffer
1142  val s2_fwd_mask = Wire(Vec((VLEN/8), Bool()))
1143  val s2_fwd_data = Wire(Vec((VLEN/8), UInt(8.W)))
1144  s2_full_fwd := ((~s2_fwd_mask.asUInt).asUInt & s2_in.mask) === 0.U && !io.lsq.forward.dataInvalid
1145  // generate XLEN/8 Muxs
1146  for (i <- 0 until VLEN / 8) {
1147    s2_fwd_mask(i) := io.lsq.forward.forwardMask(i) || io.sbuffer.forwardMask(i)
1148    s2_fwd_data(i) := Mux(io.lsq.forward.forwardMask(i), io.lsq.forward.forwardData(i), io.sbuffer.forwardData(i))
1149  }
1150
1151  XSDebug(s2_fire, "[FWD LOAD RESP] pc %x fwd %x(%b) + %x(%b)\n",
1152    s2_in.uop.pc,
1153    io.lsq.forward.forwardData.asUInt, io.lsq.forward.forwardMask.asUInt,
1154    s2_in.forwardData.asUInt, s2_in.forwardMask.asUInt
1155  )
1156
1157  //
1158  s2_out                     := s2_in
1159  s2_out.data                := 0.U // data will be generated in load s3
1160  s2_out.uop.fpWen           := s2_in.uop.fpWen
1161  s2_out.mmio                := s2_mmio
1162  s2_out.uop.flushPipe       := false.B
1163  s2_out.uop.exceptionVec    := s2_exception_vec
1164  s2_out.forwardMask         := s2_fwd_mask
1165  s2_out.forwardData         := s2_fwd_data
1166  s2_out.handledByMSHR       := s2_cache_handled
1167  s2_out.miss                := s2_dcache_miss && s2_troublem
1168  s2_out.feedbacked          := io.feedback_fast.valid
1169
1170  // Generate replay signal caused by:
1171  // * st-ld violation check
1172  // * tlb miss
1173  // * dcache replay
1174  // * forward data invalid
1175  // * dcache miss
1176  s2_out.rep_info.mem_amb         := s2_mem_amb && s2_troublem
1177  s2_out.rep_info.tlb_miss        := s2_tlb_miss && s2_troublem
1178  s2_out.rep_info.fwd_fail        := s2_fwd_fail && s2_troublem
1179  s2_out.rep_info.dcache_rep      := s2_mq_nack && s2_troublem
1180  s2_out.rep_info.dcache_miss     := s2_dcache_miss && s2_troublem
1181  s2_out.rep_info.bank_conflict   := s2_bank_conflict && s2_troublem
1182  s2_out.rep_info.wpu_fail        := s2_wpu_pred_fail && s2_troublem
1183  s2_out.rep_info.rar_nack        := s2_rar_nack && s2_troublem
1184  s2_out.rep_info.raw_nack        := s2_raw_nack && s2_troublem
1185  s2_out.rep_info.nuke            := s2_nuke && s2_troublem
1186  s2_out.rep_info.full_fwd        := s2_data_fwded
1187  s2_out.rep_info.data_inv_sq_idx := io.lsq.forward.dataInvalidSqIdx
1188  s2_out.rep_info.addr_inv_sq_idx := io.lsq.forward.addrInvalidSqIdx
1189  s2_out.rep_info.rep_carry       := io.dcache.resp.bits.replayCarry
1190  s2_out.rep_info.mshr_id         := io.dcache.resp.bits.mshr_id
1191  s2_out.rep_info.last_beat       := s2_in.paddr(log2Up(refillBytes))
1192  s2_out.rep_info.debug           := s2_in.uop.debugInfo
1193  s2_out.rep_info.tlb_id          := io.tlb_hint.id
1194  s2_out.rep_info.tlb_full        := io.tlb_hint.full
1195
1196  // if forward fail, replay this inst from fetch
1197  val debug_fwd_fail_rep = s2_fwd_fail && !s2_troublem && !s2_in.tlbMiss
1198  // if ld-ld violation is detected, replay from this inst from fetch
1199  val debug_ldld_nuke_rep = false.B // s2_ldld_violation && !s2_mmio && !s2_is_prefetch && !s2_in.tlbMiss
1200
1201  // to be removed
1202  io.feedback_fast.valid                 := false.B
1203  io.feedback_fast.bits.hit              := false.B
1204  io.feedback_fast.bits.flushState       := s2_in.ptwBack
1205  io.feedback_fast.bits.robIdx           := s2_in.uop.robIdx
1206  io.feedback_fast.bits.sqIdx            := s2_in.uop.sqIdx
1207  io.feedback_fast.bits.lqIdx            := s2_in.uop.lqIdx
1208  io.feedback_fast.bits.sourceType       := RSFeedbackType.lrqFull
1209  io.feedback_fast.bits.dataInvalidSqIdx := DontCare
1210
1211  io.ldCancel.ld1Cancel := false.B
1212
1213  // fast wakeup
1214  val s1_fast_uop_valid = WireInit(false.B)
1215  s1_fast_uop_valid :=
1216    !io.dcache.s1_disable_fast_wakeup &&
1217    s1_valid &&
1218    !s1_kill &&
1219    !io.tlb.resp.bits.miss &&
1220    !io.lsq.forward.dataInvalidFast
1221  io.fast_uop.valid := GatedValidRegNext(s1_fast_uop_valid) && (s2_valid && !s2_out.rep_info.need_rep && !s2_mmio && !(s2_prf && !s2_hw_prf)) && !s2_isvec && !s2_frm_mabuf
1222  io.fast_uop.bits := RegEnable(s1_out.uop, s1_fast_uop_valid)
1223
1224  //
1225  io.s2_ptr_chasing                    := RegEnable(s1_try_ptr_chasing && !s1_cancel_ptr_chasing, false.B, s1_fire)
1226
1227  // RegNext prefetch train for better timing
1228  // ** Now, prefetch train is valid at load s3 **
1229  val s2_prefetch_train_valid = WireInit(false.B)
1230  s2_prefetch_train_valid              := s2_valid && !s2_actually_mmio && (!s2_in.tlbMiss || s2_hw_prf)
1231  io.prefetch_train.valid              := GatedValidRegNext(s2_prefetch_train_valid)
1232  io.prefetch_train.bits.fromLsPipelineBundle(s2_in, latch = true, enable = s2_prefetch_train_valid)
1233  io.prefetch_train.bits.miss          := RegEnable(io.dcache.resp.bits.miss, s2_prefetch_train_valid) // TODO: use trace with bank conflict?
1234  io.prefetch_train.bits.meta_prefetch := RegEnable(io.dcache.resp.bits.meta_prefetch, s2_prefetch_train_valid)
1235  io.prefetch_train.bits.meta_access   := RegEnable(io.dcache.resp.bits.meta_access, s2_prefetch_train_valid)
1236  io.s1_prefetch_spec := s1_fire
1237  io.s2_prefetch_spec := s2_prefetch_train_valid
1238
1239  val s2_prefetch_train_l1_valid = WireInit(false.B)
1240  s2_prefetch_train_l1_valid              := s2_valid && !s2_actually_mmio
1241  io.prefetch_train_l1.valid              := GatedValidRegNext(s2_prefetch_train_l1_valid)
1242  io.prefetch_train_l1.bits.fromLsPipelineBundle(s2_in, latch = true, enable = s2_prefetch_train_l1_valid)
1243  io.prefetch_train_l1.bits.miss          := RegEnable(io.dcache.resp.bits.miss, s2_prefetch_train_l1_valid)
1244  io.prefetch_train_l1.bits.meta_prefetch := RegEnable(io.dcache.resp.bits.meta_prefetch, s2_prefetch_train_l1_valid)
1245  io.prefetch_train_l1.bits.meta_access   := RegEnable(io.dcache.resp.bits.meta_access, s2_prefetch_train_l1_valid)
1246  if (env.FPGAPlatform){
1247    io.dcache.s0_pc := DontCare
1248    io.dcache.s1_pc := DontCare
1249    io.dcache.s2_pc := DontCare
1250  }else{
1251    io.dcache.s0_pc := s0_out.uop.pc
1252    io.dcache.s1_pc := s1_out.uop.pc
1253    io.dcache.s2_pc := s2_out.uop.pc
1254  }
1255  io.dcache.s2_kill := s2_pmp.ld || s2_actually_mmio || s2_kill
1256
1257  val s1_ld_left_fire = s1_valid && !s1_kill && s2_ready
1258  val s2_ld_valid_dup = RegInit(0.U(6.W))
1259  s2_ld_valid_dup := 0x0.U(6.W)
1260  when (s1_ld_left_fire && !s1_out.isHWPrefetch) { s2_ld_valid_dup := 0x3f.U(6.W) }
1261  when (s1_kill || s1_out.isHWPrefetch) { s2_ld_valid_dup := 0x0.U(6.W) }
1262  assert(RegNext((s2_valid === s2_ld_valid_dup(0)) || RegNext(s1_out.isHWPrefetch)))
1263
1264  // Pipeline
1265  // --------------------------------------------------------------------------------
1266  // stage 3
1267  // --------------------------------------------------------------------------------
1268  // writeback and update load queue
1269  val s3_valid        = GatedValidRegNext(s2_valid && !s2_out.isHWPrefetch && !s2_out.uop.robIdx.needFlush(io.redirect))
1270  val s3_in           = RegEnable(s2_out, s2_fire)
1271  val s3_out          = Wire(Valid(new MemExuOutput))
1272  val s3_dcache_rep   = RegEnable(s2_dcache_fast_rep && s2_troublem, false.B, s2_fire)
1273  val s3_ld_valid_dup = RegEnable(s2_ld_valid_dup, s2_fire)
1274  val s3_fast_rep     = Wire(Bool())
1275  val s3_troublem     = GatedValidRegNext(s2_troublem)
1276  val s3_kill         = s3_in.uop.robIdx.needFlush(io.redirect)
1277  val s3_vecout       = Wire(new OnlyVecExuOutput)
1278  val s3_vecActive    = RegEnable(s2_out.vecActive, true.B, s2_fire)
1279  val s3_isvec        = RegEnable(s2_out.isvec, false.B, s2_fire)
1280  val s3_vec_alignedType = RegEnable(s2_out.alignedType, s2_fire)
1281  val s3_vec_mBIndex     = RegEnable(s2_out.mbIndex, s2_fire)
1282  val s3_frm_mabuf       = s3_in.isFrmMisAlignBuf
1283  val s3_mmio         = Wire(Valid(new MemExuOutput))
1284  val s3_data_select  = RegEnable(s2_data_select, 0.U(s2_data_select.getWidth.W), s2_fire)
1285  val s3_data_select_by_offset = RegEnable(s2_data_select_by_offset, 0.U.asTypeOf(s2_data_select_by_offset), s2_fire)
1286  val s3_dly_ld_err   =
1287      if (EnableAccurateLoadError) {
1288        io.dcache.resp.bits.error_delayed && GatedValidRegNext(io.csrCtrl.cache_error_enable) && s3_troublem
1289      } else {
1290        WireInit(false.B)
1291      }
1292  val s3_safe_wakeup  = RegEnable(s2_safe_wakeup, s2_fire)
1293  val s3_safe_writeback = RegEnable(s2_safe_writeback, s2_fire) || s3_dly_ld_err
1294  val s3_exception = RegEnable(s2_exception, s2_fire)
1295  val s3_mis_align = RegEnable(s2_mis_align, s2_fire)
1296  // TODO: Fix vector load merge buffer nack
1297  val s3_vec_mb_nack  = Wire(Bool())
1298  s3_vec_mb_nack     := false.B
1299  XSError(s3_valid && s3_vec_mb_nack, "Merge buffer should always accept vector loads!")
1300
1301  s3_ready := !s3_valid || s3_kill || io.ldout.ready
1302  s3_mmio.valid := RegNextN(io.lsq.uncache.fire, 3, Some(false.B))
1303  s3_mmio.bits  := RegNextN(io.lsq.uncache.bits, 3)
1304
1305  // forwrad last beat
1306  val s3_fast_rep_canceled = io.replay.valid && io.replay.bits.forward_tlDchannel || io.misalign_ldin.valid || !io.dcache.req.ready
1307
1308  // s3 load fast replay
1309  io.fast_rep_out.valid := s3_valid && s3_fast_rep && !s3_in.uop.robIdx.needFlush(io.redirect)
1310  io.fast_rep_out.bits := s3_in
1311
1312  io.lsq.ldin.valid := s3_valid && (!s3_fast_rep || s3_fast_rep_canceled) && !s3_in.feedbacked && !s3_frm_mabuf
1313  // TODO: check this --by hx
1314  // io.lsq.ldin.valid := s3_valid && (!s3_fast_rep || !io.fast_rep_out.ready) && !s3_in.feedbacked && !s3_in.lateKill
1315  io.lsq.ldin.bits := s3_in
1316  io.lsq.ldin.bits.miss := s3_in.miss
1317
1318  // connect to misalignBuffer
1319  io.misalign_buf.valid := io.lsq.ldin.valid && GatedValidRegNext(io.csrCtrl.hd_misalign_ld_enable) && !io.lsq.ldin.bits.isvec
1320  io.misalign_buf.bits  := s3_in
1321
1322  /* <------- DANGEROUS: Don't change sequence here ! -------> */
1323  io.lsq.ldin.bits.data_wen_dup := s3_ld_valid_dup.asBools
1324  io.lsq.ldin.bits.replacementUpdated := io.dcache.resp.bits.replacementUpdated
1325  io.lsq.ldin.bits.missDbUpdated := GatedValidRegNext(s2_fire && s2_in.hasROBEntry && !s2_in.tlbMiss && !s2_in.missDbUpdated)
1326
1327  io.s3_dly_ld_err := false.B // s3_dly_ld_err && s3_valid
1328  io.lsq.ldin.bits.dcacheRequireReplay  := s3_dcache_rep
1329  io.fast_rep_out.bits.delayedLoadError := s3_dly_ld_err
1330
1331  val s3_vp_match_fail = GatedValidRegNext(io.lsq.forward.matchInvalid || io.sbuffer.matchInvalid) && s3_troublem
1332  val s3_rep_frm_fetch = s3_vp_match_fail
1333  val s3_ldld_rep_inst =
1334      io.lsq.ldld_nuke_query.resp.valid &&
1335      io.lsq.ldld_nuke_query.resp.bits.rep_frm_fetch &&
1336      GatedValidRegNext(io.csrCtrl.ldld_vio_check_enable)
1337  val s3_flushPipe = s3_ldld_rep_inst
1338
1339  val s3_rep_info = WireInit(s3_in.rep_info)
1340  val s3_sel_rep_cause = PriorityEncoderOH(s3_rep_info.cause.asUInt)
1341
1342  when (s3_exception || s3_dly_ld_err || s3_rep_frm_fetch) {
1343    io.lsq.ldin.bits.rep_info.cause := 0.U.asTypeOf(s3_rep_info.cause.cloneType)
1344  } .otherwise {
1345    io.lsq.ldin.bits.rep_info.cause := VecInit(s3_sel_rep_cause.asBools)
1346  }
1347
1348  // Int load, if hit, will be writebacked at s3
1349  s3_out.valid                := s3_valid && s3_safe_writeback
1350  s3_out.bits.uop             := s3_in.uop
1351  s3_out.bits.uop.fpWen       := s3_in.uop.fpWen && !s3_exception
1352  s3_out.bits.uop.exceptionVec(loadAccessFault) := (s3_dly_ld_err || s3_in.uop.exceptionVec(loadAccessFault)) && s3_vecActive
1353  s3_out.bits.uop.flushPipe   := false.B
1354  s3_out.bits.uop.replayInst  := s3_rep_frm_fetch || s3_flushPipe
1355  s3_out.bits.data            := s3_in.data
1356  s3_out.bits.debug.isMMIO    := s3_in.mmio
1357  s3_out.bits.debug.isPerfCnt := false.B
1358  s3_out.bits.debug.paddr     := s3_in.paddr
1359  s3_out.bits.debug.vaddr     := s3_in.vaddr
1360
1361  // Vector load, writeback to merge buffer
1362  // TODO: Add assertion in merge buffer, merge buffer must accept vec load writeback
1363  s3_vecout.isvec             := s3_isvec
1364  s3_vecout.vecdata           := 0.U // Data will be assigned later
1365  s3_vecout.mask              := s3_in.mask
1366  // s3_vecout.rob_idx_valid     := s3_in.rob_idx_valid
1367  // s3_vecout.inner_idx         := s3_in.inner_idx
1368  // s3_vecout.rob_idx           := s3_in.rob_idx
1369  // s3_vecout.offset            := s3_in.offset
1370  s3_vecout.reg_offset        := s3_in.reg_offset
1371  s3_vecout.vecActive         := s3_vecActive
1372  s3_vecout.is_first_ele      := s3_in.is_first_ele
1373  // s3_vecout.uopQueuePtr       := DontCare // uopQueuePtr is already saved in flow queue
1374  // s3_vecout.flowPtr           := s3_in.flowPtr
1375  s3_vecout.elemIdx           := s3_in.elemIdx // elemIdx is already saved in flow queue // TODO:
1376  s3_vecout.elemIdxInsideVd   := s3_in.elemIdxInsideVd
1377  val s3_usSecondInv          = s3_in.usSecondInv
1378
1379  io.rollback.valid := s3_valid && (s3_rep_frm_fetch || s3_flushPipe) && !s3_exception
1380  io.rollback.bits             := DontCare
1381  io.rollback.bits.isRVC       := s3_out.bits.uop.preDecodeInfo.isRVC
1382  io.rollback.bits.robIdx      := s3_out.bits.uop.robIdx
1383  io.rollback.bits.ftqIdx      := s3_out.bits.uop.ftqPtr
1384  io.rollback.bits.ftqOffset   := s3_out.bits.uop.ftqOffset
1385  io.rollback.bits.level       := Mux(s3_rep_frm_fetch, RedirectLevel.flush, RedirectLevel.flushAfter)
1386  io.rollback.bits.cfiUpdate.target := s3_out.bits.uop.pc
1387  io.rollback.bits.debug_runahead_checkpoint_id := s3_out.bits.uop.debugInfo.runahead_checkpoint_id
1388  /* <------- DANGEROUS: Don't change sequence here ! -------> */
1389
1390  io.lsq.ldin.bits.uop := s3_out.bits.uop
1391
1392  val s3_revoke = s3_exception || io.lsq.ldin.bits.rep_info.need_rep
1393  io.lsq.ldld_nuke_query.revoke := s3_revoke
1394  io.lsq.stld_nuke_query.revoke := s3_revoke
1395
1396  // feedback slow
1397  s3_fast_rep := RegNext(s2_fast_rep)
1398
1399  val s3_fb_no_waiting = !s3_in.isLoadReplay &&
1400                        (!(s3_fast_rep && !s3_fast_rep_canceled)) &&
1401                        !s3_in.feedbacked
1402
1403  // feedback: scalar load will send feedback to RS
1404  //           vector load will send signal to VL Merge Buffer, then send feedback at granularity of uops
1405  io.feedback_slow.valid                 := s3_valid && s3_fb_no_waiting && !s3_isvec && !s3_frm_mabuf
1406  io.feedback_slow.bits.hit              := !s3_rep_info.need_rep || io.lsq.ldin.ready
1407  io.feedback_slow.bits.flushState       := s3_in.ptwBack
1408  io.feedback_slow.bits.robIdx           := s3_in.uop.robIdx
1409  io.feedback_slow.bits.sqIdx            := s3_in.uop.sqIdx
1410  io.feedback_slow.bits.lqIdx            := s3_in.uop.lqIdx
1411  io.feedback_slow.bits.sourceType       := RSFeedbackType.lrqFull
1412  io.feedback_slow.bits.dataInvalidSqIdx := DontCare
1413
1414  // TODO: vector wakeup?
1415  io.ldCancel.ld2Cancel := s3_valid && !s3_safe_wakeup && !s3_isvec && !s3_frm_mabuf
1416
1417  val s3_ld_wb_meta = Mux(s3_valid, s3_out.bits, s3_mmio.bits)
1418
1419  // data from load queue refill
1420  val s3_ld_raw_data_frm_uncache = RegNextN(io.lsq.ld_raw_data, 3)
1421  val s3_merged_data_frm_uncache = s3_ld_raw_data_frm_uncache.mergedData()
1422  val s3_picked_data_frm_uncache = LookupTree(s3_ld_raw_data_frm_uncache.addrOffset, List(
1423    "b000".U -> s3_merged_data_frm_uncache(63,  0),
1424    "b001".U -> s3_merged_data_frm_uncache(63,  8),
1425    "b010".U -> s3_merged_data_frm_uncache(63, 16),
1426    "b011".U -> s3_merged_data_frm_uncache(63, 24),
1427    "b100".U -> s3_merged_data_frm_uncache(63, 32),
1428    "b101".U -> s3_merged_data_frm_uncache(63, 40),
1429    "b110".U -> s3_merged_data_frm_uncache(63, 48),
1430    "b111".U -> s3_merged_data_frm_uncache(63, 56)
1431  ))
1432  val s3_ld_data_frm_uncache = rdataHelper(s3_ld_raw_data_frm_uncache.uop, s3_picked_data_frm_uncache)
1433
1434  // data from dcache hit
1435  val s3_ld_raw_data_frm_cache = Wire(new LoadDataFromDcacheBundle)
1436  s3_ld_raw_data_frm_cache.respDcacheData       := io.dcache.resp.bits.data
1437  s3_ld_raw_data_frm_cache.forward_D            := s2_fwd_frm_d_chan
1438  s3_ld_raw_data_frm_cache.forwardData_D        := s2_fwd_data_frm_d_chan
1439  s3_ld_raw_data_frm_cache.forward_mshr         := s2_fwd_frm_mshr
1440  s3_ld_raw_data_frm_cache.forwardData_mshr     := s2_fwd_data_frm_mshr
1441  s3_ld_raw_data_frm_cache.forward_result_valid := s2_fwd_data_valid
1442
1443  s3_ld_raw_data_frm_cache.forwardMask          := RegEnable(s2_fwd_mask, s2_valid)
1444  s3_ld_raw_data_frm_cache.forwardData          := RegEnable(s2_fwd_data, s2_valid)
1445  s3_ld_raw_data_frm_cache.uop                  := RegEnable(s2_out.uop, s2_valid)
1446  s3_ld_raw_data_frm_cache.addrOffset           := RegEnable(s2_out.paddr(3, 0), s2_valid)
1447
1448  val s3_merged_data_frm_tlD   = RegEnable(s3_ld_raw_data_frm_cache.mergeTLData(), s2_valid)
1449  val s3_merged_data_frm_cache = s3_ld_raw_data_frm_cache.mergeLsqFwdData(s3_merged_data_frm_tlD)
1450
1451  // duplicate reg for ldout and vecldout
1452  private val LdDataDup = 3
1453  require(LdDataDup >= 2)
1454  // truncate forward data and cache data to XLEN width to writeback
1455  val s3_fwd_mask_clip = VecInit(List.fill(LdDataDup)(
1456    RegEnable(Mux(
1457      s2_out.paddr(3),
1458      (s2_fwd_mask.asUInt)(VLEN / 8 - 1, 8),
1459      (s2_fwd_mask.asUInt)(7, 0)
1460    ).asTypeOf(Vec(XLEN / 8, Bool())), s2_valid)
1461  ))
1462  val s3_fwd_data_clip = VecInit(List.fill(LdDataDup)(
1463    RegEnable(Mux(
1464      s2_out.paddr(3),
1465      (s2_fwd_data.asUInt)(VLEN - 1, 64),
1466      (s2_fwd_data.asUInt)(63, 0)
1467    ).asTypeOf(Vec(XLEN / 8, UInt(8.W))), s2_valid)
1468  ))
1469  val s3_merged_data_frm_tld_clip = VecInit(List.fill(LdDataDup)(
1470    RegEnable(Mux(
1471      s2_out.paddr(3),
1472      s3_ld_raw_data_frm_cache.mergeTLData()(VLEN - 1, 64),
1473      s3_ld_raw_data_frm_cache.mergeTLData()(63, 0)
1474    ).asTypeOf(Vec(XLEN / 8, UInt(8.W))), s2_valid)
1475  ))
1476  val s3_merged_data_frm_cache_clip = VecInit((0 until LdDataDup).map(i => {
1477    VecInit((0 until XLEN / 8).map(j =>
1478      Mux(s3_fwd_mask_clip(i)(j), s3_fwd_data_clip(i)(j), s3_merged_data_frm_tld_clip(i)(j))
1479    )).asUInt
1480  }))
1481
1482  val s3_data_frm_cache = VecInit((0 until LdDataDup).map(i => {
1483    VecInit(Seq(
1484      s3_merged_data_frm_cache_clip(i)(63,    0),
1485      s3_merged_data_frm_cache_clip(i)(63,    8),
1486      s3_merged_data_frm_cache_clip(i)(63,   16),
1487      s3_merged_data_frm_cache_clip(i)(63,   24),
1488      s3_merged_data_frm_cache_clip(i)(63,   32),
1489      s3_merged_data_frm_cache_clip(i)(63,   40),
1490      s3_merged_data_frm_cache_clip(i)(63,   48),
1491      s3_merged_data_frm_cache_clip(i)(63,   56),
1492    ))
1493  }))
1494  val s3_picked_data_frm_cache = VecInit((0 until LdDataDup).map(i => {
1495    Mux1H(s3_data_select_by_offset, s3_data_frm_cache(i))
1496  }))
1497  val s3_ld_data_frm_cache = newRdataHelper(s3_data_select, s3_picked_data_frm_cache(0))
1498
1499  // FIXME: add 1 cycle delay ?
1500  // io.lsq.uncache.ready := !s3_valid
1501  val s3_outexception = ExceptionNO.selectByFu(s3_out.bits.uop.exceptionVec, LduCfg).asUInt.orR && s3_vecActive
1502  io.ldout.bits        := s3_ld_wb_meta
1503  io.ldout.bits.data   := Mux(s3_valid, s3_ld_data_frm_cache, s3_ld_data_frm_uncache)
1504  io.ldout.valid       := (s3_mmio.valid ||
1505                          (s3_out.valid && !s3_vecout.isvec && !s3_mis_align && !s3_frm_mabuf))
1506  io.ldout.bits.uop.exceptionVec := ExceptionNO.selectByFu(s3_ld_wb_meta.uop.exceptionVec, LduCfg)
1507
1508  // TODO: check this --hx
1509  // io.ldout.valid       := s3_out.valid && !s3_out.bits.uop.robIdx.needFlush(io.redirect) && !s3_vecout.isvec ||
1510  //   io.lsq.uncache.valid && !io.lsq.uncache.bits.uop.robIdx.needFlush(io.redirect) && !s3_out.valid && !io.lsq.uncache.bits.isVls
1511  //  io.ldout.bits.data   := Mux(s3_out.valid, s3_ld_data_frm_cache, s3_ld_data_frm_uncache)
1512  //  io.ldout.valid       := s3_out.valid && !s3_out.bits.uop.robIdx.needFlush(io.redirect) ||
1513  //                         s3_mmio.valid && !s3_mmio.bits.uop.robIdx.needFlush(io.redirect) && !s3_out.valid
1514
1515  // s3 load fast replay
1516  io.fast_rep_out.valid := s3_valid && s3_fast_rep
1517  io.fast_rep_out.bits := s3_in
1518  io.fast_rep_out.bits.lateKill := s3_rep_frm_fetch
1519
1520  val vecFeedback = s3_valid && s3_fb_no_waiting && s3_rep_info.need_rep && !io.lsq.ldin.ready && s3_isvec
1521
1522  // vector output
1523  io.vecldout.bits.alignedType := s3_vec_alignedType
1524  // vec feedback
1525  io.vecldout.bits.vecFeedback := vecFeedback
1526  // TODO: VLSU, uncache data logic
1527  val vecdata = rdataVecHelper(s3_vec_alignedType(1,0), s3_picked_data_frm_cache(1))
1528  io.vecldout.bits.vecdata.get := Mux(s3_in.is128bit, s3_merged_data_frm_cache, vecdata)
1529  io.vecldout.bits.isvec := s3_vecout.isvec
1530  io.vecldout.bits.elemIdx := s3_vecout.elemIdx
1531  io.vecldout.bits.elemIdxInsideVd.get := s3_vecout.elemIdxInsideVd
1532  io.vecldout.bits.mask := s3_vecout.mask
1533  io.vecldout.bits.reg_offset.get := s3_vecout.reg_offset
1534  io.vecldout.bits.usSecondInv := s3_usSecondInv
1535  io.vecldout.bits.mBIndex := s3_vec_mBIndex
1536  io.vecldout.bits.hit := !s3_rep_info.need_rep || io.lsq.ldin.ready
1537  io.vecldout.bits.sourceType := RSFeedbackType.lrqFull
1538  io.vecldout.bits.flushState := DontCare
1539  io.vecldout.bits.exceptionVec := ExceptionNO.selectByFu(s3_out.bits.uop.exceptionVec, VlduCfg)
1540  io.vecldout.bits.vaddr := s3_in.vaddr
1541  io.vecldout.bits.mmio := DontCare
1542
1543  io.vecldout.valid := s3_out.valid && !s3_out.bits.uop.robIdx.needFlush(io.redirect) && s3_vecout.isvec ||
1544  // TODO: check this, why !io.lsq.uncache.bits.isVls before?
1545    io.lsq.uncache.valid && !io.lsq.uncache.bits.uop.robIdx.needFlush(io.redirect) && !s3_out.valid && io.lsq.uncache.bits.isVls
1546    //io.lsq.uncache.valid && !io.lsq.uncache.bits.uop.robIdx.needFlush(io.redirect) && !s3_out.valid && !io.lsq.uncache.bits.isVls
1547
1548  io.misalign_ldout.valid     := s3_valid && (!s3_fast_rep || s3_fast_rep_canceled) && s3_frm_mabuf
1549  io.misalign_ldout.bits      := io.lsq.ldin.bits
1550  io.misalign_ldout.bits.data := Mux(s3_in.is128bit, s3_merged_data_frm_cache, s3_picked_data_frm_cache(2))
1551
1552  // fast load to load forward
1553  if (EnableLoadToLoadForward) {
1554    io.l2l_fwd_out.valid      := s3_valid && !s3_in.mmio && !s3_rep_info.need_rep
1555    io.l2l_fwd_out.data       := Mux(s3_in.vaddr(3), s3_merged_data_frm_cache(127, 64), s3_merged_data_frm_cache(63, 0))
1556    io.l2l_fwd_out.dly_ld_err := s3_dly_ld_err || // ecc delayed error
1557                                 s3_ldld_rep_inst ||
1558                                 s3_rep_frm_fetch
1559  } else {
1560    io.l2l_fwd_out.valid := false.B
1561    io.l2l_fwd_out.data := DontCare
1562    io.l2l_fwd_out.dly_ld_err := DontCare
1563  }
1564
1565   // trigger
1566  val last_valid_data = RegNext(RegEnable(io.ldout.bits.data, io.ldout.fire))
1567  val hit_ld_addr_trig_hit_vec = Wire(Vec(TriggerNum, Bool()))
1568  val lq_ld_addr_trig_hit_vec = io.lsq.trigger.lqLoadAddrTriggerHitVec
1569  (0 until TriggerNum).map{i => {
1570    val tdata2    = GatedRegNext(io.trigger(i).tdata2)
1571    val matchType = RegNext(io.trigger(i).matchType)
1572    val tEnable   = RegNext(io.trigger(i).tEnable)
1573
1574    hit_ld_addr_trig_hit_vec(i) := TriggerCmp(RegEnable(s2_out.vaddr, 0.U, s2_valid), tdata2, matchType, tEnable)
1575    io.trigger(i).addrHit       := Mux(s3_out.valid, hit_ld_addr_trig_hit_vec(i), lq_ld_addr_trig_hit_vec(i))
1576  }}
1577  io.lsq.trigger.hitLoadAddrTriggerHitVec := hit_ld_addr_trig_hit_vec
1578
1579  // s1
1580  io.debug_ls.s1_robIdx := s1_in.uop.robIdx.value
1581  io.debug_ls.s1_isLoadToLoadForward := s1_fire && s1_try_ptr_chasing && !s1_ptr_chasing_canceled
1582  io.debug_ls.s1_isTlbFirstMiss := s1_fire && s1_tlb_miss && s1_in.isFirstIssue
1583  // s2
1584  io.debug_ls.s2_robIdx := s2_in.uop.robIdx.value
1585  io.debug_ls.s2_isBankConflict := s2_fire && (!s2_kill && s2_bank_conflict)
1586  io.debug_ls.s2_isDcacheFirstMiss := s2_fire && io.dcache.resp.bits.miss && s2_in.isFirstIssue
1587  io.debug_ls.s2_isForwardFail := s2_fire && s2_fwd_fail
1588  // s3
1589  io.debug_ls.s3_robIdx := s3_in.uop.robIdx.value
1590  io.debug_ls.s3_isReplayFast := s3_valid && s3_fast_rep && !s3_fast_rep_canceled
1591  io.debug_ls.s3_isReplayRS :=  RegNext(io.feedback_fast.valid && !io.feedback_fast.bits.hit) || (io.feedback_slow.valid && !io.feedback_slow.bits.hit)
1592  io.debug_ls.s3_isReplaySlow := io.lsq.ldin.valid && io.lsq.ldin.bits.rep_info.need_rep
1593  io.debug_ls.s3_isReplay := s3_valid && s3_rep_info.need_rep // include fast+slow+rs replay
1594  io.debug_ls.replayCause := s3_rep_info.cause
1595  io.debug_ls.replayCnt := 1.U
1596
1597  // Topdown
1598  io.lsTopdownInfo.s1.robIdx          := s1_in.uop.robIdx.value
1599  io.lsTopdownInfo.s1.vaddr_valid     := s1_valid && s1_in.hasROBEntry
1600  io.lsTopdownInfo.s1.vaddr_bits      := s1_vaddr
1601  io.lsTopdownInfo.s2.robIdx          := s2_in.uop.robIdx.value
1602  io.lsTopdownInfo.s2.paddr_valid     := s2_fire && s2_in.hasROBEntry && !s2_in.tlbMiss
1603  io.lsTopdownInfo.s2.paddr_bits      := s2_in.paddr
1604  io.lsTopdownInfo.s2.first_real_miss := io.dcache.resp.bits.real_miss
1605  io.lsTopdownInfo.s2.cache_miss_en   := s2_fire && s2_in.hasROBEntry && !s2_in.tlbMiss && !s2_in.missDbUpdated
1606
1607  // perf cnt
1608  XSPerfAccumulate("s0_in_valid",                  io.ldin.valid)
1609  XSPerfAccumulate("s0_in_block",                  io.ldin.valid && !io.ldin.fire)
1610  XSPerfAccumulate("s0_vecin_valid",               io.vecldin.valid)
1611  XSPerfAccumulate("s0_vecin_block",               io.vecldin.valid && !io.vecldin.fire)
1612  XSPerfAccumulate("s0_in_fire_first_issue",       s0_valid && s0_sel_src.isFirstIssue)
1613  XSPerfAccumulate("s0_lsq_replay_issue",          io.replay.fire)
1614  XSPerfAccumulate("s0_lsq_replay_vecissue",       io.replay.fire && io.replay.bits.isvec)
1615  XSPerfAccumulate("s0_ldu_fire_first_issue",      io.ldin.fire && s0_sel_src.isFirstIssue)
1616  XSPerfAccumulate("s0_fast_replay_issue",         io.fast_rep_in.fire)
1617  XSPerfAccumulate("s0_fast_replay_vecissue",      io.fast_rep_in.fire && io.fast_rep_in.bits.isvec)
1618  XSPerfAccumulate("s0_stall_out",                 s0_valid && !s0_can_go)
1619  XSPerfAccumulate("s0_stall_dcache",              s0_valid && !io.dcache.req.ready)
1620  XSPerfAccumulate("s0_addr_spec_success",         s0_fire && s0_dcache_vaddr(VAddrBits-1, 12) === io.ldin.bits.src(0)(VAddrBits-1, 12))
1621  XSPerfAccumulate("s0_addr_spec_failed",          s0_fire && s0_dcache_vaddr(VAddrBits-1, 12) =/= io.ldin.bits.src(0)(VAddrBits-1, 12))
1622  XSPerfAccumulate("s0_addr_spec_success_once",    s0_fire && s0_dcache_vaddr(VAddrBits-1, 12) === io.ldin.bits.src(0)(VAddrBits-1, 12) && s0_sel_src.isFirstIssue)
1623  XSPerfAccumulate("s0_addr_spec_failed_once",     s0_fire && s0_dcache_vaddr(VAddrBits-1, 12) =/= io.ldin.bits.src(0)(VAddrBits-1, 12) && s0_sel_src.isFirstIssue)
1624  XSPerfAccumulate("s0_vec_addr_vlen_aligned",     s0_fire && s0_sel_src.isvec && s0_dcache_vaddr(3, 0) === 0.U)
1625  XSPerfAccumulate("s0_vec_addr_vlen_unaligned",   s0_fire && s0_sel_src.isvec && s0_dcache_vaddr(3, 0) =/= 0.U)
1626  XSPerfAccumulate("s0_forward_tl_d_channel",      s0_out.forward_tlDchannel)
1627  XSPerfAccumulate("s0_hardware_prefetch_fire",    s0_fire && s0_hw_prf_select)
1628  XSPerfAccumulate("s0_software_prefetch_fire",    s0_fire && s0_sel_src.prf && s0_src_select_vec(int_iss_idx))
1629  XSPerfAccumulate("s0_hardware_prefetch_blocked", io.prefetch_req.valid && !s0_hw_prf_select)
1630  XSPerfAccumulate("s0_hardware_prefetch_total",   io.prefetch_req.valid)
1631
1632  XSPerfAccumulate("s1_in_valid",                  s1_valid)
1633  XSPerfAccumulate("s1_in_fire",                   s1_fire)
1634  XSPerfAccumulate("s1_in_fire_first_issue",       s1_fire && s1_in.isFirstIssue)
1635  XSPerfAccumulate("s1_tlb_miss",                  s1_fire && s1_tlb_miss)
1636  XSPerfAccumulate("s1_tlb_miss_first_issue",      s1_fire && s1_tlb_miss && s1_in.isFirstIssue)
1637  XSPerfAccumulate("s1_stall_out",                 s1_valid && !s1_can_go)
1638  XSPerfAccumulate("s1_dly_err",                   s1_valid && s1_fast_rep_dly_err)
1639
1640  XSPerfAccumulate("s2_in_valid",                  s2_valid)
1641  XSPerfAccumulate("s2_in_fire",                   s2_fire)
1642  XSPerfAccumulate("s2_in_fire_first_issue",       s2_fire && s2_in.isFirstIssue)
1643  XSPerfAccumulate("s2_dcache_miss",               s2_fire && io.dcache.resp.bits.miss)
1644  XSPerfAccumulate("s2_dcache_miss_first_issue",   s2_fire && io.dcache.resp.bits.miss && s2_in.isFirstIssue)
1645  XSPerfAccumulate("s2_dcache_real_miss_first_issue",   s2_fire && io.dcache.resp.bits.miss && s2_in.isFirstIssue)
1646  XSPerfAccumulate("s2_full_forward",              s2_fire && s2_full_fwd)
1647  XSPerfAccumulate("s2_dcache_miss_full_forward",  s2_fire && s2_dcache_miss)
1648  XSPerfAccumulate("s2_fwd_frm_d_can",             s2_valid && s2_fwd_frm_d_chan)
1649  XSPerfAccumulate("s2_fwd_frm_d_chan_or_mshr",    s2_valid && s2_fwd_frm_d_chan_or_mshr)
1650  XSPerfAccumulate("s2_stall_out",                 s2_fire && !s2_can_go)
1651  XSPerfAccumulate("s2_prefetch",                  s2_fire && s2_prf)
1652  XSPerfAccumulate("s2_prefetch_ignored",          s2_fire && s2_prf && io.dcache.s2_mq_nack) // ignore prefetch for mshr full / miss req port conflict
1653  XSPerfAccumulate("s2_prefetch_miss",             s2_fire && s2_prf && io.dcache.resp.bits.miss) // prefetch req miss in l1
1654  XSPerfAccumulate("s2_prefetch_hit",              s2_fire && s2_prf && !io.dcache.resp.bits.miss) // prefetch req hit in l1
1655  XSPerfAccumulate("s2_prefetch_accept",           s2_fire && s2_prf && io.dcache.resp.bits.miss && !io.dcache.s2_mq_nack) // prefetch a missed line in l1, and l1 accepted it
1656  XSPerfAccumulate("s2_forward_req",               s2_fire && s2_in.forward_tlDchannel)
1657  XSPerfAccumulate("s2_successfully_forward_channel_D", s2_fire && s2_fwd_frm_d_chan && s2_fwd_data_valid)
1658  XSPerfAccumulate("s2_successfully_forward_mshr",      s2_fire && s2_fwd_frm_mshr && s2_fwd_data_valid)
1659
1660  XSPerfAccumulate("load_to_load_forward",                      s1_try_ptr_chasing && !s1_ptr_chasing_canceled)
1661  XSPerfAccumulate("load_to_load_forward_try",                  s1_try_ptr_chasing)
1662  XSPerfAccumulate("load_to_load_forward_fail",                 s1_cancel_ptr_chasing)
1663  XSPerfAccumulate("load_to_load_forward_fail_cancelled",       s1_cancel_ptr_chasing && s1_ptr_chasing_canceled)
1664  XSPerfAccumulate("load_to_load_forward_fail_wakeup_mismatch", s1_cancel_ptr_chasing && !s1_ptr_chasing_canceled && s1_not_fast_match)
1665  XSPerfAccumulate("load_to_load_forward_fail_op_not_ld",       s1_cancel_ptr_chasing && !s1_ptr_chasing_canceled && !s1_not_fast_match && s1_fu_op_type_not_ld)
1666  XSPerfAccumulate("load_to_load_forward_fail_addr_align",      s1_cancel_ptr_chasing && !s1_ptr_chasing_canceled && !s1_not_fast_match && !s1_fu_op_type_not_ld && s1_addr_misaligned)
1667  XSPerfAccumulate("load_to_load_forward_fail_set_mismatch",    s1_cancel_ptr_chasing && !s1_ptr_chasing_canceled && !s1_not_fast_match && !s1_fu_op_type_not_ld && !s1_addr_misaligned && s1_addr_mismatch)
1668
1669  // bug lyq: some signals in perfEvents are no longer suitable for the current MemBlock design
1670  // hardware performance counter
1671  val perfEvents = Seq(
1672    ("load_s0_in_fire         ", s0_fire                                                        ),
1673    ("load_to_load_forward    ", s1_fire && s1_try_ptr_chasing && !s1_ptr_chasing_canceled      ),
1674    ("stall_dcache            ", s0_valid && s0_can_go && !io.dcache.req.ready                  ),
1675    ("load_s1_in_fire         ", s0_fire                                                        ),
1676    ("load_s1_tlb_miss        ", s1_fire && io.tlb.resp.bits.miss                               ),
1677    ("load_s2_in_fire         ", s1_fire                                                        ),
1678    ("load_s2_dcache_miss     ", s2_fire && io.dcache.resp.bits.miss                            ),
1679  )
1680  generatePerfEvent()
1681
1682  when(io.ldout.fire){
1683    XSDebug("ldout %x\n", io.ldout.bits.uop.pc)
1684  }
1685  // end
1686}
1687