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