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