xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/loadpipe/LoadPipe.scala (revision dc4fac130426dbec49b49d778b9105d79b4a8eab)
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 org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.tilelink.ClientMetadata
23import utility.{ParallelPriorityMux, OneHot, ChiselDB, ParallelORR, ParallelMux, XSDebug, XSPerfAccumulate, HasPerfEvents}
24import xiangshan.{XSCoreParamsKey, L1CacheErrorInfo}
25import xiangshan.cache.wpu._
26import xiangshan.mem.HasL1PrefetchSourceParameter
27import xiangshan.mem.prefetch._
28import xiangshan.mem.LqPtr
29
30class LoadPfDbBundle(implicit p: Parameters) extends DCacheBundle {
31  val paddr = UInt(PAddrBits.W)
32}
33
34class LoadPipe(id: Int)(implicit p: Parameters) extends DCacheModule with HasPerfEvents with HasL1PrefetchSourceParameter {
35  val io = IO(new DCacheBundle {
36    // incoming requests
37    val lsu = Flipped(new DCacheLoadIO)
38    val dwpu = Flipped(new DwpuBaseIO(nWays = nWays, nPorts = 1))
39    val load128Req = Input(Bool())
40    // req got nacked in stage 0?
41    val nack      = Input(Bool())
42
43    // meta and data array read port
44    val meta_read = DecoupledIO(new MetaReadReq)
45    val meta_resp = Input(Vec(nWays, new Meta))
46    val extra_meta_resp = Input(Vec(nWays, new DCacheExtraMeta))
47
48    val tag_read = DecoupledIO(new TagReadReq)
49    val tag_resp = Input(Vec(nWays, UInt(encTagBits.W)))
50    val vtag_update = Flipped(DecoupledIO(new TagWriteReq))
51
52    val banked_data_read = DecoupledIO(new L1BankedDataReadReqWithMask)
53    val is128Req = Output(Bool())
54    val banked_data_resp = Input(Vec(VLEN/DCacheSRAMRowBits, new L1BankedDataReadResult()))
55    val read_error_delayed = Input(Vec(VLEN/DCacheSRAMRowBits, Bool()))
56
57    // access bit update
58    val access_flag_write = DecoupledIO(new FlagMetaWriteReq)
59    val prefetch_flag_write = DecoupledIO(new SourceMetaWriteReq)
60
61    // banked data read conflict
62    val bank_conflict_slow = Input(Bool())
63
64    // send miss request to miss queue
65    val miss_req    = DecoupledIO(new MissReq)
66    val miss_resp   = Input(new MissResp)
67
68    // send miss request to wbq
69    val wbq_conflict_check = Valid(UInt())
70    val wbq_block_miss_req = Input(Bool())
71
72    // update state vec in replacement algo
73    val replace_access = ValidIO(new ReplacementAccessBundle)
74    // find the way to be replaced
75    val replace_way = new ReplacementWayReqIO
76
77    // load fast wakeup should be disabled when data read is not ready
78    val disable_ld_fast_wakeup = Input(Bool())
79
80    // ecc error
81    val error = Output(ValidIO(new L1CacheErrorInfo))
82
83    val prefetch_info = new Bundle {
84      val naive = new Bundle {
85        val total_prefetch = Output(Bool())
86        val late_hit_prefetch = Output(Bool())
87        val late_prefetch_hit = Output(Bool())
88        val late_load_hit = Output(Bool())
89        val useless_prefetch = Output(Bool())
90        val useful_prefetch = Output(Bool())
91        val prefetch_hit = Output(Bool())
92      }
93
94      val fdp = new Bundle {
95        val useful_prefetch = Output(Bool())
96        val demand_miss = Output(Bool())
97        val pollution = Output(Bool())
98      }
99    }
100
101    val bloom_filter_query = new Bundle {
102      val query = ValidIO(new BloomQueryBundle(BLOOM_FILTER_ENTRY_NUM))
103      val resp = Flipped(ValidIO(new BloomRespBundle()))
104    }
105
106    val counter_filter_query = new CounterFilterQueryBundle
107    val counter_filter_enq = new ValidIO(new CounterFilterDataBundle())
108
109    // miss queue cancel the miss request
110    val mq_enq_cancel = Input(Bool())
111  })
112
113  assert(RegNext(io.meta_read.ready))
114
115  val s1_ready = Wire(Bool())
116  val s2_ready = Wire(Bool())
117  // LSU requests
118  // it you got nacked, you can directly passdown
119  val not_nacked_ready = io.meta_read.ready && io.tag_read.ready && s1_ready
120  val nacked_ready     = true.B
121
122  // Pipeline
123  // --------------------------------------------------------------------------------
124  // stage 0
125  // --------------------------------------------------------------------------------
126  // read tag
127
128  // ready can wait for valid
129  io.lsu.req.ready := (!io.nack && not_nacked_ready) || (io.nack && nacked_ready)
130  io.meta_read.valid := io.lsu.req.fire && !io.nack
131  io.tag_read.valid := io.lsu.req.fire && !io.nack
132
133  val s0_valid = io.lsu.req.fire
134  val s0_req = io.lsu.req.bits
135  val s0_fire = s0_valid && s1_ready
136  val s0_vaddr = s0_req.vaddr
137  val s0_replayCarry = s0_req.replayCarry
138  val s0_load128Req = io.load128Req
139  val s0_bank_oh_64 = UIntToOH(addr_to_dcache_bank(s0_vaddr))
140  val s0_bank_oh_128 = (s0_bank_oh_64 << 1.U).asUInt | s0_bank_oh_64.asUInt
141  val s0_bank_oh = Mux(s0_load128Req, s0_bank_oh_128, s0_bank_oh_64)
142  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!")
143  dump_pipeline_reqs("LoadPipe s0", s0_valid, s0_req)
144
145  // wpu
146  // val dwpu = Module(new DCacheWpuWrapper)
147  // req in s0
148  if(dwpuParam.enWPU){
149    io.dwpu.req(0).bits.vaddr := s0_vaddr
150    io.dwpu.req(0).bits.replayCarry := s0_replayCarry
151    io.dwpu.req(0).valid := s0_valid
152  }else{
153    io.dwpu.req(0).valid := false.B
154    io.dwpu.req(0).bits := DontCare
155  }
156
157
158  val meta_read = io.meta_read.bits
159  val tag_read = io.tag_read.bits
160
161  // Tag read for new requests
162  meta_read.idx := get_idx(io.lsu.req.bits.vaddr)
163  meta_read.way_en := ~0.U(nWays.W)
164  // meta_read.tag := DontCare
165
166  tag_read.idx := get_idx(io.lsu.req.bits.vaddr)
167  tag_read.way_en := ~0.U(nWays.W)
168
169  // --------------------------------------------------------------------------------
170  // stage 1
171  // --------------------------------------------------------------------------------
172  // tag match, read data
173
174  val s1_valid = RegInit(false.B)
175  val s1_req = RegEnable(s0_req, s0_fire)
176  // in stage 1, load unit gets the physical address
177  val s1_paddr_dup_lsu = io.lsu.s1_paddr_dup_lsu
178  val s1_paddr_dup_dcache = io.lsu.s1_paddr_dup_dcache
179  val s1_load128Req = RegEnable(s0_load128Req, s0_fire)
180  val s1_is_prefetch = s1_req.instrtype === DCACHE_PREFETCH_SOURCE.U
181  // LSU may update the address from io.lsu.s1_paddr, which affects the bank read enable only.
182  val s1_vaddr = Cat(s1_req.vaddr(VAddrBits - 1, blockOffBits), io.lsu.s1_paddr_dup_lsu(blockOffBits - 1, 0))
183  val s1_bank_oh = RegEnable(s0_bank_oh, s0_fire)
184  val s1_nack = RegNext(io.nack)
185  val s1_fire = s1_valid && s2_ready
186  s1_ready := !s1_valid || s1_fire
187
188  when (s0_fire) { s1_valid := true.B }
189  .elsewhen (s1_fire) { s1_valid := false.B }
190
191  dump_pipeline_reqs("LoadPipe s1", s1_valid, s1_req)
192
193  // tag check
194  val meta_resp = io.meta_resp
195  val tag_resp = io.tag_resp.map(r => r(tagBits - 1, 0))
196  def wayMap[T <: Data](f: Int => T) = VecInit((0 until nWays).map(f))
197
198  // resp in s1
199  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
200  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
201  val s1_wpu_pred_valid = RegEnable(io.dwpu.resp(0).valid, s0_fire)
202  val s1_wpu_pred_way_en = RegEnable(io.dwpu.resp(0).bits.s0_pred_way_en, s0_fire)
203
204  // lookup update
205  io.dwpu.lookup_upd(0).valid := s1_valid
206  io.dwpu.lookup_upd(0).bits.vaddr := s1_vaddr
207  io.dwpu.lookup_upd(0).bits.s1_real_way_en := s1_tag_match_way_dup_dc
208  io.dwpu.lookup_upd(0).bits.s1_pred_way_en := s1_wpu_pred_way_en
209  // replace / tag write
210  io.vtag_update.ready := true.B
211  // dwpu.io.tagwrite_upd.valid := io.vtag_update.valid
212  // dwpu.io.tagwrite_upd.bits.vaddr := io.vtag_update.bits.vaddr
213  // dwpu.io.tagwrite_upd.bits.s1_real_way_en := io.vtag_update.bits.way_en
214
215  val s1_direct_map_way_num = get_direct_map_way(s1_req.vaddr)
216  if(dwpuParam.enCfPred || !env.FPGAPlatform){
217    /* method1: record the pc */
218    // if (!env.FPGAPlatform){
219    //    io.dwpu.cfpred(0).s0_vaddr := io.lsu.s0_pc
220    //    io.dwpu.cfpred(0).s1_vaddr := io.lsu.s1_pc
221    // }
222
223    /* method2: record the vaddr */
224    io.dwpu.cfpred(0).s0_vaddr := s0_vaddr
225    io.dwpu.cfpred(0).s1_vaddr := s1_vaddr
226    // whether direct_map_way miss with valid tag value
227    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
228  }else{
229    io.dwpu.cfpred(0) := DontCare
230  }
231
232  val s1_pred_tag_match_way_dup_dc = Wire(UInt(nWays.W))
233  val s1_wpu_pred_fail = Wire(Bool())
234  val s1_wpu_pred_fail_and_real_hit = Wire(Bool())
235  if (dwpuParam.enWPU) {
236    when(s1_wpu_pred_valid) {
237      s1_pred_tag_match_way_dup_dc := s1_wpu_pred_way_en
238    }.otherwise {
239      s1_pred_tag_match_way_dup_dc := s1_tag_match_way_dup_dc
240    }
241    s1_wpu_pred_fail := s1_valid && s1_tag_match_way_dup_dc =/= s1_pred_tag_match_way_dup_dc
242    s1_wpu_pred_fail_and_real_hit := s1_wpu_pred_fail && s1_tag_match_way_dup_dc.orR
243  } else {
244    s1_pred_tag_match_way_dup_dc := s1_tag_match_way_dup_dc
245    s1_wpu_pred_fail := false.B
246    s1_wpu_pred_fail_and_real_hit := false.B
247  }
248
249  val s1_tag_match_dup_dc = ParallelORR(s1_tag_match_way_dup_dc)
250  val s1_tag_match_dup_lsu = ParallelORR(s1_tag_match_way_dup_lsu)
251  assert(RegNext(!s1_valid || PopCount(s1_tag_match_way_dup_dc) <= 1.U), "tag should not match with more than 1 way")
252
253  // when there are no tag match, we give it a Fake Meta
254  // this simplifies our logic in s2 stage
255  val s1_hit_meta = ParallelMux(s1_tag_match_way_dup_dc.asBools, (0 until nWays).map(w => meta_resp(w)))
256  val s1_hit_coh = s1_hit_meta.coh
257  val s1_hit_error = ParallelMux(s1_tag_match_way_dup_dc.asBools, (0 until nWays).map(w => io.extra_meta_resp(w).error))
258  val s1_hit_prefetch = ParallelMux(s1_tag_match_way_dup_dc.asBools, (0 until nWays).map(w => io.extra_meta_resp(w).prefetch))
259  val s1_hit_access = ParallelMux(s1_tag_match_way_dup_dc.asBools, (0 until nWays).map(w => io.extra_meta_resp(w).access))
260
261  // io.replace_way.set.valid := RegNext(s0_fire)
262  io.replace_way.set.valid := false.B
263  io.replace_way.set.bits := get_idx(s1_vaddr)
264  io.replace_way.dmWay := get_direct_map_way(s1_vaddr)
265  val s1_invalid_vec = wayMap(w => !meta_resp(w).coh.isValid())
266  val s1_have_invalid_way = s1_invalid_vec.asUInt.orR
267  val s1_invalid_way_en = ParallelPriorityMux(s1_invalid_vec.zipWithIndex.map(x => x._1 -> UIntToOH(x._2.U(nWays.W))))
268
269  val s1_need_replacement = !s1_tag_match_dup_dc
270
271  XSPerfAccumulate("load_using_replacement", io.replace_way.set.valid && s1_need_replacement)
272
273  // query bloom filter
274  io.bloom_filter_query.query.valid := s1_valid
275  io.bloom_filter_query.query.bits.addr := io.bloom_filter_query.query.bits.get_addr(s1_paddr_dup_dcache)
276
277  // get s1_will_send_miss_req in lpad_s1
278  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
279  val s1_new_hit_coh = s1_hit_coh.onAccess(s1_req.cmd)._3
280  val s1_hit = s1_tag_match_dup_dc // && s1_has_permission && s1_hit_coh === s1_new_hit_coh
281  val s1_will_send_miss_req = s1_valid && !s1_nack && !s1_hit
282
283  // data read
284  io.banked_data_read.valid := s1_fire && !s1_nack && !s1_is_prefetch
285  io.banked_data_read.bits.addr := s1_vaddr
286  io.banked_data_read.bits.kill := io.lsu.s1_kill_data_read
287  io.banked_data_read.bits.way_en := s1_pred_tag_match_way_dup_dc
288  io.banked_data_read.bits.bankMask := s1_bank_oh
289  io.is128Req := s1_load128Req
290
291  // check ecc error
292  val s1_encTag = ParallelMux(s1_tag_match_way_dup_dc.asBools, (0 until nWays).map(w => io.tag_resp(w)))
293  val s1_flag_error = Mux(s1_need_replacement, false.B, s1_hit_error) // error reported by exist dcache error bit
294
295  // --------------------------------------------------------------------------------
296  // stage 2
297  // --------------------------------------------------------------------------------
298  // return data
299
300  // val s2_valid = RegEnable(next = s1_valid && !io.lsu.s1_kill, init = false.B, enable = s1_fire)
301  val s2_valid = RegInit(false.B)
302  val s2_valid_dup = RegInit(false.B)
303  val s2_req = RegEnable(s1_req, s1_fire)
304  val s2_load128Req = RegEnable(s1_load128Req, s1_fire)
305  val s2_paddr = RegEnable(s1_paddr_dup_dcache, s1_fire)
306  val s2_vaddr = RegEnable(s1_vaddr, s1_fire)
307  val s2_bank_oh = RegEnable(s1_bank_oh, s1_fire)
308  val s2_bank_oh_dup_0 = RegEnable(s1_bank_oh, s1_fire)
309  val s2_wpu_pred_fail = RegEnable(s1_wpu_pred_fail, s1_fire)
310  val s2_real_way_en = RegEnable(s1_tag_match_way_dup_dc, s1_fire)
311  val s2_pred_way_en = RegEnable(s1_pred_tag_match_way_dup_dc, s1_fire)
312  val s2_dm_way_num = RegEnable(s1_direct_map_way_num, s1_fire)
313  val s2_wpu_pred_fail_and_real_hit = RegEnable(s1_wpu_pred_fail_and_real_hit, s1_fire)
314
315  s2_ready := true.B
316
317  val s2_fire = s2_valid
318
319  when (s1_fire) {
320    s2_valid := !io.lsu.s1_kill
321    s2_valid_dup := !io.lsu.s1_kill
322  }
323  .elsewhen(io.lsu.resp.fire) {
324    s2_valid := false.B
325    s2_valid_dup := false.B
326  }
327
328  dump_pipeline_reqs("LoadPipe s2", s2_valid, s2_req)
329
330
331  // hit, miss, nack, permission checking
332  // dcache side tag match
333  val s2_tag_match_way = RegEnable(s1_tag_match_way_dup_dc, s1_fire)
334  val s2_tag_match = RegEnable(s1_tag_match_dup_dc, s1_fire)
335
336  val s2_can_send_miss_req = RegEnable(s1_will_send_miss_req, s1_fire)
337  val s2_can_send_miss_req_dup = RegEnable(s1_will_send_miss_req, s1_fire)
338
339  val s2_miss_req_valid     = s2_valid && s2_can_send_miss_req
340  val s2_miss_req_valid_dup = s2_valid_dup && s2_can_send_miss_req_dup
341  val s2_miss_req_fire      = s2_miss_req_valid_dup && io.miss_req.ready
342
343  // lsu side tag match
344  val s2_hit_dup_lsu = RegNext(s1_tag_match_dup_lsu)
345
346  io.lsu.s2_hit := s2_hit_dup_lsu && !s2_wpu_pred_fail
347
348  val s2_hit_meta = RegEnable(s1_hit_meta, s1_fire)
349  val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire)
350  val s2_has_permission = s2_hit_coh.onAccess(s2_req.cmd)._1 // for write prefetch
351  val s2_new_hit_coh = s2_hit_coh.onAccess(s2_req.cmd)._3 // for write prefetch
352
353  val s2_encTag = RegEnable(s1_encTag, s1_fire)
354
355  // when req got nacked, upper levels should replay this request
356  // nacked or not
357  val s2_nack_hit = RegEnable(s1_nack, s1_fire)
358  // can no allocate mshr for load miss
359  val s2_nack_no_mshr = s2_miss_req_valid_dup && !io.miss_req.ready
360  // block with a wbq valid req
361  val s2_nack_wbq_conflict = s2_miss_req_valid_dup && io.wbq_block_miss_req
362  // Bank conflict on data arrays
363  val s2_nack_data = RegEnable(!io.banked_data_read.ready, s1_fire)
364  val s2_nack = s2_nack_hit || s2_nack_no_mshr || s2_nack_data || s2_nack_wbq_conflict
365  // s2 miss merged
366  val s2_miss_merged = s2_miss_req_fire && !io.mq_enq_cancel && !io.wbq_block_miss_req && io.miss_resp.merged
367
368  val s2_bank_addr = addr_to_dcache_bank(s2_paddr)
369  dontTouch(s2_bank_addr)
370
371  val s2_instrtype = s2_req.instrtype
372
373  val s2_tag_error = WireInit(false.B)
374  val s2_flag_error = RegEnable(s1_flag_error, s1_fire)
375
376  val s2_hit_prefetch = RegEnable(s1_hit_prefetch, s1_fire)
377  val s2_hit_access = RegEnable(s1_hit_access, s1_fire)
378
379  val s2_hit = s2_tag_match && s2_has_permission && s2_hit_coh === s2_new_hit_coh && !s2_wpu_pred_fail
380
381  val s2_data128bit = Cat(io.banked_data_resp(1).raw_data, io.banked_data_resp(0).raw_data)
382  val s2_resp_data  = s2_data128bit
383
384  // only dump these signals when they are actually valid
385  dump_pipeline_valids("LoadPipe s2", "s2_hit", s2_valid && s2_hit)
386  dump_pipeline_valids("LoadPipe s2", "s2_nack", s2_valid && s2_nack)
387  dump_pipeline_valids("LoadPipe s2", "s2_nack_hit", s2_valid && s2_nack_hit)
388  dump_pipeline_valids("LoadPipe s2", "s2_nack_no_mshr", s2_valid && s2_nack_no_mshr)
389
390  if(EnableTagEcc) {
391    s2_tag_error := dcacheParameters.tagCode.decode(s2_encTag).error // error reported by tag ecc check
392  }else {
393    s2_tag_error := false.B
394  }
395
396  // send load miss to miss queue
397  io.miss_req.valid := s2_miss_req_valid
398  io.miss_req.bits := DontCare
399  io.miss_req.bits.source := s2_instrtype
400  io.miss_req.bits.pf_source := RegNext(RegNext(io.lsu.pf_source))  // TODO: clock gate
401  io.miss_req.bits.cmd := s2_req.cmd
402  io.miss_req.bits.addr := get_block_addr(s2_paddr)
403  io.miss_req.bits.vaddr := s2_vaddr
404  io.miss_req.bits.req_coh := s2_hit_coh
405  io.miss_req.bits.cancel := io.lsu.s2_kill || s2_tag_error
406  io.miss_req.bits.pc := io.lsu.s2_pc
407  io.miss_req.bits.lqIdx := io.lsu.req.bits.lqIdx
408
409  //send load miss to wbq
410  io.wbq_conflict_check.valid := s2_miss_req_valid_dup
411  io.wbq_conflict_check.bits := get_block_addr(s2_paddr)
412
413  // send back response
414  val resp = Wire(ValidIO(new DCacheWordResp))
415  resp.valid := s2_valid
416  resp.bits := DontCare
417  // resp.bits.data := s2_word_decoded
418  // resp.bits.data := banked_data_resp_word.raw_data
419  // * on miss or nack, upper level should replay request
420  // but if we successfully sent the request to miss queue
421  // upper level does not need to replay request
422  // they can sit in load queue and wait for refill
423  //
424  // * report a miss if bank conflict is detected
425  val real_miss = !s2_real_way_en.orR
426
427  resp.bits.real_miss := real_miss
428  resp.bits.miss := real_miss
429  resp.bits.data := s2_resp_data
430  io.lsu.s2_first_hit := s2_req.isFirstIssue && s2_hit
431  // load pipe need replay when there is a bank conflict or wpu predict fail
432  resp.bits.replay := (resp.bits.miss && (s2_nack || io.mq_enq_cancel)) || io.bank_conflict_slow || s2_wpu_pred_fail
433  resp.bits.replayCarry.valid := (resp.bits.miss && (s2_nack || io.mq_enq_cancel)) || io.bank_conflict_slow || s2_wpu_pred_fail
434  resp.bits.replayCarry.real_way_en := s2_real_way_en
435  resp.bits.meta_prefetch := s2_hit_prefetch
436  resp.bits.meta_access := s2_hit_access
437  resp.bits.tag_error := false.B
438  resp.bits.mshr_id := io.miss_resp.id
439  resp.bits.handled := s2_miss_req_fire && !io.mq_enq_cancel && !io.wbq_block_miss_req && io.miss_resp.handled
440  resp.bits.debug_robIdx := s2_req.debug_robIdx
441  // debug info
442  io.lsu.s2_first_hit := s2_req.isFirstIssue && s2_hit
443  io.lsu.debug_s2_real_way_num := OneHot.OHToUIntStartOne(s2_real_way_en)
444  if(dwpuParam.enWPU) {
445    io.lsu.debug_s2_pred_way_num := OneHot.OHToUIntStartOne(s2_pred_way_en)
446  }else{
447    io.lsu.debug_s2_pred_way_num := 0.U
448  }
449  if(dwpuParam.enWPU && dwpuParam.enCfPred || !env.FPGAPlatform){
450    io.lsu.debug_s2_dm_way_num :=  s2_dm_way_num + 1.U
451  }else{
452    io.lsu.debug_s2_dm_way_num := 0.U
453  }
454
455
456  XSPerfAccumulate("dcache_read_bank_conflict", io.bank_conflict_slow && s2_valid)
457  XSPerfAccumulate("dcache_read_from_prefetched_line", s2_valid && isPrefetchRelated(s2_hit_prefetch) && !resp.bits.miss)
458  XSPerfAccumulate("dcache_first_read_from_prefetched_line", s2_valid && isPrefetchRelated(s2_hit_prefetch) && !resp.bits.miss && !s2_hit_access)
459
460  // if ldu0 and ldu1 hit the same, count for 1
461  val total_prefetch = s2_valid && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U)
462  val late_hit_prefetch = s2_valid && s2_hit && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U)
463  val late_load_hit = s2_valid && s2_hit && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U) && !isFromL1Prefetch(s2_hit_prefetch)
464  val late_prefetch_hit = s2_valid && s2_hit && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U) && isFromL1Prefetch(s2_hit_prefetch)
465  val useless_prefetch = s2_miss_req_fire && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U)
466  val useful_prefetch = s2_valid && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U) && resp.bits.handled && !io.miss_resp.merged
467
468  val prefetch_hit = s2_valid && (s2_req.instrtype =/= DCACHE_PREFETCH_SOURCE.U) && s2_hit && isFromL1Prefetch(s2_hit_prefetch) && s2_req.isFirstIssue
469
470  io.prefetch_info.naive.total_prefetch := total_prefetch
471  io.prefetch_info.naive.late_hit_prefetch := late_hit_prefetch
472  io.prefetch_info.naive.late_load_hit := late_load_hit
473  io.prefetch_info.naive.late_prefetch_hit := late_prefetch_hit
474  io.prefetch_info.naive.useless_prefetch := useless_prefetch
475  io.prefetch_info.naive.useful_prefetch := useful_prefetch
476  io.prefetch_info.naive.prefetch_hit := prefetch_hit
477
478  io.prefetch_info.fdp.demand_miss := s2_valid && (s2_req.instrtype =/= DCACHE_PREFETCH_SOURCE.U) && !s2_hit && s2_req.isFirstIssue
479  io.prefetch_info.fdp.pollution := io.prefetch_info.fdp.demand_miss && io.bloom_filter_query.resp.valid && io.bloom_filter_query.resp.bits.res
480
481  io.lsu.resp.valid := resp.valid
482  io.lsu.resp.bits := resp.bits
483  assert(RegNext(!(resp.valid && !io.lsu.resp.ready)), "lsu should be ready in s2")
484
485  when (resp.valid) {
486    resp.bits.dump()
487  }
488
489  io.lsu.debug_s1_hit_way := s1_tag_match_way_dup_dc
490  io.lsu.s1_disable_fast_wakeup := io.disable_ld_fast_wakeup
491  io.lsu.s2_bank_conflict := io.bank_conflict_slow
492  io.lsu.s2_wpu_pred_fail := s2_wpu_pred_fail_and_real_hit
493  io.lsu.s2_mq_nack       := (resp.bits.miss && (s2_nack_no_mshr || io.mq_enq_cancel || io.wbq_block_miss_req))
494  assert(RegNext(s1_ready && s2_ready), "load pipeline should never be blocked")
495
496  // --------------------------------------------------------------------------------
497  // stage 3
498  // --------------------------------------------------------------------------------
499  // report ecc error and get selected dcache data
500
501  val s3_valid = RegNext(s2_valid)
502  val s3_load128Req = RegEnable(s2_load128Req, s2_fire)
503  val s3_vaddr = RegEnable(s2_vaddr, s2_fire)
504  val s3_paddr = RegEnable(s2_paddr, s2_fire)
505  val s3_hit = RegEnable(s2_hit, s2_fire)
506  val s3_tag_match_way = RegEnable(s2_tag_match_way, s2_fire)
507  val s3_req_instrtype = RegEnable(s2_req.instrtype, s2_fire)
508  val s3_is_prefetch = s3_req_instrtype === DCACHE_PREFETCH_SOURCE.U
509
510  val s3_banked_data_resp_word = RegEnable(s2_resp_data, s2_fire)
511  val s3_data_error = Mux(s3_load128Req, io.read_error_delayed.asUInt.orR, io.read_error_delayed(0)) && s3_hit
512  val s3_tag_error = RegEnable(s2_tag_error, s2_fire)
513  val s3_flag_error = RegEnable(s2_flag_error, s2_fire)
514  val s3_hit_prefetch = RegEnable(s2_hit_prefetch, s2_fire)
515  val s3_error = s3_tag_error || s3_flag_error || s3_data_error
516
517  // error_delayed signal will be used to update uop.exception 1 cycle after load writeback
518  resp.bits.error_delayed := s3_error && (s3_hit || s3_tag_error) && s3_valid
519  resp.bits.data_delayed := s3_banked_data_resp_word
520  resp.bits.replacementUpdated := io.replace_access.valid
521
522  // report tag / data / l2 error (with paddr) to bus error unit
523  io.error := 0.U.asTypeOf(ValidIO(new L1CacheErrorInfo))
524  io.error.bits.report_to_beu := (s3_tag_error || s3_data_error) && s3_valid
525  io.error.bits.paddr := s3_paddr
526  io.error.bits.source.tag := s3_tag_error
527  io.error.bits.source.data := s3_data_error
528  io.error.bits.source.l2 := s3_flag_error
529  io.error.bits.opType.load := true.B
530  // report tag error / l2 corrupted to CACHE_ERROR csr
531  io.error.valid := s3_error && s3_valid
532
533  io.replace_access.valid := s3_valid && s3_hit
534  io.replace_access.bits.set := RegNext(RegNext(get_idx(s1_req.vaddr)))
535  io.replace_access.bits.way := RegNext(RegNext(OHToUInt(s1_tag_match_way_dup_dc)))
536
537  // update access bit
538  io.access_flag_write.valid := s3_valid && s3_hit && !s3_is_prefetch
539  io.access_flag_write.bits.idx := get_idx(s3_vaddr)
540  io.access_flag_write.bits.way_en := s3_tag_match_way
541  io.access_flag_write.bits.flag := true.B
542
543  // clear prefetch source when prefetch hit
544  val s3_clear_pf_flag_en = s3_valid && s3_hit && !s3_is_prefetch && isFromL1Prefetch(s3_hit_prefetch)
545  io.prefetch_flag_write.valid := s3_clear_pf_flag_en && !io.counter_filter_query.resp
546  io.prefetch_flag_write.bits.idx := get_idx(s3_vaddr)
547  io.prefetch_flag_write.bits.way_en := s3_tag_match_way
548  io.prefetch_flag_write.bits.source := L1_HW_PREFETCH_CLEAR
549
550  io.counter_filter_query.req.valid := s3_clear_pf_flag_en
551  io.counter_filter_query.req.bits.idx := get_idx(s3_vaddr)
552  io.counter_filter_query.req.bits.way := OHToUInt(s3_tag_match_way)
553
554  io.counter_filter_enq.valid := io.prefetch_flag_write.valid
555  io.counter_filter_enq.bits.idx := get_idx(s3_vaddr)
556  io.counter_filter_enq.bits.way := OHToUInt(s3_tag_match_way)
557
558  io.prefetch_info.fdp.useful_prefetch := s3_clear_pf_flag_en && !io.counter_filter_query.resp
559
560  XSPerfAccumulate("s3_pf_hit", s3_clear_pf_flag_en)
561  XSPerfAccumulate("s3_pf_hit_filter", s3_clear_pf_flag_en && !io.counter_filter_query.resp)
562
563  // --------------------------------------------------------------------------------
564  // Debug logging functions
565  def dump_pipeline_reqs(pipeline_stage_name: String, valid: Bool,
566    req: DCacheWordReq ) = {
567      when (valid) {
568        XSDebug(s"$pipeline_stage_name: ")
569        req.dump()
570      }
571  }
572
573  def dump_pipeline_valids(pipeline_stage_name: String, signal_name: String, valid: Bool) = {
574    when (valid) {
575      XSDebug(s"$pipeline_stage_name $signal_name\n")
576    }
577  }
578
579  val load_trace = Wire(new LoadPfDbBundle)
580  val pf_trace = Wire(new LoadPfDbBundle)
581  val miss_trace = Wire(new LoadPfDbBundle)
582  val mshr_trace = Wire(new LoadPfDbBundle)
583
584  load_trace.paddr := get_block_addr(s2_paddr)
585  pf_trace.paddr := get_block_addr(s2_paddr)
586  miss_trace.paddr := get_block_addr(s2_paddr)
587  mshr_trace.paddr := get_block_addr(s2_paddr)
588
589  val table_load = ChiselDB.createTable("LoadTrace" + id.toString + "_hart"+ p(XSCoreParamsKey).HartId.toString, new LoadPfDbBundle, basicDB = false)
590  val site_load = "LoadPipe_load" + id.toString
591  table_load.log(load_trace, s2_valid && s2_req.isFirstIssue && (s2_req.instrtype =/= DCACHE_PREFETCH_SOURCE.U), site_load, clock, reset)
592
593  val table_pf = ChiselDB.createTable("LoadPfTrace" + id.toString + "_hart"+ p(XSCoreParamsKey).HartId.toString, new LoadPfDbBundle, basicDB = false)
594  val site_pf = "LoadPipe_pf" + id.toString
595  table_pf.log(pf_trace, s2_valid && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U), site_pf, clock, reset)
596
597  val table_miss = ChiselDB.createTable("LoadTraceMiss" + id.toString + "_hart"+ p(XSCoreParamsKey).HartId.toString, new LoadPfDbBundle, basicDB = false)
598  val site_load_miss = "LoadPipe_load_miss" + id.toString
599  table_miss.log(miss_trace, s2_valid && s2_req.isFirstIssue && (s2_req.instrtype =/= DCACHE_PREFETCH_SOURCE.U) && real_miss, site_load_miss, clock, reset)
600
601  val table_mshr = ChiselDB.createTable("LoadPfMshr" + id.toString + "_hart"+ p(XSCoreParamsKey).HartId.toString, new LoadPfDbBundle, basicDB = false)
602  val site_mshr = "LoadPipe_mshr" + id.toString
603  table_mshr.log(mshr_trace, s2_valid && (s2_req.instrtype === DCACHE_PREFETCH_SOURCE.U) && io.miss_req.fire, site_mshr, clock, reset)
604
605  // performance counters
606  XSPerfAccumulate("load_req", io.lsu.req.fire)
607  XSPerfAccumulate("load_s1_kill", s1_fire && io.lsu.s1_kill)
608  XSPerfAccumulate("load_hit_way", s1_fire && s1_tag_match_dup_dc)
609  XSPerfAccumulate("load_replay", io.lsu.resp.fire && resp.bits.replay)
610  XSPerfAccumulate("load_replay_for_dcache_data_nack", io.lsu.resp.fire && resp.bits.replay && s2_nack_data)
611  XSPerfAccumulate("load_replay_for_dcache_no_mshr", io.lsu.resp.fire && resp.bits.replay && s2_nack_no_mshr)
612  XSPerfAccumulate("load_replay_for_dcache_conflict", io.lsu.resp.fire && resp.bits.replay && io.bank_conflict_slow)
613  XSPerfAccumulate("load_replay_for_dcache_wpu_pred_fail", io.lsu.resp.fire && resp.bits.replay && s2_wpu_pred_fail)
614  XSPerfAccumulate("load_hit", io.lsu.resp.fire && !real_miss)
615  XSPerfAccumulate("load_miss", io.lsu.resp.fire && real_miss)
616  XSPerfAccumulate("load_succeed", io.lsu.resp.fire && !resp.bits.miss && !resp.bits.replay)
617  XSPerfAccumulate("load_miss_or_conflict", io.lsu.resp.fire && resp.bits.miss)
618  XSPerfAccumulate("actual_ld_fast_wakeup", s1_fire && s1_tag_match_dup_dc && !io.disable_ld_fast_wakeup)
619  XSPerfAccumulate("ideal_ld_fast_wakeup", io.banked_data_read.fire && s1_tag_match_dup_dc)
620
621  val perfEvents = Seq(
622    ("load_req                 ", io.lsu.req.fire                                               ),
623    ("load_replay              ", io.lsu.resp.fire && resp.bits.replay                          ),
624    ("load_replay_for_data_nack", io.lsu.resp.fire && resp.bits.replay && s2_nack_data          ),
625    ("load_replay_for_no_mshr  ", io.lsu.resp.fire && resp.bits.replay && s2_nack_no_mshr       ),
626    ("load_replay_for_conflict ", io.lsu.resp.fire && resp.bits.replay && io.bank_conflict_slow ),
627  )
628  generatePerfEvent()
629}
630