xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/loadpipe/LoadPipe.scala (revision b8b991d636e3eae0d6cc2e36846166652699f0c2)
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.cache
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.tilelink.ClientMetadata
23import utils.{HasPerfEvents, XSDebug, XSPerfAccumulate}
24import utility.ParallelPriorityMux
25import xiangshan.L1CacheErrorInfo
26import xiangshan.cache.dcache.{DCacheWPU, IdealWPU}
27
28class LoadPipe(id: Int)(implicit p: Parameters) extends DCacheModule with HasPerfEvents {
29  val io = IO(new DCacheBundle {
30    // incoming requests
31    val lsu = Flipped(new DCacheLoadIO)
32    // req got nacked in stage 0?
33    val nack      = Input(Bool())
34
35    // meta and data array read port
36    val meta_read = DecoupledIO(new MetaReadReq)
37    val meta_resp = Input(Vec(nWays, new Meta))
38    val extra_meta_resp = Input(Vec(nWays, new DCacheExtraMeta))
39
40    val tag_read = DecoupledIO(new TagReadReq)
41    val tag_resp = Input(Vec(nWays, UInt(encTagBits.W)))
42
43    val banked_data_read = DecoupledIO(new L1BankedDataReadReq)
44    val banked_data_resp = Input(new L1BankedDataReadResult())
45    val read_error_delayed = Input(Bool())
46
47    // access bit update
48    val access_flag_write = DecoupledIO(new FlagMetaWriteReq)
49
50    // banked data read conflict
51    val bank_conflict_slow = Input(Bool())
52
53    // send miss request to miss queue
54    val miss_req    = DecoupledIO(new MissReq)
55    val miss_resp   = Input(new MissResp)
56
57    // update state vec in replacement algo
58    val replace_access = ValidIO(new ReplacementAccessBundle)
59    // find the way to be replaced
60    val replace_way = new ReplacementWayReqIO
61
62    // load fast wakeup should be disabled when data read is not ready
63    val disable_ld_fast_wakeup = Input(Bool())
64
65    // ecc error
66    val error = Output(new L1CacheErrorInfo())
67
68    // // debug_ls_info
69    // val debug_s2_cache_miss = Bool()
70  })
71
72  assert(RegNext(io.meta_read.ready))
73
74  val s1_ready = Wire(Bool())
75  val s2_ready = Wire(Bool())
76  // LSU requests
77  // it you got nacked, you can directly passdown
78  val not_nacked_ready = io.meta_read.ready && io.tag_read.ready && s1_ready
79  val nacked_ready     = true.B
80
81  // ready can wait for valid
82  io.lsu.req.ready := (!io.nack && not_nacked_ready) || (io.nack && nacked_ready)
83  io.meta_read.valid := io.lsu.req.fire() && !io.nack
84  io.tag_read.valid := io.lsu.req.fire() && !io.nack
85
86  val meta_read = io.meta_read.bits
87  val tag_read = io.tag_read.bits
88
89  // Tag read for new requests
90  meta_read.idx := get_idx(io.lsu.req.bits.vaddr)
91  meta_read.way_en := ~0.U(nWays.W)
92  // meta_read.tag := DontCare
93
94  tag_read.idx := get_idx(io.lsu.req.bits.vaddr)
95  tag_read.way_en := ~0.U(nWays.W)
96
97  // Pipeline
98  // --------------------------------------------------------------------------------
99  // stage 0
100  // --------------------------------------------------------------------------------
101  // read tag
102
103  val s0_valid = io.lsu.req.fire()
104  val s0_req = io.lsu.req.bits
105  val s0_fire = s0_valid && s1_ready
106  val s0_vaddr = s0_req.vaddr
107  val s0_replayCarry = s0_req.replayCarry
108  assert(RegNext(!(s0_valid && (s0_req.cmd =/= MemoryOpConstants.M_XRD && s0_req.cmd =/= MemoryOpConstants.M_PFR && s0_req.cmd =/= MemoryOpConstants.M_PFW))), "LoadPipe only accepts load req / softprefetch read or write!")
109  dump_pipeline_reqs("LoadPipe s0", s0_valid, s0_req)
110
111  // --------------------------------------------------------------------------------
112  // stage 1
113  // --------------------------------------------------------------------------------
114  // tag match, read data
115
116  val s1_valid = RegInit(false.B)
117  val s1_req = RegEnable(s0_req, s0_fire)
118  // in stage 1, load unit gets the physical address
119  val s1_paddr_dup_lsu = io.lsu.s1_paddr_dup_lsu
120  val s1_paddr_dup_dcache = io.lsu.s1_paddr_dup_dcache
121  // LSU may update the address from io.lsu.s1_paddr, which affects the bank read enable only.
122  val s1_vaddr = Cat(s1_req.vaddr(VAddrBits - 1, blockOffBits), io.lsu.s1_paddr_dup_lsu(blockOffBits - 1, 0))
123  val s1_bank_oh = UIntToOH(addr_to_dcache_bank(s1_vaddr))
124  val s1_nack = RegNext(io.nack)
125  val s1_nack_data = !io.banked_data_read.ready
126  val s1_fire = s1_valid && s2_ready
127  s1_ready := !s1_valid || s1_fire
128
129  when (s0_fire) { s1_valid := true.B }
130  .elsewhen (s1_fire) { s1_valid := false.B }
131
132  dump_pipeline_reqs("LoadPipe s1", s1_valid, s1_req)
133
134  // tag check
135  val meta_resp = io.meta_resp
136  val tag_resp = io.tag_resp.map(r => r(tagBits - 1, 0))
137  def wayMap[T <: Data](f: Int => T) = VecInit((0 until nWays).map(f))
138
139  // dcache side tag match
140  /* // just ideal situation
141  val idealWPU = Module(new IdealWPU)
142  val s1_vaddr_dup_dc = Wire(UInt(PAddrBits.W))
143  s1_vaddr_dup_dc := RegEnable(s0_req.addr, s0_fire)
144  idealWPU.io.req.bits.vaddr := s1_vaddr_dup_dc
145  idealWPU.io.req.valid := true.B
146  idealWPU.io.idealIf.s1_tag_resp := tag_resp
147  idealWPU.io.idealIf.s1_meta_resp := meta_resp
148  idealWPU.io.idealIf.s1_real_tag := get_tag(s1_paddr_dup_dcache)
149  */
150  // real wpu
151  val wpu = Module(new DCacheWPU)
152  // req in s0
153  wpu.io.req.bits.vaddr := s0_vaddr
154  wpu.io.req.bits.replayCarry := s0_replayCarry
155  wpu.io.req.valid := s0_valid
156  // check in s1
157  wpu.io.check.bits.s1_tag_resp := tag_resp
158  wpu.io.check.bits.s1_meta_resp := meta_resp
159  wpu.io.check.bits.s1_real_tag := get_tag(s1_paddr_dup_dcache)
160  wpu.io.check.valid := s1_valid
161  // correct in s2
162  val s2_wpu_pred_fail = wpu.io.s2_pred_fail
163  val s2_real_way_en = wpu.io.s2_real_way_en
164
165  // resp in s1
166  val s1_tag_match_way_dup_dc = Wire(UInt(nWays.W))
167  val s1_tag_match_way_dup_lsu = Wire(UInt(nWays.W))
168  when (wpu.io.resp.valid){
169    s1_tag_match_way_dup_dc := wpu.io.resp.bits.predict_way_en
170    s1_tag_match_way_dup_lsu := wpu.io.resp.bits.predict_way_en
171  }.otherwise {
172    val s1_tag_eq_way_dup_dc = wayMap((w: Int) => tag_resp(w) === (get_tag(s1_paddr_dup_dcache))).asUInt
173    s1_tag_match_way_dup_dc := wayMap((w: Int) => s1_tag_eq_way_dup_dc(w) && meta_resp(w).coh.isValid()).asUInt
174
175    // lsu side tag match
176    val s1_tag_eq_way_dup_lsu = wayMap((w: Int) => tag_resp(w) === (get_tag(s1_paddr_dup_lsu))).asUInt
177    s1_tag_match_way_dup_lsu := wayMap((w: Int) => s1_tag_eq_way_dup_lsu(w) && meta_resp(w).coh.isValid()).asUInt
178  }
179  val s1_tag_match_dup_dc = s1_tag_match_way_dup_dc.orR
180  val s1_tag_match_dup_lsu = s1_tag_match_way_dup_lsu.orR
181  assert(RegNext(!s1_valid || PopCount(s1_tag_match_way_dup_dc) <= 1.U), "tag should not match with more than 1 way")
182
183  val s1_fake_meta = Wire(new Meta)
184  // s1_fake_meta.tag := get_tag(s1_paddr_dup_dcache)
185  s1_fake_meta.coh := ClientMetadata.onReset
186  val s1_fake_tag = get_tag(s1_paddr_dup_dcache)
187
188  // when there are no tag match, we give it a Fake Meta
189  // this simplifies our logic in s2 stage
190  val s1_hit_meta = Mux(s1_tag_match_dup_dc, Mux1H(s1_tag_match_way_dup_dc, wayMap((w: Int) => meta_resp(w))), s1_fake_meta)
191  val s1_hit_coh = s1_hit_meta.coh
192  val s1_hit_error = Mux(s1_tag_match_dup_dc, Mux1H(s1_tag_match_way_dup_dc, wayMap((w: Int) => io.extra_meta_resp(w).error)), false.B)
193  val s1_hit_prefetch = Mux(s1_tag_match_dup_dc, Mux1H(s1_tag_match_way_dup_dc, wayMap((w: Int) => io.extra_meta_resp(w).prefetch)), false.B)
194  val s1_hit_access = Mux(s1_tag_match_dup_dc, Mux1H(s1_tag_match_way_dup_dc, wayMap((w: Int) => io.extra_meta_resp(w).access)), false.B)
195
196  io.replace_way.set.valid := RegNext(s0_fire)
197  io.replace_way.set.bits := get_idx(s1_vaddr)
198
199  val s1_invalid_vec = wayMap(w => !meta_resp(w).coh.isValid())
200  val s1_have_invalid_way = s1_invalid_vec.asUInt.orR
201  val s1_invalid_way_en = ParallelPriorityMux(s1_invalid_vec.zipWithIndex.map(x => x._1 -> UIntToOH(x._2.U(nWays.W))))
202  val s1_repl_way_en_oh = Mux(s1_have_invalid_way, s1_invalid_way_en, UIntToOH(io.replace_way.way))
203  val s1_repl_way_en_enc = OHToUInt(s1_repl_way_en_oh)
204  val s1_repl_tag = Mux1H(s1_repl_way_en_oh, wayMap(w => tag_resp(w)))
205  val s1_repl_coh = Mux1H(s1_repl_way_en_oh, wayMap(w => meta_resp(w).coh))
206  val s1_repl_extra_meta = Mux1H(s1_repl_way_en_oh, wayMap(w => io.extra_meta_resp(w)))
207
208  val s1_need_replacement = !s1_tag_match_dup_dc
209  val s1_way_en = Mux(s1_need_replacement, s1_repl_way_en_oh, s1_tag_match_way_dup_dc)
210  val s1_coh = Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh)
211  val s1_tag = Mux(s1_need_replacement, s1_repl_tag, get_tag(s1_paddr_dup_dcache))
212
213  XSPerfAccumulate("load_has_invalid_way_but_select_valid_way", io.replace_way.set.valid && wayMap(w => !meta_resp(w).coh.isValid()).asUInt.orR && s1_need_replacement && s1_repl_coh.isValid())
214  XSPerfAccumulate("load_using_replacement", io.replace_way.set.valid && s1_need_replacement)
215
216  // data read
217  io.banked_data_read.valid := s1_fire && !s1_nack
218  io.banked_data_read.bits.addr := s1_vaddr
219  io.banked_data_read.bits.way_en := s1_tag_match_way_dup_dc
220
221  // get s1_will_send_miss_req in lpad_s1
222  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
223  val s1_new_hit_coh = s1_hit_coh.onAccess(s1_req.cmd)._3
224  val s1_hit = s1_tag_match_dup_dc && s1_has_permission && s1_hit_coh === s1_new_hit_coh
225  val s1_will_send_miss_req = s1_valid && !s1_nack && !s1_nack_data && !s1_hit
226
227  // check ecc error
228  val s1_encTag = Mux1H(s1_tag_match_way_dup_dc, wayMap((w: Int) => io.tag_resp(w)))
229  val s1_flag_error = Mux(s1_need_replacement, false.B, s1_hit_error) // error reported by exist dcache error bit
230
231  // --------------------------------------------------------------------------------
232  // stage 2
233  // --------------------------------------------------------------------------------
234  // return data
235
236  // val s2_valid = RegEnable(next = s1_valid && !io.lsu.s1_kill, init = false.B, enable = s1_fire)
237  val s2_valid = RegInit(false.B)
238  val s2_req = RegEnable(s1_req, s1_fire)
239  val s2_paddr = RegEnable(s1_paddr_dup_dcache, s1_fire)
240  val s2_vaddr = RegEnable(s1_vaddr, s1_fire)
241  val s2_bank_oh = RegEnable(s1_bank_oh, s1_fire)
242  val s2_bank_oh_dup_0 = RegEnable(s1_bank_oh, s1_fire)
243  s2_ready := true.B
244
245  val s2_fire = s2_valid
246
247  when (s1_fire) { s2_valid := !io.lsu.s1_kill }
248  .elsewhen(io.lsu.resp.fire()) { s2_valid := false.B }
249
250  dump_pipeline_reqs("LoadPipe s2", s2_valid, s2_req)
251
252
253  // hit, miss, nack, permission checking
254  // dcache side tag match
255  val s2_tag_match_way = RegEnable(s1_tag_match_way_dup_dc, s1_fire)
256  val s2_tag_match = RegEnable(s1_tag_match_dup_dc, s1_fire)
257
258  // lsu side tag match
259  val s2_hit_dup_lsu = RegNext(s1_tag_match_dup_lsu)
260
261  io.lsu.s2_hit := s2_hit_dup_lsu && !s2_wpu_pred_fail
262
263  val s2_hit_meta = RegEnable(s1_hit_meta, s1_fire)
264  val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire)
265  val s2_has_permission = s2_hit_coh.onAccess(s2_req.cmd)._1 // for write prefetch
266  val s2_new_hit_coh = s2_hit_coh.onAccess(s2_req.cmd)._3 // for write prefetch
267
268  val s2_way_en = RegEnable(s1_way_en, s1_fire)
269  val s2_repl_coh = RegEnable(s1_repl_coh, s1_fire)
270  val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire)
271  val s2_repl_extra_meta = RegEnable(s1_repl_extra_meta, s1_fire) // not used for now
272  val s2_encTag = RegEnable(s1_encTag, s1_fire)
273
274  // when req got nacked, upper levels should replay this request
275  // nacked or not
276  val s2_nack_hit = RegEnable(s1_nack, s1_fire)
277  // can no allocate mshr for load miss
278  val s2_nack_no_mshr = io.miss_req.valid && !io.miss_req.ready
279  // Bank conflict on data arrays
280  val s2_nack_data = RegEnable(!io.banked_data_read.ready, s1_fire)
281  val s2_nack = s2_nack_hit || s2_nack_no_mshr || s2_nack_data
282  // s2 miss merged
283  val s2_miss_merged = io.miss_req.fire && !io.miss_req.bits.cancel && io.miss_resp.merged
284
285  val s2_bank_addr = addr_to_dcache_bank(s2_paddr)
286  dontTouch(s2_bank_addr)
287
288  val s2_instrtype = s2_req.instrtype
289
290  val s2_tag_error = dcacheParameters.tagCode.decode(s2_encTag).error // error reported by tag ecc check
291  val s2_flag_error = RegEnable(s1_flag_error, s1_fire)
292
293  val s2_hit_prefetch = RegEnable(s1_hit_prefetch, s1_fire)
294  val s2_hit_access = RegEnable(s1_hit_access, s1_fire)
295
296  val s2_hit = s2_tag_match && s2_has_permission && s2_hit_coh === s2_new_hit_coh && !s2_wpu_pred_fail
297
298  // only dump these signals when they are actually valid
299  dump_pipeline_valids("LoadPipe s2", "s2_hit", s2_valid && s2_hit)
300  dump_pipeline_valids("LoadPipe s2", "s2_nack", s2_valid && s2_nack)
301  dump_pipeline_valids("LoadPipe s2", "s2_nack_hit", s2_valid && s2_nack_hit)
302  dump_pipeline_valids("LoadPipe s2", "s2_nack_no_mshr", s2_valid && s2_nack_no_mshr)
303
304  val s2_can_send_miss_req = RegEnable(s1_will_send_miss_req, s1_fire)
305
306  // send load miss to miss queue
307  io.miss_req.valid := s2_valid && s2_can_send_miss_req && !s2_wpu_pred_fail
308  io.miss_req.bits := DontCare
309  io.miss_req.bits.source := s2_instrtype
310  io.miss_req.bits.cmd := s2_req.cmd
311  io.miss_req.bits.addr := get_block_addr(s2_paddr)
312  io.miss_req.bits.vaddr := s2_vaddr
313  io.miss_req.bits.way_en := s2_way_en
314  io.miss_req.bits.req_coh := s2_hit_coh
315  io.miss_req.bits.replace_coh := s2_repl_coh
316  io.miss_req.bits.replace_tag := s2_repl_tag
317  io.miss_req.bits.cancel := io.lsu.s2_kill || s2_tag_error
318  io.miss_req.bits.pc := io.lsu.s2_pc
319
320  // send back response
321  val resp = Wire(ValidIO(new DCacheWordResp))
322  resp.valid := s2_valid
323  resp.bits := DontCare
324  // resp.bits.data := s2_word_decoded
325  // resp.bits.data := banked_data_resp_word.raw_data
326  // * on miss or nack, upper level should replay request
327  // but if we successfully sent the request to miss queue
328  // upper level does not need to replay request
329  // they can sit in load queue and wait for refill
330  //
331  // * report a miss if bank conflict is detected
332  val real_miss = Wire(Bool())
333  when (wpu.io.resp.valid){
334    real_miss := !s2_real_way_en.orR
335  }.otherwise{
336    real_miss := !s2_hit_dup_lsu
337  }
338  // io.debug_s2_cache_miss := real_miss
339  resp.bits.miss := real_miss
340  io.lsu.s2_first_hit := s2_req.isFirstIssue && s2_hit
341  // load pipe need replay when there is a bank conflict or wpu predict fail
342  resp.bits.replay := DontCare
343  resp.bits.replayCarry.valid := resp.bits.miss
344  resp.bits.replayCarry.real_way_en := s2_real_way_en
345  resp.bits.meta_prefetch := s2_hit_prefetch
346  resp.bits.meta_access := s2_hit_access
347  resp.bits.tag_error := s2_tag_error // report tag_error in load s2
348  resp.bits.mshr_id := io.miss_resp.id
349  resp.bits.handled := io.miss_req.fire && !io.miss_req.bits.cancel && io.miss_resp.handled
350  resp.bits.debug_robIdx := s2_req.debug_robIdx
351
352  XSPerfAccumulate("wpu_pred_fail", s2_wpu_pred_fail && s2_valid)
353  XSPerfAccumulate("dcache_read_bank_conflict", io.bank_conflict_slow && s2_valid)
354  XSPerfAccumulate("dcache_read_from_prefetched_line", s2_valid && s2_hit_prefetch && !resp.bits.miss)
355  XSPerfAccumulate("dcache_first_read_from_prefetched_line", s2_valid && s2_hit_prefetch && !resp.bits.miss && !s2_hit_access)
356
357  io.lsu.resp.valid := resp.valid
358  io.lsu.resp.bits := resp.bits
359  assert(RegNext(!(resp.valid && !io.lsu.resp.ready)), "lsu should be ready in s2")
360
361  when (resp.valid) {
362    resp.bits.dump()
363  }
364
365  io.lsu.debug_s1_hit_way := s1_tag_match_way_dup_dc
366  io.lsu.s1_disable_fast_wakeup := io.disable_ld_fast_wakeup
367  io.lsu.s2_bank_conflict := io.bank_conflict_slow
368  io.lsu.s2_wpu_pred_fail := s2_wpu_pred_fail
369  io.lsu.s2_mq_nack       := (resp.bits.miss && (!io.miss_req.fire() || s2_nack))
370  assert(RegNext(s1_ready && s2_ready), "load pipeline should never be blocked")
371
372  // --------------------------------------------------------------------------------
373  // stage 3
374  // --------------------------------------------------------------------------------
375  // report ecc error and get selected dcache data
376
377  val s3_valid = RegNext(s2_valid)
378  val s3_vaddr = RegEnable(s2_vaddr, s2_fire)
379  val s3_paddr = RegEnable(s2_paddr, s2_fire)
380  val s3_hit = RegEnable(s2_hit, s2_fire)
381  val s3_tag_match_way = RegEnable(s2_tag_match_way, s2_fire)
382
383  val s3_banked_data_resp_word = io.banked_data_resp.raw_data
384  val s3_data_error = io.read_error_delayed && s3_hit // banked_data_resp_word.error && !bank_conflict
385  val s3_tag_error = RegEnable(s2_tag_error, s2_fire)
386  val s3_flag_error = RegEnable(s2_flag_error, s2_fire)
387  val s3_error = s3_tag_error || s3_flag_error || s3_data_error
388
389  // error_delayed signal will be used to update uop.exception 1 cycle after load writeback
390  resp.bits.error_delayed := s3_error && (s3_hit || s3_tag_error) && s3_valid
391  resp.bits.data_delayed := s3_banked_data_resp_word
392  resp.bits.replacementUpdated := io.replace_access.valid
393
394  // report tag / data / l2 error (with paddr) to bus error unit
395  io.error := 0.U.asTypeOf(new L1CacheErrorInfo())
396  io.error.report_to_beu := (s3_tag_error || s3_data_error) && s3_valid
397  io.error.paddr := s3_paddr
398  io.error.source.tag := s3_tag_error
399  io.error.source.data := s3_data_error
400  io.error.source.l2 := s3_flag_error
401  io.error.opType.load := true.B
402  // report tag error / l2 corrupted to CACHE_ERROR csr
403  io.error.valid := s3_error && s3_valid
404
405  // update plru in s3
406  val s3_miss_merged = RegNext(s2_miss_merged)
407  val first_update = RegNext(RegNext(RegNext(!io.lsu.replacementUpdated)))
408  val hit_update_replace_en  = RegNext(s2_valid) && RegNext(!resp.bits.miss)
409  val miss_update_replace_en = RegNext(io.miss_req.fire) && RegNext(!io.miss_req.bits.cancel) && RegNext(io.miss_resp.handled)
410
411  if (!cfg.updateReplaceOn2ndmiss) {
412    // replacement is only updated on 1st miss
413    // io.replace_access.valid := RegNext(RegNext(
414    //   RegNext(io.meta_read.fire()) && s1_valid && !io.lsu.s1_kill) &&
415    //   !s2_nack_no_mshr &&
416    //   !s2_miss_merged
417    // )
418    io.replace_access.valid := (hit_update_replace_en || (miss_update_replace_en && !s3_miss_merged)) && first_update
419    io.replace_access.bits.set := RegNext(RegNext(get_idx(s1_req.vaddr)))
420    io.replace_access.bits.way := RegNext(RegNext(Mux(s1_tag_match_dup_dc, OHToUInt(s1_tag_match_way_dup_dc), s1_repl_way_en_enc)))
421  } else {
422    // replacement is updated on both 1st and 2nd miss
423    // timing is worse than !cfg.updateReplaceOn2ndmiss
424    // io.replace_access.valid := RegNext(RegNext(
425    //   RegNext(io.meta_read.fire()) && s1_valid && !io.lsu.s1_kill) &&
426    //   !s2_nack_no_mshr &&
427    //   // replacement is updated on 2nd miss only when this req is firstly issued
428    //   (!s2_miss_merged || s2_req.isFirstIssue)
429    // )
430    io.replace_access.valid := (hit_update_replace_en || miss_update_replace_en) && first_update
431    io.replace_access.bits.set := RegNext(RegNext(get_idx(s1_req.vaddr)))
432    io.replace_access.bits.way := RegNext(
433      Mux(
434        RegNext(s1_tag_match_dup_dc),
435        RegNext(OHToUInt(s1_tag_match_way_dup_dc)), // if hit, access hit way in plru
436        Mux( // if miss
437          !s2_miss_merged,
438          RegNext(s1_repl_way_en_enc), // 1st fire: access new selected replace way
439          OHToUInt(io.miss_resp.repl_way_en) // 2nd fire: access replace way selected at miss queue allocate time
440        )
441      )
442    )
443  }
444
445  // update access bit
446  io.access_flag_write.valid := s3_valid && s3_hit
447  io.access_flag_write.bits.idx := get_idx(s3_vaddr)
448  io.access_flag_write.bits.way_en := s3_tag_match_way
449  io.access_flag_write.bits.flag := true.B
450
451  // --------------------------------------------------------------------------------
452  // Debug logging functions
453  def dump_pipeline_reqs(pipeline_stage_name: String, valid: Bool,
454    req: DCacheWordReq ) = {
455      when (valid) {
456        XSDebug(s"$pipeline_stage_name: ")
457        req.dump()
458      }
459  }
460
461  def dump_pipeline_valids(pipeline_stage_name: String, signal_name: String, valid: Bool) = {
462    when (valid) {
463      XSDebug(s"$pipeline_stage_name $signal_name\n")
464    }
465  }
466
467  // performance counters
468  XSPerfAccumulate("load_req", io.lsu.req.fire())
469  XSPerfAccumulate("load_s1_kill", s1_fire && io.lsu.s1_kill)
470  XSPerfAccumulate("load_hit_way", s1_fire && s1_tag_match_dup_dc)
471  XSPerfAccumulate("load_replay", io.lsu.resp.fire() && resp.bits.replay)
472  XSPerfAccumulate("load_replay_for_data_nack", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data)
473  XSPerfAccumulate("load_replay_for_no_mshr", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr)
474  XSPerfAccumulate("load_replay_for_conflict", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow)
475  XSPerfAccumulate("load_hit", io.lsu.resp.fire() && !real_miss)
476  XSPerfAccumulate("load_miss", io.lsu.resp.fire() && real_miss)
477  XSPerfAccumulate("load_succeed", io.lsu.resp.fire() && !resp.bits.miss && !resp.bits.replay)
478  XSPerfAccumulate("load_miss_or_conflict", io.lsu.resp.fire() && resp.bits.miss)
479  XSPerfAccumulate("actual_ld_fast_wakeup", s1_fire && s1_tag_match_dup_dc && !io.disable_ld_fast_wakeup)
480  XSPerfAccumulate("ideal_ld_fast_wakeup", io.banked_data_read.fire() && s1_tag_match_dup_dc)
481
482  val perfEvents = Seq(
483    ("load_req                 ", io.lsu.req.fire()                                               ),
484    ("load_replay              ", io.lsu.resp.fire() && resp.bits.replay                          ),
485    ("load_replay_for_data_nack", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data          ),
486    ("load_replay_for_no_mshr  ", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr       ),
487    ("load_replay_for_conflict ", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow ),
488  )
489  generatePerfEvent()
490}
491