xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/mainpipe/MissQueue.scala (revision 066ac8a465b27b54ba22458ff1a67bcd28215d73)
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 utils._
23import freechips.rocketchip.tilelink._
24import freechips.rocketchip.tilelink.ClientStates._
25import freechips.rocketchip.tilelink.MemoryOpCategories._
26import freechips.rocketchip.tilelink.TLPermissions._
27import difftest._
28import huancun.{AliasKey, DirtyKey, PreferCacheKey, PrefetchKey}
29
30class MissReq(implicit p: Parameters) extends DCacheBundle {
31  val source = UInt(sourceTypeWidth.W)
32  val cmd = UInt(M_SZ.W)
33  val addr = UInt(PAddrBits.W)
34  val vaddr = UInt(VAddrBits.W)
35  val way_en = UInt(DCacheWays.W)
36
37  // store
38  val store_data = UInt((cfg.blockBytes * 8).W)
39  val store_mask = UInt(cfg.blockBytes.W)
40
41  // which word does amo work on?
42  val word_idx = UInt(log2Up(blockWords).W)
43  val amo_data = UInt(DataBits.W)
44  val amo_mask = UInt((DataBits / 8).W)
45
46  val req_coh = new ClientMetadata
47  val replace_coh = new ClientMetadata
48  val replace_tag = UInt(tagBits.W)
49  val id = UInt(reqIdWidth.W)
50
51  def isLoad = source === LOAD_SOURCE.U
52  def isStore = source === STORE_SOURCE.U
53  def isAMO = source === AMO_SOURCE.U
54  def hit = req_coh.isValid()
55}
56
57class MissEntry(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule {
58  val io = IO(new Bundle() {
59    // MSHR ID
60    val id = Input(UInt(log2Up(cfg.nMissEntries).W))
61    // client requests
62    // this entry is free and can be allocated to new reqs
63    val primary_ready = Output(Bool())
64    // this entry is busy, but it can merge the new req
65    val secondary_ready = Output(Bool())
66    // this entry is busy and it can not merge the new req
67    val secondary_reject = Output(Bool())
68    val req    = Flipped(ValidIO(new MissReq))
69    val refill_to_ldq = ValidIO(new Refill)
70    // TODO: bypass refill data to load pipe
71
72    // bus
73    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
74    val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
75    val mem_finish = DecoupledIO(new TLBundleE(edge.bundle))
76
77    // refill pipe
78    val refill_pipe_req = DecoupledIO(new RefillPipeReq)
79
80    // replace pipe
81    val replace_pipe_req = DecoupledIO(new ReplacePipeReq)
82    val replace_pipe_resp = Input(Bool())
83
84    // main pipe: amo miss
85    val main_pipe_req = DecoupledIO(new MainPipeReq)
86    val main_pipe_resp = Input(Bool())
87
88    val block_addr = ValidIO(UInt(PAddrBits.W))
89
90    val debug_early_replace = ValidIO(new Bundle() {
91      // info about the block that has been replaced
92      val idx = UInt(idxBits.W) // vaddr
93      val tag = UInt(tagBits.W) // paddr
94    })
95  })
96
97  val req = Reg(new MissReq)
98  val req_valid = RegInit(false.B)
99  val set = addr_to_dcache_set(req.vaddr)
100
101  val s_acquire = RegInit(true.B)
102  val s_grantack = RegInit(true.B)
103  val s_replace_req = RegInit(true.B)
104  val s_refill = RegInit(true.B)
105  val s_mainpipe_req = RegInit(true.B)
106
107  val w_grantfirst = RegInit(true.B)
108  val w_grantlast = RegInit(true.B)
109  val w_replace_resp = RegInit(true.B)
110  val w_mainpipe_resp = RegInit(true.B)
111
112  val release_entry = s_grantack && s_refill && w_mainpipe_resp
113
114  val acquire_not_sent = !s_acquire && !io.mem_acquire.ready
115  val data_not_refilled = !w_grantlast
116
117  val should_refill_data_reg =  Reg(Bool())
118  val should_refill_data = WireInit(should_refill_data_reg)
119
120  val full_overwrite = req.isStore && req.store_mask.andR
121
122  val (_, _, refill_done, refill_count) = edge.count(io.mem_grant)
123  val grant_param = Reg(UInt(TLPermissions.bdWidth.W))
124
125  val grant_beats = RegInit(0.U(beatBits.W))
126
127  when (io.req.valid && io.primary_ready) {
128    req_valid := true.B
129    req := io.req.bits
130    req.addr := get_block_addr(io.req.bits.addr)
131
132    s_acquire := false.B
133    s_grantack := false.B
134
135    w_grantfirst := false.B
136    w_grantlast := false.B
137
138    when (!io.req.bits.isAMO) {
139      s_refill := false.B
140    }
141
142    when (!io.req.bits.hit && io.req.bits.replace_coh.isValid() && !io.req.bits.isAMO) {
143      s_replace_req := false.B
144      w_replace_resp := false.B
145    }
146
147    when (io.req.bits.isAMO) {
148      s_mainpipe_req := false.B
149      w_mainpipe_resp := false.B
150    }
151
152    should_refill_data_reg := io.req.bits.isLoad
153    grant_beats := 0.U
154  }.elsewhen (release_entry) {
155    req_valid := false.B
156  }
157
158  when (io.req.valid && io.secondary_ready) {
159    assert(io.req.bits.req_coh.state <= req.req_coh.state)
160    assert(!(io.req.bits.isAMO || req.isAMO))
161    // use the most uptodate meta
162    req.req_coh := io.req.bits.req_coh
163
164    when (io.req.bits.isStore) {
165      req := io.req.bits
166      req.addr := get_block_addr(io.req.bits.addr)
167      req.way_en := req.way_en
168      req.replace_coh := req.replace_coh
169      req.replace_tag := req.replace_tag
170    }
171
172    should_refill_data := should_refill_data_reg || io.req.bits.isLoad
173    should_refill_data_reg := should_refill_data
174  }
175
176  when (io.mem_acquire.fire()) {
177    s_acquire := true.B
178  }
179
180  val refill_data = Reg(Vec(blockRows, UInt(rowBits.W)))
181  val refill_data_raw = Reg(Vec(blockBytes/beatBytes, UInt(beatBits.W)))
182  val new_data = Wire(Vec(blockRows, UInt(rowBits.W)))
183  val new_mask = Wire(Vec(blockRows, UInt(rowBytes.W)))
184  def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = {
185    val full_wmask = FillInterleaved(8, wmask)
186    (~full_wmask & old_data | full_wmask & new_data)
187  }
188  for (i <- 0 until blockRows) {
189    new_data(i) := req.store_data(rowBits * (i + 1) - 1, rowBits * i)
190    // we only need to merge data for Store
191    new_mask(i) := Mux(req.isStore, req.store_mask(rowBytes * (i + 1) - 1, rowBytes * i), 0.U)
192  }
193  val hasData = RegInit(true.B)
194  val isDirty = RegInit(false.B)
195  when (io.mem_grant.fire()) {
196    w_grantfirst := true.B
197    grant_param := io.mem_grant.bits.param
198    when (edge.hasData(io.mem_grant.bits)) {
199      // GrantData
200      for (i <- 0 until beatRows) {
201        val idx = (refill_count << log2Floor(beatRows)) + i.U
202        val grant_row = io.mem_grant.bits.data(rowBits * (i + 1) - 1, rowBits * i)
203        refill_data(idx) := mergePutData(grant_row, new_data(idx), new_mask(idx))
204      }
205      w_grantlast := w_grantlast || refill_done
206      hasData := true.B
207      grant_beats := grant_beats + 1.U
208    }.otherwise {
209      // Grant
210      assert(full_overwrite)
211      for (i <- 0 until blockRows) {
212        refill_data(i) := new_data(i)
213      }
214      w_grantlast := true.B
215      hasData := false.B
216    }
217
218    refill_data_raw(refill_count) := io.mem_grant.bits.data
219    isDirty := io.mem_grant.bits.echo.lift(DirtyKey).getOrElse(false.B)
220  }
221
222  when (io.mem_finish.fire()) {
223    s_grantack := true.B
224  }
225
226  when (io.replace_pipe_req.fire()) {
227    s_replace_req := true.B
228  }
229
230  when (io.replace_pipe_resp) {
231    w_replace_resp := true.B
232  }
233
234  when (io.refill_pipe_req.fire()) {
235    s_refill := true.B
236  }
237
238  when (io.main_pipe_req.fire()) {
239    s_mainpipe_req := true.B
240  }
241
242  when (io.main_pipe_resp) {
243    w_mainpipe_resp := true.B
244  }
245
246  def before_read_sent_can_merge(new_req: MissReq): Bool = {
247    acquire_not_sent && req.isLoad && (new_req.isLoad || new_req.isStore)
248  }
249
250  def before_data_refill_can_merge(new_req: MissReq): Bool = {
251    data_not_refilled && (req.isLoad || req.isStore) && new_req.isLoad
252  }
253
254  def should_merge(new_req: MissReq): Bool = {
255    val block_match = req.addr === get_block_addr(new_req.addr)
256    val beat_match = new_req.addr(blockOffBits - 1, beatOffBits) >= grant_beats
257    block_match &&
258    (before_read_sent_can_merge(new_req) ||
259      beat_match && before_data_refill_can_merge(new_req))
260  }
261
262  def should_reject(new_req: MissReq): Bool = {
263    val block_match = req.addr === get_block_addr(new_req.addr)
264    val beat_match = new_req.addr(blockOffBits - 1, beatOffBits) >= grant_beats
265    val set_match = set === addr_to_dcache_set(new_req.vaddr)
266
267    req_valid &&
268      Mux(
269        block_match,
270        !before_read_sent_can_merge(new_req) &&
271          !(beat_match && before_data_refill_can_merge(new_req)),
272        set_match && new_req.way_en === req.way_en
273      )
274  }
275
276  io.primary_ready := !req_valid
277  io.secondary_ready := should_merge(io.req.bits)
278  io.secondary_reject := should_reject(io.req.bits)
279
280  // should not allocate, merge or reject at the same time
281  assert(RegNext(PopCount(Seq(io.primary_ready, io.secondary_ready, io.secondary_reject)) <= 1.U))
282
283  val refill_data_splited = WireInit(VecInit(Seq.tabulate(cfg.blockBytes * 8 / l1BusDataWidth)(i => {
284    val data = refill_data.asUInt
285    data((i + 1) * l1BusDataWidth - 1, i * l1BusDataWidth)
286  })))
287  io.refill_to_ldq.valid := RegNext(!w_grantlast && io.mem_grant.fire()) && should_refill_data
288  io.refill_to_ldq.bits.addr := RegNext(req.addr + (refill_count << refillOffBits))
289  io.refill_to_ldq.bits.data := refill_data_splited(RegNext(refill_count))
290  io.refill_to_ldq.bits.refill_done := RegNext(refill_done && io.mem_grant.fire())
291  io.refill_to_ldq.bits.hasdata := hasData
292  io.refill_to_ldq.bits.data_raw := refill_data_raw.asUInt
293
294  io.mem_acquire.valid := !s_acquire
295  val grow_param = req.req_coh.onAccess(req.cmd)._2
296  val acquireBlock = edge.AcquireBlock(
297    fromSource = io.id,
298    toAddress = req.addr,
299    lgSize = (log2Up(cfg.blockBytes)).U,
300    growPermissions = grow_param
301  )._2
302  val acquirePerm = edge.AcquirePerm(
303    fromSource = io.id,
304    toAddress = req.addr,
305    lgSize = (log2Up(cfg.blockBytes)).U,
306    growPermissions = grow_param
307  )._2
308  io.mem_acquire.bits := Mux(full_overwrite, acquirePerm, acquireBlock)
309  // resolve cache alias by L2
310  io.mem_acquire.bits.user.lift(AliasKey).foreach( _ := req.vaddr(13, 12))
311  // trigger prefetch
312  io.mem_acquire.bits.user.lift(PrefetchKey).foreach(_ := true.B)
313  // prefer not to cache data in L2 by default
314  io.mem_acquire.bits.user.lift(PreferCacheKey).foreach(_ := false.B)
315  require(nSets <= 256)
316
317  io.mem_grant.ready := !w_grantlast && s_acquire
318
319  val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire())
320  assert(RegNext(!io.mem_grant.fire() || edge.isRequest(io.mem_grant.bits)))
321  io.mem_finish.valid := !s_grantack && w_grantfirst
322  io.mem_finish.bits := grantack
323
324  io.replace_pipe_req.valid := !s_replace_req
325  val replace = io.replace_pipe_req.bits
326  replace.miss_id := io.id
327  replace.way_en := req.way_en
328  replace.vaddr := req.vaddr
329  replace.tag := req.replace_tag
330
331  io.refill_pipe_req.valid := !s_refill && w_replace_resp && w_grantlast
332  val refill = io.refill_pipe_req.bits
333  refill.source := req.source
334  refill.addr := req.addr
335  refill.way_en := req.way_en
336  refill.wmask := Mux(
337    hasData || req.isLoad,
338    ~0.U(DCacheBanks.W),
339    VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, req.store_mask).orR)).asUInt
340  )
341  refill.data := refill_data.asTypeOf((new RefillPipeReq).data)
342  refill.miss_id := io.id
343  refill.id := req.id
344  def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = {
345    val c = categorize(cmd)
346    MuxLookup(Cat(c, param, dirty), Nothing, Seq(
347      //(effect param) -> (next)
348      Cat(rd, toB, false.B)  -> Branch,
349      Cat(rd, toB, true.B)   -> Branch,
350      Cat(rd, toT, false.B)  -> Trunk,
351      Cat(rd, toT, true.B)   -> Dirty,
352      Cat(wi, toT, false.B)  -> Trunk,
353      Cat(wi, toT, true.B)   -> Dirty,
354      Cat(wr, toT, false.B)  -> Dirty,
355      Cat(wr, toT, true.B)   -> Dirty))
356  }
357  refill.meta.coh := ClientMetadata(missCohGen(req.cmd, grant_param, isDirty))
358  refill.alias := req.vaddr(13, 12) // TODO
359
360  io.main_pipe_req.valid := !s_mainpipe_req && w_grantlast
361  io.main_pipe_req.bits := DontCare
362  io.main_pipe_req.bits.miss := true.B
363  io.main_pipe_req.bits.miss_id := io.id
364  io.main_pipe_req.bits.miss_param := grant_param
365  io.main_pipe_req.bits.miss_dirty := isDirty
366  io.main_pipe_req.bits.probe := false.B
367  io.main_pipe_req.bits.source := req.source
368  io.main_pipe_req.bits.cmd := req.cmd
369  io.main_pipe_req.bits.vaddr := req.vaddr
370  io.main_pipe_req.bits.addr := req.addr
371  io.main_pipe_req.bits.store_data := refill_data.asUInt
372  io.main_pipe_req.bits.store_mask := ~0.U(blockBytes.W)
373  io.main_pipe_req.bits.word_idx := req.word_idx
374  io.main_pipe_req.bits.amo_data := req.amo_data
375  io.main_pipe_req.bits.amo_mask := req.amo_mask
376  io.main_pipe_req.bits.id := req.id
377
378  io.block_addr.valid := req_valid && w_grantlast && !s_refill
379  io.block_addr.bits := req.addr
380
381  io.debug_early_replace.valid := BoolStopWatch(io.replace_pipe_resp, io.refill_pipe_req.fire())
382  io.debug_early_replace.bits.idx := addr_to_dcache_set(req.vaddr)
383  io.debug_early_replace.bits.tag := req.replace_tag
384
385  XSPerfAccumulate("miss_req_primary", io.req.valid && io.primary_ready)
386  XSPerfAccumulate("miss_req_merged", io.req.valid && io.secondary_ready)
387  XSPerfAccumulate("load_miss_penalty_to_use",
388    should_refill_data &&
389      BoolStopWatch(io.req.valid && io.primary_ready, io.refill_to_ldq.valid, true)
390  )
391  XSPerfAccumulate("main_pipe_penalty", BoolStopWatch(io.main_pipe_req.fire(), io.main_pipe_resp))
392  XSPerfAccumulate("penalty_blocked_by_channel_A", io.mem_acquire.valid && !io.mem_acquire.ready)
393  XSPerfAccumulate("penalty_waiting_for_channel_D", s_acquire && !w_grantlast && !io.mem_grant.valid)
394  XSPerfAccumulate("penalty_waiting_for_channel_E", io.mem_finish.valid && !io.mem_finish.ready)
395  XSPerfAccumulate("penalty_from_grant_to_refill", !s_refill && w_grantlast)
396  XSPerfAccumulate("soft_prefetch_number", io.req.valid && io.primary_ready && io.req.bits.source === SOFT_PREFETCH.U)
397
398  val (mshr_penalty_sample, mshr_penalty) = TransactionLatencyCounter(RegNext(io.req.valid && io.primary_ready), release_entry)
399  XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 0, 20, 1, true, true)
400  XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 20, 100, 10, true, false)
401
402  val load_miss_begin = io.req.valid && io.primary_ready && io.req.bits.isLoad
403  val refill_finished = RegNext(!w_grantlast && refill_done) && should_refill_data
404  val (load_miss_penalty_sample, load_miss_penalty) = TransactionLatencyCounter(load_miss_begin, refill_finished) // not real refill finish time
405  XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 0, 20, 1, true, true)
406  XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 20, 100, 10, true, false)
407
408  val (a_to_d_penalty_sample, a_to_d_penalty) = TransactionLatencyCounter(io.mem_acquire.fire(), io.mem_grant.fire() && refill_done)
409  XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 0, 20, 1, true, true)
410  XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 20, 100, 10, true, false)
411}
412
413class MissQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule {
414  val io = IO(new Bundle {
415    val req = Flipped(DecoupledIO(new MissReq))
416    val refill_to_ldq = ValidIO(new Refill)
417
418    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
419    val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
420    val mem_finish = DecoupledIO(new TLBundleE(edge.bundle))
421
422    val refill_pipe_req = DecoupledIO(new RefillPipeReq)
423
424    val replace_pipe_req = DecoupledIO(new ReplacePipeReq)
425    val replace_pipe_resp = Flipped(Vec(numReplaceRespPorts, ValidIO(new ReplacePipeResp)))
426
427    val main_pipe_req = DecoupledIO(new MainPipeReq)
428    val main_pipe_resp = Flipped(ValidIO(new AtomicsResp))
429
430    // block probe
431    val probe_addr = Input(UInt(PAddrBits.W))
432    val probe_block = Output(Bool())
433
434    val full = Output(Bool())
435
436    // only for performance counter
437    // This is valid when an mshr has finished replacing a block (w_replace_resp),
438    // but hasn't received Grant from L2 (!w_grantlast)
439    val debug_early_replace = Vec(cfg.nMissEntries, ValidIO(new Bundle() {
440      // info about the block that has been replaced
441      val idx = UInt(idxBits.W) // vaddr
442      val tag = UInt(tagBits.W) // paddr
443    }))
444  })
445
446  // 128KBL1: FIXME: provide vaddr for l2
447
448  val entries = Seq.fill(cfg.nMissEntries)(Module(new MissEntry(edge)))
449
450  val primary_ready_vec = entries.map(_.io.primary_ready)
451  val secondary_ready_vec = entries.map(_.io.secondary_ready)
452  val secondary_reject_vec = entries.map(_.io.secondary_reject)
453  val probe_block_vec = entries.map { case e => e.io.block_addr.valid && e.io.block_addr.bits === io.probe_addr }
454
455  val merge = Cat(secondary_ready_vec).orR
456  val merge_idx = PriorityEncoder(secondary_ready_vec)
457
458  val reject = Cat(secondary_reject_vec).orR
459
460  val alloc = !reject && !merge && Cat(primary_ready_vec).orR
461  val alloc_idx = PriorityEncoder(primary_ready_vec)
462
463  val accept = alloc || merge
464  val entry_idx = Mux(alloc, alloc_idx, merge_idx)
465
466  assert(RegNext(PopCount(secondary_ready_vec) <= 1.U))
467//  assert(RegNext(PopCount(secondary_reject_vec) <= 1.U))
468  // It is possible that one mshr wants to merge a req, while another mshr wants to reject it.
469  // That is, a coming req has the same paddr as that of mshr_0 (merge),
470  // while it has the same set and the same way as mshr_1 (reject).
471  // In this situation, the coming req should be merged by mshr_0
472//  assert(RegNext(PopCount(Seq(merge, reject)) <= 1.U))
473
474  def rrArbiter[T <: Bundle](
475    in: Seq[DecoupledIO[T]],
476    out: DecoupledIO[T],
477    name: Option[String] = None): Unit = {
478    val arb = Module(new RRArbiter[T](chiselTypeOf(out.bits), in.size))
479    if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") }
480    for ((a, req) <- arb.io.in.zip(in)) {
481      a <> req
482    }
483    out <> arb.io.out
484  }
485
486  io.mem_grant.ready := false.B
487
488  entries.zipWithIndex.foreach {
489    case (e, i) =>
490      e.io.id := i.U
491      e.io.req.valid := entry_idx === i.U && accept && io.req.valid
492      e.io.req.bits := io.req.bits
493
494      e.io.mem_grant.valid := false.B
495      e.io.mem_grant.bits := DontCare
496      when (io.mem_grant.bits.source === i.U) {
497        e.io.mem_grant <> io.mem_grant
498      }
499
500      e.io.replace_pipe_resp := Cat(io.replace_pipe_resp.map { case r => r.valid && r.bits.miss_id === i.U }).orR
501      e.io.main_pipe_resp := io.main_pipe_resp.valid && io.main_pipe_resp.bits.ack_miss_queue && io.main_pipe_resp.bits.miss_id === i.U
502
503      io.debug_early_replace(i) := e.io.debug_early_replace
504  }
505
506  io.req.ready := accept
507  io.refill_to_ldq.valid := Cat(entries.map(_.io.refill_to_ldq.valid)).orR
508  io.refill_to_ldq.bits := ParallelMux(entries.map(_.io.refill_to_ldq.valid) zip entries.map(_.io.refill_to_ldq.bits))
509
510  TLArbiter.lowest(edge, io.mem_acquire, entries.map(_.io.mem_acquire):_*)
511  TLArbiter.lowest(edge, io.mem_finish, entries.map(_.io.mem_finish):_*)
512
513  rrArbiter(entries.map(_.io.refill_pipe_req), io.refill_pipe_req, Some("refill_pipe_req"))
514  rrArbiter(entries.map(_.io.replace_pipe_req), io.replace_pipe_req, Some("replace_pipe_req"))
515  rrArbiter(entries.map(_.io.main_pipe_req), io.main_pipe_req, Some("main_pipe_req"))
516
517  io.probe_block := Cat(probe_block_vec).orR
518
519  io.full := ~Cat(entries.map(_.io.primary_ready)).andR
520
521  if (env.EnableDifftest) {
522    val difftest = Module(new DifftestRefillEvent)
523    difftest.io.clock := clock
524    difftest.io.coreid := hardId.U
525    difftest.io.valid := io.refill_to_ldq.valid && io.refill_to_ldq.bits.hasdata && io.refill_to_ldq.bits.refill_done
526    difftest.io.addr := io.refill_to_ldq.bits.addr
527    difftest.io.data := io.refill_to_ldq.bits.data_raw.asTypeOf(difftest.io.data)
528  }
529
530  XSPerfAccumulate("miss_req", io.req.fire())
531  XSPerfAccumulate("miss_req_allocate", io.req.fire() && alloc)
532  XSPerfAccumulate("miss_req_merge_load", io.req.fire() && merge && io.req.bits.isLoad)
533  XSPerfAccumulate("miss_req_reject_load", io.req.valid && reject && io.req.bits.isLoad)
534  XSPerfAccumulate("probe_blocked_by_miss", io.probe_block)
535  val max_inflight = RegInit(0.U((log2Up(cfg.nMissEntries) + 1).W))
536  val num_valids = PopCount(~Cat(primary_ready_vec).asUInt)
537  when (num_valids > max_inflight) {
538    max_inflight := num_valids
539  }
540  // max inflight (average) = max_inflight_total / cycle cnt
541  XSPerfAccumulate("max_inflight", max_inflight)
542  QueuePerf(cfg.nMissEntries, num_valids, num_valids === cfg.nMissEntries.U)
543  io.full := num_valids === cfg.nMissEntries.U
544  XSPerfHistogram("num_valids", num_valids, true.B, 0, cfg.nMissEntries, 1)
545  val perfinfo = IO(new Bundle(){
546    val perfEvents = Output(new PerfEventsBundle(5))
547  })
548  val perfEvents = Seq(
549    ("dcache_missq_req          ", io.req.fire()                                                                                                                                                                       ),
550    ("dcache_missq_1/4_valid    ", (PopCount(entries.map(entry => (!entry.io.primary_ready))) < (cfg.nMissEntries.U/4.U))                                                                                              ),
551    ("dcache_missq_2/4_valid    ", (PopCount(entries.map(entry => (!entry.io.primary_ready))) > (cfg.nMissEntries.U/4.U)) & (PopCount(entries.map(entry => (!entry.io.primary_ready))) <= (cfg.nMissEntries.U/2.U))    ),
552    ("dcache_missq_3/4_valid    ", (PopCount(entries.map(entry => (!entry.io.primary_ready))) > (cfg.nMissEntries.U/2.U)) & (PopCount(entries.map(entry => (!entry.io.primary_ready))) <= (cfg.nMissEntries.U*3.U/4.U))),
553    ("dcache_missq_4/4_valid    ", (PopCount(entries.map(entry => (!entry.io.primary_ready))) > (cfg.nMissEntries.U*3.U/4.U))                                                                                          ),
554  )
555
556  for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
557    perf_out.incr_step := RegNext(perf)
558  }
559}
560