xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/LoadUnit.scala (revision 67fcf090b92eeb68aff35affc52316c799043ffb)
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 chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils._
23import utility._
24import xiangshan.ExceptionNO._
25import xiangshan._
26import xiangshan.backend.fu.PMPRespBundle
27import xiangshan.backend.fu.FuConfig.LduCfg
28import xiangshan.backend.rob.DebugLsInfoBundle
29import xiangshan.cache._
30import xiangshan.cache.dcache.ReplayCarry
31import xiangshan.cache.mmu.{TlbCmd, TlbReq, TlbRequestIO, TlbResp}
32import xiangshan.backend.Bundles.{DynInst, MemExuInput, MemExuOutput}
33
34class LoadToLsqFastIO(implicit p: Parameters) extends XSBundle {
35  val valid = Output(Bool())
36  val ld_ld_check_ok = Output(Bool())
37  val st_ld_check_ok = Output(Bool())
38  val cache_bank_no_conflict = Output(Bool())
39  val ld_idx = Output(UInt(log2Ceil(LoadQueueSize).W))
40  val debug = Output(new PerfDebugInfo)
41
42  def needreplay: Bool = {
43    !ld_ld_check_ok || !st_ld_check_ok || !cache_bank_no_conflict
44  }
45}
46
47class LoadToLsqSlowIO(implicit p: Parameters) extends XSBundle with HasDCacheParameters {
48  val valid = Output(Bool())
49  val tlb_hited = Output(Bool())
50  val st_ld_check_ok = Output(Bool())
51  val cache_no_replay = Output(Bool())
52  val forward_data_valid = Output(Bool())
53  val cache_hited = Output(Bool())
54  val can_forward_full_data = Output(Bool())
55  val ld_idx = Output(UInt(log2Ceil(LoadQueueSize).W))
56  val data_invalid_sq_idx = Output(UInt(log2Ceil(StoreQueueSize).W))
57  val replayCarry = Output(new ReplayCarry)
58  val miss_mshr_id = Output(UInt(log2Up(cfg.nMissEntries).W))
59  val data_in_last_beat = Output(Bool())
60  val debug = Output(new PerfDebugInfo)
61
62  def needreplay: Bool = {
63    !tlb_hited || !st_ld_check_ok || !cache_no_replay || !forward_data_valid || !cache_hited
64  }
65}
66
67class LoadToLsqIO(implicit p: Parameters) extends XSBundle {
68  val loadIn = ValidIO(new LqWriteBundle)
69  val loadPaddrIn = ValidIO(new LqPaddrWriteBundle)
70  val loadVaddrIn = ValidIO(new LqVaddrWriteBundle)
71  val ldout = Flipped(DecoupledIO(new MemExuOutput))
72  val ldRawData = Input(new LoadDataFromLQBundle)
73  val s2_load_data_forwarded = Output(Bool())
74  val s3_delayed_load_error = Output(Bool())
75  val s2_dcache_require_replay = Output(Bool())
76  val s3_replay_from_fetch = Output(Bool()) // update uop.ctrl.replayInst in load queue in s3
77  val forward = new PipeLoadForwardQueryIO
78  val loadViolationQuery = new LoadViolationQueryIO
79  val trigger = Flipped(new LqTriggerIO)
80
81  // for load replay
82  val replayFast = new LoadToLsqFastIO
83  val replaySlow = new LoadToLsqSlowIO
84}
85
86class LoadToLoadIO(implicit p: Parameters) extends XSBundle {
87  // load to load fast path is limited to ld (64 bit) used as vaddr src1 only
88  val data = UInt(XLEN.W)
89  val valid = Bool()
90}
91
92class LoadUnitTriggerIO(implicit p: Parameters) extends XSBundle {
93  val tdata2 = Input(UInt(64.W))
94  val matchType = Input(UInt(2.W))
95  val tEnable = Input(Bool()) // timing is calculated before this
96  val addrHit = Output(Bool())
97  val lastDataHit = Output(Bool())
98}
99
100// Load Pipeline Stage 0
101// Generate addr, use addr to query DCache and DTLB
102class LoadUnit_S0(implicit p: Parameters) extends XSModule with HasDCacheParameters{
103  val io = IO(new Bundle() {
104    val in = Flipped(Decoupled(new MemExuInput))
105    val out = Decoupled(new LsPipelineBundle)
106    val prefetch_in = Flipped(ValidIO(new L1PrefetchReq))
107    val dtlbReq = DecoupledIO(new TlbReq)
108    val dcacheReq = DecoupledIO(new DCacheWordReq)
109    val rsIdx = Input(UInt(log2Up(MemIQSizeMax).W))
110    val isFirstIssue = Input(Bool())
111    val fastpath = Input(new LoadToLoadIO)
112    val s0_kill = Input(Bool())
113    // wire from lq to load pipeline
114    val lsqOut = Flipped(Decoupled(new LsPipelineBundle))
115
116    val s0_sqIdx = Output(new SqPtr)
117    // l2l
118    val l2lForward_select = Output(Bool())
119  })
120  require(LoadPipelineWidth == backendParams.LduCnt)
121
122  val s0_vaddr = Wire(UInt(VAddrBits.W))
123  val s0_mask = Wire(UInt(8.W))
124  val s0_uop = Wire(new DynInst)
125  val s0_isFirstIssue = Wire(Bool())
126  val s0_rsIdx = Wire(UInt(log2Up(MemIQSizeMax).W))
127  val s0_sqIdx = Wire(new SqPtr)
128  val s0_replayCarry = Wire(new ReplayCarry) // way info for way predict related logic
129  // default value
130  s0_replayCarry.valid := false.B
131  s0_replayCarry.real_way_en := 0.U
132
133  io.s0_sqIdx := s0_sqIdx
134
135  val tryFastpath = WireInit(false.B)
136
137  // load flow select/gen
138  //
139  // src0: load replayed by LSQ (io.lsqOut)
140  // src1: hardware prefetch from prefetchor (high confidence) (io.prefetch)
141  // src2: int read / software prefetch first issue from RS (io.in)
142  // src3: vec read first issue from RS (TODO)
143  // src4: load try pointchaising when no issued or replayed load (io.fastpath)
144  // src5: hardware prefetch from prefetchor (high confidence) (io.prefetch)
145
146  // load flow source valid
147  val lfsrc0_loadReplay_valid = io.lsqOut.valid
148  val lfsrc1_highconfhwPrefetch_valid = io.prefetch_in.valid && io.prefetch_in.bits.confidence > 0.U
149  val lfsrc2_intloadFirstIssue_valid = io.in.valid // int flow first issue or software prefetch
150  val lfsrc3_vecloadFirstIssue_valid = WireInit(false.B) // TODO
151  val lfsrc4_l2lForward_valid = io.fastpath.valid
152  val lfsrc5_lowconfhwPrefetch_valid = io.prefetch_in.valid && io.prefetch_in.bits.confidence === 0.U
153  dontTouch(lfsrc0_loadReplay_valid)
154  dontTouch(lfsrc1_highconfhwPrefetch_valid)
155  dontTouch(lfsrc2_intloadFirstIssue_valid)
156  dontTouch(lfsrc3_vecloadFirstIssue_valid)
157  dontTouch(lfsrc4_l2lForward_valid)
158  dontTouch(lfsrc5_lowconfhwPrefetch_valid)
159
160  // load flow source ready
161  val lfsrc_loadReplay_ready = WireInit(true.B)
162  val lfsrc_highconfhwPrefetch_ready = !lfsrc0_loadReplay_valid
163  val lfsrc_intloadFirstIssue_ready = !lfsrc0_loadReplay_valid &&
164    !lfsrc1_highconfhwPrefetch_valid
165  val lfsrc_vecloadFirstIssue_ready = !lfsrc0_loadReplay_valid &&
166    !lfsrc1_highconfhwPrefetch_valid &&
167    !lfsrc2_intloadFirstIssue_valid
168  val lfsrc_l2lForward_ready = !lfsrc0_loadReplay_valid &&
169    !lfsrc1_highconfhwPrefetch_valid &&
170    !lfsrc2_intloadFirstIssue_valid &&
171    !lfsrc3_vecloadFirstIssue_valid
172  val lfsrc_lowconfhwPrefetch_ready = !lfsrc0_loadReplay_valid &&
173    !lfsrc1_highconfhwPrefetch_valid &&
174    !lfsrc2_intloadFirstIssue_valid &&
175    !lfsrc3_vecloadFirstIssue_valid &&
176    !lfsrc4_l2lForward_valid
177  dontTouch(lfsrc_loadReplay_ready)
178  dontTouch(lfsrc_highconfhwPrefetch_ready)
179  dontTouch(lfsrc_intloadFirstIssue_ready)
180  dontTouch(lfsrc_vecloadFirstIssue_ready)
181  dontTouch(lfsrc_l2lForward_ready)
182  dontTouch(lfsrc_lowconfhwPrefetch_ready)
183
184  // load flow source select (OH)
185  val lfsrc_loadReplay_select = lfsrc0_loadReplay_valid && lfsrc_loadReplay_ready
186  val lfsrc_hwprefetch_select = lfsrc_highconfhwPrefetch_ready && lfsrc1_highconfhwPrefetch_valid ||
187    lfsrc_lowconfhwPrefetch_ready && lfsrc5_lowconfhwPrefetch_valid
188  val lfsrc_intloadFirstIssue_select = lfsrc_intloadFirstIssue_ready && lfsrc2_intloadFirstIssue_valid
189  val lfsrc_vecloadFirstIssue_select = lfsrc_vecloadFirstIssue_ready && lfsrc3_vecloadFirstIssue_valid
190  val lfsrc_l2lForward_select = lfsrc_l2lForward_ready && lfsrc4_l2lForward_valid
191  assert(!lfsrc_vecloadFirstIssue_select) // to be added
192  dontTouch(lfsrc_loadReplay_select)
193  dontTouch(lfsrc_hwprefetch_select)
194  dontTouch(lfsrc_intloadFirstIssue_select)
195  dontTouch(lfsrc_vecloadFirstIssue_select)
196  dontTouch(lfsrc_l2lForward_select)
197
198  io.l2lForward_select := lfsrc_l2lForward_select
199
200  // s0_valid == ture iff there is a valid load flow in load_s0
201  val s0_valid = lfsrc0_loadReplay_valid ||
202    lfsrc1_highconfhwPrefetch_valid ||
203    lfsrc2_intloadFirstIssue_valid ||
204    lfsrc3_vecloadFirstIssue_valid ||
205    lfsrc4_l2lForward_valid ||
206    lfsrc5_lowconfhwPrefetch_valid
207
208  // prefetch related ctrl signal
209  val isPrefetch = WireInit(false.B)
210  val isPrefetchRead = WireInit(s0_uop.ctrl.fuOpType === LSUOpType.prefetch_r)
211  val isPrefetchWrite = WireInit(s0_uop.ctrl.fuOpType === LSUOpType.prefetch_w)
212  val isHWPrefetch = lfsrc_hwprefetch_select
213
214  // query DTLB
215  io.dtlbReq.valid := s0_valid
216  // hw prefetch addr does not need to be translated, give tlb paddr
217  io.dtlbReq.bits.vaddr := Mux(lfsrc_hwprefetch_select, io.prefetch_in.bits.paddr, s0_vaddr)
218  io.dtlbReq.bits.cmd := Mux(isPrefetch,
219    Mux(isPrefetchWrite, TlbCmd.write, TlbCmd.read),
220    TlbCmd.read
221  )
222  io.dtlbReq.bits.size := LSUOpType.size(s0_uop.ctrl.fuOpType)
223  io.dtlbReq.bits.kill := DontCare
224  io.dtlbReq.bits.memidx.is_ld := true.B
225  io.dtlbReq.bits.memidx.is_st := false.B
226  io.dtlbReq.bits.memidx.idx := s0_uop.lqIdx.value
227  io.dtlbReq.bits.debug.robIdx := s0_uop.robIdx
228  // hw prefetch addr does not need to be translated
229  io.dtlbReq.bits.no_translate := lfsrc_hwprefetch_select
230  io.dtlbReq.bits.debug.pc := s0_uop.cf.pc
231  io.dtlbReq.bits.debug.isFirstIssue := s0_isFirstIssue
232
233  // query DCache
234  io.dcacheReq.valid := s0_valid
235  when (isPrefetchRead) {
236    io.dcacheReq.bits.cmd  := MemoryOpConstants.M_PFR
237  }.elsewhen (isPrefetchWrite) {
238    io.dcacheReq.bits.cmd  := MemoryOpConstants.M_PFW
239  }.otherwise {
240    io.dcacheReq.bits.cmd  := MemoryOpConstants.M_XRD
241  }
242  io.dcacheReq.bits.addr := s0_vaddr
243  io.dcacheReq.bits.mask := s0_mask
244  io.dcacheReq.bits.data := DontCare
245  when(isPrefetch) {
246    io.dcacheReq.bits.instrtype := DCACHE_PREFETCH_SOURCE.U
247  }.otherwise {
248    io.dcacheReq.bits.instrtype := LOAD_SOURCE.U
249  }
250  io.dcacheReq.bits.isFirstIssue := s0_isFirstIssue
251  io.dcacheReq.bits.replayCarry := s0_replayCarry
252  io.dcacheReq.bits.debug_robIdx := s0_uop.robIdx.value
253
254  // TODO: update cache meta
255  io.dcacheReq.bits.id := DontCare
256
257  // assign default value
258  s0_uop := DontCare
259
260  // load flow priority mux
261  when(lfsrc_loadReplay_select) {
262    s0_vaddr := io.lsqOut.bits.vaddr
263    s0_mask := io.lsqOut.bits.mask
264    s0_uop := io.lsqOut.bits.uop
265    s0_isFirstIssue := io.lsqOut.bits.isFirstIssue
266    s0_rsIdx := io.lsqOut.bits.rsIdx
267    s0_sqIdx := io.lsqOut.bits.uop.sqIdx
268    s0_replayCarry := io.lsqOut.bits.replayCarry
269    val replayUopIsPrefetch = WireInit(LSUOpType.isPrefetch(io.lsqOut.bits.uop.fuOpType))
270    when (replayUopIsPrefetch) {
271      isPrefetch := true.B
272    }
273  }.elsewhen(lfsrc_hwprefetch_select) {
274    // vaddr based index for dcache
275    s0_vaddr := io.prefetch_in.bits.getVaddr()
276    s0_mask := 0.U
277    s0_uop := DontCare
278    s0_isFirstIssue := false.B
279    s0_rsIdx := DontCare
280    s0_sqIdx := DontCare
281    s0_replayCarry := DontCare
282    // ctrl signal
283    isPrefetch := true.B
284    isPrefetchRead := !io.prefetch_in.bits.is_store
285    isPrefetchWrite := io.prefetch_in.bits.is_store
286  }.elsewhen(lfsrc_intloadFirstIssue_select) {
287    val imm12 = io.in.bits.uop.imm(11, 0)
288    s0_vaddr := io.in.bits.src(0) + SignExt(imm12, VAddrBits)
289    s0_mask := genWmask(s0_vaddr, io.in.bits.uop.fuOpType(1,0))
290    s0_uop := io.in.bits.uop
291    s0_isFirstIssue := io.isFirstIssue
292    s0_rsIdx := io.rsIdx
293    s0_sqIdx := io.in.bits.uop.sqIdx
294    val issueUopIsPrefetch = WireInit(LSUOpType.isPrefetch(io.in.bits.uop.ctrl.fuOpType))
295    when (issueUopIsPrefetch) {
296      isPrefetch := true.B
297    }
298  }.otherwise {
299    if (EnableLoadToLoadForward) {
300      tryFastpath := lfsrc_l2lForward_select
301      // When there's no valid instruction from RS and LSQ, we try the load-to-load forwarding.
302      s0_vaddr := io.fastpath.data
303      // Assume the pointer chasing is always ld.
304      s0_uop.fuOpType := LSUOpType.ld
305      s0_mask := genWmask(0.U, LSUOpType.ld)
306      // we dont care s0_isFirstIssue and s0_rsIdx and s0_sqIdx in S0 when trying pointchasing
307      // because these signals will be updated in S1
308      s0_isFirstIssue := true.B
309      s0_rsIdx := DontCare
310      s0_sqIdx := DontCare
311    }
312  }
313
314  // address align check
315  val addrAligned = LookupTree(s0_uop.fuOpType(1, 0), List(
316    "b00".U   -> true.B,                   //b
317    "b01".U   -> (s0_vaddr(0)    === 0.U), //h
318    "b10".U   -> (s0_vaddr(1, 0) === 0.U), //w
319    "b11".U   -> (s0_vaddr(2, 0) === 0.U)  //d
320  ))
321
322  // accept load flow if dcache ready (dtlb is always ready)
323  io.out.valid := s0_valid && io.dcacheReq.ready && !io.s0_kill
324  io.out.bits := DontCare
325  io.out.bits.vaddr := s0_vaddr
326  io.out.bits.mask := s0_mask
327  io.out.bits.uop := s0_uop
328  io.out.bits.uop.exceptionVec(loadAddrMisaligned) := !addrAligned
329  io.out.bits.rsIdx := s0_rsIdx
330  io.out.bits.isFirstIssue := s0_isFirstIssue
331  io.out.bits.isPrefetch := isPrefetch
332  io.out.bits.isHWPrefetch := isHWPrefetch
333  io.out.bits.isLoadReplay := io.lsqOut.valid
334  io.out.bits.mshrid := io.lsqOut.bits.mshrid
335  io.out.bits.forward_tlDchannel := io.lsqOut.valid && io.lsqOut.bits.forward_tlDchannel
336  when(io.dtlbReq.valid && s0_isFirstIssue) {
337    io.out.bits.uop.debugInfo.tlbFirstReqTime := GTimer()
338  }.otherwise{
339    io.out.bits.uop.debugInfo.tlbFirstReqTime := s0_uop.debugInfo.tlbFirstReqTime
340  }
341
342  // load flow source ready
343  // always accept load flow from load replay queue
344  // io.lsqOut has highest priority
345  io.lsqOut.ready := (io.out.ready && io.dcacheReq.ready && lfsrc_loadReplay_ready)
346
347  // accept load flow from rs when:
348  // 1) there is no lsq-replayed load
349  // 2) there is no high confidence prefetch request
350  io.in.ready := (io.out.ready && io.dcacheReq.ready && lfsrc_intloadFirstIssue_select)
351
352  // for hw prefetch load flow feedback, to be added later
353  // io.prefetch_in.ready := lfsrc_hwprefetch_select
354
355  XSDebug(io.dcacheReq.fire,
356    p"[DCACHE LOAD REQ] pc ${Hexadecimal(s0_uop.pc)}, vaddr ${Hexadecimal(s0_vaddr)}\n"
357  )
358  XSPerfAccumulate("in_valid", io.in.valid)
359  XSPerfAccumulate("in_fire", io.in.fire)
360  XSPerfAccumulate("in_fire_first_issue", s0_valid && s0_isFirstIssue)
361  XSPerfAccumulate("lsq_fire_first_issue", io.lsqOut.valid && io.lsqOut.bits.isFirstIssue)
362  XSPerfAccumulate("ldu_fire_first_issue", io.in.valid && io.isFirstIssue)
363  XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready && io.dcacheReq.ready)
364  XSPerfAccumulate("stall_dcache", io.out.valid && io.out.ready && !io.dcacheReq.ready)
365  XSPerfAccumulate("addr_spec_success", io.out.fire && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12))
366  XSPerfAccumulate("addr_spec_failed", io.out.fire && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12))
367  XSPerfAccumulate("addr_spec_success_once", io.out.fire && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue)
368  XSPerfAccumulate("addr_spec_failed_once", io.out.fire && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue)
369  XSPerfAccumulate("forward_tlDchannel", io.out.bits.forward_tlDchannel)
370  XSPerfAccumulate("hardware_prefetch_fire", io.out.fire && lfsrc_hwprefetch_select)
371  XSPerfAccumulate("software_prefetch_fire", io.out.fire && isPrefetch && lfsrc_intloadFirstIssue_select)
372  XSPerfAccumulate("hardware_prefetch_blocked", io.prefetch_in.valid && !lfsrc_hwprefetch_select)
373  XSPerfAccumulate("hardware_prefetch_total", io.prefetch_in.valid)
374}
375
376
377// Load Pipeline Stage 1
378// TLB resp (send paddr to dcache)
379class LoadUnit_S1(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
380  val io = IO(new Bundle() {
381    val in = Flipped(Decoupled(new LsPipelineBundle))
382    val s1_kill = Input(Bool())
383    val out = Decoupled(new LsPipelineBundle)
384    val dtlbResp = Flipped(DecoupledIO(new TlbResp(2)))
385    val lsuPAddr = Output(UInt(PAddrBits.W))
386    val dcachePAddr = Output(UInt(PAddrBits.W))
387    val dcacheKill = Output(Bool())
388    val dcacheBankConflict = Input(Bool())
389    val fullForwardFast = Output(Bool())
390    val sbuffer = new LoadForwardQueryIO
391    val lsq = new PipeLoadForwardQueryIO
392    val loadViolationQueryReq = Decoupled(new LoadViolationQueryReq)
393    val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO)))
394    val rsFeedback = ValidIO(new RSFeedback)
395    val replayFast = new LoadToLsqFastIO
396    val csrCtrl = Flipped(new CustomCSRCtrlIO)
397    val needLdVioCheckRedo = Output(Bool())
398    val needReExecute = Output(Bool())
399  })
400
401  val s1_uop = io.in.bits.uop
402  val s1_paddr_dup_lsu = io.dtlbResp.bits.paddr(0)
403  val s1_paddr_dup_dcache = io.dtlbResp.bits.paddr(1)
404  // af & pf exception were modified below.
405  val s1_exception = ExceptionNO.selectByFu(io.out.bits.uop.exceptionVec, LduCfg).asUInt.orR
406  val s1_tlb_miss = io.dtlbResp.bits.miss
407  val s1_mask = io.in.bits.mask
408  val s1_is_prefetch = io.in.bits.isPrefetch
409  val s1_is_hw_prefetch = io.in.bits.isHWPrefetch
410  val s1_is_sw_prefetch = s1_is_prefetch && !s1_is_hw_prefetch
411  val s1_bank_conflict = io.dcacheBankConflict
412
413  io.out.bits := io.in.bits // forwardXX field will be updated in s1
414
415  val s1_tlb_memidx = io.dtlbResp.bits.memidx
416  when(s1_tlb_memidx.is_ld && io.dtlbResp.valid && !s1_tlb_miss && s1_tlb_memidx.idx === io.out.bits.uop.lqIdx.value) {
417    // printf("load idx = %d\n", s1_tlb_memidx.idx)
418    io.out.bits.uop.debugInfo.tlbRespTime := GTimer()
419  }
420
421  io.dtlbResp.ready := true.B
422
423  io.lsuPAddr := s1_paddr_dup_lsu
424  io.dcachePAddr := s1_paddr_dup_dcache
425  //io.dcacheKill := s1_tlb_miss || s1_exception || s1_mmio
426  io.dcacheKill := s1_tlb_miss || s1_exception || io.s1_kill
427  // load forward query datapath
428  io.sbuffer.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_prefetch)
429  io.sbuffer.vaddr := io.in.bits.vaddr
430  io.sbuffer.paddr := s1_paddr_dup_lsu
431  io.sbuffer.uop := s1_uop
432  io.sbuffer.sqIdx := s1_uop.sqIdx
433  io.sbuffer.mask := s1_mask
434  io.sbuffer.pc := s1_uop.pc // FIXME: remove it
435
436  io.lsq.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_prefetch)
437  io.lsq.vaddr := io.in.bits.vaddr
438  io.lsq.paddr := s1_paddr_dup_lsu
439  io.lsq.uop := s1_uop
440  io.lsq.sqIdx := s1_uop.sqIdx
441  io.lsq.sqIdxMask := DontCare // will be overwritten by sqIdxMask pre-generated in s0
442  io.lsq.mask := s1_mask
443  io.lsq.pc := s1_uop.pc // FIXME: remove it
444
445  // ld-ld violation query
446  io.loadViolationQueryReq.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_prefetch)
447  io.loadViolationQueryReq.bits.paddr := s1_paddr_dup_lsu
448  io.loadViolationQueryReq.bits.uop := s1_uop
449
450  // st-ld violation query
451  val needReExecuteVec = Wire(Vec(StorePipelineWidth, Bool()))
452  val needReExecute = Wire(Bool())
453
454  for (w <- 0 until StorePipelineWidth) {
455    //  needReExecute valid when
456    //  1. ReExecute query request valid.
457    //  2. Load instruction is younger than requestors(store instructions).
458    //  3. Physical address match.
459    //  4. Data contains.
460
461    needReExecuteVec(w) := io.reExecuteQuery(w).valid &&
462                          isAfter(io.in.bits.uop.robIdx, io.reExecuteQuery(w).bits.robIdx) &&
463                          !s1_tlb_miss &&
464                          (s1_paddr_dup_lsu(PAddrBits-1, 3) === io.reExecuteQuery(w).bits.paddr(PAddrBits-1, 3)) &&
465                          (s1_mask & io.reExecuteQuery(w).bits.mask).orR
466  }
467  needReExecute := needReExecuteVec.asUInt.orR
468  io.needReExecute := needReExecute
469
470  // Generate forwardMaskFast to wake up insts earlier
471  val forwardMaskFast = io.lsq.forwardMaskFast.asUInt | io.sbuffer.forwardMaskFast.asUInt
472  io.fullForwardFast := ((~forwardMaskFast).asUInt & s1_mask) === 0.U
473
474  // Generate feedback signal caused by:
475  // * dcache bank conflict
476  // * need redo ld-ld violation check
477  val needLdVioCheckRedo = io.loadViolationQueryReq.valid &&
478    !io.loadViolationQueryReq.ready &&
479    RegNext(io.csrCtrl.ldld_vio_check_enable)
480  io.needLdVioCheckRedo := needLdVioCheckRedo
481
482  // make nanhu rs feedback port happy
483  // if a load flow comes from rs, always feedback hit (no need to replay from rs)
484  io.rsFeedback.valid := Mux(io.in.bits.isLoadReplay, false.B, io.in.valid && !io.s1_kill && !s1_is_hw_prefetch)
485  io.rsFeedback.bits.hit := true.B // we have found s1_bank_conflict / re do ld-ld violation check
486  io.rsFeedback.bits.rsIdx := io.in.bits.rsIdx
487  io.rsFeedback.bits.flushState := io.in.bits.ptwBack
488  io.rsFeedback.bits.sourceType := Mux(s1_bank_conflict, RSFeedbackType.bankConflict, RSFeedbackType.ldVioCheckRedo)
489  io.rsFeedback.bits.dataInvalidSqIdx := DontCare
490
491  // request replay from load replay queue, fast port
492  io.replayFast.valid := io.in.valid && !io.s1_kill && !s1_is_hw_prefetch
493  io.replayFast.ld_ld_check_ok := !needLdVioCheckRedo || s1_is_sw_prefetch
494  io.replayFast.st_ld_check_ok := !needReExecute || s1_is_sw_prefetch
495  io.replayFast.cache_bank_no_conflict := !s1_bank_conflict || s1_is_sw_prefetch
496  io.replayFast.ld_idx := io.in.bits.uop.lqIdx.value
497  io.replayFast.debug := io.in.bits.uop.debugInfo
498
499  // if replay is detected in load_s1,
500  // load inst will be canceled immediately
501  io.out.valid := io.in.valid && (!needLdVioCheckRedo && !s1_bank_conflict && !needReExecute || s1_is_sw_prefetch) && !io.s1_kill
502  io.out.bits.paddr := s1_paddr_dup_lsu
503  io.out.bits.tlbMiss := s1_tlb_miss
504
505  // current ori test will cause the case of ldest == 0, below will be modifeid in the future.
506  // af & pf exception were modified
507  io.out.bits.uop.exceptionVec(loadPageFault) := io.dtlbResp.bits.excp(0).pf.ld
508  io.out.bits.uop.exceptionVec(loadAccessFault) := io.dtlbResp.bits.excp(0).af.ld
509
510  io.out.bits.ptwBack := io.dtlbResp.bits.ptwBack
511  io.out.bits.rsIdx := io.in.bits.rsIdx
512
513  io.in.ready := !io.in.valid || io.out.ready
514
515  XSPerfAccumulate("in_valid", io.in.valid)
516  XSPerfAccumulate("in_fire", io.in.fire)
517  XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue)
518  XSPerfAccumulate("tlb_miss", io.in.fire && s1_tlb_miss)
519  XSPerfAccumulate("tlb_miss_first_issue", io.in.fire && s1_tlb_miss && io.in.bits.isFirstIssue)
520  XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready)
521}
522
523// Load Pipeline Stage 2
524// DCache resp
525class LoadUnit_S2(implicit p: Parameters) extends XSModule with HasLoadHelper with HasCircularQueuePtrHelper with HasDCacheParameters {
526  val io = IO(new Bundle() {
527    val in = Flipped(Decoupled(new LsPipelineBundle))
528    val out = Decoupled(new LsPipelineBundle)
529    val rsFeedback = ValidIO(new RSFeedback)
530    val replaySlow = new LoadToLsqSlowIO
531    val dcacheResp = Flipped(DecoupledIO(new DCacheWordResp))
532    val pmpResp = Flipped(new PMPRespBundle())
533    val lsq = new LoadForwardQueryIO
534    val dataInvalidSqIdx = Input(UInt())
535    val sbuffer = new LoadForwardQueryIO
536    val dataForwarded = Output(Bool())
537    val s2_dcache_require_replay = Output(Bool())
538    val fullForward = Output(Bool())
539    val dcache_kill = Output(Bool())
540    val s3_delayed_load_error = Output(Bool())
541    val loadViolationQueryResp = Flipped(Valid(new LoadViolationQueryResp))
542    val csrCtrl = Flipped(new CustomCSRCtrlIO)
543    val sentFastUop = Input(Bool())
544    val static_pm = Input(Valid(Bool())) // valid for static, bits for mmio
545    val s2_can_replay_from_fetch = Output(Bool()) // dirty code
546    val loadDataFromDcache = Output(new LoadDataFromDcacheBundle)
547    val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO)))
548    val needReExecute = Output(Bool())
549    // forward tilelink D channel
550    val forward_D = Input(Bool())
551    val forwardData_D = Input(Vec(8, UInt(8.W)))
552
553    // forward mshr data
554    val forward_mshr = Input(Bool())
555    val forwardData_mshr = Input(Vec(8, UInt(8.W)))
556
557    // indicate whether forward tilelink D channel or mshr data is valid
558    val forward_result_valid = Input(Bool())
559
560    val s2_forward_fail = Output(Bool())
561  })
562
563  val pmp = WireInit(io.pmpResp)
564  when (io.static_pm.valid) {
565    pmp.ld := false.B
566    pmp.st := false.B
567    pmp.instr := false.B
568    pmp.mmio := io.static_pm.bits
569  }
570
571  val s2_is_prefetch = io.in.bits.isPrefetch
572  val s2_is_hw_prefetch = io.in.bits.isHWPrefetch
573
574  val forward_D_or_mshr_valid = io.forward_result_valid && (io.forward_D || io.forward_mshr)
575
576  // assert(!reset && io.forward_D && io.forward_mshr && io.in.valid && io.in.bits.forward_tlDchannel, "forward D and mshr at the same time")
577
578  // exception that may cause load addr to be invalid / illegal
579  //
580  // if such exception happen, that inst and its exception info
581  // will be force writebacked to rob
582  val s2_exception_vec = WireInit(io.in.bits.uop.exceptionVec)
583  s2_exception_vec(loadAccessFault) := io.in.bits.uop.exceptionVec(loadAccessFault) || pmp.ld
584  // soft prefetch will not trigger any exception (but ecc error interrupt may be triggered)
585  when (s2_is_prefetch) {
586    s2_exception_vec := 0.U.asTypeOf(s2_exception_vec.cloneType)
587  }
588  val s2_exception = ExceptionNO.selectByFu(s2_exception_vec, LduCfg).asUInt.orR && !io.in.bits.tlbMiss
589
590  // writeback access fault caused by ecc error / bus error
591  //
592  // * ecc data error is slow to generate, so we will not use it until load stage 3
593  // * in load stage 3, an extra signal io.load_error will be used to
594
595  // now cache ecc error will raise an access fault
596  // at the same time, error info (including error paddr) will be write to
597  // an customized CSR "CACHE_ERROR"
598  if (EnableAccurateLoadError) {
599    io.s3_delayed_load_error := io.dcacheResp.bits.error_delayed &&
600      io.csrCtrl.cache_error_enable &&
601      RegNext(io.out.valid)
602  } else {
603    io.s3_delayed_load_error := false.B
604  }
605
606  val actually_mmio = pmp.mmio
607  val s2_uop = io.in.bits.uop
608  val s2_mask = io.in.bits.mask
609  val s2_paddr = io.in.bits.paddr
610  val s2_tlb_miss = io.in.bits.tlbMiss
611  val s2_mmio = !s2_is_prefetch && actually_mmio && !s2_exception
612  val s2_cache_miss = io.dcacheResp.bits.miss && !forward_D_or_mshr_valid
613  val s2_cache_replay = io.dcacheResp.bits.replay && !forward_D_or_mshr_valid
614  val s2_cache_tag_error = io.dcacheResp.bits.tag_error
615  val s2_forward_fail = io.lsq.matchInvalid || io.sbuffer.matchInvalid
616  val s2_ldld_violation = io.loadViolationQueryResp.valid &&
617    io.loadViolationQueryResp.bits.have_violation &&
618    RegNext(io.csrCtrl.ldld_vio_check_enable)
619  val s2_data_invalid = io.lsq.dataInvalid && !s2_ldld_violation && !s2_exception
620
621  io.s2_forward_fail := s2_forward_fail
622  io.dcache_kill := pmp.ld || pmp.mmio // move pmp resp kill to outside
623  io.dcacheResp.ready := true.B
624  val dcacheShouldResp = !(s2_tlb_miss || s2_exception || s2_mmio || s2_is_prefetch)
625  assert(!(io.in.valid && (dcacheShouldResp && !io.dcacheResp.valid)), "DCache response got lost")
626
627  // merge forward result
628  // lsq has higher priority than sbuffer
629  val forwardMask = Wire(Vec(8, Bool()))
630  val forwardData = Wire(Vec(8, UInt(8.W)))
631
632  val fullForward = ((~forwardMask.asUInt).asUInt & s2_mask) === 0.U && !io.lsq.dataInvalid
633  io.lsq := DontCare
634  io.sbuffer := DontCare
635  io.fullForward := fullForward
636
637  // generate XLEN/8 Muxs
638  for (i <- 0 until XLEN / 8) {
639    forwardMask(i) := io.lsq.forwardMask(i) || io.sbuffer.forwardMask(i)
640    forwardData(i) := Mux(io.lsq.forwardMask(i), io.lsq.forwardData(i), io.sbuffer.forwardData(i))
641  }
642
643  XSDebug(io.out.fire, "[FWD LOAD RESP] pc %x fwd %x(%b) + %x(%b)\n",
644    s2_uop.pc,
645    io.lsq.forwardData.asUInt, io.lsq.forwardMask.asUInt,
646    io.in.bits.forwardData.asUInt, io.in.bits.forwardMask.asUInt
647  )
648
649  // data merge
650  // val rdataVec = VecInit((0 until XLEN / 8).map(j =>
651  //   Mux(forwardMask(j), forwardData(j), io.dcacheResp.bits.data(8*(j+1)-1, 8*j))
652  // )) // s2_rdataVec will be write to load queue
653  // val rdata = rdataVec.asUInt
654  // val rdataSel = LookupTree(s2_paddr(2, 0), List(
655  //   "b000".U -> rdata(63, 0),
656  //   "b001".U -> rdata(63, 8),
657  //   "b010".U -> rdata(63, 16),
658  //   "b011".U -> rdata(63, 24),
659  //   "b100".U -> rdata(63, 32),
660  //   "b101".U -> rdata(63, 40),
661  //   "b110".U -> rdata(63, 48),
662  //   "b111".U -> rdata(63, 56)
663  // ))
664  // val rdataPartialLoad = rdataHelper(s2_uop, rdataSel) // s2_rdataPartialLoad is not used
665
666  io.out.valid := io.in.valid &&
667    !s2_tlb_miss && // always request replay and cancel current flow if tlb miss
668    (!s2_data_invalid && !io.needReExecute || s2_is_prefetch) && // prefetch does not care about ld-st dependency
669    !s2_is_hw_prefetch // hardware prefetch flow should not be writebacked
670  // write_lq_safe is needed by dup logic
671  // io.write_lq_safe := !s2_tlb_miss && !s2_data_invalid
672  // Inst will be canceled in store queue / lsq,
673  // so we do not need to care about flush in load / store unit's out.valid
674  io.out.bits := io.in.bits
675  // io.out.bits.data := rdataPartialLoad
676  io.out.bits.data := 0.U // data will be generated in load_s3
677  // when exception occurs, set it to not miss and let it write back to rob (via int port)
678  if (EnableFastForward) {
679    io.out.bits.miss := s2_cache_miss &&
680      !s2_exception &&
681      !fullForward &&
682      !s2_is_prefetch
683  } else {
684    io.out.bits.miss := s2_cache_miss &&
685      !s2_exception &&
686      !s2_is_prefetch
687  }
688  io.out.bits.uop.fpWen := io.in.bits.uop.fpWen && !s2_exception
689
690  // val s2_loadDataFromDcache = new LoadDataFromDcacheBundle
691  // s2_loadDataFromDcache.forwardMask := forwardMask
692  // s2_loadDataFromDcache.forwardData := forwardData
693  // s2_loadDataFromDcache.uop := io.out.bits.uop
694  // s2_loadDataFromDcache.addrOffset := s2_paddr(2, 0)
695  // // forward D or mshr
696  // s2_loadDataFromDcache.forward_D := io.forward_D
697  // s2_loadDataFromDcache.forwardData_D := io.forwardData_D
698  // s2_loadDataFromDcache.forward_mshr := io.forward_mshr
699  // s2_loadDataFromDcache.forwardData_mshr := io.forwardData_mshr
700  // s2_loadDataFromDcache.forward_result_valid := io.forward_result_valid
701  // io.loadDataFromDcache := RegEnable(s2_loadDataFromDcache, io.in.valid)
702  io.loadDataFromDcache.respDcacheData := io.dcacheResp.bits.data_delayed
703  io.loadDataFromDcache.forwardMask := RegEnable(forwardMask, io.in.valid)
704  io.loadDataFromDcache.forwardData := RegEnable(forwardData, io.in.valid)
705  io.loadDataFromDcache.uop := RegEnable(io.out.bits.uop, io.in.valid)
706  io.loadDataFromDcache.addrOffset := RegEnable(s2_paddr(2, 0), io.in.valid)
707  // forward D or mshr
708  io.loadDataFromDcache.forward_D := RegEnable(io.forward_D, io.in.valid)
709  io.loadDataFromDcache.forwardData_D := RegEnable(io.forwardData_D, io.in.valid)
710  io.loadDataFromDcache.forward_mshr := RegEnable(io.forward_mshr, io.in.valid)
711  io.loadDataFromDcache.forwardData_mshr := RegEnable(io.forwardData_mshr, io.in.valid)
712  io.loadDataFromDcache.forward_result_valid := RegEnable(io.forward_result_valid, io.in.valid)
713
714  io.s2_can_replay_from_fetch := !s2_mmio && !s2_is_prefetch && !s2_tlb_miss
715  // if forward fail, replay this inst from fetch
716  val debug_forwardFailReplay = s2_forward_fail && !s2_mmio && !s2_is_prefetch && !s2_tlb_miss
717  // if ld-ld violation is detected, replay from this inst from fetch
718  val debug_ldldVioReplay = s2_ldld_violation && !s2_mmio && !s2_is_prefetch && !s2_tlb_miss
719  // io.out.bits.uop.ctrl.replayInst := false.B
720
721  io.out.bits.mmio := s2_mmio
722  io.out.bits.uop.flushPipe := s2_mmio && io.sentFastUop
723  io.out.bits.uop.exceptionVec := s2_exception_vec // cache error not included
724
725  // For timing reasons, sometimes we can not let
726  // io.out.bits.miss := s2_cache_miss && !s2_exception && !fullForward
727  // We use io.dataForwarded instead. It means:
728  // 1. Forward logic have prepared all data needed,
729  //    and dcache query is no longer needed.
730  // 2. ... or data cache tag error is detected, this kind of inst
731  //    will not update miss queue. That is to say, if miss, that inst
732  //    may not be refilled
733  // Such inst will be writebacked from load queue.
734  io.dataForwarded := s2_cache_miss && !s2_exception &&
735    (fullForward || io.csrCtrl.cache_error_enable && s2_cache_tag_error)
736  // io.out.bits.forwardX will be send to lq
737  io.out.bits.forwardMask := forwardMask
738  // data from dcache is not included in io.out.bits.forwardData
739  io.out.bits.forwardData := forwardData
740
741  io.in.ready := io.out.ready || !io.in.valid
742
743
744  // st-ld violation query
745  val needReExecuteVec = Wire(Vec(StorePipelineWidth, Bool()))
746  val needReExecute = Wire(Bool())
747
748  for (i <- 0 until StorePipelineWidth) {
749    //  NeedFastRecovery Valid when
750    //  1. Fast recovery query request Valid.
751    //  2. Load instruction is younger than requestors(store instructions).
752    //  3. Physical address match.
753    //  4. Data contains.
754    needReExecuteVec(i) := io.reExecuteQuery(i).valid &&
755                              isAfter(io.in.bits.uop.robIdx, io.reExecuteQuery(i).bits.robIdx) &&
756                              !s2_tlb_miss &&
757                              (s2_paddr(PAddrBits-1,3) === io.reExecuteQuery(i).bits.paddr(PAddrBits-1, 3)) &&
758                              (s2_mask & io.reExecuteQuery(i).bits.mask).orR
759  }
760  needReExecute := needReExecuteVec.asUInt.orR
761  io.needReExecute := needReExecute
762
763  // rs slow feedback port in nanhu is not used for now
764  io.rsFeedback.valid := false.B
765  io.rsFeedback.bits := DontCare
766
767  // request replay from load replay queue, fast port
768  io.replaySlow.valid := io.in.valid && !s2_is_hw_prefetch // hardware prefetch flow should not be reported to load replay queue
769  io.replaySlow.tlb_hited := !s2_tlb_miss
770  io.replaySlow.st_ld_check_ok := !needReExecute || s2_is_prefetch // Note: soft prefetch does not care about ld-st dependency
771  if (EnableFastForward) {
772    io.replaySlow.cache_no_replay := !s2_cache_replay || s2_is_prefetch || s2_mmio || s2_exception || fullForward
773  }else {
774    io.replaySlow.cache_no_replay := !s2_cache_replay || s2_is_prefetch || s2_mmio || s2_exception || io.dataForwarded
775  }
776  io.replaySlow.forward_data_valid := !s2_data_invalid || s2_is_prefetch // Note: soft prefetch does not care about ld-st dependency
777  io.replaySlow.cache_hited := !io.out.bits.miss || io.out.bits.mmio
778  io.replaySlow.can_forward_full_data := io.dataForwarded
779  io.replaySlow.ld_idx := io.in.bits.uop.lqIdx.value
780  io.replaySlow.data_invalid_sq_idx := io.dataInvalidSqIdx
781  io.replaySlow.replayCarry := io.dcacheResp.bits.replayCarry
782  io.replaySlow.miss_mshr_id := io.dcacheResp.bits.mshr_id
783  io.replaySlow.data_in_last_beat := io.in.bits.paddr(log2Up(refillBytes))
784  io.replaySlow.debug := io.in.bits.uop.debugInfo
785
786  // To be removed
787  val s2_need_replay_from_rs = Wire(Bool())
788  if (EnableFastForward) {
789    s2_need_replay_from_rs :=
790      needReExecute ||
791      s2_tlb_miss || // replay if dtlb miss
792      s2_cache_replay && !s2_is_prefetch && !s2_mmio && !s2_exception && !fullForward || // replay if dcache miss queue full / busy
793      s2_data_invalid && !s2_is_prefetch // replay if store to load forward data is not ready
794  } else {
795    // Note that if all parts of data are available in sq / sbuffer, replay required by dcache will not be scheduled
796    s2_need_replay_from_rs :=
797      needReExecute ||
798      s2_tlb_miss || // replay if dtlb miss
799      s2_cache_replay && !s2_is_prefetch && !s2_mmio && !s2_exception && !io.dataForwarded || // replay if dcache miss queue full / busy
800      s2_data_invalid && !s2_is_prefetch // replay if store to load forward data is not ready
801  }
802
803  // s2_cache_replay is quite slow to generate, send it separately to LQ
804  if (EnableFastForward) {
805    io.s2_dcache_require_replay := s2_cache_replay && !fullForward
806  } else {
807    io.s2_dcache_require_replay := s2_cache_replay &&
808      s2_need_replay_from_rs &&
809      !io.dataForwarded &&
810      !s2_is_prefetch &&
811      io.out.bits.miss
812  }
813
814  XSPerfAccumulate("in_valid", io.in.valid)
815  XSPerfAccumulate("in_fire", io.in.fire)
816  XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue)
817  XSPerfAccumulate("dcache_miss", io.in.fire && s2_cache_miss)
818  XSPerfAccumulate("dcache_miss_first_issue", io.in.fire && s2_cache_miss && io.in.bits.isFirstIssue)
819  XSPerfAccumulate("full_forward", io.in.valid && fullForward)
820  XSPerfAccumulate("dcache_miss_full_forward", io.in.valid && s2_cache_miss && fullForward)
821  XSPerfAccumulate("replay",  io.rsFeedback.valid && !io.rsFeedback.bits.hit)
822  XSPerfAccumulate("replay_tlb_miss", io.rsFeedback.valid && !io.rsFeedback.bits.hit && s2_tlb_miss)
823  XSPerfAccumulate("replay_cache", io.rsFeedback.valid && !io.rsFeedback.bits.hit && !s2_tlb_miss && s2_cache_replay)
824  XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready)
825  XSPerfAccumulate("replay_from_fetch_forward", io.out.valid && debug_forwardFailReplay)
826  XSPerfAccumulate("replay_from_fetch_load_vio", io.out.valid && debug_ldldVioReplay)
827  XSPerfAccumulate("replay_lq",  io.replaySlow.valid && (!io.replaySlow.tlb_hited || !io.replaySlow.cache_no_replay || !io.replaySlow.forward_data_valid))
828  XSPerfAccumulate("replay_tlb_miss_lq", io.replaySlow.valid && !io.replaySlow.tlb_hited)
829  XSPerfAccumulate("replay_sl_vio", io.replaySlow.valid && io.replaySlow.tlb_hited && !io.replaySlow.st_ld_check_ok)
830  XSPerfAccumulate("replay_cache_lq", io.replaySlow.valid && io.replaySlow.tlb_hited && io.replaySlow.st_ld_check_ok && !io.replaySlow.cache_no_replay)
831  XSPerfAccumulate("replay_cache_miss_lq", io.replaySlow.valid && !io.replaySlow.cache_hited)
832  XSPerfAccumulate("prefetch", io.in.fire && s2_is_prefetch)
833  XSPerfAccumulate("prefetch_ignored", io.in.fire && s2_is_prefetch && s2_cache_replay) // ignore prefetch for mshr full / miss req port conflict
834  XSPerfAccumulate("prefetch_miss", io.in.fire && s2_is_prefetch && s2_cache_miss) // prefetch req miss in l1
835  XSPerfAccumulate("prefetch_hit", io.in.fire && s2_is_prefetch && !s2_cache_miss) // prefetch req hit in l1
836  // prefetch a missed line in l1, and l1 accepted it
837  XSPerfAccumulate("prefetch_accept", io.in.fire && s2_is_prefetch && s2_cache_miss && !s2_cache_replay)
838}
839
840class LoadUnit(implicit p: Parameters) extends XSModule
841  with HasLoadHelper
842  with HasPerfEvents
843  with HasDCacheParameters
844{
845  val io = IO(new Bundle() {
846    val ldin = Flipped(Decoupled(new MemExuInput))
847    val ldout = Decoupled(new MemExuOutput)
848    val redirect = Flipped(ValidIO(new Redirect))
849    val feedbackSlow = ValidIO(new RSFeedback)
850    val feedbackFast = ValidIO(new RSFeedback)
851    val rsIdx = Input(UInt(log2Up(MemIQSizeMax).W))
852    val isFirstIssue = Input(Bool())
853    val dcache = new DCacheLoadIO
854    val sbuffer = new LoadForwardQueryIO
855    val lsq = new LoadToLsqIO
856    val tlDchannel = Input(new DcacheToLduForwardIO)
857    val forward_mshr = Flipped(new LduToMissqueueForwardIO)
858    val refill = Flipped(ValidIO(new Refill))
859    val fastUop = ValidIO(new DynInst) // early wakeup signal generated in load_s1, send to RS in load_s2
860    val trigger = Vec(3, new LoadUnitTriggerIO)
861
862    val tlb = new TlbRequestIO(2)
863    val pmp = Flipped(new PMPRespBundle()) // arrive same to tlb now
864
865    // provide prefetch info
866    val prefetch_train = ValidIO(new LdPrefetchTrainBundle())
867
868    // hardware prefetch to l1 cache req
869    val prefetch_req = Flipped(ValidIO(new L1PrefetchReq))
870
871    // load to load fast path
872    val fastpathOut = Output(new LoadToLoadIO)
873    val fastpathIn = Input(new LoadToLoadIO)
874    val loadFastMatch = Input(Bool())
875    val loadFastImm = Input(UInt(12.W))
876
877    // load ecc
878    val s3_delayed_load_error = Output(Bool()) // load ecc error
879    // Note that io.s3_delayed_load_error and io.lsq.s3_delayed_load_error is different
880
881    // load unit ctrl
882    val csrCtrl = Flipped(new CustomCSRCtrlIO)
883
884    val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO)))    // load replay
885    val lsqOut = Flipped(Decoupled(new LsPipelineBundle))
886    val debug_ls = Output(new DebugLsInfoBundle)
887    val s2IsPointerChasing = Output(Bool()) // provide right pc for hw prefetch
888  })
889
890  val load_s0 = Module(new LoadUnit_S0)
891  val load_s1 = Module(new LoadUnit_S1)
892  val load_s2 = Module(new LoadUnit_S2)
893
894  load_s0.io.lsqOut <> io.lsqOut
895
896  // load s0
897  load_s0.io.in <> io.ldin
898  load_s0.io.dtlbReq <> io.tlb.req
899  load_s0.io.dcacheReq <> io.dcache.req
900  load_s0.io.rsIdx := io.rsIdx
901  load_s0.io.isFirstIssue := io.isFirstIssue
902  load_s0.io.s0_kill := false.B
903
904  // we try pointerchasing if lfsrc_l2lForward_select condition is satisfied
905  val s0_tryPointerChasing = load_s0.io.l2lForward_select
906  val s0_pointerChasingVAddr = io.fastpathIn.data(5, 0) +& io.loadFastImm(5, 0)
907  load_s0.io.fastpath.valid := io.fastpathIn.valid
908  load_s0.io.fastpath.data := Cat(io.fastpathIn.data(XLEN-1, 6), s0_pointerChasingVAddr(5,0))
909
910  val s1_data = PipelineConnect(load_s0.io.out, load_s1.io.in, true.B,
911    load_s0.io.out.bits.uop.robIdx.needFlush(io.redirect) && !s0_tryPointerChasing).get
912
913  // load s1
914  // update s1_kill when any source has valid request
915  load_s1.io.s1_kill := RegEnable(load_s0.io.s0_kill, false.B, io.ldin.valid || io.lsqOut.valid || io.fastpathIn.valid)
916  io.tlb.req_kill := load_s1.io.s1_kill
917  load_s1.io.dtlbResp <> io.tlb.resp
918  io.dcache.s1_paddr_dup_lsu <> load_s1.io.lsuPAddr
919  io.dcache.s1_paddr_dup_dcache <> load_s1.io.dcachePAddr
920  io.dcache.s1_kill := load_s1.io.dcacheKill
921  load_s1.io.sbuffer <> io.sbuffer
922  load_s1.io.lsq <> io.lsq.forward
923  load_s1.io.loadViolationQueryReq <> io.lsq.loadViolationQuery.req
924  load_s1.io.dcacheBankConflict <> io.dcache.s1_bank_conflict
925  load_s1.io.csrCtrl <> io.csrCtrl
926  load_s1.io.reExecuteQuery := io.reExecuteQuery
927  // provide paddr and vaddr for lq
928  io.lsq.loadPaddrIn.valid := load_s1.io.out.valid && !load_s1.io.out.bits.isHWPrefetch
929  io.lsq.loadPaddrIn.bits.lqIdx := load_s1.io.out.bits.uop.lqIdx
930  io.lsq.loadPaddrIn.bits.paddr := load_s1.io.lsuPAddr
931
932  io.lsq.loadVaddrIn.valid := load_s1.io.in.valid && !load_s1.io.s1_kill && !load_s1.io.out.bits.isHWPrefetch
933  io.lsq.loadVaddrIn.bits.lqIdx := load_s1.io.out.bits.uop.lqIdx
934  io.lsq.loadVaddrIn.bits.vaddr := load_s1.io.out.bits.vaddr
935
936  // when S0 has opportunity to try pointerchasing, make sure it truely goes to S1
937  // which is S0's out is ready and dcache is ready
938  val s0_doTryPointerChasing = s0_tryPointerChasing && load_s0.io.out.ready && load_s0.io.dcacheReq.ready
939  val s1_tryPointerChasing = RegNext(s0_doTryPointerChasing, false.B)
940  val s1_pointerChasingVAddr = RegEnable(s0_pointerChasingVAddr, s0_doTryPointerChasing)
941  val cancelPointerChasing = WireInit(false.B)
942  if (EnableLoadToLoadForward) {
943    // Sometimes, we need to cancel the load-load forwarding.
944    // These can be put at S0 if timing is bad at S1.
945    // Case 0: CACHE_SET(base + offset) != CACHE_SET(base) (lowest 6-bit addition has an overflow)
946    val addressMisMatch = s1_pointerChasingVAddr(6) || RegEnable(io.loadFastImm(11, 6).orR, s0_doTryPointerChasing)
947    // Case 1: the address is not 64-bit aligned or the fuOpType is not LD
948    val addressNotAligned = s1_pointerChasingVAddr(2, 0).orR
949    val fuOpTypeIsNotLd = io.ldin.bits.uop.fuOpType =/= LSUOpType.ld
950    // Case 2: this is not a valid load-load pair
951    val notFastMatch = RegEnable(!io.loadFastMatch, s0_tryPointerChasing)
952    // Case 3: this load-load uop is cancelled
953    val isCancelled = !io.ldin.valid
954    when (s1_tryPointerChasing) {
955      cancelPointerChasing := addressMisMatch || addressNotAligned || fuOpTypeIsNotLd || notFastMatch || isCancelled
956      load_s1.io.in.bits.uop := io.ldin.bits.uop
957      val spec_vaddr = s1_data.vaddr
958      val vaddr = Cat(spec_vaddr(VAddrBits - 1, 6), s1_pointerChasingVAddr(5, 3), 0.U(3.W))
959      load_s1.io.in.bits.vaddr := vaddr
960      load_s1.io.in.bits.rsIdx := io.rsIdx
961      load_s1.io.in.bits.isFirstIssue := io.isFirstIssue
962      // We need to replace vaddr(5, 3).
963      val spec_paddr = io.tlb.resp.bits.paddr(0)
964      load_s1.io.dtlbResp.bits.paddr.foreach(_ := Cat(spec_paddr(PAddrBits - 1, 6), s1_pointerChasingVAddr(5, 3), 0.U(3.W)))
965      // 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)
966      load_s1.io.in.bits.uop.debugInfo.tlbFirstReqTime := GTimer()
967      load_s1.io.in.bits.uop.debugInfo.tlbRespTime := GTimer()
968    }
969    when (cancelPointerChasing) {
970      load_s1.io.s1_kill := true.B
971    }.otherwise {
972      load_s0.io.s0_kill := s1_tryPointerChasing && !io.lsqOut.valid
973      when (s1_tryPointerChasing) {
974        io.ldin.ready := true.B
975      }
976    }
977
978    XSPerfAccumulate("load_to_load_forward", s1_tryPointerChasing && !cancelPointerChasing)
979    XSPerfAccumulate("load_to_load_forward_try", s1_tryPointerChasing)
980    XSPerfAccumulate("load_to_load_forward_fail", cancelPointerChasing)
981    XSPerfAccumulate("load_to_load_forward_fail_cancelled", cancelPointerChasing && isCancelled)
982    XSPerfAccumulate("load_to_load_forward_fail_wakeup_mismatch", cancelPointerChasing && !isCancelled && notFastMatch)
983    XSPerfAccumulate("load_to_load_forward_fail_op_not_ld",
984      cancelPointerChasing && !isCancelled && !notFastMatch && fuOpTypeIsNotLd)
985    XSPerfAccumulate("load_to_load_forward_fail_addr_align",
986      cancelPointerChasing && !isCancelled && !notFastMatch && !fuOpTypeIsNotLd && addressNotAligned)
987    XSPerfAccumulate("load_to_load_forward_fail_set_mismatch",
988      cancelPointerChasing && !isCancelled && !notFastMatch && !fuOpTypeIsNotLd && !addressNotAligned && addressMisMatch)
989  }
990  PipelineConnect(load_s1.io.out, load_s2.io.in, true.B,
991    load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect) || cancelPointerChasing)
992
993  val (forward_D, forwardData_D) = io.tlDchannel.forward(load_s1.io.out.valid && load_s1.io.out.bits.forward_tlDchannel, load_s1.io.out.bits.mshrid, load_s1.io.out.bits.paddr)
994
995  io.forward_mshr.valid := load_s1.io.out.valid && load_s1.io.out.bits.forward_tlDchannel
996  io.forward_mshr.mshrid := load_s1.io.out.bits.mshrid
997  io.forward_mshr.paddr := load_s1.io.out.bits.paddr
998  val (forward_result_valid, forward_mshr, forwardData_mshr) = io.forward_mshr.forward()
999
1000  XSPerfAccumulate("successfully_forward_channel_D", forward_D && forward_result_valid)
1001  XSPerfAccumulate("successfully_forward_mshr", forward_mshr && forward_result_valid)
1002  // load s2
1003  load_s2.io.forward_D := forward_D
1004  load_s2.io.forwardData_D := forwardData_D
1005  load_s2.io.forward_result_valid := forward_result_valid
1006  load_s2.io.forward_mshr := forward_mshr
1007  load_s2.io.forwardData_mshr := forwardData_mshr
1008  io.s2IsPointerChasing := RegEnable(s1_tryPointerChasing && !cancelPointerChasing, load_s1.io.out.fire)
1009  io.prefetch_train.bits.fromLsPipelineBundle(load_s2.io.in.bits)
1010  // override miss bit
1011  io.prefetch_train.bits.miss := io.dcache.resp.bits.miss
1012  io.prefetch_train.bits.meta_prefetch := io.dcache.resp.bits.meta_prefetch
1013  io.prefetch_train.bits.meta_access := io.dcache.resp.bits.meta_access
1014  io.prefetch_train.valid := load_s2.io.in.fire && !load_s2.io.out.bits.mmio && !load_s2.io.in.bits.tlbMiss
1015  io.dcache.s2_kill := load_s2.io.dcache_kill // to kill mmio resp which are redirected
1016  if (env.FPGAPlatform)
1017    io.dcache.s2_pc := DontCare
1018  else
1019    io.dcache.s2_pc := load_s2.io.out.bits.uop.cf.pc
1020  load_s2.io.dcacheResp <> io.dcache.resp
1021  load_s2.io.pmpResp <> io.pmp
1022  load_s2.io.static_pm := RegNext(io.tlb.resp.bits.static_pm)
1023  load_s2.io.lsq.forwardData <> io.lsq.forward.forwardData
1024  load_s2.io.lsq.forwardMask <> io.lsq.forward.forwardMask
1025  load_s2.io.lsq.forwardMaskFast <> io.lsq.forward.forwardMaskFast // should not be used in load_s2
1026  load_s2.io.lsq.dataInvalid <> io.lsq.forward.dataInvalid
1027  load_s2.io.lsq.matchInvalid <> io.lsq.forward.matchInvalid
1028  load_s2.io.sbuffer.forwardData <> io.sbuffer.forwardData
1029  load_s2.io.sbuffer.forwardMask <> io.sbuffer.forwardMask
1030  load_s2.io.sbuffer.forwardMaskFast <> io.sbuffer.forwardMaskFast // should not be used in load_s2
1031  load_s2.io.sbuffer.dataInvalid <> io.sbuffer.dataInvalid // always false
1032  load_s2.io.sbuffer.matchInvalid <> io.sbuffer.matchInvalid
1033  load_s2.io.dataForwarded <> io.lsq.s2_load_data_forwarded
1034  load_s2.io.dataInvalidSqIdx := io.lsq.forward.dataInvalidSqIdx // provide dataInvalidSqIdx to make wakeup faster
1035  load_s2.io.loadViolationQueryResp <> io.lsq.loadViolationQuery.resp
1036  load_s2.io.csrCtrl <> io.csrCtrl
1037  load_s2.io.sentFastUop := io.fastUop.valid
1038  load_s2.io.reExecuteQuery := io.reExecuteQuery
1039  // feedback bank conflict / ld-vio check struct hazard to rs
1040  io.feedbackFast.bits := RegNext(load_s1.io.rsFeedback.bits)
1041  io.feedbackFast.valid := RegNext(load_s1.io.rsFeedback.valid && !load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect))
1042
1043  // pre-calcuate sqIdx mask in s0, then send it to lsq in s1 for forwarding
1044  val sqIdxMaskReg = RegNext(UIntToMask(load_s0.io.s0_sqIdx.value, StoreQueueSize))
1045  // to enable load-load, sqIdxMask must be calculated based on ldin.uop
1046  // If the timing here is not OK, load-load forwarding has to be disabled.
1047  // Or we calculate sqIdxMask at RS??
1048  io.lsq.forward.sqIdxMask := sqIdxMaskReg
1049  if (EnableLoadToLoadForward) {
1050    when (s1_tryPointerChasing) {
1051      io.lsq.forward.sqIdxMask := UIntToMask(io.ldin.bits.uop.sqIdx.value, StoreQueueSize)
1052    }
1053  }
1054
1055  // // use s2_hit_way to select data received in s1
1056  // load_s2.io.dcacheResp.bits.data := Mux1H(RegNext(io.dcache.s1_hit_way), RegNext(io.dcache.s1_data))
1057  // assert(load_s2.io.dcacheResp.bits.data === io.dcache.resp.bits.data)
1058
1059  // now io.fastUop.valid is sent to RS in load_s2
1060  val forward_D_or_mshr_valid = forward_result_valid && (forward_D || forward_mshr)
1061  val s2_dcache_hit = io.dcache.s2_hit || forward_D_or_mshr_valid // dcache hit dup in lsu side
1062
1063  io.fastUop.valid := RegNext(
1064      !io.dcache.s1_disable_fast_wakeup &&  // load fast wakeup should be disabled when dcache data read is not ready
1065      load_s1.io.in.valid && // valid load request
1066      !load_s1.io.in.bits.isHWPrefetch && // is not hardware prefetch req
1067      !load_s1.io.s1_kill && // killed by load-load forwarding
1068      !load_s1.io.dtlbResp.bits.fast_miss && // not mmio or tlb miss, pf / af not included here
1069      !io.lsq.forward.dataInvalidFast // forward failed
1070    ) &&
1071    !RegNext(load_s1.io.needLdVioCheckRedo) && // load-load violation check: load paddr cam struct hazard
1072    !RegNext(load_s1.io.needReExecute) &&
1073    !RegNext(load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect)) &&
1074    (load_s2.io.in.valid && !load_s2.io.needReExecute && s2_dcache_hit) // dcache hit in lsu side
1075
1076  io.fastUop.bits := RegNext(load_s1.io.out.bits.uop)
1077
1078  XSDebug(load_s0.io.out.valid,
1079    p"S0: pc ${Hexadecimal(load_s0.io.out.bits.uop.pc)}, lId ${Hexadecimal(load_s0.io.out.bits.uop.lqIdx.asUInt)}, " +
1080    p"vaddr ${Hexadecimal(load_s0.io.out.bits.vaddr)}, mask ${Hexadecimal(load_s0.io.out.bits.mask)}\n")
1081  XSDebug(load_s1.io.out.valid,
1082    p"S1: pc ${Hexadecimal(load_s1.io.out.bits.uop.pc)}, lId ${Hexadecimal(load_s1.io.out.bits.uop.lqIdx.asUInt)}, tlb_miss ${io.tlb.resp.bits.miss}, " +
1083    p"paddr ${Hexadecimal(load_s1.io.out.bits.paddr)}, mmio ${load_s1.io.out.bits.mmio}\n")
1084
1085  // writeback to LSQ
1086  // Current dcache use MSHR
1087  // Load queue will be updated at s2 for both hit/miss int/fp load
1088  io.lsq.loadIn.valid := load_s2.io.out.valid && !load_s2.io.out.bits.isHWPrefetch
1089  // generate LqWriteBundle from LsPipelineBundle
1090  io.lsq.loadIn.bits.fromLsPipelineBundle(load_s2.io.out.bits)
1091
1092  io.lsq.replayFast := load_s1.io.replayFast
1093  io.lsq.replaySlow := load_s2.io.replaySlow
1094  io.lsq.replaySlow.valid := load_s2.io.replaySlow.valid && !load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect)
1095
1096  // generate duplicated load queue data wen
1097  val load_s2_valid_vec = RegInit(0.U(6.W))
1098  val load_s2_leftFire = load_s1.io.out.valid && load_s2.io.in.ready
1099  // val write_lq_safe = load_s2.io.write_lq_safe
1100  load_s2_valid_vec := 0x0.U(6.W)
1101  when (load_s2_leftFire && !load_s1.io.out.bits.isHWPrefetch) { load_s2_valid_vec := 0x3f.U(6.W)} // TODO: refactor me
1102  when (load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect)) { load_s2_valid_vec := 0x0.U(6.W) }
1103  assert(RegNext((load_s2.io.in.valid === load_s2_valid_vec(0)) || RegNext(load_s1.io.out.bits.isHWPrefetch)))
1104  io.lsq.loadIn.bits.lq_data_wen_dup := load_s2_valid_vec.asBools()
1105
1106  // s2_dcache_require_replay signal will be RegNexted, then used in s3
1107  io.lsq.s2_dcache_require_replay := load_s2.io.s2_dcache_require_replay
1108
1109  // write to rob and writeback bus
1110  val s2_wb_valid = load_s2.io.out.valid && !load_s2.io.out.bits.miss && !load_s2.io.out.bits.mmio
1111
1112  // Int load, if hit, will be writebacked at s2
1113  val hitLoadOut = Wire(Valid(new MemExuOutput))
1114  hitLoadOut.valid := s2_wb_valid
1115  hitLoadOut.bits.uop := load_s2.io.out.bits.uop
1116  hitLoadOut.bits.data := load_s2.io.out.bits.data
1117  hitLoadOut.bits.debug.isMMIO := load_s2.io.out.bits.mmio
1118  hitLoadOut.bits.debug.isPerfCnt := false.B
1119  hitLoadOut.bits.debug.paddr := load_s2.io.out.bits.paddr
1120  hitLoadOut.bits.debug.vaddr := load_s2.io.out.bits.vaddr
1121
1122  load_s2.io.out.ready := true.B
1123
1124  // load s3
1125  val s3_load_wb_meta_reg = RegNext(Mux(hitLoadOut.valid, hitLoadOut.bits, io.lsq.ldout.bits))
1126
1127  // data from load queue refill
1128  val s3_loadDataFromLQ = RegEnable(io.lsq.ldRawData, io.lsq.ldout.valid)
1129  val s3_rdataLQ = s3_loadDataFromLQ.mergedData()
1130  val s3_rdataSelLQ = LookupTree(s3_loadDataFromLQ.addrOffset, List(
1131    "b000".U -> s3_rdataLQ(63, 0),
1132    "b001".U -> s3_rdataLQ(63, 8),
1133    "b010".U -> s3_rdataLQ(63, 16),
1134    "b011".U -> s3_rdataLQ(63, 24),
1135    "b100".U -> s3_rdataLQ(63, 32),
1136    "b101".U -> s3_rdataLQ(63, 40),
1137    "b110".U -> s3_rdataLQ(63, 48),
1138    "b111".U -> s3_rdataLQ(63, 56)
1139  ))
1140  val s3_rdataPartialLoadLQ = rdataHelper(s3_loadDataFromLQ.uop, s3_rdataSelLQ)
1141
1142  // data from dcache hit
1143  val s3_loadDataFromDcache = load_s2.io.loadDataFromDcache
1144  val s3_rdataDcache = s3_loadDataFromDcache.mergedData()
1145  val s3_rdataSelDcache = LookupTree(s3_loadDataFromDcache.addrOffset, List(
1146    "b000".U -> s3_rdataDcache(63, 0),
1147    "b001".U -> s3_rdataDcache(63, 8),
1148    "b010".U -> s3_rdataDcache(63, 16),
1149    "b011".U -> s3_rdataDcache(63, 24),
1150    "b100".U -> s3_rdataDcache(63, 32),
1151    "b101".U -> s3_rdataDcache(63, 40),
1152    "b110".U -> s3_rdataDcache(63, 48),
1153    "b111".U -> s3_rdataDcache(63, 56)
1154  ))
1155  val s3_rdataPartialLoadDcache = rdataHelper(s3_loadDataFromDcache.uop, s3_rdataSelDcache)
1156
1157  io.ldout.bits := s3_load_wb_meta_reg
1158  io.ldout.bits.data := Mux(RegNext(hitLoadOut.valid), s3_rdataPartialLoadDcache, s3_rdataPartialLoadLQ)
1159  io.ldout.valid := RegNext(hitLoadOut.valid) && !RegNext(load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect)) ||
1160    RegNext(io.lsq.ldout.valid) && !RegNext(io.lsq.ldout.bits.uop.robIdx.needFlush(io.redirect)) && !RegNext(hitLoadOut.valid)
1161
1162  io.ldout.bits.uop.exceptionVec(loadAccessFault) := s3_load_wb_meta_reg.uop.exceptionVec(loadAccessFault) ||
1163    RegNext(hitLoadOut.valid) && load_s2.io.s3_delayed_load_error
1164
1165  // fast load to load forward
1166  io.fastpathOut.valid := RegNext(load_s2.io.out.valid) // for debug only
1167  io.fastpathOut.data := s3_loadDataFromDcache.mergedData() // fastpath is for ld only
1168
1169  // feedback tlb miss / dcache miss queue full
1170  io.feedbackSlow.bits := RegNext(load_s2.io.rsFeedback.bits)
1171  io.feedbackSlow.valid := RegNext(load_s2.io.rsFeedback.valid && !load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect))
1172  // If replay is reported at load_s1, inst will be canceled (will not enter load_s2),
1173  // in that case:
1174  // * replay should not be reported twice
1175  assert(!(RegNext(io.feedbackFast.valid) && io.feedbackSlow.valid))
1176  // * io.fastUop.valid should not be reported
1177  assert(!RegNext(io.feedbackFast.valid && !io.feedbackFast.bits.hit && io.fastUop.valid))
1178
1179  // load forward_fail/ldld_violation check
1180  // check for inst in load pipeline
1181  val s3_forward_fail = RegNext(io.lsq.forward.matchInvalid || io.sbuffer.matchInvalid)
1182  val s3_ldld_violation = RegNext(
1183    io.lsq.loadViolationQuery.resp.valid &&
1184    io.lsq.loadViolationQuery.resp.bits.have_violation &&
1185    RegNext(io.csrCtrl.ldld_vio_check_enable)
1186  )
1187  val s3_need_replay_from_fetch = s3_forward_fail || s3_ldld_violation
1188  val s3_can_replay_from_fetch = RegEnable(load_s2.io.s2_can_replay_from_fetch, load_s2.io.out.valid)
1189  // 1) use load pipe check result generated in load_s3 iff load_hit
1190  when (RegNext(hitLoadOut.valid)) {
1191    io.ldout.bits.uop.replayInst := s3_need_replay_from_fetch
1192  }
1193  // 2) otherwise, write check result to load queue
1194  io.lsq.s3_replay_from_fetch := s3_need_replay_from_fetch && s3_can_replay_from_fetch
1195
1196  // s3_delayed_load_error path is not used for now, as we writeback load result in load_s3
1197  // but we keep this path for future use
1198  io.s3_delayed_load_error := false.B
1199  io.lsq.s3_delayed_load_error := false.B //load_s2.io.s3_delayed_load_error
1200
1201  io.lsq.ldout.ready := !hitLoadOut.valid
1202
1203  when(io.feedbackSlow.valid && !io.feedbackSlow.bits.hit){
1204    // when need replay from rs, inst should not be writebacked to rob
1205    assert(RegNext(!hitLoadOut.valid))
1206    assert(RegNext(!io.lsq.loadIn.valid) || RegNext(load_s2.io.s2_dcache_require_replay))
1207  }
1208
1209  // hareware prefetch to l1
1210  io.prefetch_req <> load_s0.io.prefetch_in
1211
1212  // trigger
1213  val lastValidData = RegEnable(io.ldout.bits.data, io.ldout.fire)
1214  val hitLoadAddrTriggerHitVec = Wire(Vec(3, Bool()))
1215  val lqLoadAddrTriggerHitVec = io.lsq.trigger.lqLoadAddrTriggerHitVec
1216  (0 until 3).map{i => {
1217    val tdata2 = io.trigger(i).tdata2
1218    val matchType = io.trigger(i).matchType
1219    val tEnable = io.trigger(i).tEnable
1220
1221    hitLoadAddrTriggerHitVec(i) := TriggerCmp(load_s2.io.out.bits.vaddr, tdata2, matchType, tEnable)
1222    io.trigger(i).addrHit := Mux(hitLoadOut.valid, hitLoadAddrTriggerHitVec(i), lqLoadAddrTriggerHitVec(i))
1223    io.trigger(i).lastDataHit := TriggerCmp(lastValidData, tdata2, matchType, tEnable)
1224  }}
1225  io.lsq.trigger.hitLoadAddrTriggerHitVec := hitLoadAddrTriggerHitVec
1226
1227  // s1
1228  io.debug_ls.s1.isBankConflict := load_s1.io.in.fire && (!load_s1.io.dcacheKill && load_s1.io.dcacheBankConflict)
1229  io.debug_ls.s1.isLoadToLoadForward := load_s1.io.out.valid && s1_tryPointerChasing && !cancelPointerChasing
1230  io.debug_ls.s1.isTlbFirstMiss := io.tlb.resp.valid && io.tlb.resp.bits.miss && io.tlb.resp.bits.debug.isFirstIssue
1231  io.debug_ls.s1.isReplayFast := io.lsq.replayFast.valid && io.lsq.replayFast.needreplay
1232  io.debug_ls.s1_robIdx := load_s1.io.in.bits.uop.robIdx.value
1233  // s2
1234  io.debug_ls.s2.isDcacheFirstMiss := load_s2.io.in.fire && load_s2.io.in.bits.isFirstIssue && load_s2.io.dcacheResp.bits.miss
1235  io.debug_ls.s2.isForwardFail := load_s2.io.in.fire && load_s2.io.s2_forward_fail
1236  io.debug_ls.s2.isReplaySlow := io.lsq.replaySlow.valid && io.lsq.replaySlow.needreplay
1237  io.debug_ls.s2.isLoadReplayTLBMiss := io.lsq.replaySlow.valid && !io.lsq.replaySlow.tlb_hited
1238  io.debug_ls.s2.isLoadReplayCacheMiss := io.lsq.replaySlow.valid && !io.lsq.replaySlow.cache_hited
1239  io.debug_ls.replayCnt := DontCare
1240  io.debug_ls.s2_robIdx := load_s2.io.in.bits.uop.robIdx.value
1241
1242  // bug lyq: some signals in perfEvents are no longer suitable for the current MemBlock design
1243  // hardware performance counter
1244  val perfEvents = Seq(
1245    ("load_s0_in_fire         ", load_s0.io.in.fire                                                                                                              ),
1246    ("load_to_load_forward    ", load_s1.io.out.valid && s1_tryPointerChasing && !cancelPointerChasing                                                           ),
1247    ("stall_dcache            ", load_s0.io.out.valid && load_s0.io.out.ready && !load_s0.io.dcacheReq.ready                                                     ),
1248    ("load_s1_in_fire         ", load_s1.io.in.fire                                                                                                              ),
1249    ("load_s1_tlb_miss        ", load_s1.io.in.fire && load_s1.io.dtlbResp.bits.miss                                                                             ),
1250    ("load_s2_in_fire         ", load_s2.io.in.fire                                                                                                              ),
1251    ("load_s2_dcache_miss     ", load_s2.io.in.fire && load_s2.io.dcacheResp.bits.miss                                                                           ),
1252    ("load_s2_replay          ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit                                                                  ),
1253    ("load_s2_replay_tlb_miss ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit && load_s2.io.in.bits.tlbMiss                                    ),
1254    ("load_s2_replay_cache    ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit && !load_s2.io.in.bits.tlbMiss && load_s2.io.dcacheResp.bits.miss),
1255  )
1256  generatePerfEvent()
1257
1258  when(io.ldout.fire){
1259    XSDebug("ldout %x\n", io.ldout.bits.uop.pc)
1260  }
1261}
1262