xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/Uncache.scala (revision 42b75a597e916f6a6887cb8bc626483d0d2645dd)
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 utils._
23import utility._
24import freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp, TransferSizes}
25import freechips.rocketchip.tilelink.{TLArbiter, TLBundleA, TLBundleD, TLClientNode, TLEdgeOut, TLMasterParameters, TLMasterPortParameters}
26import xiangshan._
27import xiangshan.mem._
28import xiangshan.mem.Bundles._
29import coupledL2.{MemBackTypeMM, MemBackTypeMMField, MemPageTypeNC, MemPageTypeNCField}
30
31trait HasUncacheBufferParameters extends HasXSParameter with HasDCacheParameters {
32
33  def doMerge(oldData: UInt, oldMask: UInt, newData:UInt, newMask: UInt):(UInt, UInt) = {
34    val resData = VecInit((0 until DataBytes).map(j =>
35      Mux(newMask(j), newData(8*(j+1)-1, 8*j), oldData(8*(j+1)-1, 8*j))
36    )).asUInt
37    val resMask = newMask | oldMask
38    (resData, resMask)
39  }
40
41  def INDEX_WIDTH = log2Up(UncacheBufferSize)
42  def BLOCK_OFFSET = log2Up(XLEN / 8)
43  def getBlockAddr(x: UInt) = x >> BLOCK_OFFSET
44}
45
46abstract class UncacheBundle(implicit p: Parameters) extends XSBundle with HasUncacheBufferParameters
47
48abstract class UncacheModule(implicit p: Parameters) extends XSModule with HasUncacheBufferParameters
49
50
51class UncacheFlushBundle extends Bundle {
52  val valid = Output(Bool())
53  val empty = Input(Bool())
54}
55
56class UncacheEntry(implicit p: Parameters) extends UncacheBundle {
57  val cmd = UInt(M_SZ.W)
58  val addr = UInt(PAddrBits.W)
59  val vaddr = UInt(VAddrBits.W)
60  val data = UInt(XLEN.W)
61  val mask = UInt(DataBytes.W)
62  val nc = Bool()
63  val atomic = Bool()
64  val memBackTypeMM = Bool()
65
66  val resp_nderr = Bool()
67
68  /* NOTE: if it support the internal forward logic, here can uncomment */
69  // val fwd_data = UInt(XLEN.W)
70  // val fwd_mask = UInt(DataBytes.W)
71
72  def set(x: UncacheWordReq): Unit = {
73    cmd := x.cmd
74    addr := x.addr
75    vaddr := x.vaddr
76    data := x.data
77    mask := x.mask
78    nc := x.nc
79    memBackTypeMM := x.memBackTypeMM
80    atomic := x.atomic
81    resp_nderr := false.B
82    // fwd_data := 0.U
83    // fwd_mask := 0.U
84  }
85
86  def update(x: UncacheWordReq): Unit = {
87    val (resData, resMask) = doMerge(data, mask, x.data, x.mask)
88    // mask -> get the first position as 1 -> for address align
89    val (resOffset, resFlag) = PriorityEncoderWithFlag(resMask)
90    data := resData
91    mask := resMask
92    when(resFlag){
93      addr := (getBlockAddr(addr) << BLOCK_OFFSET) | resOffset
94      vaddr := (getBlockAddr(vaddr) << BLOCK_OFFSET) | resOffset
95    }
96  }
97
98  def update(x: TLBundleD): Unit = {
99    when(cmd === MemoryOpConstants.M_XRD) {
100      data := x.data
101    }
102    resp_nderr := x.denied || x.corrupt
103  }
104
105  // def update(forwardData: UInt, forwardMask: UInt): Unit = {
106  //   fwd_data := forwardData
107  //   fwd_mask := forwardMask
108  // }
109
110  def toUncacheWordResp(eid: UInt): UncacheWordResp = {
111    // val resp_fwd_data = VecInit((0 until DataBytes).map(j =>
112    //   Mux(fwd_mask(j), fwd_data(8*(j+1)-1, 8*j), data(8*(j+1)-1, 8*j))
113    // )).asUInt
114    val resp_fwd_data = data
115    val r = Wire(new UncacheWordResp)
116    r := DontCare
117    r.data := resp_fwd_data
118    r.id := eid
119    r.nderr := resp_nderr
120    r.nc := nc
121    r.is2lq := cmd === MemoryOpConstants.M_XRD
122    r.miss := false.B
123    r.replay := false.B
124    r.tag_error := false.B
125    r.error := false.B
126    r
127  }
128}
129
130class UncacheEntryState(implicit p: Parameters) extends DCacheBundle {
131  // valid (-> waitSame) -> inflight -> waitReturn
132  val valid = Bool()
133  val inflight = Bool() // uncache -> L2
134  val waitSame = Bool()
135  val waitReturn = Bool() // uncache -> LSQ
136
137  def init: Unit = {
138    valid := false.B
139    inflight := false.B
140    waitSame := false.B
141    waitReturn := false.B
142  }
143
144  def isValid(): Bool = valid
145  def isInflight(): Bool = valid && inflight
146  def isWaitReturn(): Bool = valid && waitReturn
147  def isWaitSame(): Bool = valid && waitSame
148  def can2Bus(): Bool = valid && !inflight && !waitSame && !waitReturn
149  def can2Lsq(): Bool = valid && waitReturn
150  def canMerge(): Bool = valid && !inflight
151  def isFwdOld(): Bool = valid && (inflight || waitReturn)
152  def isFwdNew(): Bool = valid && !inflight && !waitReturn
153
154  def setValid(x: Bool): Unit = { valid := x}
155  def setInflight(x: Bool): Unit = { inflight := x}
156  def setWaitReturn(x: Bool): Unit = { waitReturn := x }
157  def setWaitSame(x: Bool): Unit = { waitSame := x}
158
159  def updateUncacheResp(): Unit = {
160    assert(inflight, "The request was not sent and a response was received")
161    inflight := false.B
162    waitReturn := true.B
163  }
164  def updateReturn(): Unit = {
165    valid := false.B
166    inflight := false.B
167    waitSame := false.B
168    waitReturn := false.B
169  }
170}
171
172class UncacheIO(implicit p: Parameters) extends DCacheBundle {
173  val hartId = Input(UInt())
174  val enableOutstanding = Input(Bool())
175  val flush = Flipped(new UncacheFlushBundle)
176  val lsq = Flipped(new UncacheWordIO)
177  val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO))
178}
179
180// convert DCacheIO to TileLink
181// for Now, we only deal with TL-UL
182
183class Uncache()(implicit p: Parameters) extends LazyModule with HasXSParameter {
184  override def shouldBeInlined: Boolean = false
185  def idRange: Int = UncacheBufferSize
186
187  val clientParameters = TLMasterPortParameters.v1(
188    clients = Seq(TLMasterParameters.v1(
189      "uncache",
190      sourceId = IdRange(0, idRange)
191    )),
192    requestFields = Seq(MemBackTypeMMField(), MemPageTypeNCField())
193  )
194  val clientNode = TLClientNode(Seq(clientParameters))
195
196  lazy val module = new UncacheImp(this)
197}
198
199/* Uncache Buffer */
200class UncacheImp(outer: Uncache)extends LazyModuleImp(outer)
201  with HasTLDump
202  with HasXSParameter
203  with HasUncacheBufferParameters
204  with HasPerfEvents
205{
206  println(s"Uncahe Buffer Size: $UncacheBufferSize entries")
207  val io = IO(new UncacheIO)
208
209  val (bus, edge) = outer.clientNode.out.head
210
211  val req  = io.lsq.req
212  val resp = io.lsq.resp
213  val mem_acquire = bus.a
214  val mem_grant   = bus.d
215  val req_ready = WireInit(false.B)
216
217  // assign default values to output signals
218  bus.b.ready := false.B
219  bus.c.valid := false.B
220  bus.c.bits  := DontCare
221  bus.d.ready := false.B
222  bus.e.valid := false.B
223  bus.e.bits  := DontCare
224  io.lsq.req.ready := req_ready
225  io.lsq.resp.valid := false.B
226  io.lsq.resp.bits := DontCare
227
228
229  /******************************************************************
230   * Data Structure
231   ******************************************************************/
232
233  val entries = Reg(Vec(UncacheBufferSize, new UncacheEntry))
234  val states = RegInit(VecInit(Seq.fill(UncacheBufferSize)(0.U.asTypeOf(new UncacheEntryState))))
235  val s_idle :: s_inflight :: s_wait_return :: Nil = Enum(3)
236  val uState = RegInit(s_idle)
237
238  // drain buffer
239  val empty = Wire(Bool())
240  val f1_needDrain = Wire(Bool())
241  val do_uarch_drain = RegInit(false.B)
242  when((f1_needDrain || io.flush.valid) && !empty){
243    do_uarch_drain := true.B
244  }.elsewhen(empty){
245    do_uarch_drain := false.B
246  }.otherwise{
247    do_uarch_drain := false.B
248  }
249
250  val q0_entry = Wire(new UncacheEntry)
251  val q0_canSentIdx = Wire(UInt(INDEX_WIDTH.W))
252  val q0_canSent = Wire(Bool())
253
254
255  /******************************************************************
256   * Functions
257   ******************************************************************/
258  def sizeMap[T <: Data](f: Int => T) = VecInit((0 until UncacheBufferSize).map(f))
259  def sizeForeach[T <: Data](f: Int => Unit) = (0 until UncacheBufferSize).map(f)
260  def isStore(e: UncacheEntry): Bool = e.cmd === MemoryOpConstants.M_XWR
261  def isStore(x: UInt): Bool = x === MemoryOpConstants.M_XWR
262  def addrMatch(x: UncacheEntry, y: UncacheWordReq) : Bool = getBlockAddr(x.addr) === getBlockAddr(y.addr)
263  def addrMatch(x: UncacheWordReq, y: UncacheEntry) : Bool = getBlockAddr(x.addr) === getBlockAddr(y.addr)
264  def addrMatch(x: UncacheEntry, y: UncacheEntry) : Bool = getBlockAddr(x.addr) === getBlockAddr(y.addr)
265  def addrMatch(x: UInt, y: UInt) : Bool = getBlockAddr(x) === getBlockAddr(y)
266
267  def continueAndAlign(mask: UInt): Bool = {
268    val res =
269      PopCount(mask) === 1.U ||
270      mask === 0b00000011.U ||
271      mask === 0b00001100.U ||
272      mask === 0b00110000.U ||
273      mask === 0b11000000.U ||
274      mask === 0b00001111.U ||
275      mask === 0b11110000.U ||
276      mask === 0b11111111.U
277    res
278  }
279
280  def canMergePrimary(x: UncacheWordReq, e: UncacheEntry, eid: UInt): Bool = {
281    // vaddr same, properties same
282    getBlockAddr(x.vaddr) === getBlockAddr(e.vaddr) &&
283      x.cmd === e.cmd && x.nc && e.nc &&
284      x.memBackTypeMM === e.memBackTypeMM && !x.atomic && !e.atomic &&
285      continueAndAlign(x.mask | e.mask) &&
286    // not receiving uncache response, not waitReturn -> no wake-up signal in these cases
287      !(mem_grant.fire && mem_grant.bits.source === eid || states(eid).isWaitReturn())
288  }
289
290  def canMergeSecondary(eid: UInt): Bool = {
291    // old entry is not inflight and senting
292    states(eid).canMerge() && !(q0_canSent && q0_canSentIdx === eid)
293  }
294
295  /******************************************************************
296   * uState for non-outstanding
297   ******************************************************************/
298
299  switch(uState){
300    is(s_idle){
301      when(mem_acquire.fire){
302        uState := s_inflight
303      }
304    }
305    is(s_inflight){
306      when(mem_grant.fire){
307        uState := s_wait_return
308      }
309    }
310    is(s_wait_return){
311      when(resp.fire){
312        uState := s_idle
313      }
314    }
315  }
316
317
318  /******************************************************************
319   * Enter Buffer
320   *  Version 0 (better timing)
321   *    e0 judge: alloc/merge write vec
322   *    e1 alloc
323   *
324   *  Version 1 (better performance)
325   *    e0: solved in one cycle for achieving the original performance.
326   *    e1: return idResp to set sid for handshake
327   ******************************************************************/
328
329  /* e0: merge/alloc */
330  val e0_fire = req.fire
331  val e0_req_valid = req.valid
332  val e0_req = req.bits
333
334  val e0_rejectVec = Wire(Vec(UncacheBufferSize, Bool()))
335  val e0_mergeVec = Wire(Vec(UncacheBufferSize, Bool()))
336  val e0_allocWaitSameVec = Wire(Vec(UncacheBufferSize, Bool()))
337  sizeForeach(i => {
338    val valid = e0_req_valid && states(i).isValid()
339    val isAddrMatch = addrMatch(e0_req, entries(i))
340    val canMerge1 = canMergePrimary(e0_req, entries(i), i.U)
341    val canMerge2 = canMergeSecondary(i.U)
342    e0_rejectVec(i) := valid && isAddrMatch && !canMerge1
343    e0_mergeVec(i) := valid && isAddrMatch && canMerge1 && canMerge2
344    e0_allocWaitSameVec(i) := valid && isAddrMatch && canMerge1 && !canMerge2
345  })
346  assert(PopCount(e0_mergeVec) <= 1.U, "Uncache buffer should not merge multiple entries")
347
348  val e0_invalidVec = sizeMap(i => !states(i).isValid())
349  val e0_reject = do_uarch_drain || !e0_invalidVec.asUInt.orR || e0_rejectVec.reduce(_ || _)
350  val (e0_mergeIdx, e0_canMerge) = PriorityEncoderWithFlag(e0_mergeVec)
351  val (e0_allocIdx, e0_canAlloc) = PriorityEncoderWithFlag(e0_invalidVec)
352  val e0_allocWaitSame = e0_allocWaitSameVec.reduce(_ || _)
353  val e0_sid = Mux(e0_canMerge, e0_mergeIdx, e0_allocIdx)
354
355  // e0_fire is used to guarantee that it will not be rejected
356  when(e0_canMerge && e0_fire){
357    entries(e0_mergeIdx).update(e0_req)
358  }.elsewhen(e0_canAlloc && e0_fire){
359    entries(e0_allocIdx).set(e0_req)
360    states(e0_allocIdx).setValid(true.B)
361    when(e0_allocWaitSame){
362      states(e0_allocIdx).setWaitSame(true.B)
363    }
364  }
365
366  req_ready := !e0_reject
367
368  /* e1: return accept */
369  io.lsq.idResp.valid := RegNext(e0_fire)
370  io.lsq.idResp.bits.mid := RegEnable(e0_req.id, e0_fire)
371  io.lsq.idResp.bits.sid := RegEnable(e0_sid, e0_fire)
372  io.lsq.idResp.bits.is2lq := RegEnable(!isStore(e0_req.cmd), e0_fire)
373  io.lsq.idResp.bits.nc := RegEnable(e0_req.nc, e0_fire)
374
375  /******************************************************************
376   * Uncache Req
377   *  Version 0 (better timing)
378   *    q0: choose which one is sent
379   *    q0: sent
380   *
381   *  Version 1 (better performance)
382   *    solved in one cycle for achieving the original performance.
383   *    NOTE: "Enter Buffer" & "Uncache Req" not a continuous pipeline,
384   *          because there is no guarantee that mem_aquire will be always ready.
385   ******************************************************************/
386
387  val q0_canSentVec = sizeMap(i =>
388    (io.enableOutstanding || uState === s_idle) &&
389    states(i).can2Bus()
390  )
391  val q0_res = PriorityEncoderWithFlag(q0_canSentVec)
392  q0_canSentIdx := q0_res._1
393  q0_canSent := q0_res._2
394  q0_entry := entries(q0_canSentIdx)
395
396  val size = PopCount(q0_entry.mask)
397  val (lgSize, legal) = PriorityMuxWithFlag(Seq(
398    1.U -> 0.U,
399    2.U -> 1.U,
400    4.U -> 2.U,
401    8.U -> 3.U
402  ).map(m => (size===m._1) -> m._2))
403  assert(!(q0_canSent && !legal))
404
405  val q0_load = edge.Get(
406    fromSource      = q0_canSentIdx,
407    toAddress       = q0_entry.addr,
408    lgSize          = lgSize
409  )._2
410
411  val q0_store = edge.Put(
412    fromSource      = q0_canSentIdx,
413    toAddress       = q0_entry.addr,
414    lgSize          = lgSize,
415    data            = q0_entry.data,
416    mask            = q0_entry.mask
417  )._2
418
419  val q0_isStore = q0_entry.cmd === MemoryOpConstants.M_XWR
420
421  mem_acquire.valid := q0_canSent
422  mem_acquire.bits := Mux(q0_isStore, q0_store, q0_load)
423  mem_acquire.bits.user.lift(MemBackTypeMM).foreach(_ := q0_entry.memBackTypeMM)
424  mem_acquire.bits.user.lift(MemPageTypeNC).foreach(_ := q0_entry.nc)
425  when(mem_acquire.fire){
426    states(q0_canSentIdx).setInflight(true.B)
427
428    // q0 should judge whether wait same block
429    (0 until UncacheBufferSize).map(j =>
430      when(q0_canSentIdx =/= j.U && states(j).isValid() && !states(j).isWaitReturn() && addrMatch(q0_entry, entries(j))){
431        states(j).setWaitSame(true.B)
432      }
433    )
434  }
435
436
437  /******************************************************************
438   * Uncache Resp
439   ******************************************************************/
440
441  val (_, _, refill_done, _) = edge.addr_inc(mem_grant)
442
443  mem_grant.ready := true.B
444  when (mem_grant.fire) {
445    val id = mem_grant.bits.source
446    entries(id).update(mem_grant.bits)
447    states(id).updateUncacheResp()
448    assert(refill_done, "Uncache response should be one beat only!")
449
450    // remove state of wait same block
451    (0 until UncacheBufferSize).map(j =>
452      when(id =/= j.U && states(j).isValid() && states(j).isWaitSame() && addrMatch(entries(id), entries(j))){
453        states(j).setWaitSame(false.B)
454      }
455    )
456  }
457
458
459  /******************************************************************
460   * Return to LSQ
461   ******************************************************************/
462
463  val r0_canSentVec = sizeMap(i => states(i).can2Lsq())
464  val (r0_canSentIdx, r0_canSent) = PriorityEncoderWithFlag(r0_canSentVec)
465  resp.valid := r0_canSent
466  resp.bits := entries(r0_canSentIdx).toUncacheWordResp(r0_canSentIdx)
467  when(resp.fire){
468    states(r0_canSentIdx).updateReturn()
469  }
470
471
472  /******************************************************************
473   * Buffer Flush
474   * 1. when io.flush.valid is true: drain store queue and ubuffer
475   * 2. when io.lsq.req.bits.atomic is true: not support temporarily
476   ******************************************************************/
477  empty := !VecInit(states.map(_.isValid())).asUInt.orR
478  io.flush.empty := empty
479
480
481  /******************************************************************
482   * Load Data Forward to loadunit
483   *  f0: vaddr match, fast resp
484   *  f1: mask & data select, merge; paddr match; resp
485   *      NOTE: forward.paddr from dtlb, which is far from uncache f0
486   ******************************************************************/
487
488  val f0_validMask = sizeMap(i => isStore(entries(i)) && states(i).isValid())
489  val f0_fwdMaskCandidates = VecInit(entries.map(e => e.mask))
490  val f0_fwdDataCandidates = VecInit(entries.map(e => e.data))
491  val f1_fwdMaskCandidates = sizeMap(i => RegEnable(entries(i).mask, f0_validMask(i)))
492  val f1_fwdDataCandidates = sizeMap(i => RegEnable(entries(i).data, f0_validMask(i)))
493  val f1_tagMismatchVec = Wire(Vec(LoadPipelineWidth, Bool()))
494  f1_needDrain := f1_tagMismatchVec.asUInt.orR && !empty
495
496  for ((forward, i) <- io.forward.zipWithIndex) {
497    val f0_fwdValid = forward.valid
498    val f1_fwdValid = RegNext(f0_fwdValid)
499
500    /* f0 */
501    // vaddr match
502    val f0_vtagMatches = sizeMap(w => addrMatch(entries(w).vaddr, forward.vaddr))
503    val f0_flyTagMatches = sizeMap(w => f0_vtagMatches(w) && f0_validMask(w) && f0_fwdValid && states(w).isFwdOld())
504    val f0_idleTagMatches = sizeMap(w => f0_vtagMatches(w) && f0_validMask(w) && f0_fwdValid && states(w).isFwdNew())
505    // ONLY for fast use to get better timing
506    val f0_flyMaskFast = shiftMaskToHigh(
507      forward.vaddr,
508      Mux1H(f0_flyTagMatches, f0_fwdMaskCandidates)
509    ).asTypeOf(Vec(VDataBytes, Bool()))
510    val f0_idleMaskFast = shiftMaskToHigh(
511      forward.vaddr,
512      Mux1H(f0_idleTagMatches, f0_fwdMaskCandidates)
513    ).asTypeOf(Vec(VDataBytes, Bool()))
514
515    /* f1 */
516    val f1_flyTagMatches = RegEnable(f0_flyTagMatches, f0_fwdValid)
517    val f1_idleTagMatches = RegEnable(f0_idleTagMatches, f0_fwdValid)
518    val f1_fwdPAddr = RegEnable(forward.paddr, f0_fwdValid)
519    // select
520    val f1_flyMask = Mux1H(f1_flyTagMatches, f1_fwdMaskCandidates)
521    val f1_flyData = Mux1H(f1_flyTagMatches, f1_fwdDataCandidates)
522    val f1_idleMask = Mux1H(f1_idleTagMatches, f1_fwdMaskCandidates)
523    val f1_idleData = Mux1H(f1_idleTagMatches, f1_fwdDataCandidates)
524    // merge old(inflight) and new(idle)
525    val (f1_fwdDataTmp, f1_fwdMaskTmp) = doMerge(f1_flyData, f1_flyMask, f1_idleData, f1_idleMask)
526    val f1_fwdMask = shiftMaskToHigh(f1_fwdPAddr, f1_fwdMaskTmp).asTypeOf(Vec(VDataBytes, Bool()))
527    val f1_fwdData = shiftDataToHigh(f1_fwdPAddr, f1_fwdDataTmp).asTypeOf(Vec(VDataBytes, UInt(8.W)))
528    // paddr match and mismatch judge
529    val f1_ptagMatches = sizeMap(w => addrMatch(RegEnable(entries(w).addr, f0_fwdValid), f1_fwdPAddr))
530    f1_tagMismatchVec(i) := sizeMap(w =>
531      RegEnable(f0_vtagMatches(w), f0_fwdValid) =/= f1_ptagMatches(w) && RegEnable(f0_validMask(w), f0_fwdValid) && f1_fwdValid
532    ).asUInt.orR
533    XSDebug(
534      f1_tagMismatchVec(i),
535      "forward tag mismatch: pmatch %x vmatch %x vaddr %x paddr %x\n",
536      f1_ptagMatches.asUInt,
537      RegEnable(f0_vtagMatches.asUInt, f0_fwdValid),
538      RegEnable(forward.vaddr, f0_fwdValid),
539      RegEnable(forward.paddr, f0_fwdValid)
540    )
541    // response
542    forward.addrInvalid := false.B // addr in ubuffer is always ready
543    forward.dataInvalid := false.B // data in ubuffer is always ready
544    forward.matchInvalid := f1_tagMismatchVec(i) // paddr / vaddr cam result does not match
545    for (j <- 0 until VDataBytes) {
546      forward.forwardMaskFast(j) := f0_flyMaskFast(j) || f0_idleMaskFast(j)
547
548      forward.forwardData(j) := f1_fwdData(j)
549      forward.forwardMask(j) := false.B
550      when(f1_fwdMask(j) && f1_fwdValid) {
551        forward.forwardMask(j) := true.B
552      }
553    }
554
555  }
556
557
558  /******************************************************************
559   * Debug / Performance
560   ******************************************************************/
561
562  /* Debug Counters */
563  // print all input/output requests for debug purpose
564  // print req/resp
565  XSDebug(req.fire, "req cmd: %x addr: %x data: %x mask: %x\n",
566    req.bits.cmd, req.bits.addr, req.bits.data, req.bits.mask)
567  XSDebug(resp.fire, "data: %x\n", req.bits.data)
568  // print tilelink messages
569  XSDebug(mem_acquire.valid, "mem_acquire valid, ready=%d ", mem_acquire.ready)
570  mem_acquire.bits.dump(mem_acquire.valid)
571
572  XSDebug(mem_grant.fire, "mem_grant fire ")
573  mem_grant.bits.dump(mem_grant.fire)
574
575  /* Performance Counters */
576  XSPerfAccumulate("e0_reject", e0_reject && e0_req_valid)
577  XSPerfAccumulate("e0_total_enter", e0_fire)
578  XSPerfAccumulate("e0_merge", e0_fire && e0_canMerge)
579  XSPerfAccumulate("e0_alloc_simple", e0_fire && e0_canAlloc && !e0_allocWaitSame)
580  XSPerfAccumulate("e0_alloc_wait_same", e0_fire && e0_canAlloc && e0_allocWaitSame)
581  XSPerfAccumulate("q0_acquire", q0_canSent)
582  XSPerfAccumulate("q0_acquire_store", q0_canSent && q0_isStore)
583  XSPerfAccumulate("q0_acquire_load", q0_canSent && !q0_isStore)
584  XSPerfAccumulate("uncache_memBackTypeMM", io.lsq.req.fire && io.lsq.req.bits.memBackTypeMM)
585  XSPerfAccumulate("uncache_mmio_store", io.lsq.req.fire && isStore(io.lsq.req.bits.cmd) && !io.lsq.req.bits.nc)
586  XSPerfAccumulate("uncache_mmio_load", io.lsq.req.fire && !isStore(io.lsq.req.bits.cmd) && !io.lsq.req.bits.nc)
587  XSPerfAccumulate("uncache_nc_store", io.lsq.req.fire && isStore(io.lsq.req.bits.cmd) && io.lsq.req.bits.nc)
588  XSPerfAccumulate("uncache_nc_load", io.lsq.req.fire && !isStore(io.lsq.req.bits.cmd) && io.lsq.req.bits.nc)
589  XSPerfAccumulate("uncache_outstanding", uState =/= s_idle && mem_acquire.fire)
590  XSPerfAccumulate("forward_count", PopCount(io.forward.map(_.forwardMask.asUInt.orR)))
591  XSPerfAccumulate("forward_vaddr_match_failed", PopCount(f1_tagMismatchVec))
592
593  val perfEvents = Seq(
594    ("uncache_mmio_store", io.lsq.req.fire && isStore(io.lsq.req.bits.cmd) && !io.lsq.req.bits.nc),
595    ("uncache_mmio_load", io.lsq.req.fire && !isStore(io.lsq.req.bits.cmd) && !io.lsq.req.bits.nc),
596    ("uncache_nc_store", io.lsq.req.fire && isStore(io.lsq.req.bits.cmd) && io.lsq.req.bits.nc),
597    ("uncache_nc_load", io.lsq.req.fire && !isStore(io.lsq.req.bits.cmd) && io.lsq.req.bits.nc),
598    ("uncache_outstanding", uState =/= s_idle && mem_acquire.fire),
599    ("forward_count", PopCount(io.forward.map(_.forwardMask.asUInt.orR))),
600    ("forward_vaddr_match_failed", PopCount(f1_tagMismatchVec))
601  )
602
603  generatePerfEvent()
604  //  End
605}
606