xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 3889e11e8ee0c9c8ef29f03dbca39f0a9b372ed7)
16d5ddbceSLemover/***************************************************************************************
26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
46d5ddbceSLemover*
56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
86d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
96d5ddbceSLemover*
106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
136d5ddbceSLemover*
146d5ddbceSLemover* See the Mulan PSL v2 for more details.
156d5ddbceSLemover***************************************************************************************/
166d5ddbceSLemover
176d5ddbceSLemoverpackage xiangshan.cache.mmu
186d5ddbceSLemover
196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
22b848eea5SLemoverimport chisel3.internal.naming.chiselName
236d5ddbceSLemoverimport xiangshan._
246d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
256d5ddbceSLemoverimport utils._
266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
276d5ddbceSLemoverimport freechips.rocketchip.tilelink._
286d5ddbceSLemover
296d5ddbceSLemover/* ptw cache caches the page table of all the three layers
306d5ddbceSLemover * ptw cache resp at next cycle
316d5ddbceSLemover * the cache should not be blocked
326d5ddbceSLemover * when miss queue if full, just block req outside
336d5ddbceSLemover */
34*3889e11eSLemover
35*3889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
36*3889e11eSLemover  val hit = Bool()
37*3889e11eSLemover  val pre = Bool()
38*3889e11eSLemover  val ppn = UInt(ppnLen.W)
39*3889e11eSLemover  val perm = new PtePermBundle()
40*3889e11eSLemover  val ecc = Bool()
41*3889e11eSLemover  val level = UInt(2.W)
42*3889e11eSLemover
43*3889e11eSLemover  def apply(hit: Bool, pre: Bool, ppn: UInt, perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
44*3889e11eSLemover            ecc: Bool = false.B, level: UInt = 0.U) {
45*3889e11eSLemover    this.hit := hit && !ecc
46*3889e11eSLemover    this.pre := pre
47*3889e11eSLemover    this.ppn := ppn
48*3889e11eSLemover    this.perm := perm
49*3889e11eSLemover    this.ecc := ecc && hit
50*3889e11eSLemover    this.level := level
51*3889e11eSLemover  }
52*3889e11eSLemover}
53*3889e11eSLemover
54*3889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
55*3889e11eSLemover  val l1 = new PageCachePerPespBundle
56*3889e11eSLemover  val l2 = new PageCachePerPespBundle
57*3889e11eSLemover  val l3 = new PageCachePerPespBundle
58*3889e11eSLemover  val sp = new PageCachePerPespBundle
59*3889e11eSLemover}
60*3889e11eSLemover
61*3889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
62*3889e11eSLemover  val req_info = new L2TlbInnerBundle()
63*3889e11eSLemover  val isFirst = Bool()
64*3889e11eSLemover}
65*3889e11eSLemover
66*3889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
67*3889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
686d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
6945f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
706d5ddbceSLemover    val hit = Bool()
71bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
726d5ddbceSLemover    val toFsm = new Bundle {
736d5ddbceSLemover      val l1Hit = Bool()
746d5ddbceSLemover      val l2Hit = Bool()
756d5ddbceSLemover      val ppn = UInt(ppnLen.W)
766d5ddbceSLemover    }
776d5ddbceSLemover    val toTlb = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
786d5ddbceSLemover  })
796d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
805854c1edSLemover    val ptes = UInt(blockBits.W)
8145f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
826d5ddbceSLemover    val level = UInt(log2Up(Level).W)
83b848eea5SLemover    val addr_low = UInt((log2Up(l2tlbParams.blockBytes) - log2Up(XLEN/8)).W)
846d5ddbceSLemover  }))
856d5ddbceSLemover}
866d5ddbceSLemover
87b848eea5SLemover@chiselName
886d5ddbceSLemoverclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst {
896d5ddbceSLemover  val io = IO(new PtwCacheIO)
906d5ddbceSLemover
917196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
927196f5a2SLemover  val l2EntryType = new PTWEntriesWithEcc(ecc, num = PtwL2SectorSize, tagLen = PtwL2TagLen, level = 1, hasPerm = false)
937196f5a2SLemover  val l3EntryType = new PTWEntriesWithEcc(ecc, num = PtwL3SectorSize, tagLen = PtwL3TagLen, level = 2, hasPerm = true)
947196f5a2SLemover
956d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
966d5ddbceSLemover
976d5ddbceSLemover  val sfence = io.sfence
986d5ddbceSLemover  val refill = io.refill.bits
9945f497a4Shappy-lx  val refill_prefetch = from_pre(io.refill.bits.req_info.source)
100*3889e11eSLemover  val flush = sfence.valid || io.csr.satp.changed
1016d5ddbceSLemover
1026d5ddbceSLemover  // when refill, refuce to accept new req
1035854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
104*3889e11eSLemover
105*3889e11eSLemover  // handle hand signal and req_info
106*3889e11eSLemover  val stage1 = Wire(Decoupled(new PtwCacheReq()))
107*3889e11eSLemover  val stage2 = Wire(Decoupled(new PtwCacheReq()))
108*3889e11eSLemover  val stage3 = Wire(Decoupled(new PtwCacheReq()))
109*3889e11eSLemover  stage1 <> io.req
110*3889e11eSLemover  PipelineConnect(stage1, stage2, stage3.ready, flush, rwHarzad)
111*3889e11eSLemover  PipelineConnect(stage2, stage3, io.resp.ready, flush)
112*3889e11eSLemover  stage3.ready := io.resp.ready
1136d5ddbceSLemover
1146d5ddbceSLemover  // l1: level 0 non-leaf pte
1155854c1edSLemover  val l1 = Reg(Vec(l2tlbParams.l1Size, new PtwEntry(tagLen = PtwL1TagLen)))
1165854c1edSLemover  val l1v = RegInit(0.U(l2tlbParams.l1Size.W))
1175854c1edSLemover  val l1g = Reg(UInt(l2tlbParams.l1Size.W))
11845f497a4Shappy-lx  val l1asids = Reg(Vec(l2tlbParams.l1Size, UInt(AsidLength.W)))
1196d5ddbceSLemover
1206d5ddbceSLemover  // l2: level 1 non-leaf pte
1216d5ddbceSLemover  val l2 = Module(new SRAMTemplate(
1227196f5a2SLemover    l2EntryType,
1235854c1edSLemover    set = l2tlbParams.l2nSets,
1245854c1edSLemover    way = l2tlbParams.l2nWays,
1255854c1edSLemover    singlePort = sramSinglePort
1266d5ddbceSLemover  ))
1275854c1edSLemover  val l2v = RegInit(0.U((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
1285854c1edSLemover  val l2g = Reg(UInt((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
12945f497a4Shappy-lx  val l2asids = Reg(Vec(l2tlbParams.l2nSets, Vec(l2tlbParams.l2nWays, UInt(AsidLength.W))))
1306d5ddbceSLemover  def getl2vSet(vpn: UInt) = {
1315854c1edSLemover    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
1326d5ddbceSLemover    val set = genPtwL2SetIdx(vpn)
1335854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
1345854c1edSLemover    val l2vVec = l2v.asTypeOf(Vec(l2tlbParams.l2nSets, UInt(l2tlbParams.l2nWays.W)))
1356d5ddbceSLemover    l2vVec(set)
1366d5ddbceSLemover  }
13745f497a4Shappy-lx  def getl2asidSet(vpn: UInt) = {
13845f497a4Shappy-lx    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
13945f497a4Shappy-lx    val set = genPtwL2SetIdx(vpn)
14045f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
14145f497a4Shappy-lx    l2asids(set)
14245f497a4Shappy-lx  }
1436d5ddbceSLemover
1446d5ddbceSLemover  // l3: level 2 leaf pte of 4KB pages
1456d5ddbceSLemover  val l3 = Module(new SRAMTemplate(
1467196f5a2SLemover    l3EntryType,
1475854c1edSLemover    set = l2tlbParams.l3nSets,
1485854c1edSLemover    way = l2tlbParams.l3nWays,
1495854c1edSLemover    singlePort = sramSinglePort
1506d5ddbceSLemover  ))
1515854c1edSLemover  val l3v = RegInit(0.U((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
1525854c1edSLemover  val l3g = Reg(UInt((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
15345f497a4Shappy-lx  val l3asids = Reg(Vec(l2tlbParams.l3nSets, Vec(l2tlbParams.l3nWays, UInt(AsidLength.W))))
1546d5ddbceSLemover  def getl3vSet(vpn: UInt) = {
1555854c1edSLemover    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
1566d5ddbceSLemover    val set = genPtwL3SetIdx(vpn)
1575854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
1585854c1edSLemover    val l3vVec = l3v.asTypeOf(Vec(l2tlbParams.l3nSets, UInt(l2tlbParams.l3nWays.W)))
1596d5ddbceSLemover    l3vVec(set)
1606d5ddbceSLemover  }
16145f497a4Shappy-lx  def getl3asidSet(vpn: UInt) = {
16245f497a4Shappy-lx    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
16345f497a4Shappy-lx    val set = genPtwL3SetIdx(vpn)
16445f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
16545f497a4Shappy-lx    l3asids(set)
16645f497a4Shappy-lx  }
1676d5ddbceSLemover
1686d5ddbceSLemover  // sp: level 0/1 leaf pte of 1GB/2MB super pages
1695854c1edSLemover  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true)))
1705854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
1715854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
17245f497a4Shappy-lx  val spasids = Reg(Vec(l2tlbParams.spSize, UInt(AsidLength.W)))
1736d5ddbceSLemover
1746d5ddbceSLemover  // Access Perf
1755854c1edSLemover  val l1AccessPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
1765854c1edSLemover  val l2AccessPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
1775854c1edSLemover  val l3AccessPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
1785854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
1796d5ddbceSLemover  l1AccessPerf.map(_ := false.B)
1806d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
1816d5ddbceSLemover  l3AccessPerf.map(_ := false.B)
1826d5ddbceSLemover  spAccessPerf.map(_ := false.B)
1836d5ddbceSLemover
184*3889e11eSLemover  // stage1 & stage2, read page cache and data resp
185*3889e11eSLemover
186*3889e11eSLemover  val cache_read_valid = OneCycleValid(stage1.fire, flush)
1876d5ddbceSLemover  // l1
1885854c1edSLemover  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer, l2tlbParams.l1Size)
189bc063562SLemover  val (l1Hit, l1HitPPN, l1Pre) = {
190*3889e11eSLemover    val hitVecT = l1.zipWithIndex.map { case (e, i) => e.hit(stage1.bits.req_info.vpn, io.csr.satp.asid) && l1v(i) }
191*3889e11eSLemover    val hitVec = hitVecT.map(RegEnable(_, stage1.fire))
1926d5ddbceSLemover    val hitPPN = ParallelPriorityMux(hitVec zip l1.map(_.ppn))
193bc063562SLemover    val hitPre = ParallelPriorityMux(hitVec zip l1.map(_.prefetch))
1941af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid
1956d5ddbceSLemover
1966d5ddbceSLemover    when (hit) { ptwl1replace.access(OHToUInt(hitVec)) }
1976d5ddbceSLemover
198*3889e11eSLemover    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(stage1.fire)}
1995854c1edSLemover    for (i <- 0 until l2tlbParams.l1Size) {
200*3889e11eSLemover      XSDebug(stage1.fire, p"[l1] l1(${i.U}) ${l1(i)} hit:${l1(i).hit(stage1.bits.req_info.vpn, io.csr.satp.asid)}\n")
2016d5ddbceSLemover    }
202*3889e11eSLemover    XSDebug(stage1.fire, p"[l1] l1v:${Binary(l1v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
203*3889e11eSLemover    XSDebug(stage2.valid, p"[l1] l1Hit:${hit} l1HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
2046d5ddbceSLemover
2056d5ddbceSLemover    VecInit(hitVecT).suggestName(s"l1_hitVecT")
2066d5ddbceSLemover    VecInit(hitVec).suggestName(s"l1_hitVec")
2076d5ddbceSLemover
208bc063562SLemover    (hit, hitPPN, hitPre)
2096d5ddbceSLemover  }
2106d5ddbceSLemover
2116d5ddbceSLemover  // l2
2125854c1edSLemover  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer,l2tlbParams.l2nWays,l2tlbParams.l2nSets)
213bc063562SLemover  val (l2Hit, l2HitPPN, l2Pre, l2eccError) = {
214*3889e11eSLemover    val ridx = genPtwL2SetIdx(stage1.bits.req_info.vpn)
215*3889e11eSLemover    val vidx = RegEnable(VecInit(getl2vSet(stage1.bits.req_info.vpn).asBools), stage1.fire)
216*3889e11eSLemover    val asids_idx = RegEnable(getl2asidSet(stage1.bits.req_info.vpn), stage1.fire)
217*3889e11eSLemover    l2.io.r.req.valid := stage1.fire
2186d5ddbceSLemover    l2.io.r.req.bits.apply(setIdx = ridx)
2196d5ddbceSLemover    val ramDatas = l2.io.r.resp.data
220*3889e11eSLemover    val hitVec = VecInit(ramDatas.zip(vidx).map { case (wayData, v) => wayData.entries.hit(stage2.bits.req_info.vpn, io.csr.satp.asid) && v })
2217196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
2227196f5a2SLemover    val hitWayData = hitWayEntry.entries
2231af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid && RegNext(l2.io.r.req.ready, init = false.B)
2245854c1edSLemover    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l2nWays).map(_.U))
225*3889e11eSLemover    val eccError = hitWayEntry.decode()
2267196f5a2SLemover
2276d5ddbceSLemover    ridx.suggestName(s"l2_ridx")
2286d5ddbceSLemover    vidx.suggestName(s"l2_vidx")
2296d5ddbceSLemover    ramDatas.suggestName(s"l2_ramDatas")
2306d5ddbceSLemover    hitVec.suggestName(s"l2_hitVec")
2316d5ddbceSLemover    hitWayData.suggestName(s"l2_hitWayData")
2326d5ddbceSLemover    hitWay.suggestName(s"l2_hitWay")
2336d5ddbceSLemover
234*3889e11eSLemover    when (hit) { ptwl2replace.access(genPtwL2SetIdx(stage2.bits.req_info.vpn), hitWay) }
2356d5ddbceSLemover
236*3889e11eSLemover    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(stage1.fire) }
237*3889e11eSLemover    XSDebug(stage1.fire, p"[l2] ridx:0x${Hexadecimal(ridx)}\n")
2385854c1edSLemover    for (i <- 0 until l2tlbParams.l2nWays) {
239*3889e11eSLemover      XSDebug(RegNext(stage1.fire), p"[l2] ramDatas(${i.U}) ${ramDatas(i)}  l2v:${vidx(i)}  hit:${ramDatas(i).entries.hit(stage2.bits.req_info.vpn, io.csr.satp.asid)}\n")
2406d5ddbceSLemover    }
241*3889e11eSLemover    XSDebug(stage2.valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitWayData.ppns(genPtwL2SectorIdx(stage2.bits.req_info.vpn)))} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${Binary(vidx.asUInt)}\n")
2426d5ddbceSLemover
243*3889e11eSLemover    (hit, hitWayData.ppns(genPtwL2SectorIdx(stage2.bits.req_info.vpn)), hitWayData.prefetch, eccError)
2446d5ddbceSLemover  }
2456d5ddbceSLemover
2466d5ddbceSLemover  // l3
2475854c1edSLemover  val ptwl3replace = ReplacementPolicy.fromString(l2tlbParams.l3Replacer,l2tlbParams.l3nWays,l2tlbParams.l3nSets)
248bc063562SLemover  val (l3Hit, l3HitData, l3Pre, l3eccError) = {
249*3889e11eSLemover    val ridx = genPtwL3SetIdx(stage1.bits.req_info.vpn)
250*3889e11eSLemover    val vidx = RegEnable(VecInit(getl3vSet(stage1.bits.req_info.vpn).asBools), stage1.fire)
251*3889e11eSLemover    val asids_idx = RegEnable(getl3asidSet(stage1.bits.req_info.vpn), stage1.fire)
252*3889e11eSLemover    l3.io.r.req.valid := stage1.fire
2536d5ddbceSLemover    l3.io.r.req.bits.apply(setIdx = ridx)
2546d5ddbceSLemover    val ramDatas = l3.io.r.resp.data
255*3889e11eSLemover    val hitVec = VecInit(ramDatas.zip(vidx).map{ case (wayData, v) => wayData.entries.hit(stage2.bits.req_info.vpn, io.csr.satp.asid) && v })
2567196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
2577196f5a2SLemover    val hitWayData = hitWayEntry.entries
2587196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
2591af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid && RegNext(l3.io.r.req.ready, init = false.B)
2605854c1edSLemover    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l3nWays).map(_.U))
261*3889e11eSLemover    val eccError = hitWayEntry.decode()
2626d5ddbceSLemover
263*3889e11eSLemover    when (hit) { ptwl3replace.access(genPtwL3SetIdx(stage2.bits.req_info.vpn), hitWay) }
2647196f5a2SLemover
265*3889e11eSLemover    l3AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(stage1.fire) }
266*3889e11eSLemover    XSDebug(stage1.fire, p"[l3] ridx:0x${Hexadecimal(ridx)}\n")
2675854c1edSLemover    for (i <- 0 until l2tlbParams.l3nWays) {
268*3889e11eSLemover      XSDebug(RegNext(stage1.fire), p"[l3] ramDatas(${i.U}) ${ramDatas(i)}  l3v:${vidx(i)}  hit:${ramDatas(i).entries.hit(stage2.bits.req_info.vpn, io.csr.satp.asid)}\n")
2696d5ddbceSLemover    }
270*3889e11eSLemover    XSDebug(stage2.valid, p"[l3] l3Hit:${hit} l3HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${Binary(vidx.asUInt)}\n")
2716d5ddbceSLemover
2726d5ddbceSLemover    ridx.suggestName(s"l3_ridx")
2736d5ddbceSLemover    vidx.suggestName(s"l3_vidx")
2746d5ddbceSLemover    ramDatas.suggestName(s"l3_ramDatas")
2756d5ddbceSLemover    hitVec.suggestName(s"l3_hitVec")
2766d5ddbceSLemover    hitWay.suggestName(s"l3_hitWay")
2776d5ddbceSLemover
278*3889e11eSLemover    (hit, hitWayData, hitWayData.prefetch, eccError)
2796d5ddbceSLemover  }
280*3889e11eSLemover  val l3HitPPN = l3HitData.ppns(genPtwL3SectorIdx(stage2.bits.req_info.vpn))
281*3889e11eSLemover  val l3HitPerm = l3HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL3SectorSize, new PtePermBundle)))(genPtwL3SectorIdx(stage2.bits.req_info.vpn))
2826d5ddbceSLemover
2836d5ddbceSLemover  // super page
2845854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
285bc063562SLemover  val (spHit, spHitData, spPre) = {
286*3889e11eSLemover    val hitVecT = sp.zipWithIndex.map { case (e, i) => e.hit(stage1.bits.req_info.vpn, io.csr.satp.asid) && spv(i) }
287*3889e11eSLemover    val hitVec = hitVecT.map(RegEnable(_, stage1.fire))
2886d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
2891af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid
2906d5ddbceSLemover
2916d5ddbceSLemover    when (hit) { spreplace.access(OHToUInt(hitVec)) }
2926d5ddbceSLemover
293*3889e11eSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && RegNext(stage1.fire) }
2945854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
295*3889e11eSLemover      XSDebug(stage1.fire, p"[sp] sp(${i.U}) ${sp(i)} hit:${sp(i).hit(stage1.bits.req_info.vpn, io.csr.satp.asid)} spv:${spv(i)}\n")
2966d5ddbceSLemover    }
297*3889e11eSLemover    XSDebug(stage2.valid, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
2986d5ddbceSLemover
2996d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
3006d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
3016d5ddbceSLemover
302bc063562SLemover    (hit, hitData, hitData.prefetch)
3036d5ddbceSLemover  }
3046d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
3056d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
3066d5ddbceSLemover
307*3889e11eSLemover  val s2_res = Wire(new PageCacheRespBundle)
308*3889e11eSLemover  s2_res.l1.apply(l1Hit, l1Pre, l1HitPPN)
309*3889e11eSLemover  s2_res.l2.apply(l2Hit, l2Pre, l2HitPPN, ecc = l2eccError)
310*3889e11eSLemover  s2_res.l3.apply(l3Hit, l3Pre, l3HitPPN, l3HitPerm, l3eccError)
311*3889e11eSLemover  s2_res.sp.apply(spHit, spPre, spHitData.ppn, spHitPerm, false.B, spHitLevel)
312*3889e11eSLemover  val s2_res_reg = DataHoldBypass(s2_res, RegNext(stage1.fire()))
3136d5ddbceSLemover
314*3889e11eSLemover  // stage3, add stage 3 for ecc check...
315*3889e11eSLemover  val s3_res = Reg(new PageCacheRespBundle)
316*3889e11eSLemover  when (stage2.fire()) {
317*3889e11eSLemover    s3_res := s2_res_reg
318*3889e11eSLemover  }
319*3889e11eSLemover
320*3889e11eSLemover  io.resp.bits.req_info   := stage3.bits.req_info
321*3889e11eSLemover  io.resp.bits.hit      := s3_res.l3.hit || s3_res.sp.hit
322*3889e11eSLemover  io.resp.bits.prefetch := s3_res.l3.pre && s3_res.l3.hit || s3_res.sp.pre && s3_res.sp.hit
323*3889e11eSLemover  io.resp.bits.toFsm.l1Hit := s3_res.l1.hit
324*3889e11eSLemover  io.resp.bits.toFsm.l2Hit := s3_res.l2.hit
325*3889e11eSLemover  io.resp.bits.toFsm.ppn   := Mux(s3_res.l2.hit, s3_res.l2.ppn, s3_res.l1.ppn)
326*3889e11eSLemover  io.resp.bits.toTlb.tag   := stage3.bits.req_info.vpn
327*3889e11eSLemover  io.resp.bits.toTlb.asid  := io.csr.satp.asid // DontCare
328*3889e11eSLemover  io.resp.bits.toTlb.ppn   := Mux(s3_res.l3.hit, s3_res.l3.ppn, s3_res.sp.ppn)
329*3889e11eSLemover  io.resp.bits.toTlb.perm.map(_ := Mux(s3_res.l3.hit, s3_res.l3.perm, s3_res.sp.perm))
330*3889e11eSLemover  io.resp.bits.toTlb.level.map(_ := Mux(s3_res.l3.hit, 2.U, s3_res.sp.level))
331*3889e11eSLemover  io.resp.bits.toTlb.prefetch := from_pre(stage3.bits.req_info.source)
332*3889e11eSLemover  io.resp.valid := stage3.valid
3336d5ddbceSLemover  assert(!(l3Hit && spHit), "normal page and super page both hit")
3346d5ddbceSLemover
3356d5ddbceSLemover  // refill Perf
3365854c1edSLemover  val l1RefillPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
3375854c1edSLemover  val l2RefillPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
3385854c1edSLemover  val l3RefillPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
3395854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
3406d5ddbceSLemover  l1RefillPerf.map(_ := false.B)
3416d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
3426d5ddbceSLemover  l3RefillPerf.map(_ := false.B)
3436d5ddbceSLemover  spRefillPerf.map(_ := false.B)
3446d5ddbceSLemover
3456d5ddbceSLemover  // refill
3466d5ddbceSLemover  l2.io.w.req <> DontCare
3476d5ddbceSLemover  l3.io.w.req <> DontCare
3486d5ddbceSLemover  l2.io.w.req.valid := false.B
3496d5ddbceSLemover  l3.io.w.req.valid := false.B
3506d5ddbceSLemover
3515854c1edSLemover  def get_part(data: UInt, index: UInt): UInt = {
3525854c1edSLemover    val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W)))
3535854c1edSLemover    inner_data(index)
3545854c1edSLemover  }
3555854c1edSLemover
3566d5ddbceSLemover  val memRdata = refill.ptes
357b848eea5SLemover  val memSelData = get_part(memRdata, refill.addr_low)
3585854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
3596d5ddbceSLemover  val memPte = memSelData.asTypeOf(new PteBundle)
3606d5ddbceSLemover
361b848eea5SLemover  memPte.suggestName("memPte")
362b848eea5SLemover
3636d5ddbceSLemover  // TODO: handle sfenceLatch outsize
364*3889e11eSLemover  when (io.refill.valid && !memPte.isPf(refill.level) && !flush ) {
3656d5ddbceSLemover    when (refill.level === 0.U && !memPte.isLeaf()) {
3665854c1edSLemover      // val refillIdx = LFSR64()(log2Up(l2tlbParams.l1Size)-1,0) // TODO: may be LRU
3676d5ddbceSLemover      val refillIdx = replaceWrapper(l1v, ptwl1replace.way)
3686d5ddbceSLemover      refillIdx.suggestName(s"PtwL1RefillIdx")
3696d5ddbceSLemover      val rfOH = UIntToOH(refillIdx)
37045f497a4Shappy-lx      l1(refillIdx).refill(
37145f497a4Shappy-lx        refill.req_info.vpn,
37245f497a4Shappy-lx        io.csr.satp.asid,
37345f497a4Shappy-lx        memSelData,
37445f497a4Shappy-lx        0.U,
37545f497a4Shappy-lx        refill_prefetch
37645f497a4Shappy-lx      )
3776d5ddbceSLemover      ptwl1replace.access(refillIdx)
3786d5ddbceSLemover      l1v := l1v | rfOH
3796d5ddbceSLemover      l1g := (l1g & ~rfOH) | Mux(memPte.perm.g, rfOH, 0.U)
3806d5ddbceSLemover
3815854c1edSLemover      for (i <- 0 until l2tlbParams.l1Size) {
3826d5ddbceSLemover        l1RefillPerf(i) := i.U === refillIdx
3836d5ddbceSLemover      }
3846d5ddbceSLemover
38545f497a4Shappy-lx      XSDebug(p"[l1 refill] refillIdx:${refillIdx} refillEntry:${l1(refillIdx).genPtwEntry(refill.req_info.vpn, io.csr.satp.asid, memSelData, 0.U, refill_prefetch)}\n")
3866d5ddbceSLemover      XSDebug(p"[l1 refill] l1v:${Binary(l1v)}->${Binary(l1v | rfOH)} l1g:${Binary(l1g)}->${Binary((l1g & ~rfOH) | Mux(memPte.perm.g, rfOH, 0.U))}\n")
3876d5ddbceSLemover
3886d5ddbceSLemover      refillIdx.suggestName(s"l1_refillIdx")
3896d5ddbceSLemover      rfOH.suggestName(s"l1_rfOH")
3906d5ddbceSLemover    }
3916d5ddbceSLemover
3926d5ddbceSLemover    when (refill.level === 1.U && !memPte.isLeaf()) {
39345f497a4Shappy-lx      val refillIdx = genPtwL2SetIdx(refill.req_info.vpn)
394*3889e11eSLemover      val victimWay = replaceWrapper(RegEnable(VecInit(getl2vSet(refill.req_info.vpn).asBools).asUInt, stage1.fire), ptwl2replace.way(refillIdx))
3956d5ddbceSLemover      val victimWayOH = UIntToOH(victimWay)
3966d5ddbceSLemover      val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
3977196f5a2SLemover      val wdata = Wire(l2EntryType)
398*3889e11eSLemover      wdata.gen(
39945f497a4Shappy-lx        vpn = refill.req_info.vpn,
40045f497a4Shappy-lx        asid = io.csr.satp.asid,
40145f497a4Shappy-lx        data = memRdata,
40245f497a4Shappy-lx        levelUInt = 1.U,
40345f497a4Shappy-lx        refill_prefetch
40445f497a4Shappy-lx      )
4056d5ddbceSLemover      l2.io.w.apply(
4066d5ddbceSLemover        valid = true.B,
4076d5ddbceSLemover        setIdx = refillIdx,
4087196f5a2SLemover        data = wdata,
4096d5ddbceSLemover        waymask = victimWayOH
4106d5ddbceSLemover      )
4116d5ddbceSLemover      ptwl2replace.access(refillIdx, victimWay)
4126d5ddbceSLemover      l2v := l2v | rfvOH
4136d5ddbceSLemover      l2g := l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
4146d5ddbceSLemover
4155854c1edSLemover      for (i <- 0 until l2tlbParams.l2nWays) {
4166d5ddbceSLemover        l2RefillPerf(i) := i.U === victimWay
4176d5ddbceSLemover      }
4186d5ddbceSLemover
4196d5ddbceSLemover      XSDebug(p"[l2 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
42045f497a4Shappy-lx      XSDebug(p"[l2 refill] refilldata:0x${wdata}\n")
4216d5ddbceSLemover      XSDebug(p"[l2 refill] l2v:${Binary(l2v)} -> ${Binary(l2v | rfvOH)}\n")
4226d5ddbceSLemover      XSDebug(p"[l2 refill] l2g:${Binary(l2g)} -> ${Binary(l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
4236d5ddbceSLemover
4246d5ddbceSLemover      refillIdx.suggestName(s"l2_refillIdx")
4256d5ddbceSLemover      victimWay.suggestName(s"l2_victimWay")
4266d5ddbceSLemover      victimWayOH.suggestName(s"l2_victimWayOH")
4276d5ddbceSLemover      rfvOH.suggestName(s"l2_rfvOH")
4286d5ddbceSLemover    }
4296d5ddbceSLemover
4306d5ddbceSLemover    when (refill.level === 2.U && memPte.isLeaf()) {
43145f497a4Shappy-lx      val refillIdx = genPtwL3SetIdx(refill.req_info.vpn)
432*3889e11eSLemover      val victimWay = replaceWrapper(RegEnable(VecInit(getl3vSet(refill.req_info.vpn).asBools).asUInt, stage1.fire), ptwl3replace.way(refillIdx))
4336d5ddbceSLemover      val victimWayOH = UIntToOH(victimWay)
4346d5ddbceSLemover      val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
4357196f5a2SLemover      val wdata = Wire(l3EntryType)
436*3889e11eSLemover      wdata.gen(
43745f497a4Shappy-lx        vpn = refill.req_info.vpn,
43845f497a4Shappy-lx        asid = io.csr.satp.asid,
43945f497a4Shappy-lx        data = memRdata,
44045f497a4Shappy-lx        levelUInt = 2.U,
44145f497a4Shappy-lx        refill_prefetch
44245f497a4Shappy-lx      )
4436d5ddbceSLemover      l3.io.w.apply(
4446d5ddbceSLemover        valid = true.B,
4456d5ddbceSLemover        setIdx = refillIdx,
4467196f5a2SLemover        data = wdata,
4476d5ddbceSLemover        waymask = victimWayOH
4486d5ddbceSLemover      )
4496d5ddbceSLemover      ptwl3replace.access(refillIdx, victimWay)
4506d5ddbceSLemover      l3v := l3v | rfvOH
4516d5ddbceSLemover      l3g := l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
4526d5ddbceSLemover
4535854c1edSLemover      for (i <- 0 until l2tlbParams.l3nWays) {
4546d5ddbceSLemover        l3RefillPerf(i) := i.U === victimWay
4556d5ddbceSLemover      }
4566d5ddbceSLemover
4576d5ddbceSLemover      XSDebug(p"[l3 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
45845f497a4Shappy-lx      XSDebug(p"[l3 refill] refilldata:0x${wdata}\n")
4596d5ddbceSLemover      XSDebug(p"[l3 refill] l3v:${Binary(l3v)} -> ${Binary(l3v | rfvOH)}\n")
4606d5ddbceSLemover      XSDebug(p"[l3 refill] l3g:${Binary(l3g)} -> ${Binary(l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
4616d5ddbceSLemover
4626d5ddbceSLemover      refillIdx.suggestName(s"l3_refillIdx")
4636d5ddbceSLemover      victimWay.suggestName(s"l3_victimWay")
4646d5ddbceSLemover      victimWayOH.suggestName(s"l3_victimWayOH")
4656d5ddbceSLemover      rfvOH.suggestName(s"l3_rfvOH")
4666d5ddbceSLemover    }
4676d5ddbceSLemover    when ((refill.level === 0.U || refill.level === 1.U) && memPte.isLeaf()) {
4685854c1edSLemover      val refillIdx = spreplace.way// LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
4696d5ddbceSLemover      val rfOH = UIntToOH(refillIdx)
47045f497a4Shappy-lx      sp(refillIdx).refill(
47145f497a4Shappy-lx        refill.req_info.vpn,
47245f497a4Shappy-lx        io.csr.satp.asid,
47345f497a4Shappy-lx        memSelData,
47445f497a4Shappy-lx        refill.level,
47545f497a4Shappy-lx        refill_prefetch
47645f497a4Shappy-lx      )
4776d5ddbceSLemover      spreplace.access(refillIdx)
4786d5ddbceSLemover      spv := spv | rfOH
4796d5ddbceSLemover      spg := spg & ~rfOH | Mux(memPte.perm.g, rfOH, 0.U)
4806d5ddbceSLemover
4815854c1edSLemover      for (i <- 0 until l2tlbParams.spSize) {
4826d5ddbceSLemover        spRefillPerf(i) := i.U === refillIdx
4836d5ddbceSLemover      }
4846d5ddbceSLemover
48545f497a4Shappy-lx      XSDebug(p"[sp refill] refillIdx:${refillIdx} refillEntry:${sp(refillIdx).genPtwEntry(refill.req_info.vpn, io.csr.satp.asid, memSelData, refill.level, refill_prefetch)}\n")
4866d5ddbceSLemover      XSDebug(p"[sp refill] spv:${Binary(spv)}->${Binary(spv | rfOH)} spg:${Binary(spg)}->${Binary(spg & ~rfOH | Mux(memPte.perm.g, rfOH, 0.U))}\n")
4876d5ddbceSLemover
4886d5ddbceSLemover      refillIdx.suggestName(s"sp_refillIdx")
4896d5ddbceSLemover      rfOH.suggestName(s"sp_rfOH")
4906d5ddbceSLemover    }
4916d5ddbceSLemover  }
4926d5ddbceSLemover
493*3889e11eSLemover  val l2eccFlush = s3_res.l2.ecc && stage3.fire() // RegNext(l2eccError, init = false.B)
494*3889e11eSLemover  val l3eccFlush = s3_res.l3.ecc && stage3.fire() // RegNext(l3eccError, init = false.B)
495*3889e11eSLemover  val eccVpn = stage3.bits.req_info.vpn
4967196f5a2SLemover
4977196f5a2SLemover  assert(!l2eccFlush)
4987196f5a2SLemover  assert(!l3eccFlush)
4997196f5a2SLemover  when (l2eccFlush) {
5007196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL2SetIdx(eccVpn))
5017196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l2nWays, a.asUInt) }).asUInt
5027196f5a2SLemover    l2v := l2v & ~flushMask
5037196f5a2SLemover    l2g := l2g & ~flushMask
5047196f5a2SLemover  }
5057196f5a2SLemover
5067196f5a2SLemover  when (l3eccFlush) {
5077196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(eccVpn))
5087196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
5097196f5a2SLemover    l3v := l3v & ~flushMask
5107196f5a2SLemover    l3g := l3g & ~flushMask
5117196f5a2SLemover  }
5127196f5a2SLemover
5136d5ddbceSLemover  // sfence
5146d5ddbceSLemover  when (sfence.valid) {
51545f497a4Shappy-lx    val l1asidhit = VecInit(l1asids.map(_ === sfence.bits.asid)).asUInt
51645f497a4Shappy-lx    val spasidhit = VecInit(spasids.map(_ === sfence.bits.asid)).asUInt
51745f497a4Shappy-lx    val sfence_vpn = sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)
51845f497a4Shappy-lx
5196d5ddbceSLemover    when (sfence.bits.rs1/*va*/) {
5206d5ddbceSLemover      when (sfence.bits.rs2) {
5216d5ddbceSLemover        // all va && all asid
5226d5ddbceSLemover        l1v := 0.U
5236d5ddbceSLemover        l2v := 0.U
5246d5ddbceSLemover        l3v := 0.U
5256d5ddbceSLemover        spv := 0.U
5266d5ddbceSLemover      } .otherwise {
5276d5ddbceSLemover        // all va && specific asid except global
52845f497a4Shappy-lx
52945f497a4Shappy-lx        l1v := l1v & (~l1asidhit | l1g)
5306d5ddbceSLemover        l2v := l2v & l2g
5316d5ddbceSLemover        l3v := l3v & l3g
53245f497a4Shappy-lx        spv := spv & (~spasidhit | spg)
5336d5ddbceSLemover      }
5346d5ddbceSLemover    } .otherwise {
5356d5ddbceSLemover      // val flushMask = UIntToOH(genTlbL2Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
53645f497a4Shappy-lx      val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(sfence_vpn))
5375854c1edSLemover      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l3nWays, _.asUInt))).asUInt
5385854c1edSLemover      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
5396d5ddbceSLemover      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
5406d5ddbceSLemover      flushMask.suggestName(s"sfence_nrs1_flushMask")
54145f497a4Shappy-lx
5426d5ddbceSLemover      when (sfence.bits.rs2) {
5436d5ddbceSLemover        // specific leaf of addr && all asid
5446d5ddbceSLemover        l3v := l3v & ~flushMask
54545f497a4Shappy-lx        spv := spv & (~VecInit(sp.map(_.hit(sfence_vpn, sfence.bits.asid, ignoreAsid = true))).asUInt | spg)
5466d5ddbceSLemover      } .otherwise {
5476d5ddbceSLemover        // specific leaf of addr && specific asid
5486d5ddbceSLemover        l3v := l3v & (~flushMask | l3g)
54945f497a4Shappy-lx        spv := spv & (~VecInit(sp.map(_.hit(sfence_vpn, sfence.bits.asid))).asUInt | spg)
5506d5ddbceSLemover      }
5516d5ddbceSLemover    }
5526d5ddbceSLemover  }
5536d5ddbceSLemover
5546d5ddbceSLemover  // Perf Count
555*3889e11eSLemover  val resp_l3 = s3_res.l3.hit
556*3889e11eSLemover  val resp_sp = s3_res.sp.hit
557*3889e11eSLemover  val resp_l1_pre = s3_res.l1.pre
558*3889e11eSLemover  val resp_l2_pre = s3_res.l2.pre
559*3889e11eSLemover  val resp_l3_pre = s3_res.l3.pre
560*3889e11eSLemover  val resp_sp_pre = s3_res.sp.pre
56145f497a4Shappy-lx  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire()
562bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
563bc063562SLemover  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
564bc063562SLemover  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
565bc063562SLemover  XSPerfAccumulate("l3_hit", base_valid_access_0 && resp_l3)
566bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
567bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
568bc063562SLemover
569bc063562SLemover  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
570bc063562SLemover  XSPerfAccumulate("l2_hit_pre", base_valid_access_0 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
571bc063562SLemover  XSPerfAccumulate("l3_hit_pre", base_valid_access_0 && resp_l3_pre && resp_l3)
572bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
573bc063562SLemover  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
574bc063562SLemover
57545f497a4Shappy-lx  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire()
576bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
577bc063562SLemover  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
578bc063562SLemover  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
579bc063562SLemover  XSPerfAccumulate("pre_l3_hit", base_valid_access_1 && resp_l3)
580bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
581bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
582bc063562SLemover
583bc063562SLemover  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
584bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre", base_valid_access_1 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
585bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre", base_valid_access_1 && resp_l3_pre && resp_l3)
586bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
587bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
588bc063562SLemover
589*3889e11eSLemover  val base_valid_access_2 = stage3.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire()
590bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
591bc063562SLemover  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
592bc063562SLemover  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
593bc063562SLemover  XSPerfAccumulate("l3_hit_first", base_valid_access_2 && resp_l3)
594bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
595bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
596bc063562SLemover
597bc063562SLemover  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
598bc063562SLemover  XSPerfAccumulate("l2_hit_pre_first", base_valid_access_2 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
599bc063562SLemover  XSPerfAccumulate("l3_hit_pre_first", base_valid_access_2 && resp_l3_pre && resp_l3)
600bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
601bc063562SLemover  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
602bc063562SLemover
603*3889e11eSLemover  val base_valid_access_3 = stage3.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire()
604bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
605bc063562SLemover  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
606bc063562SLemover  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
607bc063562SLemover  XSPerfAccumulate("pre_l3_hit_first", base_valid_access_3 && resp_l3)
608bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
609bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
610bc063562SLemover
611bc063562SLemover  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
612bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre_first", base_valid_access_3 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
613bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre_first", base_valid_access_3 && resp_l3_pre && resp_l3)
614bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
615bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre_first",base_valid_access_3 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
616bc063562SLemover
6176d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
6186d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
6196d5ddbceSLemover  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1AccessIndex${i}", l) }
6206d5ddbceSLemover  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2AccessIndex${i}", l) }
6216d5ddbceSLemover  l3AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3AccessIndex${i}", l) }
6226d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
6236d5ddbceSLemover  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1RefillIndex${i}", l) }
6246d5ddbceSLemover  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2RefillIndex${i}", l) }
6256d5ddbceSLemover  l3RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3RefillIndex${i}", l) }
6266d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
6276d5ddbceSLemover
628bc063562SLemover  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
629bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
630bc063562SLemover  XSPerfAccumulate("l3Refill", Cat(l3RefillPerf).orR)
631bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
63245f497a4Shappy-lx  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch)
63345f497a4Shappy-lx  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch)
63445f497a4Shappy-lx  XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf).orR && refill_prefetch)
63545f497a4Shappy-lx  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch)
636bc063562SLemover
6376d5ddbceSLemover  // debug
6386d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] original v and g vector:\n")
6396d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l1v:${Binary(l1v)}\n")
6406d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l2v:${Binary(l2v)}\n")
6416d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l3v:${Binary(l3v)}\n")
6426d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l3g:${Binary(l3g)}\n")
6436d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] spv:${Binary(spv)}\n")
6446d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] new v and g vector:\n")
6456d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l1v:${Binary(l1v)}\n")
6466d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l2v:${Binary(l2v)}\n")
6476d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l3v:${Binary(l3v)}\n")
6486d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l3g:${Binary(l3g)}\n")
6496d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] spv:${Binary(spv)}\n")
6506d5ddbceSLemover}
651