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