xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 8fe4f15f79364a6880feb786112f401dbe368c1b)
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
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
226d5ddbceSLemoverimport xiangshan._
236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
246d5ddbceSLemoverimport utils._
253c02ee8fSwakafaimport utility._
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 */
343889e11eSLemover
353889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
363889e11eSLemover  val hit = Bool()
373889e11eSLemover  val pre = Bool()
38d61cd5eeSpeixiaokun  val ppn = if (HasHExtension) UInt((vpnLen max ppnLen).W) else UInt(ppnLen.W)
393889e11eSLemover  val perm = new PtePermBundle()
403889e11eSLemover  val ecc = Bool()
413889e11eSLemover  val level = UInt(2.W)
428d8ac704SLemover  val v = Bool()
433889e11eSLemover
443889e11eSLemover  def apply(hit: Bool, pre: Bool, ppn: UInt, perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
458d8ac704SLemover            ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B) {
463889e11eSLemover    this.hit := hit && !ecc
473889e11eSLemover    this.pre := pre
483889e11eSLemover    this.ppn := ppn
493889e11eSLemover    this.perm := perm
503889e11eSLemover    this.ecc := ecc && hit
513889e11eSLemover    this.level := level
528d8ac704SLemover    this.v := valid
533889e11eSLemover  }
543889e11eSLemover}
553889e11eSLemover
5663632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle {
5763632028SHaoyuan Feng  assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
5863632028SHaoyuan Feng  val hit = Bool()
5963632028SHaoyuan Feng  val pre = Bool()
60d61cd5eeSpeixiaokun  val ppn = Vec(tlbcontiguous, if(HasHExtension) UInt((vpnLen max ppnLen).W) else UInt(ppnLen.W))
6163632028SHaoyuan Feng  val perm = Vec(tlbcontiguous, new PtePermBundle())
6263632028SHaoyuan Feng  val ecc = Bool()
6363632028SHaoyuan Feng  val level = UInt(2.W)
6463632028SHaoyuan Feng  val v = Vec(tlbcontiguous, Bool())
6563632028SHaoyuan Feng
6663632028SHaoyuan Feng  def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())),
6763632028SHaoyuan Feng            ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B)) {
6863632028SHaoyuan Feng    this.hit := hit && !ecc
6963632028SHaoyuan Feng    this.pre := pre
7063632028SHaoyuan Feng    this.ppn := ppn
7163632028SHaoyuan Feng    this.perm := perm
7263632028SHaoyuan Feng    this.ecc := ecc && hit
7363632028SHaoyuan Feng    this.level := level
7463632028SHaoyuan Feng    this.v := valid
7563632028SHaoyuan Feng  }
7663632028SHaoyuan Feng}
7763632028SHaoyuan Feng
783889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
793889e11eSLemover  val l1 = new PageCachePerPespBundle
803889e11eSLemover  val l2 = new PageCachePerPespBundle
8163632028SHaoyuan Feng  val l3 = new PageCacheMergePespBundle
823889e11eSLemover  val sp = new PageCachePerPespBundle
833889e11eSLemover}
843889e11eSLemover
853889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
863889e11eSLemover  val req_info = new L2TlbInnerBundle()
873889e11eSLemover  val isFirst = Bool()
881f4a7c0cSLemover  val bypassed = Vec(3, Bool())
89325f0a4eSpeixiaokun  val isHptwReq = Bool()
90d0de7e4aSpeixiaokun  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
913889e11eSLemover}
923889e11eSLemover
933889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
943889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
956d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
9645f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
9794133605SLemover    val isFirst = Bool()
986d5ddbceSLemover    val hit = Bool()
99bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
1001f4a7c0cSLemover    val bypassed = Bool()
1016d5ddbceSLemover    val toFsm = new Bundle {
1026d5ddbceSLemover      val l1Hit = Bool()
1036d5ddbceSLemover      val l2Hit = Bool()
104d61cd5eeSpeixiaokun      val ppn = if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W)
10530104977Speixiaokun      val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW
1066d5ddbceSLemover    }
10763632028SHaoyuan Feng    val toTlb = new PtwMergeResp()
108325f0a4eSpeixiaokun    val isHptwReq = Bool()
109d0de7e4aSpeixiaokun    val toHptw = new Bundle {
110d0de7e4aSpeixiaokun      val l1Hit = Bool()
111d0de7e4aSpeixiaokun      val l2Hit = Bool()
112d0de7e4aSpeixiaokun      val ppn = UInt(ppnLen.W)
113d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
114d0de7e4aSpeixiaokun      val resp = new HptwResp() // used if hit
11583d93d53Speixiaokun      val bypassed = Bool()
116d0de7e4aSpeixiaokun    }
1176d5ddbceSLemover  })
1186d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
1195854c1edSLemover    val ptes = UInt(blockBits.W)
1207797f035SbugGenerator    val levelOH = new Bundle {
1217797f035SbugGenerator      // NOTE: levelOH has (Level+1) bits, each stands for page cache entries
1227797f035SbugGenerator      val sp = Bool()
1237797f035SbugGenerator      val l3 = Bool()
1247797f035SbugGenerator      val l2 = Bool()
1257797f035SbugGenerator      val l1 = Bool()
1267797f035SbugGenerator      def apply(levelUInt: UInt, valid: Bool) = {
1277797f035SbugGenerator        sp := RegNext((levelUInt === 0.U || levelUInt === 1.U) && valid, false.B)
1287797f035SbugGenerator        l3 := RegNext((levelUInt === 2.U) & valid, false.B)
1297797f035SbugGenerator        l2 := RegNext((levelUInt === 1.U) & valid, false.B)
1307797f035SbugGenerator        l1 := RegNext((levelUInt === 0.U) & valid, false.B)
1317797f035SbugGenerator      }
1327797f035SbugGenerator    }
1337797f035SbugGenerator    // duplicate level and sel_pte for each page caches, for better fanout
1347797f035SbugGenerator    val req_info_dup = Vec(3, new L2TlbInnerBundle())
1357797f035SbugGenerator    val level_dup = Vec(3, UInt(log2Up(Level).W))
1367797f035SbugGenerator    val sel_pte_dup = Vec(3, UInt(XLEN.W))
1376d5ddbceSLemover  }))
1387797f035SbugGenerator  val sfence_dup = Vec(4, Input(new SfenceBundle()))
1397797f035SbugGenerator  val csr_dup = Vec(3, Input(new TlbCsrBundle()))
1406d5ddbceSLemover}
1416d5ddbceSLemover
1421ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
1436d5ddbceSLemover  val io = IO(new PtwCacheIO)
1447196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
1457196f5a2SLemover  val l2EntryType = new PTWEntriesWithEcc(ecc, num = PtwL2SectorSize, tagLen = PtwL2TagLen, level = 1, hasPerm = false)
1467196f5a2SLemover  val l3EntryType = new PTWEntriesWithEcc(ecc, num = PtwL3SectorSize, tagLen = PtwL3TagLen, level = 2, hasPerm = true)
1477196f5a2SLemover
1486d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
1496d5ddbceSLemover
1507797f035SbugGenerator  val sfence_dup = io.sfence_dup
1516d5ddbceSLemover  val refill = io.refill.bits
1527797f035SbugGenerator  val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source))
153d0de7e4aSpeixiaokun  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)
1547797f035SbugGenerator  val flush = flush_dup(0)
1556d5ddbceSLemover
1566d5ddbceSLemover  // when refill, refuce to accept new req
1575854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
1583889e11eSLemover
1593889e11eSLemover  // handle hand signal and req_info
1606c4dcc2dSLemover  // TODO: replace with FlushableQueue
1616c4dcc2dSLemover  val stageReq = Wire(Decoupled(new PtwCacheReq()))         // enq stage & read page cache valid
1626c4dcc2dSLemover  val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp
1636c4dcc2dSLemover  val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc
1646c4dcc2dSLemover  val stageResp = Wire(Decoupled(new PtwCacheReq()))         // deq stage
1657797f035SbugGenerator
1667797f035SbugGenerator  val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush)      // catch ram data
1677797f035SbugGenerator  val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter
1687797f035SbugGenerator  val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool()))
1697797f035SbugGenerator  stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush))  // ecc flush
1707797f035SbugGenerator
1716c4dcc2dSLemover  stageReq <> io.req
1726c4dcc2dSLemover  PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad)
1737797f035SbugGenerator  InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle)
1746c4dcc2dSLemover  PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush)
1757797f035SbugGenerator  InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle)
1766c4dcc2dSLemover  PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush)
1776c4dcc2dSLemover  stageResp.ready := !stageResp.valid || io.resp.ready
1786d5ddbceSLemover
1796d5ddbceSLemover  // l1: level 0 non-leaf pte
1805854c1edSLemover  val l1 = Reg(Vec(l2tlbParams.l1Size, new PtwEntry(tagLen = PtwL1TagLen)))
1815854c1edSLemover  val l1v = RegInit(0.U(l2tlbParams.l1Size.W))
1825854c1edSLemover  val l1g = Reg(UInt(l2tlbParams.l1Size.W))
1831dd3e32dSHaoyuan Feng  val l1asids = l1.map(_.asid)
184d0de7e4aSpeixiaokun  val l1vmids = l1.map(_.vmid)
185875ae3b4SXiaokun-Pei  val l1h = Reg(Vec(l2tlbParams.l1Size, UInt(2.W)))
1866d5ddbceSLemover
1876d5ddbceSLemover  // l2: level 1 non-leaf pte
1886d5ddbceSLemover  val l2 = Module(new SRAMTemplate(
1897196f5a2SLemover    l2EntryType,
1905854c1edSLemover    set = l2tlbParams.l2nSets,
1915854c1edSLemover    way = l2tlbParams.l2nWays,
1925854c1edSLemover    singlePort = sramSinglePort
1936d5ddbceSLemover  ))
1945854c1edSLemover  val l2v = RegInit(0.U((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
1955854c1edSLemover  val l2g = Reg(UInt((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
196d0de7e4aSpeixiaokun  val l2h = Reg(Vec(l2tlbParams.l2nSets, Vec(l2tlbParams.l2nWays, UInt(2.W))))
1976d5ddbceSLemover  def getl2vSet(vpn: UInt) = {
1985854c1edSLemover    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
1996d5ddbceSLemover    val set = genPtwL2SetIdx(vpn)
2005854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
2015854c1edSLemover    val l2vVec = l2v.asTypeOf(Vec(l2tlbParams.l2nSets, UInt(l2tlbParams.l2nWays.W)))
2026d5ddbceSLemover    l2vVec(set)
2036d5ddbceSLemover  }
204d0de7e4aSpeixiaokun  def getl2hSet(vpn: UInt) = {
20545f497a4Shappy-lx    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
20645f497a4Shappy-lx    val set = genPtwL2SetIdx(vpn)
20745f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
208d0de7e4aSpeixiaokun    l2h(set)
20945f497a4Shappy-lx  }
2106d5ddbceSLemover
211d0de7e4aSpeixiaokun
212d0de7e4aSpeixiaokun
2136d5ddbceSLemover  // l3: level 2 leaf pte of 4KB pages
2146d5ddbceSLemover  val l3 = Module(new SRAMTemplate(
2157196f5a2SLemover    l3EntryType,
2165854c1edSLemover    set = l2tlbParams.l3nSets,
2175854c1edSLemover    way = l2tlbParams.l3nWays,
2185854c1edSLemover    singlePort = sramSinglePort
2196d5ddbceSLemover  ))
2205854c1edSLemover  val l3v = RegInit(0.U((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
2215854c1edSLemover  val l3g = Reg(UInt((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
222d0de7e4aSpeixiaokun  val l3h = Reg(Vec(l2tlbParams.l3nSets, Vec(l2tlbParams.l3nWays, UInt(2.W))))
2236d5ddbceSLemover  def getl3vSet(vpn: UInt) = {
2245854c1edSLemover    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
2256d5ddbceSLemover    val set = genPtwL3SetIdx(vpn)
2265854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
2275854c1edSLemover    val l3vVec = l3v.asTypeOf(Vec(l2tlbParams.l3nSets, UInt(l2tlbParams.l3nWays.W)))
2286d5ddbceSLemover    l3vVec(set)
2296d5ddbceSLemover  }
230d0de7e4aSpeixiaokun  def getl3hSet(vpn: UInt) = {
23145f497a4Shappy-lx    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
23245f497a4Shappy-lx    val set = genPtwL3SetIdx(vpn)
23345f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
234d0de7e4aSpeixiaokun    l3h(set)
23545f497a4Shappy-lx  }
2366d5ddbceSLemover
2376d5ddbceSLemover  // sp: level 0/1 leaf pte of 1GB/2MB super pages
2385854c1edSLemover  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true)))
2395854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
2405854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
2411dd3e32dSHaoyuan Feng  val spasids = sp.map(_.asid)
242d0de7e4aSpeixiaokun  val spvmids = sp.map(_.vmid)
243d61cd5eeSpeixiaokun  val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W)))
2446d5ddbceSLemover
2456d5ddbceSLemover  // Access Perf
2465854c1edSLemover  val l1AccessPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
2475854c1edSLemover  val l2AccessPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
2485854c1edSLemover  val l3AccessPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
2495854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
2506d5ddbceSLemover  l1AccessPerf.map(_ := false.B)
2516d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
2526d5ddbceSLemover  l3AccessPerf.map(_ := false.B)
2536d5ddbceSLemover  spAccessPerf.map(_ := false.B)
2546d5ddbceSLemover
2553889e11eSLemover
2561f4a7c0cSLemover
25782978df9Speixiaokun  def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = {
25882978df9Speixiaokun    (vpn1(vpnLen-1, vpnnLen*(2-level)+3) === vpn2(vpnLen-1, vpnnLen*(2-level)+3))
2591f4a7c0cSLemover  }
2601f4a7c0cSLemover  // NOTE: not actually bypassed, just check if hit, re-access the page cache
261d0de7e4aSpeixiaokun  def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = {
2626f508cb5Speixiaokun    val change_h = MuxLookup(h_search, noS2xlate)(Seq(
263980ddf4cSpeixiaokun      allStage -> onlyStage1,
264980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
265980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
266980ddf4cSpeixiaokun    ))
267d0de7e4aSpeixiaokun    val refill_vpn = io.refill.bits.req_info_dup(0).vpn
268980ddf4cSpeixiaokun    io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === io.refill.bits.req_info_dup(0).s2xlate
2691f4a7c0cSLemover  }
2701f4a7c0cSLemover
27182978df9Speixiaokun  val vpn_search = stageReq.bits.req_info.vpn
2726f508cb5Speixiaokun  val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq(
27309280d15Speixiaokun    allStage -> onlyStage1,
27409280d15Speixiaokun    onlyStage1 -> onlyStage1,
27509280d15Speixiaokun    onlyStage2 -> onlyStage2
27609280d15Speixiaokun  ))
2776d5ddbceSLemover  // l1
2785854c1edSLemover  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer, l2tlbParams.l1Size)
279bc063562SLemover  val (l1Hit, l1HitPPN, l1Pre) = {
280d0de7e4aSpeixiaokun    val hitVecT = l1.zipWithIndex.map {
281b188e334Speixiaokun      case (e, i) => (e.hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate)
282d0de7e4aSpeixiaokun        && l1v(i) && h_search === l1h(i))
283d0de7e4aSpeixiaokun    }
2846c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
2851f4a7c0cSLemover
2861f4a7c0cSLemover    // stageDelay, but check for l1
2879c503409SLemover    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l1.map(_.ppn)), stageDelay_valid_1cycle)
2889c503409SLemover    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l1.map(_.prefetch)), stageDelay_valid_1cycle)
2891f4a7c0cSLemover    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
2906d5ddbceSLemover
2916c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { ptwl1replace.access(OHToUInt(hitVec)) }
2926d5ddbceSLemover
2936c4dcc2dSLemover    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
2945854c1edSLemover    for (i <- 0 until l2tlbParams.l1Size) {
295b188e334Speixiaokun      XSDebug(stageReq.fire, p"[l1] l1(${i.U}) ${l1(i)} hit:${l1(i).hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate)}\n")
2966d5ddbceSLemover    }
2976c4dcc2dSLemover    XSDebug(stageReq.fire, p"[l1] l1v:${Binary(l1v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
2986c4dcc2dSLemover    XSDebug(stageDelay(0).valid, p"[l1] l1Hit:${hit} l1HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
2996d5ddbceSLemover
3006d5ddbceSLemover    VecInit(hitVecT).suggestName(s"l1_hitVecT")
3016d5ddbceSLemover    VecInit(hitVec).suggestName(s"l1_hitVec")
3026d5ddbceSLemover
3036c4dcc2dSLemover    // synchronize with other entries with RegEnable
3046c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
3056c4dcc2dSLemover     RegEnable(hitPPN, stageDelay(1).fire),
3066c4dcc2dSLemover     RegEnable(hitPre, stageDelay(1).fire))
3076d5ddbceSLemover  }
3086d5ddbceSLemover
3096d5ddbceSLemover  // l2
3105854c1edSLemover  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer,l2tlbParams.l2nWays,l2tlbParams.l2nSets)
311bc063562SLemover  val (l2Hit, l2HitPPN, l2Pre, l2eccError) = {
312d0de7e4aSpeixiaokun    val ridx = genPtwL2SetIdx(vpn_search)
3136c4dcc2dSLemover    l2.io.r.req.valid := stageReq.fire
3146d5ddbceSLemover    l2.io.r.req.bits.apply(setIdx = ridx)
315d0de7e4aSpeixiaokun    val vVec_req = getl2vSet(vpn_search)
316d0de7e4aSpeixiaokun    val hVec_req = getl2hSet(vpn_search)
3176c4dcc2dSLemover
3186c4dcc2dSLemover    // delay one cycle after sram read
31982978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
3206f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
321980ddf4cSpeixiaokun      allStage -> onlyStage1,
322980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
323980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
324980ddf4cSpeixiaokun    ))
3256c4dcc2dSLemover    val data_resp = DataHoldBypass(l2.io.r.resp.data, stageDelay_valid_1cycle)
3267797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
327d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
328d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
329b188e334Speixiaokun      wayData.entries.hit(delay_vpn, io.csr_dup(1).satp.asid, io.csr_dup(1).vsatp.asid, io.csr_dup(1).hgatp.asid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)})
3306c4dcc2dSLemover
3316c4dcc2dSLemover    // check hit and ecc
33282978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
3336c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
334935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
3356c4dcc2dSLemover
3367797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
3377196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
3387196f5a2SLemover    val hitWayData = hitWayEntry.entries
3396c4dcc2dSLemover    val hit = ParallelOR(hitVec)
340f3034303SHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l2nWays).map(_.U(log2Up(l2tlbParams.l2nWays).W)))
341eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
342eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
343eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
344eef81af7SHaoyuan Feng    } else {
345eef81af7SHaoyuan Feng      eccError := false.B
346eef81af7SHaoyuan Feng    }
3477196f5a2SLemover
3486d5ddbceSLemover    ridx.suggestName(s"l2_ridx")
3496d5ddbceSLemover    ramDatas.suggestName(s"l2_ramDatas")
3506d5ddbceSLemover    hitVec.suggestName(s"l2_hitVec")
3516d5ddbceSLemover    hitWayData.suggestName(s"l2_hitWayData")
3526d5ddbceSLemover    hitWay.suggestName(s"l2_hitWay")
3536d5ddbceSLemover
3546c4dcc2dSLemover    when (hit && stageCheck_valid_1cycle) { ptwl2replace.access(genPtwL2SetIdx(check_vpn), hitWay) }
3556d5ddbceSLemover
3566c4dcc2dSLemover    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
3576c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[l2] ridx:0x${Hexadecimal(ridx)}\n")
3585854c1edSLemover    for (i <- 0 until l2tlbParams.l2nWays) {
3596c4dcc2dSLemover      XSDebug(stageCheck_valid_1cycle, p"[l2] ramDatas(${i.U}) ${ramDatas(i)}  l2v:${vVec(i)}  hit:${hit}\n")
3606d5ddbceSLemover    }
3616c4dcc2dSLemover    XSDebug(stageCheck_valid_1cycle, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitWayData.ppns(genPtwL2SectorIdx(check_vpn)))} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${vVec}\n")
3626d5ddbceSLemover
3636c4dcc2dSLemover    (hit, hitWayData.ppns(genPtwL2SectorIdx(check_vpn)), hitWayData.prefetch, eccError)
3646d5ddbceSLemover  }
3656d5ddbceSLemover
3666d5ddbceSLemover  // l3
3675854c1edSLemover  val ptwl3replace = ReplacementPolicy.fromString(l2tlbParams.l3Replacer,l2tlbParams.l3nWays,l2tlbParams.l3nSets)
368bc063562SLemover  val (l3Hit, l3HitData, l3Pre, l3eccError) = {
369d0de7e4aSpeixiaokun    val ridx = genPtwL3SetIdx(vpn_search)
3706c4dcc2dSLemover    l3.io.r.req.valid := stageReq.fire
3716d5ddbceSLemover    l3.io.r.req.bits.apply(setIdx = ridx)
372d0de7e4aSpeixiaokun    val vVec_req = getl3vSet(vpn_search)
373d0de7e4aSpeixiaokun    val hVec_req = getl3hSet(vpn_search)
3746c4dcc2dSLemover
3756c4dcc2dSLemover    // delay one cycle after sram read
37682978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
3776f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
378980ddf4cSpeixiaokun      allStage -> onlyStage1,
379980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
380980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
381980ddf4cSpeixiaokun    ))
3826c4dcc2dSLemover    val data_resp = DataHoldBypass(l3.io.r.resp.data, stageDelay_valid_1cycle)
3837797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
384d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
385d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
386b188e334Speixiaokun      wayData.entries.hit(delay_vpn, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.asid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)})
3876c4dcc2dSLemover
3886c4dcc2dSLemover    // check hit and ecc
38982978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
3906c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
391935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
3926c4dcc2dSLemover
3937797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
3947196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
3957196f5a2SLemover    val hitWayData = hitWayEntry.entries
3967196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
3976c4dcc2dSLemover    val hit = ParallelOR(hitVec)
398f3034303SHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l3nWays).map(_.U(log2Up(l2tlbParams.l3nWays).W)))
399eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
400eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
401eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
402eef81af7SHaoyuan Feng    } else {
403eef81af7SHaoyuan Feng      eccError := false.B
404eef81af7SHaoyuan Feng    }
4056d5ddbceSLemover
4066c4dcc2dSLemover    when (hit && stageCheck_valid_1cycle) { ptwl3replace.access(genPtwL3SetIdx(check_vpn), hitWay) }
4077196f5a2SLemover
4086c4dcc2dSLemover    l3AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
4096c4dcc2dSLemover    XSDebug(stageReq.fire, p"[l3] ridx:0x${Hexadecimal(ridx)}\n")
4105854c1edSLemover    for (i <- 0 until l2tlbParams.l3nWays) {
4116c4dcc2dSLemover      XSDebug(stageCheck_valid_1cycle, p"[l3] ramDatas(${i.U}) ${ramDatas(i)}  l3v:${vVec(i)}  hit:${hitVec(i)}\n")
4126d5ddbceSLemover    }
4136c4dcc2dSLemover    XSDebug(stageCheck_valid_1cycle, p"[l3] l3Hit:${hit} l3HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n")
4146d5ddbceSLemover
4156d5ddbceSLemover    ridx.suggestName(s"l3_ridx")
4166d5ddbceSLemover    ramDatas.suggestName(s"l3_ramDatas")
4176d5ddbceSLemover    hitVec.suggestName(s"l3_hitVec")
4186d5ddbceSLemover    hitWay.suggestName(s"l3_hitWay")
4196d5ddbceSLemover
4203889e11eSLemover    (hit, hitWayData, hitWayData.prefetch, eccError)
4216d5ddbceSLemover  }
42263632028SHaoyuan Feng  val l3HitPPN = l3HitData.ppns
42363632028SHaoyuan Feng  val l3HitPerm = l3HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL3SectorSize, new PtePermBundle)))
42463632028SHaoyuan Feng  val l3HitValid = l3HitData.vs
4256d5ddbceSLemover
4266d5ddbceSLemover  // super page
4275854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
4288d8ac704SLemover  val (spHit, spHitData, spPre, spValid) = {
429b188e334Speixiaokun    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.asid, s2xlate = h_search =/= noS2xlate) && spv(i) && (sph(i) === h_search) }
4306c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4316d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
4326c4dcc2dSLemover    val hit = ParallelOR(hitVec)
4336d5ddbceSLemover
4346c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) }
4356d5ddbceSLemover
4366c4dcc2dSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle }
4375854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
438b188e334Speixiaokun      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.asid, s2xlate = h_search =/= noS2xlate)} spv:${spv(i)}\n")
4396d5ddbceSLemover    }
4406c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
4416d5ddbceSLemover
4426d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
4436d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
4446d5ddbceSLemover
4456c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
4466c4dcc2dSLemover     RegEnable(hitData, stageDelay(1).fire),
4476c4dcc2dSLemover     RegEnable(hitData.prefetch, stageDelay(1).fire),
448935edac4STang Haojin     RegEnable(hitData.v, stageDelay(1).fire))
4496d5ddbceSLemover  }
4506d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
4516d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
4526d5ddbceSLemover
4536c4dcc2dSLemover  val check_res = Wire(new PageCacheRespBundle)
4546c4dcc2dSLemover  check_res.l1.apply(l1Hit, l1Pre, l1HitPPN)
4556c4dcc2dSLemover  check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, ecc = l2eccError)
4561f4a7c0cSLemover  check_res.l3.apply(l3Hit, l3Pre, l3HitPPN, l3HitPerm, l3eccError, valid = l3HitValid)
4576c4dcc2dSLemover  check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitPerm, false.B, spHitLevel, spValid)
4586d5ddbceSLemover
4596c4dcc2dSLemover  val resp_res = Reg(new PageCacheRespBundle)
4606c4dcc2dSLemover  when (stageCheck(1).fire) { resp_res := check_res }
4613889e11eSLemover
4621f4a7c0cSLemover  // stageResp bypass
4631f4a7c0cSLemover  val bypassed = Wire(Vec(3, Bool()))
4641f4a7c0cSLemover  bypassed.indices.foreach(i =>
4651f4a7c0cSLemover    bypassed(i) := stageResp.bits.bypassed(i) ||
4666967f5d5Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
4671f4a7c0cSLemover        OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid)
4681f4a7c0cSLemover  )
4691f4a7c0cSLemover
47083d93d53Speixiaokun  // stageResp bypass to hptw
47183d93d53Speixiaokun  val hptw_bypassed = Wire(Vec(3, Bool()))
47283d93d53Speixiaokun  hptw_bypassed.indices.foreach(i =>
47383d93d53Speixiaokun    hptw_bypassed(i) := stageResp.bits.bypassed(i) ||
47483d93d53Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
47583d93d53Speixiaokun        io.resp.fire)
47683d93d53Speixiaokun  )
47783d93d53Speixiaokun
47830104977Speixiaokun  val isAllStage = stageResp.bits.req_info.s2xlate === allStage
479c0991f6aSpeixiaokun  val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2
480980ddf4cSpeixiaokun  val stage1Hit = (resp_res.l3.hit || resp_res.sp.hit) && isAllStage
4816c4dcc2dSLemover  io.resp.bits.req_info   := stageResp.bits.req_info
4826c4dcc2dSLemover  io.resp.bits.isFirst  := stageResp.bits.isFirst
48330104977Speixiaokun  io.resp.bits.hit      := (resp_res.l3.hit || resp_res.sp.hit) && !isAllStage
4846967f5d5Speixiaokun  io.resp.bits.bypassed := (bypassed(2) || (bypassed(1) && !resp_res.l2.hit) || (bypassed(0) && !resp_res.l1.hit)) && !isAllStage
4856c4dcc2dSLemover  io.resp.bits.prefetch := resp_res.l3.pre && resp_res.l3.hit || resp_res.sp.pre && resp_res.sp.hit
486325f0a4eSpeixiaokun  io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
487325f0a4eSpeixiaokun  io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
4886c4dcc2dSLemover  io.resp.bits.toFsm.ppn   := Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l1.ppn)
489980ddf4cSpeixiaokun  io.resp.bits.toFsm.stage1Hit := stage1Hit
490d0de7e4aSpeixiaokun
491325f0a4eSpeixiaokun  io.resp.bits.isHptwReq := stageResp.bits.isHptwReq
49283d93d53Speixiaokun  io.resp.bits.toHptw.bypassed := (hptw_bypassed(2) || (hptw_bypassed(1) && !resp_res.l2.hit) || (hptw_bypassed(0) && !resp_res.l1.hit)) && stageResp.bits.isHptwReq
493d0de7e4aSpeixiaokun  io.resp.bits.toHptw.id := stageResp.bits.hptwId
494325f0a4eSpeixiaokun  io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq
495325f0a4eSpeixiaokun  io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq
496d0de7e4aSpeixiaokun  io.resp.bits.toHptw.ppn := Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l1.ppn)
49782978df9Speixiaokun  val idx = stageResp.bits.req_info.vpn(2, 0)
49882978df9Speixiaokun  io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn
499eb4bf3f2Speixiaokun  io.resp.bits.toHptw.resp.entry.asid := DontCare
500d61cd5eeSpeixiaokun  io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.asid)
501d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l3.hit, 2.U, resp_res.sp.level))
502d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source)
503d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.ppn := Mux(resp_res.l3.hit, resp_res.l3.ppn(idx), resp_res.sp.ppn)
504d61cd5eeSpeixiaokun  io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l3.hit, resp_res.l3.perm(idx), resp_res.sp.perm))
505d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l3.hit, resp_res.l3.v(idx), resp_res.sp.v)
506d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v
507d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gaf := false.B
508d0de7e4aSpeixiaokun
50982978df9Speixiaokun  io.resp.bits.toTlb.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3))
510eb4bf3f2Speixiaokun  io.resp.bits.toTlb.entry.map(_.asid := Mux(stageResp.bits.req_info.hasS2xlate(), io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid)) // DontCare
511eb4bf3f2Speixiaokun  io.resp.bits.toTlb.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.asid))
51263632028SHaoyuan Feng  io.resp.bits.toTlb.entry.map(_.level.map(_ := Mux(resp_res.l3.hit, 2.U, resp_res.sp.level)))
51363632028SHaoyuan Feng  io.resp.bits.toTlb.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source))
51463632028SHaoyuan Feng  for (i <- 0 until tlbcontiguous) {
51563632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).ppn := Mux(resp_res.l3.hit, resp_res.l3.ppn(i)(ppnLen - 1, sectortlbwidth), resp_res.sp.ppn(ppnLen - 1, sectortlbwidth))
51663632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).ppn_low := Mux(resp_res.l3.hit, resp_res.l3.ppn(i)(sectortlbwidth - 1, 0), resp_res.sp.ppn(sectortlbwidth - 1, 0))
51763632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).perm.map(_ := Mux(resp_res.l3.hit, resp_res.l3.perm(i), resp_res.sp.perm))
51863632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).v := Mux(resp_res.l3.hit, resp_res.l3.v(i), resp_res.sp.v)
51963632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).pf := !io.resp.bits.toTlb.entry(i).v
52063632028SHaoyuan Feng    io.resp.bits.toTlb.entry(i).af := false.B
52163632028SHaoyuan Feng  }
52263632028SHaoyuan Feng  io.resp.bits.toTlb.pteidx := UIntToOH(stageResp.bits.req_info.vpn(2, 0)).asBools
52363632028SHaoyuan Feng  io.resp.bits.toTlb.not_super := Mux(resp_res.l3.hit, true.B, false.B)
5246c4dcc2dSLemover  io.resp.valid := stageResp.valid
5256c4dcc2dSLemover  XSError(stageResp.valid && resp_res.l3.hit && resp_res.sp.hit, "normal page and super page both hit")
5261f4a7c0cSLemover  XSError(stageResp.valid && io.resp.bits.hit && bypassed(2), "page cache, bypassed but hit")
5276d5ddbceSLemover
5286d5ddbceSLemover  // refill Perf
5295854c1edSLemover  val l1RefillPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
5305854c1edSLemover  val l2RefillPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
5315854c1edSLemover  val l3RefillPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
5325854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
5336d5ddbceSLemover  l1RefillPerf.map(_ := false.B)
5346d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
5356d5ddbceSLemover  l3RefillPerf.map(_ := false.B)
5366d5ddbceSLemover  spRefillPerf.map(_ := false.B)
5376d5ddbceSLemover
5386d5ddbceSLemover  // refill
5396d5ddbceSLemover  l2.io.w.req <> DontCare
5406d5ddbceSLemover  l3.io.w.req <> DontCare
5416d5ddbceSLemover  l2.io.w.req.valid := false.B
5426d5ddbceSLemover  l3.io.w.req.valid := false.B
5436d5ddbceSLemover
5446d5ddbceSLemover  val memRdata = refill.ptes
5455854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
5467797f035SbugGenerator  val memSelData = io.refill.bits.sel_pte_dup
5477797f035SbugGenerator  val memPte = memSelData.map(a => a.asTypeOf(new PteBundle))
548b848eea5SLemover
5496d5ddbceSLemover  // TODO: handle sfenceLatch outsize
5500d94d540SHaoyuan Feng  when (!flush_dup(0) && refill.levelOH.l1 && !memPte(0).isLeaf() && !memPte(0).isPf(refill.level_dup(0)) && !memPte(0).isAf()) {
5515854c1edSLemover    // val refillIdx = LFSR64()(log2Up(l2tlbParams.l1Size)-1,0) // TODO: may be LRU
5526d5ddbceSLemover    val refillIdx = replaceWrapper(l1v, ptwl1replace.way)
5536d5ddbceSLemover    refillIdx.suggestName(s"PtwL1RefillIdx")
5546d5ddbceSLemover    val rfOH = UIntToOH(refillIdx)
55545f497a4Shappy-lx    l1(refillIdx).refill(
55682978df9Speixiaokun      refill.req_info_dup(0).vpn,
557980ddf4cSpeixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
558d0de7e4aSpeixiaokun      io.csr_dup(0).hgatp.asid,
5597797f035SbugGenerator      memSelData(0),
56045f497a4Shappy-lx      0.U,
5617797f035SbugGenerator      refill_prefetch_dup(0)
56245f497a4Shappy-lx    )
5636d5ddbceSLemover    ptwl1replace.access(refillIdx)
5646d5ddbceSLemover    l1v := l1v | rfOH
5657797f035SbugGenerator    l1g := (l1g & ~rfOH) | Mux(memPte(0).perm.g, rfOH, 0.U)
566d0de7e4aSpeixiaokun    l1h(refillIdx) := refill.req_info_dup(0).s2xlate
5676d5ddbceSLemover
5685854c1edSLemover    for (i <- 0 until l2tlbParams.l1Size) {
5696d5ddbceSLemover      l1RefillPerf(i) := i.U === refillIdx
5706d5ddbceSLemover    }
5716d5ddbceSLemover
572b188e334Speixiaokun    XSDebug(p"[l1 refill] refillIdx:${refillIdx} refillEntry:${l1(refillIdx).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), 0.U, prefetch = refill_prefetch_dup(0))}\n")
5737797f035SbugGenerator    XSDebug(p"[l1 refill] l1v:${Binary(l1v)}->${Binary(l1v | rfOH)} l1g:${Binary(l1g)}->${Binary((l1g & ~rfOH) | Mux(memPte(0).perm.g, rfOH, 0.U))}\n")
5746d5ddbceSLemover
5756d5ddbceSLemover    refillIdx.suggestName(s"l1_refillIdx")
5766d5ddbceSLemover    rfOH.suggestName(s"l1_rfOH")
5776d5ddbceSLemover  }
5786d5ddbceSLemover
5790d94d540SHaoyuan Feng  when (!flush_dup(1) && refill.levelOH.l2 && !memPte(1).isLeaf() && !memPte(1).isPf(refill.level_dup(1)) && !memPte(1).isAf()) {
5807797f035SbugGenerator    val refillIdx = genPtwL2SetIdx(refill.req_info_dup(1).vpn)
5817797f035SbugGenerator    val victimWay = replaceWrapper(getl2vSet(refill.req_info_dup(1).vpn), ptwl2replace.way(refillIdx))
5826d5ddbceSLemover    val victimWayOH = UIntToOH(victimWay)
5836d5ddbceSLemover    val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
5847196f5a2SLemover    val wdata = Wire(l2EntryType)
5853889e11eSLemover    wdata.gen(
58682978df9Speixiaokun      vpn = refill.req_info_dup(1).vpn,
587cca17e78Speixiaokun      asid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid),
588d0de7e4aSpeixiaokun      vmid = io.csr_dup(1).hgatp.asid,
58945f497a4Shappy-lx      data = memRdata,
59045f497a4Shappy-lx      levelUInt = 1.U,
5917797f035SbugGenerator      refill_prefetch_dup(1)
59245f497a4Shappy-lx    )
5936d5ddbceSLemover    l2.io.w.apply(
5946d5ddbceSLemover      valid = true.B,
5956d5ddbceSLemover      setIdx = refillIdx,
5967196f5a2SLemover      data = wdata,
5976d5ddbceSLemover      waymask = victimWayOH
5986d5ddbceSLemover    )
5996d5ddbceSLemover    ptwl2replace.access(refillIdx, victimWay)
6006d5ddbceSLemover    l2v := l2v | rfvOH
6016d5ddbceSLemover    l2g := l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
602980ddf4cSpeixiaokun    l2h(refillIdx)(victimWay) := refill.req_info_dup(1).s2xlate
6036d5ddbceSLemover
6045854c1edSLemover    for (i <- 0 until l2tlbParams.l2nWays) {
6056d5ddbceSLemover      l2RefillPerf(i) := i.U === victimWay
6066d5ddbceSLemover    }
6076d5ddbceSLemover
6086d5ddbceSLemover    XSDebug(p"[l2 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
60945f497a4Shappy-lx    XSDebug(p"[l2 refill] refilldata:0x${wdata}\n")
6106d5ddbceSLemover    XSDebug(p"[l2 refill] l2v:${Binary(l2v)} -> ${Binary(l2v | rfvOH)}\n")
6116d5ddbceSLemover    XSDebug(p"[l2 refill] l2g:${Binary(l2g)} -> ${Binary(l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
6126d5ddbceSLemover
6136d5ddbceSLemover    refillIdx.suggestName(s"l2_refillIdx")
6146d5ddbceSLemover    victimWay.suggestName(s"l2_victimWay")
6156d5ddbceSLemover    victimWayOH.suggestName(s"l2_victimWayOH")
6166d5ddbceSLemover    rfvOH.suggestName(s"l2_rfvOH")
6176d5ddbceSLemover  }
6186d5ddbceSLemover
6190d94d540SHaoyuan Feng  when (!flush_dup(2) && refill.levelOH.l3 && !memPte(2).isAf()) {
6207797f035SbugGenerator    val refillIdx = genPtwL3SetIdx(refill.req_info_dup(2).vpn)
6217797f035SbugGenerator    val victimWay = replaceWrapper(getl3vSet(refill.req_info_dup(2).vpn), ptwl3replace.way(refillIdx))
6226d5ddbceSLemover    val victimWayOH = UIntToOH(victimWay)
6236d5ddbceSLemover    val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
6247196f5a2SLemover    val wdata = Wire(l3EntryType)
6253889e11eSLemover    wdata.gen(
62682978df9Speixiaokun      vpn =  refill.req_info_dup(2).vpn,
627cca17e78Speixiaokun      asid = Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
628d0de7e4aSpeixiaokun      vmid = io.csr_dup(2).hgatp.asid,
62945f497a4Shappy-lx      data = memRdata,
63045f497a4Shappy-lx      levelUInt = 2.U,
6317797f035SbugGenerator      refill_prefetch_dup(2)
63245f497a4Shappy-lx    )
6336d5ddbceSLemover    l3.io.w.apply(
6346d5ddbceSLemover      valid = true.B,
6356d5ddbceSLemover      setIdx = refillIdx,
6367196f5a2SLemover      data = wdata,
6376d5ddbceSLemover      waymask = victimWayOH
6386d5ddbceSLemover    )
6396d5ddbceSLemover    ptwl3replace.access(refillIdx, victimWay)
6406d5ddbceSLemover    l3v := l3v | rfvOH
6416d5ddbceSLemover    l3g := l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
642d0de7e4aSpeixiaokun    l3h(refillIdx)(victimWay) := refill.req_info_dup(2).s2xlate
6436d5ddbceSLemover
6445854c1edSLemover    for (i <- 0 until l2tlbParams.l3nWays) {
6456d5ddbceSLemover      l3RefillPerf(i) := i.U === victimWay
6466d5ddbceSLemover    }
6476d5ddbceSLemover
6486d5ddbceSLemover    XSDebug(p"[l3 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
64945f497a4Shappy-lx    XSDebug(p"[l3 refill] refilldata:0x${wdata}\n")
6506d5ddbceSLemover    XSDebug(p"[l3 refill] l3v:${Binary(l3v)} -> ${Binary(l3v | rfvOH)}\n")
6516d5ddbceSLemover    XSDebug(p"[l3 refill] l3g:${Binary(l3g)} -> ${Binary(l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
6526d5ddbceSLemover
6536d5ddbceSLemover    refillIdx.suggestName(s"l3_refillIdx")
6546d5ddbceSLemover    victimWay.suggestName(s"l3_victimWay")
6556d5ddbceSLemover    victimWayOH.suggestName(s"l3_victimWayOH")
6566d5ddbceSLemover    rfvOH.suggestName(s"l3_rfvOH")
6576d5ddbceSLemover  }
6587797f035SbugGenerator
6598d8ac704SLemover
6608d8ac704SLemover  // misc entries: super & invalid
6610d94d540SHaoyuan Feng  when (!flush_dup(0) && refill.levelOH.sp && (memPte(0).isLeaf() || memPte(0).isPf(refill.level_dup(0))) && !memPte(0).isAf()) {
6625854c1edSLemover    val refillIdx = spreplace.way// LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
6636d5ddbceSLemover    val rfOH = UIntToOH(refillIdx)
66445f497a4Shappy-lx    sp(refillIdx).refill(
6657797f035SbugGenerator      refill.req_info_dup(0).vpn,
666b188e334Speixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
667d0de7e4aSpeixiaokun      io.csr_dup(0).hgatp.asid,
6687797f035SbugGenerator      memSelData(0),
6697797f035SbugGenerator      refill.level_dup(2),
6707797f035SbugGenerator      refill_prefetch_dup(0),
6717797f035SbugGenerator      !memPte(0).isPf(refill.level_dup(0)),
67245f497a4Shappy-lx    )
6736d5ddbceSLemover    spreplace.access(refillIdx)
6746d5ddbceSLemover    spv := spv | rfOH
6757797f035SbugGenerator    spg := spg & ~rfOH | Mux(memPte(0).perm.g, rfOH, 0.U)
676d0de7e4aSpeixiaokun    sph(refillIdx) := refill.req_info_dup(0).s2xlate
6776d5ddbceSLemover
6785854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
6796d5ddbceSLemover      spRefillPerf(i) := i.U === refillIdx
6806d5ddbceSLemover    }
6816d5ddbceSLemover
682b188e334Speixiaokun    XSDebug(p"[sp refill] refillIdx:${refillIdx} refillEntry:${sp(refillIdx).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")
6837797f035SbugGenerator    XSDebug(p"[sp refill] spv:${Binary(spv)}->${Binary(spv | rfOH)} spg:${Binary(spg)}->${Binary(spg & ~rfOH | Mux(memPte(0).perm.g, rfOH, 0.U))}\n")
6846d5ddbceSLemover
6856d5ddbceSLemover    refillIdx.suggestName(s"sp_refillIdx")
6866d5ddbceSLemover    rfOH.suggestName(s"sp_rfOH")
6876d5ddbceSLemover  }
6886d5ddbceSLemover
6897797f035SbugGenerator  val l2eccFlush = resp_res.l2.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l2eccError, init = false.B)
6907797f035SbugGenerator  val l3eccFlush = resp_res.l3.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l3eccError, init = false.B)
6916c4dcc2dSLemover  val eccVpn = stageResp.bits.req_info.vpn
6927196f5a2SLemover
6936c4dcc2dSLemover  XSError(l2eccFlush, "l2tlb.cache.l2 ecc error. Should not happen at sim stage")
6946c4dcc2dSLemover  XSError(l3eccFlush, "l2tlb.cache.l3 ecc error. Should not happen at sim stage")
6957196f5a2SLemover  when (l2eccFlush) {
6967196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL2SetIdx(eccVpn))
6977196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l2nWays, a.asUInt) }).asUInt
6987196f5a2SLemover    l2v := l2v & ~flushMask
6997196f5a2SLemover    l2g := l2g & ~flushMask
7007196f5a2SLemover  }
7017196f5a2SLemover
7027196f5a2SLemover  when (l3eccFlush) {
7037196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(eccVpn))
7047196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
7057196f5a2SLemover    l3v := l3v & ~flushMask
7067196f5a2SLemover    l3g := l3g & ~flushMask
7077196f5a2SLemover  }
7087196f5a2SLemover
709a1d4b4bfSpeixiaokun  // sfence for l3
710d0de7e4aSpeixiaokun  val sfence_valid_l3 = sfence_dup(3).valid && !sfence_dup(3).bits.hg && !sfence_dup(3).bits.hv
711a1d4b4bfSpeixiaokun  when (sfence_valid_l3) {
712*8fe4f15fSXiaokun-Pei    val l3hhit = VecInit(l3h.flatMap(_.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate})).asUInt
7137797f035SbugGenerator    val sfence_vpn = sfence_dup(3).bits.addr(sfence_dup(3).bits.addr.getWidth-1, offLen)
7147797f035SbugGenerator    when (sfence_dup(3).bits.rs1/*va*/) {
7157797f035SbugGenerator      when (sfence_dup(3).bits.rs2) {
7167797f035SbugGenerator        // all va && all asid
717a1d4b4bfSpeixiaokun        l3v := l3v & ~l3hhit
7187797f035SbugGenerator      } .otherwise {
7197797f035SbugGenerator        // all va && specific asid except global
720*8fe4f15fSXiaokun-Pei        l3v := l3v & (l3g | ~l3hhit)
7217797f035SbugGenerator      }
7227797f035SbugGenerator    } .otherwise {
7237797f035SbugGenerator      // val flushMask = UIntToOH(genTlbL2Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
7247797f035SbugGenerator      val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(sfence_vpn))
7257797f035SbugGenerator      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l3nWays, _.asUInt))).asUInt
7267797f035SbugGenerator      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
7277797f035SbugGenerator      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
7287797f035SbugGenerator      flushMask.suggestName(s"sfence_nrs1_flushMask")
7297797f035SbugGenerator
7307797f035SbugGenerator      when (sfence_dup(3).bits.rs2) {
7317797f035SbugGenerator        // specific leaf of addr && all asid
732a1d4b4bfSpeixiaokun        l3v := l3v & ~flushMask & ~l3hhit
7337797f035SbugGenerator      } .otherwise {
7347797f035SbugGenerator        // specific leaf of addr && specific asid
735a1d4b4bfSpeixiaokun        l3v := l3v & (~flushMask | l3g | ~l3hhit)
7367797f035SbugGenerator      }
7377797f035SbugGenerator    }
7387797f035SbugGenerator  }
7397797f035SbugGenerator
7405f64f303Speixiaokun  // hfencev, simple implementation for l3
741d0de7e4aSpeixiaokun  val hfencev_valid_l3 = sfence_dup(3).valid && sfence_dup(3).bits.hv
742a1d4b4bfSpeixiaokun  when(hfencev_valid_l3) {
743cca17e78Speixiaokun    val flushMask = VecInit(l3h.flatMap(_.map(_  === onlyStage1))).asUInt
744d0de7e4aSpeixiaokun    l3v := l3v & ~flushMask // all VS-stage l3 pte
745d0de7e4aSpeixiaokun  }
746d0de7e4aSpeixiaokun
747d0de7e4aSpeixiaokun  // hfenceg, simple implementation for l3
748d0de7e4aSpeixiaokun  val hfenceg_valid_l3 = sfence_dup(3).valid && sfence_dup(3).bits.hg
749d0de7e4aSpeixiaokun  when(hfenceg_valid_l3) {
750cca17e78Speixiaokun    val flushMask = VecInit(l3h.flatMap(_.map(_ === onlyStage2))).asUInt
751d0de7e4aSpeixiaokun    l3v := l3v & ~flushMask // all G-stage l3 pte
752d0de7e4aSpeixiaokun  }
753d0de7e4aSpeixiaokun
754d0de7e4aSpeixiaokun
755d0de7e4aSpeixiaokun  val l1asidhit = VecInit(l1asids.map(_ === sfence_dup(0).bits.id)).asUInt
756d0de7e4aSpeixiaokun  val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt
757d0de7e4aSpeixiaokun  val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
758d0de7e4aSpeixiaokun  when (sfence_valid) {
759cca17e78Speixiaokun    val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt
760cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt
761e5da58f0Speixiaokun    val l1hhit = VecInit(l1h.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
762e5da58f0Speixiaokun    val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
763e5da58f0Speixiaokun    val l2hhit = VecInit(l2h.flatMap(_.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate})).asUInt
7647797f035SbugGenerator    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
7657797f035SbugGenerator
7667797f035SbugGenerator    when (sfence_dup(0).bits.rs1/*va*/) {
7677797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
7686d5ddbceSLemover        // all va && all asid
769d0de7e4aSpeixiaokun        l2v := l2v & ~l2hhit
7705f64f303Speixiaokun        l1v := l1v & ~(l1hhit & VecInit(l1vmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
771447c794eSpeixiaokun        spv := spv & ~(sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
7726d5ddbceSLemover      } .otherwise {
7736d5ddbceSLemover        // all va && specific asid except global
774d0de7e4aSpeixiaokun        l2v := l2v & (l2g | ~l2hhit)
7755f64f303Speixiaokun        l1v := l1v & ~(~l1g & l1hhit & l1asidhit & VecInit(l1vmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
7765f64f303Speixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
7776d5ddbceSLemover      }
7786d5ddbceSLemover    } .otherwise {
7797797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
7806d5ddbceSLemover        // specific leaf of addr && all asid
781a0c90508Speixiaokun        spv := spv & ~(sphhit & VecInit(sp.map(_.hit(sfence_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, ignoreAsid = true, s2xlate = io.csr_dup(0).priv.virt))).asUInt)
7826d5ddbceSLemover      } .otherwise {
7836d5ddbceSLemover        // specific leaf of addr && specific asid
784a0c90508Speixiaokun        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.asid, s2xlate = io.csr_dup(0).priv.virt))).asUInt)
785d0de7e4aSpeixiaokun      }
786d0de7e4aSpeixiaokun    }
787d0de7e4aSpeixiaokun  }
788d0de7e4aSpeixiaokun
789d0de7e4aSpeixiaokun  val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv
790d0de7e4aSpeixiaokun  when (hfencev_valid) {
791cca17e78Speixiaokun    val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt
792cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt
793cca17e78Speixiaokun    val l1hhit = VecInit(l1h.map(_ === onlyStage1)).asUInt
794cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt
795cca17e78Speixiaokun    val l2hhit = VecInit(l2h.flatMap(_.map(_ === onlyStage1))).asUInt
796d0de7e4aSpeixiaokun    val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
797d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
798d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
799d0de7e4aSpeixiaokun        l2v := l2v & ~l2hhit
800d0de7e4aSpeixiaokun        l1v := l1v & ~(l1hhit & l1vmidhit)
801447c794eSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
802d0de7e4aSpeixiaokun      }.otherwise {
803d0de7e4aSpeixiaokun        l2v := l2v & (l2g | ~l2hhit)
804d0de7e4aSpeixiaokun        l1v := l1v & ~(~l1g & l1hhit & l1asidhit & l1vmidhit)
805d0de7e4aSpeixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & spvmidhit)
806d0de7e4aSpeixiaokun      }
807d0de7e4aSpeixiaokun    }.otherwise {
808d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
809a0c90508Speixiaokun        spv := spv & ~(sphhit & VecInit(sp.map(_.hit(hfencev_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, ignoreAsid = true, s2xlate = true.B))).asUInt)
810d0de7e4aSpeixiaokun      }.otherwise {
811a0c90508Speixiaokun        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.asid, s2xlate = true.B))).asUInt)
812d0de7e4aSpeixiaokun      }
813d0de7e4aSpeixiaokun    }
814d0de7e4aSpeixiaokun  }
815d0de7e4aSpeixiaokun
816d0de7e4aSpeixiaokun
817d0de7e4aSpeixiaokun  val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg
818d0de7e4aSpeixiaokun  when(hfenceg_valid) {
819cca17e78Speixiaokun    val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
820cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
821cca17e78Speixiaokun    val l1hhit = VecInit(l1h.map(_ === onlyStage2)).asUInt
822cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt
823cca17e78Speixiaokun    val l2hhit = VecInit(l2h.flatMap(_.map(_ === onlyStage2))).asUInt
824887df0f4Speixiaokun    val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen)
825d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
826d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
827d0de7e4aSpeixiaokun        l2v := l2v & ~l2hhit
828d0de7e4aSpeixiaokun        l1v := l1v & ~l1hhit
829d0de7e4aSpeixiaokun        spv := spv & ~sphhit
830d0de7e4aSpeixiaokun      }.otherwise {
831d0de7e4aSpeixiaokun        l2v := l2v & ~l2hhit
832d0de7e4aSpeixiaokun        l1v := l1v & ~(l1hhit & l1vmidhit)
833d0de7e4aSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
834d0de7e4aSpeixiaokun      }
835d0de7e4aSpeixiaokun    }.otherwise {
836d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
837*8fe4f15fSXiaokun-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)
838d0de7e4aSpeixiaokun      }.otherwise {
839*8fe4f15fSXiaokun-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)
8406d5ddbceSLemover      }
8416d5ddbceSLemover    }
8426d5ddbceSLemover  }
8436d5ddbceSLemover
8447797f035SbugGenerator  def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = {
8452c86e165SZhangZifei    in.ready := !in.valid || out.ready
8462c86e165SZhangZifei    out.valid := in.valid
8472c86e165SZhangZifei    out.bits := in.bits
8481f4a7c0cSLemover    out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) =>
8497797f035SbugGenerator      val bypassed_reg = Reg(Bool())
850d0de7e4aSpeixiaokun      val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid
8517797f035SbugGenerator      when (inFire) { bypassed_reg := bypassed_wire }
8527797f035SbugGenerator      .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire }
8537797f035SbugGenerator
8547797f035SbugGenerator      b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire))
8551f4a7c0cSLemover    }
8562c86e165SZhangZifei  }
8572c86e165SZhangZifei
8586d5ddbceSLemover  // Perf Count
8596c4dcc2dSLemover  val resp_l3 = resp_res.l3.hit
8606c4dcc2dSLemover  val resp_sp = resp_res.sp.hit
8616c4dcc2dSLemover  val resp_l1_pre = resp_res.l1.pre
8626c4dcc2dSLemover  val resp_l2_pre = resp_res.l2.pre
8636c4dcc2dSLemover  val resp_l3_pre = resp_res.l3.pre
8646c4dcc2dSLemover  val resp_sp_pre = resp_res.sp.pre
865935edac4STang Haojin  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire
866bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
867bc063562SLemover  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
868bc063562SLemover  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
869bc063562SLemover  XSPerfAccumulate("l3_hit", base_valid_access_0 && resp_l3)
870bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
871bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
872bc063562SLemover
873bc063562SLemover  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)
874bc063562SLemover  XSPerfAccumulate("l2_hit_pre", base_valid_access_0 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
875bc063562SLemover  XSPerfAccumulate("l3_hit_pre", base_valid_access_0 && resp_l3_pre && resp_l3)
876bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
877bc063562SLemover  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
878bc063562SLemover
879935edac4STang Haojin  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire
880bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
881bc063562SLemover  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
882bc063562SLemover  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
883bc063562SLemover  XSPerfAccumulate("pre_l3_hit", base_valid_access_1 && resp_l3)
884bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
885bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
886bc063562SLemover
887bc063562SLemover  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)
888bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre", base_valid_access_1 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
889bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre", base_valid_access_1 && resp_l3_pre && resp_l3)
890bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
891bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
892bc063562SLemover
893935edac4STang Haojin  val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire
894bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
895bc063562SLemover  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
896bc063562SLemover  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
897bc063562SLemover  XSPerfAccumulate("l3_hit_first", base_valid_access_2 && resp_l3)
898bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
899bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
900bc063562SLemover
901bc063562SLemover  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)
902bc063562SLemover  XSPerfAccumulate("l2_hit_pre_first", base_valid_access_2 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
903bc063562SLemover  XSPerfAccumulate("l3_hit_pre_first", base_valid_access_2 && resp_l3_pre && resp_l3)
904bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
905bc063562SLemover  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
906bc063562SLemover
907935edac4STang Haojin  val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire
908bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
909bc063562SLemover  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
910bc063562SLemover  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
911bc063562SLemover  XSPerfAccumulate("pre_l3_hit_first", base_valid_access_3 && resp_l3)
912bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
913bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
914bc063562SLemover
915bc063562SLemover  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)
916bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre_first", base_valid_access_3 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
917bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre_first", base_valid_access_3 && resp_l3_pre && resp_l3)
918bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
919bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre_first",base_valid_access_3 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
920bc063562SLemover
9216d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
9226d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
9236d5ddbceSLemover  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1AccessIndex${i}", l) }
9246d5ddbceSLemover  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2AccessIndex${i}", l) }
9256d5ddbceSLemover  l3AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3AccessIndex${i}", l) }
9266d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
9276d5ddbceSLemover  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1RefillIndex${i}", l) }
9286d5ddbceSLemover  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2RefillIndex${i}", l) }
9296d5ddbceSLemover  l3RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3RefillIndex${i}", l) }
9306d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
9316d5ddbceSLemover
932bc063562SLemover  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
933bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
934bc063562SLemover  XSPerfAccumulate("l3Refill", Cat(l3RefillPerf).orR)
935bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
9367797f035SbugGenerator  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0))
9377797f035SbugGenerator  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0))
9387797f035SbugGenerator  XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf).orR && refill_prefetch_dup(0))
9397797f035SbugGenerator  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0))
940bc063562SLemover
9416d5ddbceSLemover  // debug
9427797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n")
9437797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n")
9447797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n")
9457797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v)}\n")
9467797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l3g:${Binary(l3g)}\n")
9477797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n")
9487797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n")
9497797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n")
9507797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n")
9517797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v)}\n")
9527797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3g:${Binary(l3g)}\n")
9537797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n")
954cd365d4cSrvcoresjw
955cd365d4cSrvcoresjw  val perfEvents = Seq(
95656be8e20SYinan Xu    ("access           ", base_valid_access_0             ),
957cd365d4cSrvcoresjw    ("l1_hit           ", l1Hit                           ),
958cd365d4cSrvcoresjw    ("l2_hit           ", l2Hit                           ),
959cd365d4cSrvcoresjw    ("l3_hit           ", l3Hit                           ),
960cd365d4cSrvcoresjw    ("sp_hit           ", spHit                           ),
961cd365d4cSrvcoresjw    ("pte_hit          ", l3Hit || spHit                  ),
962cd365d4cSrvcoresjw    ("rwHarzad         ",  io.req.valid && !io.req.ready  ),
963cd365d4cSrvcoresjw    ("out_blocked      ",  io.resp.valid && !io.resp.ready),
964cd365d4cSrvcoresjw  )
9651ca0e4f3SYinan Xu  generatePerfEvent()
9666d5ddbceSLemover}
967