xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 0a56a7dc9f74ec769397fa5c4840277345a0ac7c)
16d5ddbceSLemover/***************************************************************************************
2e3da8badSTang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
56d5ddbceSLemover*
66d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
76d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
86d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
96d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
106d5ddbceSLemover*
116d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
126d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
136d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
146d5ddbceSLemover*
156d5ddbceSLemover* See the Mulan PSL v2 for more details.
166d5ddbceSLemover***************************************************************************************/
176d5ddbceSLemover
186d5ddbceSLemoverpackage xiangshan.cache.mmu
196d5ddbceSLemover
208891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
216d5ddbceSLemoverimport chisel3._
226d5ddbceSLemoverimport chisel3.util._
236d5ddbceSLemoverimport xiangshan._
246d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
256d5ddbceSLemoverimport utils._
263c02ee8fSwakafaimport utility._
27abc4432bSHaoyuan Fengimport coupledL2.utils.SplittedSRAM
286d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
296d5ddbceSLemoverimport freechips.rocketchip.tilelink._
306d5ddbceSLemover
316d5ddbceSLemover/* ptw cache caches the page table of all the three layers
326d5ddbceSLemover * ptw cache resp at next cycle
336d5ddbceSLemover * the cache should not be blocked
346d5ddbceSLemover * when miss queue if full, just block req outside
356d5ddbceSLemover */
363889e11eSLemover
373889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
383889e11eSLemover  val hit = Bool()
393889e11eSLemover  val pre = Bool()
404c0e0181SXiaokun-Pei  val ppn = UInt(gvpnLen.W)
41002c10a4SYanqin Li  val pbmt = UInt(ptePbmtLen.W)
423889e11eSLemover  val perm = new PtePermBundle()
43718a93f5SHaoyuan Feng  val n = UInt(pteNLen.W)
443889e11eSLemover  val ecc = Bool()
453889e11eSLemover  val level = UInt(2.W)
468d8ac704SLemover  val v = Bool()
473889e11eSLemover
48718a93f5SHaoyuan Feng  def apply(hit: Bool, pre: Bool, ppn: UInt, pbmt: UInt = 0.U, n: UInt = 0.U,
49002c10a4SYanqin Li            perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
50e3da8badSTang Haojin            ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B): Unit = {
513889e11eSLemover    this.hit := hit && !ecc
523889e11eSLemover    this.pre := pre
533889e11eSLemover    this.ppn := ppn
54718a93f5SHaoyuan Feng    this.n := n
55002c10a4SYanqin Li    this.pbmt := pbmt
563889e11eSLemover    this.perm := perm
573889e11eSLemover    this.ecc := ecc && hit
583889e11eSLemover    this.level := level
598d8ac704SLemover    this.v := valid
603889e11eSLemover  }
613889e11eSLemover}
623889e11eSLemover
6363632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle {
6463632028SHaoyuan Feng  assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
6563632028SHaoyuan Feng  val hit = Bool()
6663632028SHaoyuan Feng  val pre = Bool()
674c0e0181SXiaokun-Pei  val ppn = Vec(tlbcontiguous, UInt(gvpnLen.W))
68002c10a4SYanqin Li  val pbmt = Vec(tlbcontiguous, UInt(ptePbmtLen.W))
6963632028SHaoyuan Feng  val perm = Vec(tlbcontiguous, new PtePermBundle())
7063632028SHaoyuan Feng  val ecc = Bool()
7163632028SHaoyuan Feng  val level = UInt(2.W)
7263632028SHaoyuan Feng  val v = Vec(tlbcontiguous, Bool())
7363632028SHaoyuan Feng
74002c10a4SYanqin Li  def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], pbmt: Vec[UInt] = Vec(tlbcontiguous, 0.U),
75002c10a4SYanqin Li            perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())),
76e0c1f271SHaoyuan Feng            ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B)): Unit = {
7763632028SHaoyuan Feng    this.hit := hit && !ecc
7863632028SHaoyuan Feng    this.pre := pre
7963632028SHaoyuan Feng    this.ppn := ppn
80002c10a4SYanqin Li    this.pbmt := pbmt
8163632028SHaoyuan Feng    this.perm := perm
8263632028SHaoyuan Feng    this.ecc := ecc && hit
8363632028SHaoyuan Feng    this.level := level
8463632028SHaoyuan Feng    this.v := valid
8563632028SHaoyuan Feng  }
8663632028SHaoyuan Feng}
8763632028SHaoyuan Feng
883889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
893ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(new PageCachePerPespBundle) else None
903889e11eSLemover  val l2 = new PageCachePerPespBundle
913ea4388cSHaoyuan Feng  val l1 = new PageCachePerPespBundle
923ea4388cSHaoyuan Feng  val l0 = new PageCacheMergePespBundle
933889e11eSLemover  val sp = new PageCachePerPespBundle
943889e11eSLemover}
953889e11eSLemover
963889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
973889e11eSLemover  val req_info = new L2TlbInnerBundle()
983889e11eSLemover  val isFirst = Bool()
993ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Vec(4, Bool()) else Vec(3, Bool())
100325f0a4eSpeixiaokun  val isHptwReq = Bool()
101d0de7e4aSpeixiaokun  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
1023889e11eSLemover}
1033889e11eSLemover
1043889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
1053889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
1066d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
10745f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
10894133605SLemover    val isFirst = Bool()
1096d5ddbceSLemover    val hit = Bool()
110bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
1111f4a7c0cSLemover    val bypassed = Bool()
1126d5ddbceSLemover    val toFsm = new Bundle {
1133ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
1146d5ddbceSLemover      val l2Hit = Bool()
1153ea4388cSHaoyuan Feng      val l1Hit = Bool()
1164c0e0181SXiaokun-Pei      val ppn = UInt(gvpnLen.W)
11730104977Speixiaokun      val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW
1186d5ddbceSLemover    }
1196979864eSXiaokun-Pei    val stage1 = new PtwMergeResp()
120325f0a4eSpeixiaokun    val isHptwReq = Bool()
121d0de7e4aSpeixiaokun    val toHptw = new Bundle {
1223ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
123d0de7e4aSpeixiaokun      val l2Hit = Bool()
1243ea4388cSHaoyuan Feng      val l1Hit = Bool()
125d0de7e4aSpeixiaokun      val ppn = UInt(ppnLen.W)
126d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
127d0de7e4aSpeixiaokun      val resp = new HptwResp() // used if hit
12883d93d53Speixiaokun      val bypassed = Bool()
129d0de7e4aSpeixiaokun    }
1306d5ddbceSLemover  })
1316d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
1325854c1edSLemover    val ptes = UInt(blockBits.W)
1337797f035SbugGenerator    val levelOH = new Bundle {
1347797f035SbugGenerator      // NOTE: levelOH has (Level+1) bits, each stands for page cache entries
1357797f035SbugGenerator      val sp = Bool()
1363ea4388cSHaoyuan Feng      val l0 = Bool()
1377797f035SbugGenerator      val l1 = Bool()
1383ea4388cSHaoyuan Feng      val l2 = Bool()
1393ea4388cSHaoyuan Feng      val l3 = if (EnableSv48) Some(Bool()) else None
1407797f035SbugGenerator      def apply(levelUInt: UInt, valid: Bool) = {
1413ea4388cSHaoyuan Feng        sp := GatedValidRegNext((levelUInt === 1.U || levelUInt === 2.U || levelUInt === 3.U) && valid, false.B)
1423ea4388cSHaoyuan Feng        l0 := GatedValidRegNext((levelUInt === 0.U) & valid, false.B)
1433ea4388cSHaoyuan Feng        l1 := GatedValidRegNext((levelUInt === 1.U) & valid, false.B)
1443ea4388cSHaoyuan Feng        l2 := GatedValidRegNext((levelUInt === 2.U) & valid, false.B)
1453ea4388cSHaoyuan Feng        l3.map(_ := GatedValidRegNext((levelUInt === 3.U) & valid, false.B))
1467797f035SbugGenerator      }
1477797f035SbugGenerator    }
1487797f035SbugGenerator    // duplicate level and sel_pte for each page caches, for better fanout
1497797f035SbugGenerator    val req_info_dup = Vec(3, new L2TlbInnerBundle())
1503ea4388cSHaoyuan Feng    val level_dup = Vec(3, UInt(log2Up(Level + 1).W))
1517797f035SbugGenerator    val sel_pte_dup = Vec(3, UInt(XLEN.W))
1526d5ddbceSLemover  }))
1537797f035SbugGenerator  val sfence_dup = Vec(4, Input(new SfenceBundle()))
1547797f035SbugGenerator  val csr_dup = Vec(3, Input(new TlbCsrBundle()))
1556d5ddbceSLemover}
1566d5ddbceSLemover
1571ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
1586d5ddbceSLemover  val io = IO(new PtwCacheIO)
1597196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
160abc4432bSHaoyuan Feng  val l1EntryType = new PTWEntriesWithEcc(ecc, num = PtwL1SectorSize, tagLen = PtwL1TagLen, level = 1, hasPerm = false, ReservedBits = l2tlbParams.l1ReservedBits)
161abc4432bSHaoyuan Feng  val l0EntryType = new PTWEntriesWithEcc(ecc, num = PtwL0SectorSize, tagLen = PtwL0TagLen, level = 0, hasPerm = true, ReservedBits = l2tlbParams.l0ReservedBits)
1627196f5a2SLemover
1636d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
1646d5ddbceSLemover
1657797f035SbugGenerator  val sfence_dup = io.sfence_dup
1666d5ddbceSLemover  val refill = io.refill.bits
1677797f035SbugGenerator  val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source))
1684ed5afbdSXiaokun-Pei  val refill_h = io.refill.bits.req_info_dup.map(a => Mux(a.s2xlate === allStage, onlyStage1, a.s2xlate))
169d0de7e4aSpeixiaokun  val flush_dup = sfence_dup.zip(io.csr_dup).map(f => f._1.valid || f._2.satp.changed || f._2.vsatp.changed || f._2.hgatp.changed)
1707797f035SbugGenerator  val flush = flush_dup(0)
1716d5ddbceSLemover
1726d5ddbceSLemover  // when refill, refuce to accept new req
1735854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
1743889e11eSLemover
1753889e11eSLemover  // handle hand signal and req_info
1766c4dcc2dSLemover  // TODO: replace with FlushableQueue
1776c4dcc2dSLemover  val stageReq = Wire(Decoupled(new PtwCacheReq()))         // enq stage & read page cache valid
1786c4dcc2dSLemover  val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp
1796c4dcc2dSLemover  val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc
1806c4dcc2dSLemover  val stageResp = Wire(Decoupled(new PtwCacheReq()))         // deq stage
1817797f035SbugGenerator
1827797f035SbugGenerator  val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush)      // catch ram data
1837797f035SbugGenerator  val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter
1847797f035SbugGenerator  val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool()))
1857797f035SbugGenerator  stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush))  // ecc flush
1867797f035SbugGenerator
1876c4dcc2dSLemover  stageReq <> io.req
1886c4dcc2dSLemover  PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad)
1897797f035SbugGenerator  InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle)
1906c4dcc2dSLemover  PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush)
1917797f035SbugGenerator  InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle)
1926c4dcc2dSLemover  PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush)
1936c4dcc2dSLemover  stageResp.ready := !stageResp.valid || io.resp.ready
1946d5ddbceSLemover
1953ea4388cSHaoyuan Feng  // l3: level 3 non-leaf pte
1963ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, new PtwEntry(tagLen = PtwL3TagLen)))) else None
1973ea4388cSHaoyuan Feng  val l3v = if (EnableSv48) Some(RegInit(0.U(l2tlbParams.l3Size.W))) else None
1983ea4388cSHaoyuan Feng  val l3g = if (EnableSv48) Some(Reg(UInt(l2tlbParams.l3Size.W))) else None
1993ea4388cSHaoyuan Feng  val l3asids = if (EnableSv48) Some(l3.get.map(_.asid)) else None
2003ea4388cSHaoyuan Feng  val l3vmids = if (EnableSv48) Some(l3.get.map(_.vmid)) else None
2013ea4388cSHaoyuan Feng  val l3h = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, UInt(2.W)))) else None
2026d5ddbceSLemover
2033ea4388cSHaoyuan Feng  // l2: level 2 non-leaf pte
2043ea4388cSHaoyuan Feng  val l2 = Reg(Vec(l2tlbParams.l2Size, new PtwEntry(tagLen = PtwL2TagLen)))
2053ea4388cSHaoyuan Feng  val l2v = RegInit(0.U(l2tlbParams.l2Size.W))
2063ea4388cSHaoyuan Feng  val l2g = Reg(UInt(l2tlbParams.l2Size.W))
2073ea4388cSHaoyuan Feng  val l2asids = l2.map(_.asid)
2083ea4388cSHaoyuan Feng  val l2vmids = l2.map(_.vmid)
2093ea4388cSHaoyuan Feng  val l2h = Reg(Vec(l2tlbParams.l2Size, UInt(2.W)))
2103ea4388cSHaoyuan Feng
2113ea4388cSHaoyuan Feng  // l1: level 1 non-leaf pte
212abc4432bSHaoyuan Feng  val l1 = Module(new SplittedSRAM(
2133ea4388cSHaoyuan Feng    l1EntryType,
2143ea4388cSHaoyuan Feng    set = l2tlbParams.l1nSets,
2153ea4388cSHaoyuan Feng    way = l2tlbParams.l1nWays,
216d4265a7fSHaoyuan Feng    waySplit = 1,
217abc4432bSHaoyuan Feng    dataSplit = 4,
218abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
219abc4432bSHaoyuan Feng    readMCP2 = false
2206d5ddbceSLemover  ))
2213ea4388cSHaoyuan Feng  val l1v = RegInit(0.U((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2223ea4388cSHaoyuan Feng  val l1g = Reg(UInt((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2233ea4388cSHaoyuan Feng  val l1h = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(2.W))))
2243ea4388cSHaoyuan Feng  def getl1vSet(vpn: UInt) = {
2253ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2263ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2273ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2283ea4388cSHaoyuan Feng    val l1vVec = l1v.asTypeOf(Vec(l2tlbParams.l1nSets, UInt(l2tlbParams.l1nWays.W)))
2293ea4388cSHaoyuan Feng    l1vVec(set)
2306d5ddbceSLemover  }
2313ea4388cSHaoyuan Feng  def getl1hSet(vpn: UInt) = {
2323ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2333ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2343ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2353ea4388cSHaoyuan Feng    l1h(set)
23645f497a4Shappy-lx  }
2376d5ddbceSLemover
2383ea4388cSHaoyuan Feng  // l0: level 0 leaf pte of 4KB pages
239abc4432bSHaoyuan Feng  val l0 = Module(new SplittedSRAM(
2403ea4388cSHaoyuan Feng    l0EntryType,
2413ea4388cSHaoyuan Feng    set = l2tlbParams.l0nSets,
2423ea4388cSHaoyuan Feng    way = l2tlbParams.l0nWays,
243d4265a7fSHaoyuan Feng    waySplit = 2,
244abc4432bSHaoyuan Feng    dataSplit = 4,
245abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
246abc4432bSHaoyuan Feng    readMCP2 = false
2476d5ddbceSLemover  ))
2483ea4388cSHaoyuan Feng  val l0v = RegInit(0.U((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
2493ea4388cSHaoyuan Feng  val l0g = Reg(UInt((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
2503ea4388cSHaoyuan Feng  val l0h = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(2.W))))
2513ea4388cSHaoyuan Feng  def getl0vSet(vpn: UInt) = {
2523ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
2533ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
2543ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
2553ea4388cSHaoyuan Feng    val l0vVec = l0v.asTypeOf(Vec(l2tlbParams.l0nSets, UInt(l2tlbParams.l0nWays.W)))
2563ea4388cSHaoyuan Feng    l0vVec(set)
2576d5ddbceSLemover  }
2583ea4388cSHaoyuan Feng  def getl0hSet(vpn: UInt) = {
2593ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
2603ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
2613ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
2623ea4388cSHaoyuan Feng    l0h(set)
26345f497a4Shappy-lx  }
2646d5ddbceSLemover
2653ea4388cSHaoyuan Feng  // sp: level 1/2/3 leaf pte of 512GB/1GB/2MB super pages
266718a93f5SHaoyuan Feng  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true, hasNapot = true)))
2675854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
2685854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
2691dd3e32dSHaoyuan Feng  val spasids = sp.map(_.asid)
270d0de7e4aSpeixiaokun  val spvmids = sp.map(_.vmid)
271d61cd5eeSpeixiaokun  val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W)))
2726d5ddbceSLemover
2736d5ddbceSLemover  // Access Perf
2743ea4388cSHaoyuan Feng  val l3AccessPerf = if(EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
2753ea4388cSHaoyuan Feng  val l2AccessPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
2763ea4388cSHaoyuan Feng  val l1AccessPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
2773ea4388cSHaoyuan Feng  val l0AccessPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
2785854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
2793ea4388cSHaoyuan Feng  if (EnableSv48) l3AccessPerf.map(_.map(_ := false.B))
2806d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
2813ea4388cSHaoyuan Feng  l1AccessPerf.map(_ := false.B)
2823ea4388cSHaoyuan Feng  l0AccessPerf.map(_ := false.B)
2836d5ddbceSLemover  spAccessPerf.map(_ := false.B)
2846d5ddbceSLemover
2853889e11eSLemover
2861f4a7c0cSLemover
28782978df9Speixiaokun  def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = {
2883ea4388cSHaoyuan Feng    (vpn1(vpnLen-1, vpnnLen*level+3) === vpn2(vpnLen-1, vpnnLen*level+3))
2891f4a7c0cSLemover  }
2901f4a7c0cSLemover  // NOTE: not actually bypassed, just check if hit, re-access the page cache
291d0de7e4aSpeixiaokun  def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = {
2926f508cb5Speixiaokun    val change_h = MuxLookup(h_search, noS2xlate)(Seq(
293980ddf4cSpeixiaokun      allStage -> onlyStage1,
294980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
295980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
296980ddf4cSpeixiaokun    ))
2974ed5afbdSXiaokun-Pei    val change_refill_h = MuxLookup(io.refill.bits.req_info_dup(0).s2xlate, noS2xlate)(Seq(
2984ed5afbdSXiaokun-Pei      allStage -> onlyStage1,
2994ed5afbdSXiaokun-Pei      onlyStage1 -> onlyStage1,
3004ed5afbdSXiaokun-Pei      onlyStage2 -> onlyStage2
3014ed5afbdSXiaokun-Pei    ))
302d0de7e4aSpeixiaokun    val refill_vpn = io.refill.bits.req_info_dup(0).vpn
3034ed5afbdSXiaokun-Pei    io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === change_refill_h
3041f4a7c0cSLemover  }
3051f4a7c0cSLemover
30682978df9Speixiaokun  val vpn_search = stageReq.bits.req_info.vpn
3076f508cb5Speixiaokun  val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq(
30809280d15Speixiaokun    allStage -> onlyStage1,
30909280d15Speixiaokun    onlyStage1 -> onlyStage1,
31009280d15Speixiaokun    onlyStage2 -> onlyStage2
31109280d15Speixiaokun  ))
3123ea4388cSHaoyuan Feng
3133ea4388cSHaoyuan Feng  // l3
3143ea4388cSHaoyuan Feng  val l3Hit = if(EnableSv48) Some(Wire(Bool())) else None
3153ea4388cSHaoyuan Feng  val l3HitPPN = if(EnableSv48) Some(Wire(UInt(ppnLen.W))) else None
316002c10a4SYanqin Li  val l3HitPbmt = if(EnableSv48) Some(Wire(UInt(ptePbmtLen.W))) else None
3173ea4388cSHaoyuan Feng  val l3Pre = if(EnableSv48) Some(Wire(Bool())) else None
3183ea4388cSHaoyuan Feng  val ptwl3replace = if(EnableSv48) Some(ReplacementPolicy.fromString(l2tlbParams.l3Replacer, l2tlbParams.l3Size)) else None
3193ea4388cSHaoyuan Feng  if (EnableSv48) {
3203ea4388cSHaoyuan Feng    val hitVecT = l3.get.zipWithIndex.map {
32197929664SXiaokun-Pei        case (e, i) => (e.hit(vpn_search, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.vmid, s2xlate = h_search =/= noS2xlate)
3223ea4388cSHaoyuan Feng          && l3v.get(i) && h_search === l3h.get(i))
323d0de7e4aSpeixiaokun    }
3246c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
3251f4a7c0cSLemover
3263ea4388cSHaoyuan Feng    // stageDelay, but check for l3
3273ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.ppn)), stageDelay_valid_1cycle)
328002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.pbmt)), stageDelay_valid_1cycle)
3293ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.prefetch)), stageDelay_valid_1cycle)
3301f4a7c0cSLemover    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
3316d5ddbceSLemover
3323ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl3replace.get.access(OHToUInt(hitVec)) }
3336d5ddbceSLemover
3343ea4388cSHaoyuan Feng    l3AccessPerf.get.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
3353ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l3Size) {
33697929664SXiaokun-Pei      XSDebug(stageReq.fire, p"[l3] l3(${i.U}) ${l3.get(i)} hit:${l3.get(i).hit(vpn_search, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.vmid, s2xlate = h_search =/= noS2xlate)}\n")
3376d5ddbceSLemover    }
3383ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l3] l3v:${Binary(l3v.get)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
3393ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l3] l3Hit:${hit} l3HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
3406d5ddbceSLemover
3413ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l3_hitVecT")
3423ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l3_hitVec")
3433ea4388cSHaoyuan Feng
3443ea4388cSHaoyuan Feng    // synchronize with other entries with RegEnable
3453ea4388cSHaoyuan Feng    l3Hit.map(_ := RegEnable(hit, stageDelay(1).fire))
3463ea4388cSHaoyuan Feng    l3HitPPN.map(_ := RegEnable(hitPPN, stageDelay(1).fire))
347002c10a4SYanqin Li    l3HitPbmt.map(_ := RegEnable(hitPbmt, stageDelay(1).fire))
3483ea4388cSHaoyuan Feng    l3Pre.map(_ := RegEnable(hitPre, stageDelay(1).fire))
3493ea4388cSHaoyuan Feng  }
3503ea4388cSHaoyuan Feng
3513ea4388cSHaoyuan Feng  // l2
3523ea4388cSHaoyuan Feng  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer, l2tlbParams.l2Size)
353002c10a4SYanqin Li  val (l2Hit, l2HitPPN, l2HitPbmt, l2Pre) = {
3543ea4388cSHaoyuan Feng    val hitVecT = l2.zipWithIndex.map {
35597929664SXiaokun-Pei      case (e, i) => (e.hit(vpn_search, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.vmid, s2xlate = h_search =/= noS2xlate)
3563ea4388cSHaoyuan Feng        && l2v(i) && h_search === l2h(i))
3573ea4388cSHaoyuan Feng    }
3583ea4388cSHaoyuan Feng    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
3593ea4388cSHaoyuan Feng
3603ea4388cSHaoyuan Feng    // stageDelay, but check for l2
3613ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.ppn)), stageDelay_valid_1cycle)
362002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.pbmt)), stageDelay_valid_1cycle)
3633ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.prefetch)), stageDelay_valid_1cycle)
3643ea4388cSHaoyuan Feng    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
3653ea4388cSHaoyuan Feng
3663ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl2replace.access(OHToUInt(hitVec)) }
3673ea4388cSHaoyuan Feng
3683ea4388cSHaoyuan Feng    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
3693ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
37097929664SXiaokun-Pei      XSDebug(stageReq.fire, p"[l2] l2(${i.U}) ${l2(i)} hit:${l2(i).hit(vpn_search, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.vmid, s2xlate = h_search =/= noS2xlate)}\n")
3713ea4388cSHaoyuan Feng    }
3723ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l2] l2v:${Binary(l2v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
3733ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
3743ea4388cSHaoyuan Feng
3753ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l2_hitVecT")
3763ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l2_hitVec")
3776d5ddbceSLemover
3786c4dcc2dSLemover    // synchronize with other entries with RegEnable
3796c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
3806c4dcc2dSLemover     RegEnable(hitPPN, stageDelay(1).fire),
381002c10a4SYanqin Li     RegEnable(hitPbmt, stageDelay(1).fire),
3826c4dcc2dSLemover     RegEnable(hitPre, stageDelay(1).fire))
3836d5ddbceSLemover  }
3846d5ddbceSLemover
3853ea4388cSHaoyuan Feng  // l1
3863ea4388cSHaoyuan Feng  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer,l2tlbParams.l1nWays,l2tlbParams.l1nSets)
387002c10a4SYanqin Li  val (l1Hit, l1HitPPN, l1HitPbmt, l1Pre, l1eccError) = {
3883ea4388cSHaoyuan Feng    val ridx = genPtwL1SetIdx(vpn_search)
3893ea4388cSHaoyuan Feng    l1.io.r.req.valid := stageReq.fire
3903ea4388cSHaoyuan Feng    l1.io.r.req.bits.apply(setIdx = ridx)
3913ea4388cSHaoyuan Feng    val vVec_req = getl1vSet(vpn_search)
3923ea4388cSHaoyuan Feng    val hVec_req = getl1hSet(vpn_search)
3936c4dcc2dSLemover
3946c4dcc2dSLemover    // delay one cycle after sram read
39582978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
3966f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
397980ddf4cSpeixiaokun      allStage -> onlyStage1,
398980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
399980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
400980ddf4cSpeixiaokun    ))
4013ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l1.io.r.resp.data, stageDelay_valid_1cycle)
4027797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
403d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
404d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
40597929664SXiaokun-Pei      wayData.entries.hit(delay_vpn, io.csr_dup(1).satp.asid, io.csr_dup(1).vsatp.asid, io.csr_dup(1).hgatp.vmid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)})
4066c4dcc2dSLemover
4076c4dcc2dSLemover    // check hit and ecc
40882978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
4096c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
410935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
4116c4dcc2dSLemover
4127797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
4137196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
4147196f5a2SLemover    val hitWayData = hitWayEntry.entries
4156c4dcc2dSLemover    val hit = ParallelOR(hitVec)
4163ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l1nWays).map(_.U(log2Up(l2tlbParams.l1nWays).W)))
417eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
418eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
419eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
420eef81af7SHaoyuan Feng    } else {
421eef81af7SHaoyuan Feng      eccError := false.B
422eef81af7SHaoyuan Feng    }
4237196f5a2SLemover
4243ea4388cSHaoyuan Feng    ridx.suggestName(s"l1_ridx")
4253ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l1_ramDatas")
4263ea4388cSHaoyuan Feng    hitVec.suggestName(s"l1_hitVec")
4273ea4388cSHaoyuan Feng    hitWayData.suggestName(s"l1_hitWayData")
4283ea4388cSHaoyuan Feng    hitWay.suggestName(s"l1_hitWay")
4296d5ddbceSLemover
4303ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl1replace.access(genPtwL1SetIdx(check_vpn), hitWay) }
4316d5ddbceSLemover
4323ea4388cSHaoyuan Feng    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
4333ea4388cSHaoyuan Feng    XSDebug(stageDelay_valid_1cycle, p"[l1] ridx:0x${Hexadecimal(ridx)}\n")
4343ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
4353ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l1] ramDatas(${i.U}) ${ramDatas(i)}  l1v:${vVec(i)}  hit:${hit}\n")
4366d5ddbceSLemover    }
4373ea4388cSHaoyuan Feng    XSDebug(stageCheck_valid_1cycle, p"[l1] l1Hit:${hit} l1HitPPN:0x${Hexadecimal(hitWayData.ppns(genPtwL1SectorIdx(check_vpn)))} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${vVec}\n")
4386d5ddbceSLemover
439002c10a4SYanqin Li    (hit, hitWayData.ppns(genPtwL1SectorIdx(check_vpn)), hitWayData.pbmts(genPtwL1SectorIdx(check_vpn)), hitWayData.prefetch, eccError)
4406d5ddbceSLemover  }
4416d5ddbceSLemover
442b32e9518SHuijin Li  val l0_masked_clock = ClockGate(false.B, stageReq.fire | (!flush_dup(0) && refill.levelOH.l0), clock)
443b32e9518SHuijin Li  val l1_masked_clock = ClockGate(false.B, stageReq.fire | (!flush_dup(1) && refill.levelOH.l1), clock)
444b32e9518SHuijin Li  l0.clock := l0_masked_clock
445b32e9518SHuijin Li  l1.clock := l1_masked_clock
4463ea4388cSHaoyuan Feng  // l0
4473ea4388cSHaoyuan Feng  val ptwl0replace = ReplacementPolicy.fromString(l2tlbParams.l0Replacer,l2tlbParams.l0nWays,l2tlbParams.l0nSets)
4483ea4388cSHaoyuan Feng  val (l0Hit, l0HitData, l0Pre, l0eccError) = {
4493ea4388cSHaoyuan Feng    val ridx = genPtwL0SetIdx(vpn_search)
4503ea4388cSHaoyuan Feng    l0.io.r.req.valid := stageReq.fire
4513ea4388cSHaoyuan Feng    l0.io.r.req.bits.apply(setIdx = ridx)
4523ea4388cSHaoyuan Feng    val vVec_req = getl0vSet(vpn_search)
4533ea4388cSHaoyuan Feng    val hVec_req = getl0hSet(vpn_search)
4546c4dcc2dSLemover
4556c4dcc2dSLemover    // delay one cycle after sram read
45682978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
4576f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
458980ddf4cSpeixiaokun      allStage -> onlyStage1,
459980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
460980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
461980ddf4cSpeixiaokun    ))
4623ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l0.io.r.resp.data, stageDelay_valid_1cycle)
4637797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
464d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
465d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
46697929664SXiaokun-Pei      wayData.entries.hit(delay_vpn, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.vmid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)})
4676c4dcc2dSLemover
4686c4dcc2dSLemover    // check hit and ecc
46982978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
4706c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
471935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
4726c4dcc2dSLemover
4737797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
4747196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
4757196f5a2SLemover    val hitWayData = hitWayEntry.entries
4767196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
4776c4dcc2dSLemover    val hit = ParallelOR(hitVec)
4783ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l0nWays).map(_.U(log2Up(l2tlbParams.l0nWays).W)))
479eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
480eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
481eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
482eef81af7SHaoyuan Feng    } else {
483eef81af7SHaoyuan Feng      eccError := false.B
484eef81af7SHaoyuan Feng    }
4856d5ddbceSLemover
4863ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl0replace.access(genPtwL0SetIdx(check_vpn), hitWay) }
4877196f5a2SLemover
4883ea4388cSHaoyuan Feng    l0AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
4893ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l0] ridx:0x${Hexadecimal(ridx)}\n")
4903ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
4913ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l0] ramDatas(${i.U}) ${ramDatas(i)}  l0v:${vVec(i)}  hit:${hitVec(i)}\n")
4926d5ddbceSLemover    }
4933ea4388cSHaoyuan Feng    XSDebug(stageCheck_valid_1cycle, p"[l0] l0Hit:${hit} l0HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n")
4946d5ddbceSLemover
4953ea4388cSHaoyuan Feng    ridx.suggestName(s"l0_ridx")
4963ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l0_ramDatas")
4973ea4388cSHaoyuan Feng    hitVec.suggestName(s"l0_hitVec")
4983ea4388cSHaoyuan Feng    hitWay.suggestName(s"l0_hitWay")
4996d5ddbceSLemover
5003889e11eSLemover    (hit, hitWayData, hitWayData.prefetch, eccError)
5016d5ddbceSLemover  }
5023ea4388cSHaoyuan Feng  val l0HitPPN = l0HitData.ppns
503002c10a4SYanqin Li  val l0HitPbmt = l0HitData.pbmts
5043ea4388cSHaoyuan Feng  val l0HitPerm = l0HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL0SectorSize, new PtePermBundle)))
505e0c1f271SHaoyuan Feng  val l0HitValid = VecInit(l0HitData.onlypf.map(!_))
5066d5ddbceSLemover
5076d5ddbceSLemover  // super page
5085854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
5098d8ac704SLemover  val (spHit, spHitData, spPre, spValid) = {
510718a93f5SHaoyuan Feng    val hitVecT = sp.zipWithIndex.map { case (e, i) => e.hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.vmid, allType = true, s2xlate = h_search =/= noS2xlate) && spv(i) && (sph(i) === h_search) }
5116c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
5126d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
5136c4dcc2dSLemover    val hit = ParallelOR(hitVec)
5146d5ddbceSLemover
5156c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) }
5166d5ddbceSLemover
5176c4dcc2dSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle }
5185854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
51997929664SXiaokun-Pei      XSDebug(stageReq.fire, p"[sp] sp(${i.U}) ${sp(i)} hit:${sp(i).hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.vmid, s2xlate = h_search =/= noS2xlate)} spv:${spv(i)}\n")
5206d5ddbceSLemover    }
5216c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
5226d5ddbceSLemover
5236d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
5246d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
5256d5ddbceSLemover
5266c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
5276c4dcc2dSLemover     RegEnable(hitData, stageDelay(1).fire),
5286c4dcc2dSLemover     RegEnable(hitData.prefetch, stageDelay(1).fire),
529935edac4STang Haojin     RegEnable(hitData.v, stageDelay(1).fire))
5306d5ddbceSLemover  }
5316d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
5326d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
5336d5ddbceSLemover
5346c4dcc2dSLemover  val check_res = Wire(new PageCacheRespBundle)
5353ea4388cSHaoyuan Feng  check_res.l3.map(_.apply(l3Hit.get, l3Pre.get, l3HitPPN.get))
536002c10a4SYanqin Li  check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, l2HitPbmt)
537002c10a4SYanqin Li  check_res.l1.apply(l1Hit, l1Pre, l1HitPPN, l1HitPbmt, ecc = l1eccError)
538e0c1f271SHaoyuan Feng  check_res.l0.apply(l0Hit, l0Pre, l0HitPPN, l0HitPbmt, l0HitPerm, l0eccError, valid = l0HitValid)
539718a93f5SHaoyuan Feng  check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitData.pbmt, spHitData.n.getOrElse(0.U), spHitPerm, false.B, spHitLevel, spValid)
5406d5ddbceSLemover
5416c4dcc2dSLemover  val resp_res = Reg(new PageCacheRespBundle)
5426c4dcc2dSLemover  when (stageCheck(1).fire) { resp_res := check_res }
5433889e11eSLemover
5441f4a7c0cSLemover  // stageResp bypass
5453ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
5461f4a7c0cSLemover  bypassed.indices.foreach(i =>
5471f4a7c0cSLemover    bypassed(i) := stageResp.bits.bypassed(i) ||
5486967f5d5Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
5491f4a7c0cSLemover        OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid)
5501f4a7c0cSLemover  )
5511f4a7c0cSLemover
55283d93d53Speixiaokun  // stageResp bypass to hptw
5533ea4388cSHaoyuan Feng  val hptw_bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
55483d93d53Speixiaokun  hptw_bypassed.indices.foreach(i =>
55583d93d53Speixiaokun    hptw_bypassed(i) := stageResp.bits.bypassed(i) ||
55683d93d53Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
55783d93d53Speixiaokun        io.resp.fire)
55883d93d53Speixiaokun  )
55983d93d53Speixiaokun
56030104977Speixiaokun  val isAllStage = stageResp.bits.req_info.s2xlate === allStage
561c0991f6aSpeixiaokun  val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2
5623ea4388cSHaoyuan Feng  val stage1Hit = (resp_res.l0.hit || resp_res.sp.hit) && isAllStage
563da605600Speixiaokun  val idx = stageResp.bits.req_info.vpn(2, 0)
5643ea4388cSHaoyuan Feng  val stage1Pf = !Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
5656c4dcc2dSLemover  io.resp.bits.req_info   := stageResp.bits.req_info
5666c4dcc2dSLemover  io.resp.bits.isFirst  := stageResp.bits.isFirst
5673ea4388cSHaoyuan Feng  io.resp.bits.hit      := (resp_res.l0.hit || resp_res.sp.hit) && (!isAllStage || isAllStage && stage1Pf)
5683ea4388cSHaoyuan Feng  if (EnableSv48) {
56926175c3fSHaoyuan Feng    io.resp.bits.bypassed := ((bypassed(0) && !resp_res.l0.hit) || (bypassed(1) && !resp_res.l1.hit) || (bypassed(2) && !resp_res.l2.hit) || (bypassed(3) && !resp_res.l3.get.hit)) && !isAllStage
5703ea4388cSHaoyuan Feng  } else {
57126175c3fSHaoyuan Feng    io.resp.bits.bypassed := ((bypassed(0) && !resp_res.l0.hit) || (bypassed(1) && !resp_res.l1.hit) || (bypassed(2) && !resp_res.l2.hit)) && !isAllStage
5723ea4388cSHaoyuan Feng  }
5733ea4388cSHaoyuan Feng  io.resp.bits.prefetch := resp_res.l0.pre && resp_res.l0.hit || resp_res.sp.pre && resp_res.sp.hit
5743ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l3Hit.map(_ := resp_res.l3.get.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq)
575325f0a4eSpeixiaokun  io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
5763ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
5773ea4388cSHaoyuan Feng  io.resp.bits.toFsm.ppn   := Mux(resp_res.l1.hit, resp_res.l1.ppn, Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l3.getOrElse(0.U.asTypeOf(new PageCachePerPespBundle)).ppn))
578980ddf4cSpeixiaokun  io.resp.bits.toFsm.stage1Hit := stage1Hit
579d0de7e4aSpeixiaokun
580325f0a4eSpeixiaokun  io.resp.bits.isHptwReq := stageResp.bits.isHptwReq
5813ea4388cSHaoyuan Feng  if (EnableSv48) {
58226175c3fSHaoyuan Feng    io.resp.bits.toHptw.bypassed := ((hptw_bypassed(0) && !resp_res.l0.hit) || (hptw_bypassed(1) && !resp_res.l1.hit) || (hptw_bypassed(2) && !resp_res.l2.hit) || (hptw_bypassed(3) && !resp_res.l3.get.hit)) && stageResp.bits.isHptwReq
5833ea4388cSHaoyuan Feng  } else {
58426175c3fSHaoyuan Feng    io.resp.bits.toHptw.bypassed := ((hptw_bypassed(0) && !resp_res.l0.hit) || (hptw_bypassed(1) && !resp_res.l1.hit) || (hptw_bypassed(2) && !resp_res.l2.hit)) && stageResp.bits.isHptwReq
5853ea4388cSHaoyuan Feng  }
586d0de7e4aSpeixiaokun  io.resp.bits.toHptw.id := stageResp.bits.hptwId
5873ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l3Hit.map(_ := resp_res.l3.get.hit && stageResp.bits.isHptwReq)
588325f0a4eSpeixiaokun  io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq
5893ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq
5903ea4388cSHaoyuan Feng  io.resp.bits.toHptw.ppn := Mux(resp_res.l1.hit, resp_res.l1.ppn, Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l3.getOrElse(0.U.asTypeOf(new PageCachePerPespBundle)).ppn))(ppnLen - 1, 0)
59182978df9Speixiaokun  io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn
592eb4bf3f2Speixiaokun  io.resp.bits.toHptw.resp.entry.asid := DontCare
59397929664SXiaokun-Pei  io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.vmid)
5943ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l0.hit, 0.U, resp_res.sp.level))
595d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source)
5963ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(idx), resp_res.sp.ppn)(ppnLen - 1, 0)
597002c10a4SYanqin Li  io.resp.bits.toHptw.resp.entry.pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(idx), resp_res.sp.pbmt)
598718a93f5SHaoyuan Feng  io.resp.bits.toHptw.resp.entry.n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
5993ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l0.hit, resp_res.l0.perm(idx), resp_res.sp.perm))
6003ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
601d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v
602e0c1f271SHaoyuan Feng  io.resp.bits.toHptw.resp.gaf := false.B
603d0de7e4aSpeixiaokun
6046979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3))
6056979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.asid := Mux(stageResp.bits.req_info.hasS2xlate(), io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid)) // DontCare
60697929664SXiaokun-Pei  io.resp.bits.stage1.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.vmid))
6073ea4388cSHaoyuan Feng  if (EnableSv48) {
6083ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
6093ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
6103ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U,
6113ea4388cSHaoyuan Feng          Mux(resp_res.l2.hit, 2.U, 3.U))))))
6123ea4388cSHaoyuan Feng  } else {
6133ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
6143ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
6153ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U, 2.U)))))
6163ea4388cSHaoyuan Feng  }
6176979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source))
61863632028SHaoyuan Feng  for (i <- 0 until tlbcontiguous) {
6193ea4388cSHaoyuan Feng    if (EnableSv48) {
6203ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
6213ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
6223ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
6233ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth),
6243ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(gvpnLen - 1, sectortlbwidth)))))
6253ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
6263ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
6273ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
6283ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(sectortlbwidth - 1, 0),
6293ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(sectortlbwidth - 1, 0)))))
6303ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
6313ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
6323ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
6333ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.v,
6343ea4388cSHaoyuan Feng              resp_res.l3.get.v))))
6353ea4388cSHaoyuan Feng    } else {
6363ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
6373ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
6383ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
6393ea4388cSHaoyuan Feng            resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth))))
6403ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
6413ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
6423ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
6433ea4388cSHaoyuan Feng            resp_res.l2.ppn(sectortlbwidth - 1, 0))))
6443ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
6453ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
6463ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
6473ea4388cSHaoyuan Feng            resp_res.l2.v)))
6483ea4388cSHaoyuan Feng    }
649002c10a4SYanqin Li    io.resp.bits.stage1.entry(i).pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(i),
650002c10a4SYanqin Li      Mux(resp_res.sp.hit, resp_res.sp.pbmt,
651002c10a4SYanqin Li        Mux(resp_res.l1.hit, resp_res.l1.pbmt,
652002c10a4SYanqin Li          resp_res.l2.pbmt)))
653718a93f5SHaoyuan Feng    io.resp.bits.stage1.entry(i).n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
65497929664SXiaokun-Pei    io.resp.bits.stage1.entry(i).perm.map(_ := Mux(resp_res.l0.hit, resp_res.l0.perm(i),  Mux(resp_res.sp.hit, resp_res.sp.perm, 0.U.asTypeOf(new PtePermBundle))))
6556979864eSXiaokun-Pei    io.resp.bits.stage1.entry(i).pf := !io.resp.bits.stage1.entry(i).v
656e0c1f271SHaoyuan Feng    io.resp.bits.stage1.entry(i).af := false.B
65763632028SHaoyuan Feng  }
658da605600Speixiaokun  io.resp.bits.stage1.pteidx := UIntToOH(idx).asBools
6593ea4388cSHaoyuan Feng  io.resp.bits.stage1.not_super := Mux(resp_res.l0.hit, true.B, false.B)
6606962b4ffSHaoyuan Feng  io.resp.bits.stage1.not_merge := false.B
6616c4dcc2dSLemover  io.resp.valid := stageResp.valid
6623ea4388cSHaoyuan Feng  XSError(stageResp.valid && resp_res.l0.hit && resp_res.sp.hit, "normal page and super page both hit")
6636d5ddbceSLemover
6646d5ddbceSLemover  // refill Perf
6653ea4388cSHaoyuan Feng  val l3RefillPerf = if (EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
6663ea4388cSHaoyuan Feng  val l2RefillPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
6673ea4388cSHaoyuan Feng  val l1RefillPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
6683ea4388cSHaoyuan Feng  val l0RefillPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
6695854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
6703ea4388cSHaoyuan Feng  l3RefillPerf.map(_.map(_ := false.B))
6716d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
6723ea4388cSHaoyuan Feng  l1RefillPerf.map(_ := false.B)
6733ea4388cSHaoyuan Feng  l0RefillPerf.map(_ := false.B)
6746d5ddbceSLemover  spRefillPerf.map(_ := false.B)
6756d5ddbceSLemover
6766d5ddbceSLemover  // refill
6773ea4388cSHaoyuan Feng  l1.io.w.req <> DontCare
6783ea4388cSHaoyuan Feng  l0.io.w.req <> DontCare
6793ea4388cSHaoyuan Feng  l1.io.w.req.valid := false.B
6803ea4388cSHaoyuan Feng  l0.io.w.req.valid := false.B
6816d5ddbceSLemover
6826d5ddbceSLemover  val memRdata = refill.ptes
6835854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
6847797f035SbugGenerator  val memSelData = io.refill.bits.sel_pte_dup
6857797f035SbugGenerator  val memPte = memSelData.map(a => a.asTypeOf(new PteBundle))
686dd286b6aSYanqin Li  val mPBMTE = io.csr.mPBMTE
687dd286b6aSYanqin Li  val hPBMTE = io.csr.hPBMTE
688dd286b6aSYanqin Li  val pbmte = Mux(refill.req_info_dup(0).s2xlate === onlyStage1 || refill.req_info_dup(0).s2xlate === allStage, hPBMTE, mPBMTE)
689b848eea5SLemover
6906d5ddbceSLemover  // TODO: handle sfenceLatch outsize
6913ea4388cSHaoyuan Feng  if (EnableSv48) {
6928b33cd30Sklin02    val l3Refill =
6936962b4ffSHaoyuan Feng      !flush_dup(2) &&
6946962b4ffSHaoyuan Feng      refill.levelOH.l3.get &&
6956962b4ffSHaoyuan Feng      !memPte(2).isLeaf() &&
696e0c1f271SHaoyuan Feng      memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
6978b33cd30Sklin02    val l3RefillIdx = replaceWrapper(l3v.get, ptwl3replace.get.way).suggestName(s"l3_refillIdx")
6988b33cd30Sklin02    val l3RfOH = UIntToOH(l3RefillIdx).asUInt.suggestName(s"l3_rfOH")
6998b33cd30Sklin02    when (l3Refill) {
7008b33cd30Sklin02      l3.get(l3RefillIdx).refill(
7013ea4388cSHaoyuan Feng        refill.req_info_dup(2).vpn,
7023ea4388cSHaoyuan Feng        Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
70397929664SXiaokun-Pei        io.csr_dup(2).hgatp.vmid,
7043ea4388cSHaoyuan Feng        memSelData(2),
7053ea4388cSHaoyuan Feng        3.U,
7063ea4388cSHaoyuan Feng        refill_prefetch_dup(2)
70745f497a4Shappy-lx      )
7088b33cd30Sklin02      ptwl2replace.access(l3RefillIdx)
7098b33cd30Sklin02      l3v.get := l3v.get | l3RfOH
7108b33cd30Sklin02      l3g.get := (l3g.get & ~l3RfOH) | Mux(memPte(2).perm.g, l3RfOH, 0.U)
7118b33cd30Sklin02      l3h.get(l3RefillIdx) := refill_h(2)
7126d5ddbceSLemover
7133ea4388cSHaoyuan Feng      for (i <- 0 until l2tlbParams.l3Size) {
7148b33cd30Sklin02        l3RefillPerf.get(i) := i.U === l3RefillIdx
7156d5ddbceSLemover      }
7163ea4388cSHaoyuan Feng    }
7178b33cd30Sklin02    XSDebug(l3Refill, p"[l3 refill] refillIdx:${l3RefillIdx} refillEntry:${l3.get(l3RefillIdx).genPtwEntry(refill.req_info_dup(2).vpn, Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid), memSelData(2), 0.U, prefetch = refill_prefetch_dup(2))}\n")
7188b33cd30Sklin02    XSDebug(l3Refill, p"[l3 refill] l3v:${Binary(l3v.get)}->${Binary(l3v.get | l3RfOH)} l3g:${Binary(l3g.get)}->${Binary((l3g.get & ~l3RfOH) | Mux(memPte(2).perm.g, l3RfOH, 0.U))}\n")
7196d5ddbceSLemover  }
7206d5ddbceSLemover
7216962b4ffSHaoyuan Feng  // L2 refill
7228b33cd30Sklin02  val l2Refill =
7236962b4ffSHaoyuan Feng    !flush_dup(2) &&
7246962b4ffSHaoyuan Feng    refill.levelOH.l2 &&
7256962b4ffSHaoyuan Feng    !memPte(2).isLeaf() &&
726e0c1f271SHaoyuan Feng    memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
7278b33cd30Sklin02  val l2RefillIdx = replaceWrapper(l2v, ptwl2replace.way).suggestName(s"l2_refillIdx")
7288b33cd30Sklin02  val l2RfOH = UIntToOH(l2RefillIdx).asUInt.suggestName(s"l2_rfOH")
7298b33cd30Sklin02  when (
7308b33cd30Sklin02    l2Refill
731dd286b6aSYanqin Li  ) {
7328b33cd30Sklin02    l2(l2RefillIdx).refill(
7333ea4388cSHaoyuan Feng      refill.req_info_dup(2).vpn,
7343ea4388cSHaoyuan Feng      Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
73597929664SXiaokun-Pei      io.csr_dup(2).hgatp.vmid,
7363ea4388cSHaoyuan Feng      memSelData(2),
7373ea4388cSHaoyuan Feng      2.U,
7383ea4388cSHaoyuan Feng      refill_prefetch_dup(2)
7393ea4388cSHaoyuan Feng    )
7408b33cd30Sklin02    ptwl2replace.access(l2RefillIdx)
7418b33cd30Sklin02    l2v := l2v | l2RfOH
7428b33cd30Sklin02    l2g := (l2g & ~l2RfOH) | Mux(memPte(2).perm.g, l2RfOH, 0.U)
7438b33cd30Sklin02    l2h(l2RefillIdx) := refill_h(2)
7443ea4388cSHaoyuan Feng
7453ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
7468b33cd30Sklin02      l2RefillPerf(i) := i.U === l2RefillIdx
7473ea4388cSHaoyuan Feng    }
7483ea4388cSHaoyuan Feng  }
7498b33cd30Sklin02  XSDebug(l2Refill, p"[l2 refill] refillIdx:${l2RefillIdx} refillEntry:${l2(l2RefillIdx).genPtwEntry(refill.req_info_dup(2).vpn, Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid), memSelData(2), 0.U, prefetch = refill_prefetch_dup(2))}\n")
7508b33cd30Sklin02  XSDebug(l2Refill, p"[l2 refill] l2v:${Binary(l2v)}->${Binary(l2v | l2RfOH)} l2g:${Binary(l2g)}->${Binary((l2g & ~l2RfOH) | Mux(memPte(2).perm.g, l2RfOH, 0.U))}\n")
7513ea4388cSHaoyuan Feng
7526962b4ffSHaoyuan Feng  // L1 refill
7538b33cd30Sklin02  val l1Refill = !flush_dup(1) && refill.levelOH.l1
7548b33cd30Sklin02  val l1RefillIdx = genPtwL1SetIdx(refill.req_info_dup(1).vpn).suggestName(s"l1_refillIdx")
7558b33cd30Sklin02  val l1VictimWay = replaceWrapper(getl1vSet(refill.req_info_dup(1).vpn), ptwl1replace.way(l1RefillIdx)).suggestName(s"l1_victimWay")
7568b33cd30Sklin02  val l1VictimWayOH = UIntToOH(l1VictimWay).suggestName(s"l1_victimWayOH")
7578b33cd30Sklin02  val l1RfvOH = UIntToOH(Cat(l1RefillIdx, l1VictimWay)).asUInt.suggestName(s"l1_rfvOH")
7588b33cd30Sklin02  val l1Wdata = Wire(l1EntryType)
7598b33cd30Sklin02  l1Wdata.gen(
76082978df9Speixiaokun    vpn = refill.req_info_dup(1).vpn,
761cca17e78Speixiaokun    asid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid),
76297929664SXiaokun-Pei    vmid = io.csr_dup(1).hgatp.vmid,
76345f497a4Shappy-lx    data = memRdata,
76445f497a4Shappy-lx    levelUInt = 1.U,
7654ed5afbdSXiaokun-Pei    refill_prefetch_dup(1),
766dd286b6aSYanqin Li    refill.req_info_dup(1).s2xlate,
767e0c1f271SHaoyuan Feng    pbmte,
768e0c1f271SHaoyuan Feng    io.csr_dup(1).vsatp.mode
76945f497a4Shappy-lx  )
7708b33cd30Sklin02  when (l1Refill) {
7713ea4388cSHaoyuan Feng    l1.io.w.apply(
7726d5ddbceSLemover      valid = true.B,
7738b33cd30Sklin02      setIdx = l1RefillIdx,
7748b33cd30Sklin02      data = l1Wdata,
7758b33cd30Sklin02      waymask = l1VictimWayOH
7766d5ddbceSLemover    )
7778b33cd30Sklin02    ptwl1replace.access(l1RefillIdx, l1VictimWay)
7788b33cd30Sklin02    l1v := l1v | l1RfvOH
7798b33cd30Sklin02    l1g := l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U)
7808b33cd30Sklin02    l1h(l1RefillIdx)(l1VictimWay) := refill_h(1)
7816d5ddbceSLemover
7823ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
7838b33cd30Sklin02      l1RefillPerf(i) := i.U === l1VictimWay
7846d5ddbceSLemover    }
7856d5ddbceSLemover  }
7868b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refillIdx:0x${Hexadecimal(l1RefillIdx)} victimWay:${l1VictimWay} victimWayOH:${Binary(l1VictimWayOH)} rfvOH(in UInt):${Cat(l1RefillIdx, l1VictimWay)}\n")
7878b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refilldata:0x${l1Wdata}\n")
7888b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1v:${Binary(l1v)} -> ${Binary(l1v | l1RfvOH)}\n")
7898b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1g:${Binary(l1g)} -> ${Binary(l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U))}\n")
7906d5ddbceSLemover
7916962b4ffSHaoyuan Feng  // L0 refill
792718a93f5SHaoyuan Feng  val l0Refill = !flush_dup(0) && refill.levelOH.l0 && !memPte(0).isNapot(refill.level_dup(0))
7938b33cd30Sklin02  val l0RefillIdx = genPtwL0SetIdx(refill.req_info_dup(0).vpn).suggestName(s"l0_refillIdx")
7948b33cd30Sklin02  val l0VictimWay = replaceWrapper(getl0vSet(refill.req_info_dup(0).vpn), ptwl0replace.way(l0RefillIdx)).suggestName(s"l0_victimWay")
7958b33cd30Sklin02  val l0VictimWayOH = UIntToOH(l0VictimWay).asUInt.suggestName(s"l0_victimWayOH")
7968b33cd30Sklin02  val l0RfvOH = UIntToOH(Cat(l0RefillIdx, l0VictimWay)).suggestName(s"l0_rfvOH")
7978b33cd30Sklin02  val l0Wdata = Wire(l0EntryType)
7988b33cd30Sklin02  l0Wdata.gen(
7993ea4388cSHaoyuan Feng    vpn = refill.req_info_dup(0).vpn,
8003ea4388cSHaoyuan Feng    asid = Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
80197929664SXiaokun-Pei    vmid = io.csr_dup(0).hgatp.vmid,
80245f497a4Shappy-lx    data = memRdata,
8033ea4388cSHaoyuan Feng    levelUInt = 0.U,
8043ea4388cSHaoyuan Feng    refill_prefetch_dup(0),
805dd286b6aSYanqin Li    refill.req_info_dup(0).s2xlate,
806e0c1f271SHaoyuan Feng    pbmte,
807e0c1f271SHaoyuan Feng    io.csr_dup(0).vsatp.mode
80845f497a4Shappy-lx  )
8098b33cd30Sklin02  when (l0Refill) {
8103ea4388cSHaoyuan Feng    l0.io.w.apply(
8116d5ddbceSLemover      valid = true.B,
8128b33cd30Sklin02      setIdx = l0RefillIdx,
8138b33cd30Sklin02      data = l0Wdata,
8148b33cd30Sklin02      waymask = l0VictimWayOH
8156d5ddbceSLemover    )
8168b33cd30Sklin02    ptwl0replace.access(l0RefillIdx, l0VictimWay)
8178b33cd30Sklin02    l0v := l0v | l0RfvOH
8188b33cd30Sklin02    l0g := l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U)
8198b33cd30Sklin02    l0h(l0RefillIdx)(l0VictimWay) := refill_h(0)
8206d5ddbceSLemover
8213ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
8228b33cd30Sklin02      l0RefillPerf(i) := i.U === l0VictimWay
8236d5ddbceSLemover    }
8246d5ddbceSLemover  }
8258b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refillIdx:0x${Hexadecimal(l0RefillIdx)} victimWay:${l0VictimWay} victimWayOH:${Binary(l0VictimWayOH)} rfvOH(in UInt):${Cat(l0RefillIdx, l0VictimWay)}\n")
8268b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refilldata:0x${l0Wdata}\n")
8278b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0v:${Binary(l0v)} -> ${Binary(l0v | l0RfvOH)}\n")
8288b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0g:${Binary(l0g)} -> ${Binary(l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U))}\n")
8297797f035SbugGenerator
8308d8ac704SLemover
8318d8ac704SLemover  // misc entries: super & invalid
8328b33cd30Sklin02  val spRefill =
8336962b4ffSHaoyuan Feng    !flush_dup(0) &&
834*0a56a7dcSHaoyuan Feng    (refill.levelOH.sp || (refill.levelOH.l0 && memPte(0).isNapot(refill.level_dup(0)))) &&
835e0c1f271SHaoyuan Feng    ((memPte(0).isLeaf() && memPte(0).canRefill(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte, io.csr_dup(0).vsatp.mode)) ||
836e0c1f271SHaoyuan Feng    memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte))
8378b33cd30Sklin02  val spRefillIdx = spreplace.way.suggestName(s"sp_refillIdx") // LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
8388b33cd30Sklin02  val spRfOH = UIntToOH(spRefillIdx).asUInt.suggestName(s"sp_rfOH")
8398b33cd30Sklin02  when (spRefill) {
8408b33cd30Sklin02    sp(spRefillIdx).refill(
8417797f035SbugGenerator      refill.req_info_dup(0).vpn,
842b188e334Speixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
84397929664SXiaokun-Pei      io.csr_dup(0).hgatp.vmid,
8447797f035SbugGenerator      memSelData(0),
8453ea4388cSHaoyuan Feng      refill.level_dup(0),
8467797f035SbugGenerator      refill_prefetch_dup(0),
847e0c1f271SHaoyuan Feng      !memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte)
84845f497a4Shappy-lx    )
8498b33cd30Sklin02    spreplace.access(spRefillIdx)
8508b33cd30Sklin02    spv := spv | spRfOH
8518b33cd30Sklin02    spg := spg & ~spRfOH | Mux(memPte(0).perm.g, spRfOH, 0.U)
8528b33cd30Sklin02    sph(spRefillIdx) := refill_h(0)
8536d5ddbceSLemover
8545854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
8558b33cd30Sklin02      spRefillPerf(i) := i.U === spRefillIdx
8566d5ddbceSLemover    }
8576d5ddbceSLemover  }
8588b33cd30Sklin02  XSDebug(spRefill, p"[sp refill] refillIdx:${spRefillIdx} refillEntry:${sp(spRefillIdx).genPtwEntry(refill.req_info_dup(0).vpn, Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid), memSelData(0), refill.level_dup(0), refill_prefetch_dup(0))}\n")
8598b33cd30Sklin02  XSDebug(spRefill, p"[sp refill] spv:${Binary(spv)}->${Binary(spv | spRfOH)} spg:${Binary(spg)}->${Binary(spg & ~spRfOH | Mux(memPte(0).perm.g, spRfOH, 0.U))}\n")
8606d5ddbceSLemover
8613ea4388cSHaoyuan Feng  val l1eccFlush = resp_res.l1.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l1eccError, init = false.B)
8623ea4388cSHaoyuan Feng  val l0eccFlush = resp_res.l0.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l0eccError, init = false.B)
8636c4dcc2dSLemover  val eccVpn = stageResp.bits.req_info.vpn
8647196f5a2SLemover
8653ea4388cSHaoyuan Feng  XSError(l1eccFlush, "l2tlb.cache.l1 ecc error. Should not happen at sim stage")
8663ea4388cSHaoyuan Feng  XSError(l0eccFlush, "l2tlb.cache.l0 ecc error. Should not happen at sim stage")
8673ea4388cSHaoyuan Feng  when (l1eccFlush) {
8683ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL1SetIdx(eccVpn))
8693ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l1nWays, a.asUInt) }).asUInt
8703ea4388cSHaoyuan Feng    l1v := l1v & ~flushMask
8713ea4388cSHaoyuan Feng    l1g := l1g & ~flushMask
8727196f5a2SLemover  }
8737196f5a2SLemover
8743ea4388cSHaoyuan Feng  when (l0eccFlush) {
8753ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(eccVpn))
8763ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
8773ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask
8783ea4388cSHaoyuan Feng    l0g := l0g & ~flushMask
8797196f5a2SLemover  }
8807196f5a2SLemover
8813ea4388cSHaoyuan Feng  // sfence for l0
8823ea4388cSHaoyuan Feng  val sfence_valid_l0 = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
8833ea4388cSHaoyuan Feng  when (sfence_valid_l0) {
8843ea4388cSHaoyuan Feng    val l0hhit = VecInit(l0h.flatMap(_.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate})).asUInt
8853ea4388cSHaoyuan Feng    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
8863ea4388cSHaoyuan Feng    when (sfence_dup(0).bits.rs1/*va*/) {
8873ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
8887797f035SbugGenerator        // all va && all asid
8893ea4388cSHaoyuan Feng        l0v := l0v & ~l0hhit
8907797f035SbugGenerator      } .otherwise {
8917797f035SbugGenerator        // all va && specific asid except global
8923ea4388cSHaoyuan Feng        l0v := l0v & (l0g | ~l0hhit)
8937797f035SbugGenerator      }
8947797f035SbugGenerator    } .otherwise {
8953ea4388cSHaoyuan Feng      // val flushMask = UIntToOH(genTlbl1Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
8963ea4388cSHaoyuan Feng      val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(sfence_vpn))
8973ea4388cSHaoyuan Feng      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l0nWays, _.asUInt))).asUInt
8983ea4388cSHaoyuan Feng      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
8997797f035SbugGenerator      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
9007797f035SbugGenerator      flushMask.suggestName(s"sfence_nrs1_flushMask")
9017797f035SbugGenerator
9023ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
9037797f035SbugGenerator        // specific leaf of addr && all asid
9043ea4388cSHaoyuan Feng        l0v := l0v & ~flushMask & ~l0hhit
9057797f035SbugGenerator      } .otherwise {
9067797f035SbugGenerator        // specific leaf of addr && specific asid
9073ea4388cSHaoyuan Feng        l0v := l0v & (~flushMask | l0g | ~l0hhit)
9087797f035SbugGenerator      }
9097797f035SbugGenerator    }
9107797f035SbugGenerator  }
9117797f035SbugGenerator
9123ea4388cSHaoyuan Feng  // hfencev, simple implementation for l0
9133ea4388cSHaoyuan Feng  val hfencev_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hv
9143ea4388cSHaoyuan Feng  when(hfencev_valid_l0) {
9153ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_  === onlyStage1))).asUInt
9163ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all VS-stage l0 pte
917d0de7e4aSpeixiaokun  }
918d0de7e4aSpeixiaokun
9193ea4388cSHaoyuan Feng  // hfenceg, simple implementation for l0
9203ea4388cSHaoyuan Feng  val hfenceg_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hg
9213ea4388cSHaoyuan Feng  when(hfenceg_valid_l0) {
9223ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_ === onlyStage2))).asUInt
9233ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all G-stage l0 pte
924d0de7e4aSpeixiaokun  }
925d0de7e4aSpeixiaokun
9263ea4388cSHaoyuan Feng  val l2asidhit = VecInit(l2asids.map(_ === sfence_dup(2).bits.id)).asUInt
927d0de7e4aSpeixiaokun  val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt
928d0de7e4aSpeixiaokun  val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
929d0de7e4aSpeixiaokun  when (sfence_valid) {
93097929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
93197929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
9323ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
933e5da58f0Speixiaokun    val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
9343ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map{a => io.csr_dup(1).priv.virt && a === onlyStage1 || !io.csr_dup(1).priv.virt && a === noS2xlate})).asUInt
9357797f035SbugGenerator    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
9367797f035SbugGenerator
9377797f035SbugGenerator    when (sfence_dup(0).bits.rs1/*va*/) {
9387797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
9396d5ddbceSLemover        // all va && all asid
9403ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
9413ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & VecInit(l2vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt)
942447c794eSpeixiaokun        spv := spv & ~(sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
9436d5ddbceSLemover      } .otherwise {
9446d5ddbceSLemover        // all va && specific asid except global
9453ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
9463ea4388cSHaoyuan Feng        l2v := l2v & ~(~l2g & l2hhit & l2asidhit & VecInit(l2vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt)
9475f64f303Speixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
9486d5ddbceSLemover      }
9496d5ddbceSLemover    } .otherwise {
9507797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
9516d5ddbceSLemover        // specific leaf of addr && all asid
95297929664SXiaokun-Pei        spv := spv & ~(sphhit & VecInit(sp.map(_.hit(sfence_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.vmid, ignoreAsid = true, s2xlate = io.csr_dup(0).priv.virt))).asUInt)
9536d5ddbceSLemover      } .otherwise {
9546d5ddbceSLemover        // specific leaf of addr && specific asid
95597929664SXiaokun-Pei        spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(sfence_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.vmid, s2xlate = io.csr_dup(0).priv.virt))).asUInt)
956d0de7e4aSpeixiaokun      }
957d0de7e4aSpeixiaokun    }
958d0de7e4aSpeixiaokun  }
959d0de7e4aSpeixiaokun
960d0de7e4aSpeixiaokun  val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv
961d0de7e4aSpeixiaokun  when (hfencev_valid) {
96297929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
96397929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
9643ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage1)).asUInt
965cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt
9663ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage1))).asUInt
967d0de7e4aSpeixiaokun    val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
968d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
969d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
9703ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
9713ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
972447c794eSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
973d0de7e4aSpeixiaokun      }.otherwise {
9743ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
9753ea4388cSHaoyuan Feng        l2v := l2v & ~(~l2g & l2hhit & l2asidhit & l2vmidhit)
976d0de7e4aSpeixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & spvmidhit)
977d0de7e4aSpeixiaokun      }
978d0de7e4aSpeixiaokun    }.otherwise {
979d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
98097929664SXiaokun-Pei        spv := spv & ~(sphhit & VecInit(sp.map(_.hit(hfencev_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.vmid, ignoreAsid = true, s2xlate = true.B))).asUInt)
981d0de7e4aSpeixiaokun      }.otherwise {
98297929664SXiaokun-Pei        spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(hfencev_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.vmid, s2xlate = true.B))).asUInt)
983d0de7e4aSpeixiaokun      }
984d0de7e4aSpeixiaokun    }
985d0de7e4aSpeixiaokun  }
986d0de7e4aSpeixiaokun
987d0de7e4aSpeixiaokun
988d0de7e4aSpeixiaokun  val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg
989d0de7e4aSpeixiaokun  when(hfenceg_valid) {
9903ea4388cSHaoyuan Feng    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
991cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
9923ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage2)).asUInt
993cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt
9943ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage2))).asUInt
995887df0f4Speixiaokun    val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen)
996d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
997d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
998d0de7e4aSpeixiaokun        l1v := l1v & ~l1hhit
9993ea4388cSHaoyuan Feng        l2v := l2v & ~l2hhit
1000d0de7e4aSpeixiaokun        spv := spv & ~sphhit
1001d0de7e4aSpeixiaokun      }.otherwise {
10023ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
10033ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1004d0de7e4aSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1005d0de7e4aSpeixiaokun      }
1006d0de7e4aSpeixiaokun    }.otherwise {
1007d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
10088fe4f15fSXiaokun-Pei        spv := spv & ~(sphhit & VecInit(sp.map(_.hit(hfenceg_gvpn, 0.U, 0.U, sfence_dup(0).bits.id, ignoreAsid = true, s2xlate = false.B))).asUInt)
1009d0de7e4aSpeixiaokun      }.otherwise {
10108fe4f15fSXiaokun-Pei        spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(hfenceg_gvpn, 0.U, 0.U, sfence_dup(0).bits.id, ignoreAsid = true, s2xlate = true.B))).asUInt)
10116d5ddbceSLemover      }
10126d5ddbceSLemover    }
10136d5ddbceSLemover  }
10146d5ddbceSLemover
10153ea4388cSHaoyuan Feng  if (EnableSv48) {
10163ea4388cSHaoyuan Feng    val l3asidhit = VecInit(l3asids.get.map(_ === sfence_dup(2).bits.id)).asUInt
101797929664SXiaokun-Pei    val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
10183ea4388cSHaoyuan Feng    val l3hhit = VecInit(l3h.get.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
10193ea4388cSHaoyuan Feng
10203ea4388cSHaoyuan Feng    when (sfence_valid) {
102197929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
10223ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
10233ea4388cSHaoyuan Feng      val sfence_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
10243ea4388cSHaoyuan Feng
10253ea4388cSHaoyuan Feng      when (sfence_dup(2).bits.rs1/*va*/) {
10263ea4388cSHaoyuan Feng        when (sfence_dup(2).bits.rs2) {
10273ea4388cSHaoyuan Feng          // all va && all asid
10283ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & VecInit(l3vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt))
10293ea4388cSHaoyuan Feng        } .otherwise {
10303ea4388cSHaoyuan Feng          // all va && specific asid except global
10313ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(~l3g.get & l3hhit & l3asidhit & VecInit(l3vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt))
10323ea4388cSHaoyuan Feng        }
10333ea4388cSHaoyuan Feng      }
10343ea4388cSHaoyuan Feng    }
10353ea4388cSHaoyuan Feng
10363ea4388cSHaoyuan Feng    when (hfencev_valid) {
103797929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
10383ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage1)).asUInt
10393ea4388cSHaoyuan Feng      val hfencev_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
10403ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
10413ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
10423ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
10433ea4388cSHaoyuan Feng        }.otherwise {
10443ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(~l3g.get & l3hhit & l3asidhit & l3vmidhit))
10453ea4388cSHaoyuan Feng        }
10463ea4388cSHaoyuan Feng      }
10473ea4388cSHaoyuan Feng    }
10483ea4388cSHaoyuan Feng
10493ea4388cSHaoyuan Feng    when (hfenceg_valid) {
10503ea4388cSHaoyuan Feng      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
10513ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage2)).asUInt
10523ea4388cSHaoyuan Feng      val hfenceg_gvpn = (sfence_dup(2).bits.addr << 2)(sfence_dup(2).bits.addr.getWidth - 1, offLen)
10533ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
10543ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
10553ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~l3hhit)
10563ea4388cSHaoyuan Feng        }.otherwise {
10573ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
10583ea4388cSHaoyuan Feng        }
10593ea4388cSHaoyuan Feng      }
10603ea4388cSHaoyuan Feng    }
10613ea4388cSHaoyuan Feng  }
10623ea4388cSHaoyuan Feng
10637797f035SbugGenerator  def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = {
10642c86e165SZhangZifei    in.ready := !in.valid || out.ready
10652c86e165SZhangZifei    out.valid := in.valid
10662c86e165SZhangZifei    out.bits := in.bits
10671f4a7c0cSLemover    out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) =>
10687797f035SbugGenerator      val bypassed_reg = Reg(Bool())
1069d0de7e4aSpeixiaokun      val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid
10707797f035SbugGenerator      when (inFire) { bypassed_reg := bypassed_wire }
10717797f035SbugGenerator      .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire }
10727797f035SbugGenerator
10737797f035SbugGenerator      b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire))
10741f4a7c0cSLemover    }
10752c86e165SZhangZifei  }
10762c86e165SZhangZifei
10776d5ddbceSLemover  // Perf Count
10783ea4388cSHaoyuan Feng  val resp_l0 = resp_res.l0.hit
10796c4dcc2dSLemover  val resp_sp = resp_res.sp.hit
10803ea4388cSHaoyuan Feng  val resp_l3_pre = if (EnableSv48) Some(resp_res.l3.get.pre) else None
10816c4dcc2dSLemover  val resp_l2_pre = resp_res.l2.pre
10823ea4388cSHaoyuan Feng  val resp_l1_pre = resp_res.l1.pre
10833ea4388cSHaoyuan Feng  val resp_l0_pre = resp_res.l0.pre
10846c4dcc2dSLemover  val resp_sp_pre = resp_res.sp.pre
1085935edac4STang Haojin  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1086bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
10873ea4388cSHaoyuan Feng  if (EnableSv48) {
10883ea4388cSHaoyuan Feng    XSPerfAccumulate("l3_hit", base_valid_access_0 && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
10893ea4388cSHaoyuan Feng  }
10903ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
10913ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
10923ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit", base_valid_access_0 && resp_l0)
1093bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
1094bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
1095bc063562SLemover
10963ea4388cSHaoyuan Feng  if (EnableSv48) {
10973ea4388cSHaoyuan Feng    XSPerfAccumulate("l3_hit_pre", base_valid_access_0 && resp_l3_pre.get && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
10983ea4388cSHaoyuan Feng  }
10993ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_pre", base_valid_access_0 && resp_l2_pre && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11003ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11013ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre", base_valid_access_0 && resp_l0_pre && resp_l0)
1102bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
11033ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1104bc063562SLemover
1105935edac4STang Haojin  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire
1106bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
11073ea4388cSHaoyuan Feng  if (EnableSv48) {
11083ea4388cSHaoyuan Feng    XSPerfAccumulate("pre_l3_hit", base_valid_access_1 && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11093ea4388cSHaoyuan Feng  }
11103ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11113ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11123ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit", base_valid_access_1 && resp_l0)
1113bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
1114bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
1115bc063562SLemover
11163ea4388cSHaoyuan Feng  if (EnableSv48) {
11173ea4388cSHaoyuan Feng    XSPerfAccumulate("pre_l3_hit_pre", base_valid_access_1 && resp_l3_pre.get && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11183ea4388cSHaoyuan Feng  }
11193ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_pre", base_valid_access_1 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11203ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11213ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre", base_valid_access_1 && resp_l0_pre && resp_l0)
1122bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
11233ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1124bc063562SLemover
1125935edac4STang Haojin  val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1126bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
11273ea4388cSHaoyuan Feng  if (EnableSv48) {
11283ea4388cSHaoyuan Feng    XSPerfAccumulate("l3_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11293ea4388cSHaoyuan Feng  }
11303ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11313ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11323ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_first", base_valid_access_2 && resp_l0)
1133bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
1134bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
1135bc063562SLemover
11363ea4388cSHaoyuan Feng  if (EnableSv48) {
11373ea4388cSHaoyuan Feng    XSPerfAccumulate("l3_hit_pre_first", base_valid_access_2 && resp_l3_pre.get && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11383ea4388cSHaoyuan Feng  }
11393ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_pre_first", base_valid_access_2 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11403ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11413ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre_first", base_valid_access_2 && resp_l0_pre && resp_l0)
1142bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
11433ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1144bc063562SLemover
1145935edac4STang Haojin  val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire
1146bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
11473ea4388cSHaoyuan Feng  if (EnableSv48) {
11483ea4388cSHaoyuan Feng    XSPerfAccumulate("pre_l3_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11493ea4388cSHaoyuan Feng  }
11503ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11513ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11523ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_first", base_valid_access_3 && resp_l0)
1153bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
1154bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
1155bc063562SLemover
11563ea4388cSHaoyuan Feng  if (EnableSv48) {
11573ea4388cSHaoyuan Feng    XSPerfAccumulate("pre_l3_hit_pre_first", base_valid_access_3 && resp_l3_pre.get && io.resp.bits.toFsm.l3Hit.get && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11583ea4388cSHaoyuan Feng  }
11593ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_pre_first", base_valid_access_3 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11603ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
11613ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre_first", base_valid_access_3 && resp_l0_pre && resp_l0)
1162bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
11633ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_pte_hit_pre_first",base_valid_access_3 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1164bc063562SLemover
11656d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
11666d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
11673ea4388cSHaoyuan Feng  if (EnableSv48) {
11683ea4388cSHaoyuan Feng    l3AccessPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3AccessIndex${i}", l) }
11693ea4388cSHaoyuan Feng  }
11703ea4388cSHaoyuan Feng  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2AccessIndex${i}", l) }
11713ea4388cSHaoyuan Feng  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1AccessIndex${i}", l) }
11723ea4388cSHaoyuan Feng  l0AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0AccessIndex${i}", l) }
11736d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
11743ea4388cSHaoyuan Feng  if (EnableSv48) {
11753ea4388cSHaoyuan Feng    l3RefillPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3RefillIndex${i}", l) }
11763ea4388cSHaoyuan Feng  }
11773ea4388cSHaoyuan Feng  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2RefillIndex${i}", l) }
11783ea4388cSHaoyuan Feng  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1RefillIndex${i}", l) }
11793ea4388cSHaoyuan Feng  l0RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0RefillIndex${i}", l) }
11806d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
11816d5ddbceSLemover
11823ea4388cSHaoyuan Feng  if (EnableSv48) {
11833ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill", Cat(l3RefillPerf.get).orR)
11843ea4388cSHaoyuan Feng  }
1185bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
11863ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
11873ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill", Cat(l0RefillPerf).orR)
1188bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
11893ea4388cSHaoyuan Feng  if (EnableSv48) {
11903ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf.get).orR && refill_prefetch_dup(0))
11913ea4388cSHaoyuan Feng  }
11927797f035SbugGenerator  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0))
11933ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0))
11943ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill_pre", Cat(l0RefillPerf).orR && refill_prefetch_dup(0))
11957797f035SbugGenerator  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0))
1196bc063562SLemover
11976d5ddbceSLemover  // debug
11987797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n")
11993ea4388cSHaoyuan Feng  if (EnableSv48) {
12003ea4388cSHaoyuan Feng    XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v.get)}\n")
12013ea4388cSHaoyuan Feng  }
12027797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n")
12033ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n")
12043ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0v:${Binary(l0v)}\n")
12053ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0g:${Binary(l0g)}\n")
12067797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n")
12077797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n")
12083ea4388cSHaoyuan Feng  if (EnableSv48) {
12093ea4388cSHaoyuan Feng    XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v.get)}\n")
12103ea4388cSHaoyuan Feng  }
12117797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n")
12123ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n")
12133ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0v:${Binary(l0v)}\n")
12143ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0g:${Binary(l0g)}\n")
12157797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n")
1216cd365d4cSrvcoresjw
1217cd365d4cSrvcoresjw  val perfEvents = Seq(
121856be8e20SYinan Xu    ("access           ", base_valid_access_0             ),
1219cd365d4cSrvcoresjw    ("l2_hit           ", l2Hit                           ),
12203ea4388cSHaoyuan Feng    ("l1_hit           ", l1Hit                           ),
12213ea4388cSHaoyuan Feng    ("l0_hit           ", l0Hit                           ),
1222cd365d4cSrvcoresjw    ("sp_hit           ", spHit                           ),
12233ea4388cSHaoyuan Feng    ("pte_hit          ", l0Hit || spHit                  ),
1224cd365d4cSrvcoresjw    ("rwHarzad         ", io.req.valid && !io.req.ready   ),
1225cd365d4cSrvcoresjw    ("out_blocked      ", io.resp.valid && !io.resp.ready ),
1226cd365d4cSrvcoresjw  )
12271ca0e4f3SYinan Xu  generatePerfEvent()
12286d5ddbceSLemover}
1229