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