xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 4b2c87ba1d7965f6f2b0a396be707a6e2f6fb345)
16d5ddbceSLemover/***************************************************************************************
28882eb68SXin Tian* Copyright (c) 2021-2025 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
58882eb68SXin Tian* Copyright (c) 2024-2025 Institute of Information Engineering, Chinese Academy of Sciences
66d5ddbceSLemover*
76d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
86d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
96d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
106d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
116d5ddbceSLemover*
126d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
136d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
146d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
156d5ddbceSLemover*
166d5ddbceSLemover* See the Mulan PSL v2 for more details.
176d5ddbceSLemover***************************************************************************************/
186d5ddbceSLemover
196d5ddbceSLemoverpackage xiangshan.cache.mmu
206d5ddbceSLemover
218891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
226d5ddbceSLemoverimport chisel3._
236d5ddbceSLemoverimport chisel3.util._
246d5ddbceSLemoverimport xiangshan._
256d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
266d5ddbceSLemoverimport utils._
273c02ee8fSwakafaimport utility._
28abc4432bSHaoyuan Fengimport coupledL2.utils.SplittedSRAM
296d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
306d5ddbceSLemoverimport freechips.rocketchip.tilelink._
31*4b2c87baS梁森 Liang Senimport utility.mbist.MbistPipeline
326d5ddbceSLemover
336d5ddbceSLemover/* ptw cache caches the page table of all the three layers
346d5ddbceSLemover * ptw cache resp at next cycle
356d5ddbceSLemover * the cache should not be blocked
366d5ddbceSLemover * when miss queue if full, just block req outside
376d5ddbceSLemover */
383889e11eSLemover
393889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
403889e11eSLemover  val hit = Bool()
413889e11eSLemover  val pre = Bool()
424c0e0181SXiaokun-Pei  val ppn = UInt(gvpnLen.W)
43002c10a4SYanqin Li  val pbmt = UInt(ptePbmtLen.W)
443889e11eSLemover  val perm = new PtePermBundle()
45718a93f5SHaoyuan Feng  val n = UInt(pteNLen.W)
463889e11eSLemover  val ecc = Bool()
473889e11eSLemover  val level = UInt(2.W)
488d8ac704SLemover  val v = Bool()
498882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
508882eb68SXin Tian    val jmp_bitmap_check = Bool()
518882eb68SXin Tian    val pte = UInt(XLEN.W) // Page Table Entry
528882eb68SXin Tian  })
533889e11eSLemover
54718a93f5SHaoyuan Feng  def apply(hit: Bool, pre: Bool, ppn: UInt, pbmt: UInt = 0.U, n: UInt = 0.U,
55002c10a4SYanqin Li            perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
568882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B, jmp_bitmap_check: Bool = false.B,
578882eb68SXin Tian            pte: UInt = 0.U): Unit = {
583889e11eSLemover    this.hit := hit && !ecc
593889e11eSLemover    this.pre := pre
603889e11eSLemover    this.ppn := ppn
61718a93f5SHaoyuan Feng    this.n := n
62002c10a4SYanqin Li    this.pbmt := pbmt
633889e11eSLemover    this.perm := perm
643889e11eSLemover    this.ecc := ecc && hit
653889e11eSLemover    this.level := level
668d8ac704SLemover    this.v := valid
678882eb68SXin Tian    if (HasBitmapCheck) {
688882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
698882eb68SXin Tian      this.bitmapCheck.get.pte := pte
708882eb68SXin Tian    }
713889e11eSLemover  }
723889e11eSLemover}
733889e11eSLemover
7463632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle {
7563632028SHaoyuan Feng  assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
7663632028SHaoyuan Feng  val hit = Bool()
7763632028SHaoyuan Feng  val pre = Bool()
784c0e0181SXiaokun-Pei  val ppn = Vec(tlbcontiguous, UInt(gvpnLen.W))
79002c10a4SYanqin Li  val pbmt = Vec(tlbcontiguous, UInt(ptePbmtLen.W))
8063632028SHaoyuan Feng  val perm = Vec(tlbcontiguous, new PtePermBundle())
8163632028SHaoyuan Feng  val ecc = Bool()
8263632028SHaoyuan Feng  val level = UInt(2.W)
8363632028SHaoyuan Feng  val v = Vec(tlbcontiguous, Bool())
848882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
858882eb68SXin Tian    val jmp_bitmap_check = Bool()
868882eb68SXin Tian    val hitway = UInt(l2tlbParams.l0nWays.W)
878882eb68SXin Tian    val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
888882eb68SXin Tian    val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
898882eb68SXin Tian  })
9063632028SHaoyuan Feng
91002c10a4SYanqin Li  def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], pbmt: Vec[UInt] = Vec(tlbcontiguous, 0.U),
92002c10a4SYanqin Li            perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())),
938882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B),
948882eb68SXin Tian            jmp_bitmap_check: Bool = false.B,
958882eb68SXin Tian            hitway: UInt = 0.U, ptes: Vec[UInt] , cfs: Vec[Bool]): Unit = {
9663632028SHaoyuan Feng    this.hit := hit && !ecc
9763632028SHaoyuan Feng    this.pre := pre
9863632028SHaoyuan Feng    this.ppn := ppn
99002c10a4SYanqin Li    this.pbmt := pbmt
10063632028SHaoyuan Feng    this.perm := perm
10163632028SHaoyuan Feng    this.ecc := ecc && hit
10263632028SHaoyuan Feng    this.level := level
10363632028SHaoyuan Feng    this.v := valid
1048882eb68SXin Tian    if (HasBitmapCheck) {
1058882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
1068882eb68SXin Tian      this.bitmapCheck.get.hitway := hitway
1078882eb68SXin Tian      this.bitmapCheck.get.ptes := ptes
1088882eb68SXin Tian      this.bitmapCheck.get.cfs := cfs
1098882eb68SXin Tian    }
11063632028SHaoyuan Feng  }
11163632028SHaoyuan Feng}
11263632028SHaoyuan Feng
1133889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
1143ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(new PageCachePerPespBundle) else None
1153889e11eSLemover  val l2 = new PageCachePerPespBundle
1163ea4388cSHaoyuan Feng  val l1 = new PageCachePerPespBundle
1173ea4388cSHaoyuan Feng  val l0 = new PageCacheMergePespBundle
1183889e11eSLemover  val sp = new PageCachePerPespBundle
1193889e11eSLemover}
1203889e11eSLemover
1213889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
1223889e11eSLemover  val req_info = new L2TlbInnerBundle()
1233889e11eSLemover  val isFirst = Bool()
1243ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Vec(4, Bool()) else Vec(3, Bool())
125325f0a4eSpeixiaokun  val isHptwReq = Bool()
126d0de7e4aSpeixiaokun  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
1273889e11eSLemover}
1283889e11eSLemover
1293889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
1303889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
1316d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
13245f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
13394133605SLemover    val isFirst = Bool()
1346d5ddbceSLemover    val hit = Bool()
135bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
1361f4a7c0cSLemover    val bypassed = Bool()
1376d5ddbceSLemover    val toFsm = new Bundle {
1383ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
1396d5ddbceSLemover      val l2Hit = Bool()
1403ea4388cSHaoyuan Feng      val l1Hit = Bool()
1414c0e0181SXiaokun-Pei      val ppn = UInt(gvpnLen.W)
14230104977Speixiaokun      val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW
1438882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
1448882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
1458882eb68SXin Tian        val toLLPTW = Bool()
1468882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
1478882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
1488882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
1498882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
1508882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
1518882eb68SXin Tian      })
1526d5ddbceSLemover    }
1536979864eSXiaokun-Pei    val stage1 = new PtwMergeResp()
154325f0a4eSpeixiaokun    val isHptwReq = Bool()
155d0de7e4aSpeixiaokun    val toHptw = new Bundle {
1563ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
157d0de7e4aSpeixiaokun      val l2Hit = Bool()
1583ea4388cSHaoyuan Feng      val l1Hit = Bool()
159d0de7e4aSpeixiaokun      val ppn = UInt(ppnLen.W)
160d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
161d0de7e4aSpeixiaokun      val resp = new HptwResp() // used if hit
16283d93d53Speixiaokun      val bypassed = Bool()
1638882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
1648882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
1658882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
1668882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
1678882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
1688882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
1698882eb68SXin Tian        val fromSP = Bool()
1708882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
1718882eb68SXin Tian      })
172d0de7e4aSpeixiaokun    }
1736d5ddbceSLemover  })
1746d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
1755854c1edSLemover    val ptes = UInt(blockBits.W)
1767797f035SbugGenerator    val levelOH = new Bundle {
1777797f035SbugGenerator      // NOTE: levelOH has (Level+1) bits, each stands for page cache entries
1787797f035SbugGenerator      val sp = Bool()
1793ea4388cSHaoyuan Feng      val l0 = Bool()
1807797f035SbugGenerator      val l1 = Bool()
1813ea4388cSHaoyuan Feng      val l2 = Bool()
1823ea4388cSHaoyuan Feng      val l3 = if (EnableSv48) Some(Bool()) else None
1837797f035SbugGenerator      def apply(levelUInt: UInt, valid: Bool) = {
1843ea4388cSHaoyuan Feng        sp := GatedValidRegNext((levelUInt === 1.U || levelUInt === 2.U || levelUInt === 3.U) && valid, false.B)
1853ea4388cSHaoyuan Feng        l0 := GatedValidRegNext((levelUInt === 0.U) & valid, false.B)
1863ea4388cSHaoyuan Feng        l1 := GatedValidRegNext((levelUInt === 1.U) & valid, false.B)
1873ea4388cSHaoyuan Feng        l2 := GatedValidRegNext((levelUInt === 2.U) & valid, false.B)
1883ea4388cSHaoyuan Feng        l3.map(_ := GatedValidRegNext((levelUInt === 3.U) & valid, false.B))
1897797f035SbugGenerator      }
1907797f035SbugGenerator    }
1917797f035SbugGenerator    // duplicate level and sel_pte for each page caches, for better fanout
1927797f035SbugGenerator    val req_info_dup = Vec(3, new L2TlbInnerBundle())
1933ea4388cSHaoyuan Feng    val level_dup = Vec(3, UInt(log2Up(Level + 1).W))
1947797f035SbugGenerator    val sel_pte_dup = Vec(3, UInt(XLEN.W))
1956d5ddbceSLemover  }))
1968882eb68SXin Tian  // when refill l0,save way info for late bitmap wakeup convenient
1978882eb68SXin Tian  // valid at same cycle of refill.levelOH.l0
1988882eb68SXin Tian  val l0_way_info = Option.when(HasBitmapCheck)(Output(UInt(l2tlbParams.l0nWays.W)))
1997797f035SbugGenerator  val sfence_dup = Vec(4, Input(new SfenceBundle()))
2007797f035SbugGenerator  val csr_dup = Vec(3, Input(new TlbCsrBundle()))
2018882eb68SXin Tian  val bitmap_wakeup = Option.when(HasBitmapCheck)(Flipped(ValidIO(new Bundle {
2028882eb68SXin Tian    val setIndex = Input(UInt(PtwL0SetIdxLen.W))
2038882eb68SXin Tian    val tag = Input(UInt(SPTagLen.W))
2048882eb68SXin Tian    val isSp = Input(Bool())
2058882eb68SXin Tian    val way_info = UInt(l2tlbParams.l0nWays.W)
2068882eb68SXin Tian    val pte_index = UInt(sectortlbwidth.W)
2078882eb68SXin Tian    val check_success = Bool()
2088882eb68SXin Tian  })))
2096d5ddbceSLemover}
2106d5ddbceSLemover
2111ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
2126d5ddbceSLemover  val io = IO(new PtwCacheIO)
2137196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
214abc4432bSHaoyuan Feng  val l1EntryType = new PTWEntriesWithEcc(ecc, num = PtwL1SectorSize, tagLen = PtwL1TagLen, level = 1, hasPerm = false, ReservedBits = l2tlbParams.l1ReservedBits)
215abc4432bSHaoyuan Feng  val l0EntryType = new PTWEntriesWithEcc(ecc, num = PtwL0SectorSize, tagLen = PtwL0TagLen, level = 0, hasPerm = true, ReservedBits = l2tlbParams.l0ReservedBits)
2167196f5a2SLemover
2178882eb68SXin Tian  // use two additional regs to record corresponding cache entry whether via bitmap check
2188882eb68SXin Tian  // 32(l0nSets)* 8 (l0nWays) * 8 (tlbcontiguous)
2198882eb68SXin Tian  val l0BitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.l0nSets)(VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W))))))))
2208882eb68SXin Tian  val spBitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.spSize)(0.U(1.W))))
2218882eb68SXin Tian
2228882eb68SXin Tian  val bitmapEnable = io.csr_dup(0).mbmc.BME === 1.U && io.csr_dup(0).mbmc.CMODE === 0.U
2236d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
2246d5ddbceSLemover
2257797f035SbugGenerator  val sfence_dup = io.sfence_dup
2266d5ddbceSLemover  val refill = io.refill.bits
2277797f035SbugGenerator  val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source))
2284ed5afbdSXiaokun-Pei  val refill_h = io.refill.bits.req_info_dup.map(a => Mux(a.s2xlate === allStage, onlyStage1, a.s2xlate))
229d0de7e4aSpeixiaokun  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)
2307797f035SbugGenerator  val flush = flush_dup(0)
2316d5ddbceSLemover
2326d5ddbceSLemover  // when refill, refuce to accept new req
2335854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
2343889e11eSLemover
2353889e11eSLemover  // handle hand signal and req_info
2366c4dcc2dSLemover  // TODO: replace with FlushableQueue
2376c4dcc2dSLemover  val stageReq = Wire(Decoupled(new PtwCacheReq()))         // enq stage & read page cache valid
2386c4dcc2dSLemover  val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp
2396c4dcc2dSLemover  val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc
2406c4dcc2dSLemover  val stageResp = Wire(Decoupled(new PtwCacheReq()))         // deq stage
2417797f035SbugGenerator
2427797f035SbugGenerator  val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush)      // catch ram data
2437797f035SbugGenerator  val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter
2447797f035SbugGenerator  val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool()))
2457797f035SbugGenerator  stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush))  // ecc flush
2467797f035SbugGenerator
2476c4dcc2dSLemover  stageReq <> io.req
2486c4dcc2dSLemover  PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad)
2497797f035SbugGenerator  InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle)
2506c4dcc2dSLemover  PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush)
2517797f035SbugGenerator  InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle)
2526c4dcc2dSLemover  PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush)
2536c4dcc2dSLemover  stageResp.ready := !stageResp.valid || io.resp.ready
2546d5ddbceSLemover
2553ea4388cSHaoyuan Feng  // l3: level 3 non-leaf pte
2563ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, new PtwEntry(tagLen = PtwL3TagLen)))) else None
2573ea4388cSHaoyuan Feng  val l3v = if (EnableSv48) Some(RegInit(0.U(l2tlbParams.l3Size.W))) else None
2583ea4388cSHaoyuan Feng  val l3g = if (EnableSv48) Some(Reg(UInt(l2tlbParams.l3Size.W))) else None
2593ea4388cSHaoyuan Feng  val l3asids = if (EnableSv48) Some(l3.get.map(_.asid)) else None
2603ea4388cSHaoyuan Feng  val l3vmids = if (EnableSv48) Some(l3.get.map(_.vmid)) else None
2613ea4388cSHaoyuan Feng  val l3h = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, UInt(2.W)))) else None
2626d5ddbceSLemover
2633ea4388cSHaoyuan Feng  // l2: level 2 non-leaf pte
2643ea4388cSHaoyuan Feng  val l2 = Reg(Vec(l2tlbParams.l2Size, new PtwEntry(tagLen = PtwL2TagLen)))
2653ea4388cSHaoyuan Feng  val l2v = RegInit(0.U(l2tlbParams.l2Size.W))
2663ea4388cSHaoyuan Feng  val l2g = Reg(UInt(l2tlbParams.l2Size.W))
2673ea4388cSHaoyuan Feng  val l2asids = l2.map(_.asid)
2683ea4388cSHaoyuan Feng  val l2vmids = l2.map(_.vmid)
2693ea4388cSHaoyuan Feng  val l2h = Reg(Vec(l2tlbParams.l2Size, UInt(2.W)))
2703ea4388cSHaoyuan Feng
2713ea4388cSHaoyuan Feng  // l1: level 1 non-leaf pte
272abc4432bSHaoyuan Feng  val l1 = Module(new SplittedSRAM(
2733ea4388cSHaoyuan Feng    l1EntryType,
2743ea4388cSHaoyuan Feng    set = l2tlbParams.l1nSets,
2753ea4388cSHaoyuan Feng    way = l2tlbParams.l1nWays,
276d4265a7fSHaoyuan Feng    waySplit = 1,
277abc4432bSHaoyuan Feng    dataSplit = 4,
278abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
279*4b2c87baS梁森 Liang Sen    readMCP2 = false,
280*4b2c87baS梁森 Liang Sen    hasMbist = hasMbist
2816d5ddbceSLemover  ))
282*4b2c87baS梁森 Liang Sen  val mbistPlL1 = MbistPipeline.PlaceMbistPipeline(1, s"MbistPipePtwL1", hasMbist)
2833ea4388cSHaoyuan Feng  val l1v = RegInit(0.U((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2843ea4388cSHaoyuan Feng  val l1g = Reg(UInt((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2853ea4388cSHaoyuan Feng  val l1h = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(2.W))))
2863ea4388cSHaoyuan Feng  def getl1vSet(vpn: UInt) = {
2873ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2883ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2893ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2903ea4388cSHaoyuan Feng    val l1vVec = l1v.asTypeOf(Vec(l2tlbParams.l1nSets, UInt(l2tlbParams.l1nWays.W)))
2913ea4388cSHaoyuan Feng    l1vVec(set)
2926d5ddbceSLemover  }
2933ea4388cSHaoyuan Feng  def getl1hSet(vpn: UInt) = {
2943ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2953ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2963ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2973ea4388cSHaoyuan Feng    l1h(set)
29845f497a4Shappy-lx  }
2996d5ddbceSLemover
3003ea4388cSHaoyuan Feng  // l0: level 0 leaf pte of 4KB pages
301abc4432bSHaoyuan Feng  val l0 = Module(new SplittedSRAM(
3023ea4388cSHaoyuan Feng    l0EntryType,
3033ea4388cSHaoyuan Feng    set = l2tlbParams.l0nSets,
3043ea4388cSHaoyuan Feng    way = l2tlbParams.l0nWays,
305d4265a7fSHaoyuan Feng    waySplit = 2,
306abc4432bSHaoyuan Feng    dataSplit = 4,
307abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
308*4b2c87baS梁森 Liang Sen    readMCP2 = false,
309*4b2c87baS梁森 Liang Sen    hasMbist = hasMbist
3106d5ddbceSLemover  ))
311*4b2c87baS梁森 Liang Sen  val mbistPlL0 = MbistPipeline.PlaceMbistPipeline(1, s"MbistPipePtwL0", hasMbist)
3123ea4388cSHaoyuan Feng  val l0v = RegInit(0.U((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3133ea4388cSHaoyuan Feng  val l0g = Reg(UInt((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3143ea4388cSHaoyuan Feng  val l0h = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(2.W))))
3153ea4388cSHaoyuan Feng  def getl0vSet(vpn: UInt) = {
3163ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3173ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3183ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3193ea4388cSHaoyuan Feng    val l0vVec = l0v.asTypeOf(Vec(l2tlbParams.l0nSets, UInt(l2tlbParams.l0nWays.W)))
3203ea4388cSHaoyuan Feng    l0vVec(set)
3216d5ddbceSLemover  }
3223ea4388cSHaoyuan Feng  def getl0hSet(vpn: UInt) = {
3233ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3243ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3253ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3263ea4388cSHaoyuan Feng    l0h(set)
32745f497a4Shappy-lx  }
3286d5ddbceSLemover
3293ea4388cSHaoyuan Feng  // sp: level 1/2/3 leaf pte of 512GB/1GB/2MB super pages
330718a93f5SHaoyuan Feng  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true, hasNapot = true)))
3315854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
3325854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
3331dd3e32dSHaoyuan Feng  val spasids = sp.map(_.asid)
334d0de7e4aSpeixiaokun  val spvmids = sp.map(_.vmid)
335d61cd5eeSpeixiaokun  val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W)))
3366d5ddbceSLemover
3378882eb68SXin Tian  if (HasBitmapCheck) {
3388882eb68SXin Tian    // wakeup corresponding entry
3398882eb68SXin Tian    when (io.bitmap_wakeup.get.valid) {
3408882eb68SXin Tian      when (io.bitmap_wakeup.get.bits.isSp) {
3418882eb68SXin Tian        for (i <- 0 until l2tlbParams.spSize) {
3428882eb68SXin Tian          when (sp(i).tag === io.bitmap_wakeup.get.bits.tag && spv(i) === 1.U) {
3438882eb68SXin Tian            spBitmapReg(i) := io.bitmap_wakeup.get.bits.check_success
3448882eb68SXin Tian          }
3458882eb68SXin Tian        }
3468882eb68SXin Tian      } .otherwise {
3478882eb68SXin Tian        val wakeup_setindex = io.bitmap_wakeup.get.bits.setIndex
3488882eb68SXin Tian        l0BitmapReg(wakeup_setindex)(OHToUInt(io.bitmap_wakeup.get.bits.way_info))(io.bitmap_wakeup.get.bits.pte_index) := io.bitmap_wakeup.get.bits.check_success
3498882eb68SXin Tian        assert(l0v(wakeup_setindex * l2tlbParams.l0nWays.U + OHToUInt(io.bitmap_wakeup.get.bits.way_info)) === 1.U,
3508882eb68SXin Tian          "Wakeuped entry must be valid!")
3518882eb68SXin Tian      }
3528882eb68SXin Tian    }
3538882eb68SXin Tian  }
3548882eb68SXin Tian
3556d5ddbceSLemover  // Access Perf
3563ea4388cSHaoyuan Feng  val l3AccessPerf = if(EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
3573ea4388cSHaoyuan Feng  val l2AccessPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
3583ea4388cSHaoyuan Feng  val l1AccessPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
3593ea4388cSHaoyuan Feng  val l0AccessPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
3605854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
3613ea4388cSHaoyuan Feng  if (EnableSv48) l3AccessPerf.map(_.map(_ := false.B))
3626d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
3633ea4388cSHaoyuan Feng  l1AccessPerf.map(_ := false.B)
3643ea4388cSHaoyuan Feng  l0AccessPerf.map(_ := false.B)
3656d5ddbceSLemover  spAccessPerf.map(_ := false.B)
3666d5ddbceSLemover
3673889e11eSLemover
3681f4a7c0cSLemover
36982978df9Speixiaokun  def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = {
3703ea4388cSHaoyuan Feng    (vpn1(vpnLen-1, vpnnLen*level+3) === vpn2(vpnLen-1, vpnnLen*level+3))
3711f4a7c0cSLemover  }
3721f4a7c0cSLemover  // NOTE: not actually bypassed, just check if hit, re-access the page cache
373d0de7e4aSpeixiaokun  def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = {
3746f508cb5Speixiaokun    val change_h = MuxLookup(h_search, noS2xlate)(Seq(
375980ddf4cSpeixiaokun      allStage -> onlyStage1,
376980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
377980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
378980ddf4cSpeixiaokun    ))
3794ed5afbdSXiaokun-Pei    val change_refill_h = MuxLookup(io.refill.bits.req_info_dup(0).s2xlate, noS2xlate)(Seq(
3804ed5afbdSXiaokun-Pei      allStage -> onlyStage1,
3814ed5afbdSXiaokun-Pei      onlyStage1 -> onlyStage1,
3824ed5afbdSXiaokun-Pei      onlyStage2 -> onlyStage2
3834ed5afbdSXiaokun-Pei    ))
384d0de7e4aSpeixiaokun    val refill_vpn = io.refill.bits.req_info_dup(0).vpn
3854ed5afbdSXiaokun-Pei    io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === change_refill_h
3861f4a7c0cSLemover  }
3871f4a7c0cSLemover
38882978df9Speixiaokun  val vpn_search = stageReq.bits.req_info.vpn
3896f508cb5Speixiaokun  val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq(
39009280d15Speixiaokun    allStage -> onlyStage1,
39109280d15Speixiaokun    onlyStage1 -> onlyStage1,
39209280d15Speixiaokun    onlyStage2 -> onlyStage2
39309280d15Speixiaokun  ))
3943ea4388cSHaoyuan Feng
3953ea4388cSHaoyuan Feng  // l3
3963ea4388cSHaoyuan Feng  val l3Hit = if(EnableSv48) Some(Wire(Bool())) else None
3973ea4388cSHaoyuan Feng  val l3HitPPN = if(EnableSv48) Some(Wire(UInt(ppnLen.W))) else None
398002c10a4SYanqin Li  val l3HitPbmt = if(EnableSv48) Some(Wire(UInt(ptePbmtLen.W))) else None
3993ea4388cSHaoyuan Feng  val l3Pre = if(EnableSv48) Some(Wire(Bool())) else None
4003ea4388cSHaoyuan Feng  val ptwl3replace = if(EnableSv48) Some(ReplacementPolicy.fromString(l2tlbParams.l3Replacer, l2tlbParams.l3Size)) else None
4013ea4388cSHaoyuan Feng  if (EnableSv48) {
4023ea4388cSHaoyuan Feng    val hitVecT = l3.get.zipWithIndex.map {
40397929664SXiaokun-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)
4043ea4388cSHaoyuan Feng          && l3v.get(i) && h_search === l3h.get(i))
405d0de7e4aSpeixiaokun    }
4066c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4071f4a7c0cSLemover
4083ea4388cSHaoyuan Feng    // stageDelay, but check for l3
4093ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.ppn)), stageDelay_valid_1cycle)
410002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.pbmt)), stageDelay_valid_1cycle)
4113ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.prefetch)), stageDelay_valid_1cycle)
4121f4a7c0cSLemover    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4136d5ddbceSLemover
4143ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl3replace.get.access(OHToUInt(hitVec)) }
4156d5ddbceSLemover
4163ea4388cSHaoyuan Feng    l3AccessPerf.get.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4173ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l3Size) {
41897929664SXiaokun-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")
4196d5ddbceSLemover    }
4203ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l3] l3v:${Binary(l3v.get)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4213ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l3] l3Hit:${hit} l3HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4226d5ddbceSLemover
4233ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l3_hitVecT")
4243ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l3_hitVec")
4253ea4388cSHaoyuan Feng
4263ea4388cSHaoyuan Feng    // synchronize with other entries with RegEnable
4273ea4388cSHaoyuan Feng    l3Hit.map(_ := RegEnable(hit, stageDelay(1).fire))
4283ea4388cSHaoyuan Feng    l3HitPPN.map(_ := RegEnable(hitPPN, stageDelay(1).fire))
429002c10a4SYanqin Li    l3HitPbmt.map(_ := RegEnable(hitPbmt, stageDelay(1).fire))
4303ea4388cSHaoyuan Feng    l3Pre.map(_ := RegEnable(hitPre, stageDelay(1).fire))
4313ea4388cSHaoyuan Feng  }
4323ea4388cSHaoyuan Feng
4333ea4388cSHaoyuan Feng  // l2
4343ea4388cSHaoyuan Feng  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer, l2tlbParams.l2Size)
435002c10a4SYanqin Li  val (l2Hit, l2HitPPN, l2HitPbmt, l2Pre) = {
4363ea4388cSHaoyuan Feng    val hitVecT = l2.zipWithIndex.map {
43797929664SXiaokun-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)
4383ea4388cSHaoyuan Feng        && l2v(i) && h_search === l2h(i))
4393ea4388cSHaoyuan Feng    }
4403ea4388cSHaoyuan Feng    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4413ea4388cSHaoyuan Feng
4423ea4388cSHaoyuan Feng    // stageDelay, but check for l2
4433ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.ppn)), stageDelay_valid_1cycle)
444002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.pbmt)), stageDelay_valid_1cycle)
4453ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.prefetch)), stageDelay_valid_1cycle)
4463ea4388cSHaoyuan Feng    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4473ea4388cSHaoyuan Feng
4483ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl2replace.access(OHToUInt(hitVec)) }
4493ea4388cSHaoyuan Feng
4503ea4388cSHaoyuan Feng    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4513ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
45297929664SXiaokun-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")
4533ea4388cSHaoyuan Feng    }
4543ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l2] l2v:${Binary(l2v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4553ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4563ea4388cSHaoyuan Feng
4573ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l2_hitVecT")
4583ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l2_hitVec")
4596d5ddbceSLemover
4606c4dcc2dSLemover    // synchronize with other entries with RegEnable
4616c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
4626c4dcc2dSLemover     RegEnable(hitPPN, stageDelay(1).fire),
463002c10a4SYanqin Li     RegEnable(hitPbmt, stageDelay(1).fire),
4646c4dcc2dSLemover     RegEnable(hitPre, stageDelay(1).fire))
4656d5ddbceSLemover  }
4666d5ddbceSLemover
4673ea4388cSHaoyuan Feng  // l1
4683ea4388cSHaoyuan Feng  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer,l2tlbParams.l1nWays,l2tlbParams.l1nSets)
469002c10a4SYanqin Li  val (l1Hit, l1HitPPN, l1HitPbmt, l1Pre, l1eccError) = {
4703ea4388cSHaoyuan Feng    val ridx = genPtwL1SetIdx(vpn_search)
4713ea4388cSHaoyuan Feng    l1.io.r.req.valid := stageReq.fire
4723ea4388cSHaoyuan Feng    l1.io.r.req.bits.apply(setIdx = ridx)
4733ea4388cSHaoyuan Feng    val vVec_req = getl1vSet(vpn_search)
4743ea4388cSHaoyuan Feng    val hVec_req = getl1hSet(vpn_search)
4756c4dcc2dSLemover
4766c4dcc2dSLemover    // delay one cycle after sram read
47782978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
4786f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
479980ddf4cSpeixiaokun      allStage -> onlyStage1,
480980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
481980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
482980ddf4cSpeixiaokun    ))
4833ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l1.io.r.resp.data, stageDelay_valid_1cycle)
4847797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
485d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
486d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
48797929664SXiaokun-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)})
4886c4dcc2dSLemover
4896c4dcc2dSLemover    // check hit and ecc
49082978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
4916c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
492935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
4936c4dcc2dSLemover
4947797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
4957196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
4967196f5a2SLemover    val hitWayData = hitWayEntry.entries
4976c4dcc2dSLemover    val hit = ParallelOR(hitVec)
4983ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l1nWays).map(_.U(log2Up(l2tlbParams.l1nWays).W)))
499eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
500eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
501eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
502eef81af7SHaoyuan Feng    } else {
503eef81af7SHaoyuan Feng      eccError := false.B
504eef81af7SHaoyuan Feng    }
5057196f5a2SLemover
5063ea4388cSHaoyuan Feng    ridx.suggestName(s"l1_ridx")
5073ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l1_ramDatas")
5083ea4388cSHaoyuan Feng    hitVec.suggestName(s"l1_hitVec")
5093ea4388cSHaoyuan Feng    hitWayData.suggestName(s"l1_hitWayData")
5103ea4388cSHaoyuan Feng    hitWay.suggestName(s"l1_hitWay")
5116d5ddbceSLemover
5123ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl1replace.access(genPtwL1SetIdx(check_vpn), hitWay) }
5136d5ddbceSLemover
5143ea4388cSHaoyuan Feng    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5153ea4388cSHaoyuan Feng    XSDebug(stageDelay_valid_1cycle, p"[l1] ridx:0x${Hexadecimal(ridx)}\n")
5163ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
5173ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l1] ramDatas(${i.U}) ${ramDatas(i)}  l1v:${vVec(i)}  hit:${hit}\n")
5186d5ddbceSLemover    }
5193ea4388cSHaoyuan 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")
5206d5ddbceSLemover
521002c10a4SYanqin Li    (hit, hitWayData.ppns(genPtwL1SectorIdx(check_vpn)), hitWayData.pbmts(genPtwL1SectorIdx(check_vpn)), hitWayData.prefetch, eccError)
5226d5ddbceSLemover  }
523*4b2c87baS梁森 Liang Sen  val te = ClockGate.genTeSink
524*4b2c87baS梁森 Liang Sen  val l0_masked_clock = ClockGate(te.cgen, stageReq.fire | (!flush_dup(0) && refill.levelOH.l0) | mbistPlL0.map(_.mbist.req).getOrElse(false.B), clock)
525*4b2c87baS梁森 Liang Sen  val l1_masked_clock = ClockGate(te.cgen, stageReq.fire | (!flush_dup(1) && refill.levelOH.l1) | mbistPlL1.map(_.mbist.req).getOrElse(false.B), clock)
526b32e9518SHuijin Li  l0.clock := l0_masked_clock
527b32e9518SHuijin Li  l1.clock := l1_masked_clock
5283ea4388cSHaoyuan Feng  // l0
5293ea4388cSHaoyuan Feng  val ptwl0replace = ReplacementPolicy.fromString(l2tlbParams.l0Replacer,l2tlbParams.l0nWays,l2tlbParams.l0nSets)
5308882eb68SXin Tian  val (l0Hit, l0HitData, l0Pre, l0eccError, l0HitWay, l0BitmapCheckResult, l0JmpBitmapCheck) = {
5313ea4388cSHaoyuan Feng    val ridx = genPtwL0SetIdx(vpn_search)
5323ea4388cSHaoyuan Feng    l0.io.r.req.valid := stageReq.fire
5333ea4388cSHaoyuan Feng    l0.io.r.req.bits.apply(setIdx = ridx)
5343ea4388cSHaoyuan Feng    val vVec_req = getl0vSet(vpn_search)
5353ea4388cSHaoyuan Feng    val hVec_req = getl0hSet(vpn_search)
5366c4dcc2dSLemover
5376c4dcc2dSLemover    // delay one cycle after sram read
53882978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
5396f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
540980ddf4cSpeixiaokun      allStage -> onlyStage1,
541980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
542980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
543980ddf4cSpeixiaokun    ))
5443ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l0.io.r.resp.data, stageDelay_valid_1cycle)
5457797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
546d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
547d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
54897929664SXiaokun-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)})
5496c4dcc2dSLemover
5506c4dcc2dSLemover    // check hit and ecc
55182978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
5526c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
553935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
5546c4dcc2dSLemover
5557797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
5567196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
5577196f5a2SLemover    val hitWayData = hitWayEntry.entries
5587196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
5593ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l0nWays).map(_.U(log2Up(l2tlbParams.l0nWays).W)))
5608882eb68SXin Tian
5618882eb68SXin Tian    val ishptw = RegEnable(stageDelay(0).bits.isHptwReq,stageDelay(1).fire)
5628882eb68SXin Tian    val s2x_info = RegEnable(stageDelay(0).bits.req_info.s2xlate,stageDelay(1).fire)
5638882eb68SXin Tian    val pte_index = RegEnable(stageDelay(0).bits.req_info.vpn(sectortlbwidth - 1, 0),stageDelay(1).fire)
5648882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
5658882eb68SXin Tian    val hit = WireInit(false.B)
5668882eb68SXin Tian    val l0bitmapreg = WireInit((VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W)))))))
5678882eb68SXin Tian    if (HasBitmapCheck) {
5688882eb68SXin Tian      l0bitmapreg := RegEnable(RegNext(l0BitmapReg(ridx)), stageDelay(1).fire)
5698882eb68SXin Tian      // cause llptw will trigger bitmapcheck
5708882eb68SXin Tian      // add a coniditonal logic
5718882eb68SXin Tian      // (s2x_info =/= allStage || ishptw)
5728882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 1.U, ParallelOR(hitVec))
5738882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 0.U) {
5748882eb68SXin Tian        jmp_bitmap_check := true.B
5758882eb68SXin Tian      }
5768882eb68SXin Tian    } else {
5778882eb68SXin Tian      hit := ParallelOR(hitVec)
5788882eb68SXin Tian    }
579eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
580eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
581eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
582eef81af7SHaoyuan Feng    } else {
583eef81af7SHaoyuan Feng      eccError := false.B
584eef81af7SHaoyuan Feng    }
5856d5ddbceSLemover
5863ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl0replace.access(genPtwL0SetIdx(check_vpn), hitWay) }
5877196f5a2SLemover
5883ea4388cSHaoyuan Feng    l0AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5893ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l0] ridx:0x${Hexadecimal(ridx)}\n")
5903ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
5913ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l0] ramDatas(${i.U}) ${ramDatas(i)}  l0v:${vVec(i)}  hit:${hitVec(i)}\n")
5926d5ddbceSLemover    }
5933ea4388cSHaoyuan Feng    XSDebug(stageCheck_valid_1cycle, p"[l0] l0Hit:${hit} l0HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n")
5946d5ddbceSLemover
5953ea4388cSHaoyuan Feng    ridx.suggestName(s"l0_ridx")
5963ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l0_ramDatas")
5973ea4388cSHaoyuan Feng    hitVec.suggestName(s"l0_hitVec")
5983ea4388cSHaoyuan Feng    hitWay.suggestName(s"l0_hitWay")
5996d5ddbceSLemover
6008882eb68SXin Tian    (hit, hitWayData, hitWayData.prefetch, eccError, UIntToOH(hitWay), l0bitmapreg(hitWay), jmp_bitmap_check)
6016d5ddbceSLemover  }
6023ea4388cSHaoyuan Feng  val l0HitPPN = l0HitData.ppns
603002c10a4SYanqin Li  val l0HitPbmt = l0HitData.pbmts
6043ea4388cSHaoyuan Feng  val l0HitPerm = l0HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL0SectorSize, new PtePermBundle)))
605e0c1f271SHaoyuan Feng  val l0HitValid = VecInit(l0HitData.onlypf.map(!_))
6068882eb68SXin Tian  val l0Ptes = WireInit(VecInit(Seq.fill(tlbcontiguous)(0.U(XLEN.W)))) // L0 lavel Page Table Entry Vector
6078882eb68SXin Tian  val l0cfs = WireInit(VecInit(Seq.fill(tlbcontiguous)(false.B))) // L0 lavel Bitmap Check Failed Vector
6088882eb68SXin Tian  if (HasBitmapCheck) {
6098882eb68SXin Tian    for (i <- 0 until tlbcontiguous) {
6108882eb68SXin Tian      l0Ptes(i) := Cat(l0HitData.pbmts(i).asUInt,l0HitPPN(i), 0.U(2.W),l0HitPerm(i).asUInt,l0HitValid(i).asUInt)
6118882eb68SXin Tian      l0cfs(i) := !l0BitmapCheckResult(i)
6128882eb68SXin Tian    }
6138882eb68SXin Tian  }
6146d5ddbceSLemover
6156d5ddbceSLemover  // super page
6165854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
6178882eb68SXin Tian  val (spHit, spHitData, spPre, spValid, spJmpBitmapCheck) = {
618718a93f5SHaoyuan 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) }
6196c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
6206d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
6218882eb68SXin Tian    val ishptw = RegEnable(stageReq.bits.isHptwReq, stageReq.fire)
6228882eb68SXin Tian    val s2x_info = RegEnable(stageReq.bits.req_info.s2xlate, stageReq.fire)
6238882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
6248882eb68SXin Tian    val hit = WireInit(false.B)
6258882eb68SXin Tian    if (HasBitmapCheck) {
6268882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 1.U, ParallelOR(hitVec))
6278882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 0.U) {
6288882eb68SXin Tian        jmp_bitmap_check := true.B
6298882eb68SXin Tian      }
6308882eb68SXin Tian    } else {
6318882eb68SXin Tian      hit := ParallelOR(hitVec)
6328882eb68SXin Tian    }
6336d5ddbceSLemover
6346c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) }
6356d5ddbceSLemover
6366c4dcc2dSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle }
6375854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
63897929664SXiaokun-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")
6396d5ddbceSLemover    }
6406c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
6416d5ddbceSLemover
6426d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
6436d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
6446d5ddbceSLemover
6456c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
6466c4dcc2dSLemover     RegEnable(hitData, stageDelay(1).fire),
6476c4dcc2dSLemover     RegEnable(hitData.prefetch, stageDelay(1).fire),
6488882eb68SXin Tian     RegEnable(hitData.v, stageDelay(1).fire),
6498882eb68SXin Tian     RegEnable(jmp_bitmap_check, stageDelay(1).fire))
6506d5ddbceSLemover  }
6516d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
6526d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
6538882eb68SXin Tian  val spPte = Cat(spHitData.pbmt.asUInt,spHitData.ppn, 0.U(2.W), spHitPerm.asUInt,spHitData.v.asUInt) // Super-page Page Table Entry
6546d5ddbceSLemover
6556c4dcc2dSLemover  val check_res = Wire(new PageCacheRespBundle)
6567fbc7393SYanqin Li  check_res.l3.map(_.apply(l3Hit.get, l3Pre.get, l3HitPPN.get, l3HitPbmt.get))
657002c10a4SYanqin Li  check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, l2HitPbmt)
658002c10a4SYanqin Li  check_res.l1.apply(l1Hit, l1Pre, l1HitPPN, l1HitPbmt, ecc = l1eccError)
6598882eb68SXin Tian  check_res.l0.apply(l0Hit, l0Pre, l0HitPPN, l0HitPbmt, l0HitPerm, l0eccError, valid = l0HitValid, jmp_bitmap_check = l0JmpBitmapCheck, hitway = l0HitWay, ptes = l0Ptes, cfs = l0cfs)
6608882eb68SXin Tian  check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitData.pbmt, spHitData.n.getOrElse(0.U), spHitPerm, false.B, spHitLevel, spValid, spJmpBitmapCheck, spPte)
6616d5ddbceSLemover
6626c4dcc2dSLemover  val resp_res = Reg(new PageCacheRespBundle)
6636c4dcc2dSLemover  when (stageCheck(1).fire) { resp_res := check_res }
6643889e11eSLemover
6651f4a7c0cSLemover  // stageResp bypass
6663ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
6671f4a7c0cSLemover  bypassed.indices.foreach(i =>
6681f4a7c0cSLemover    bypassed(i) := stageResp.bits.bypassed(i) ||
6696967f5d5Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
6701f4a7c0cSLemover        OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid)
6711f4a7c0cSLemover  )
6721f4a7c0cSLemover
67383d93d53Speixiaokun  // stageResp bypass to hptw
6743ea4388cSHaoyuan Feng  val hptw_bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
67583d93d53Speixiaokun  hptw_bypassed.indices.foreach(i =>
67683d93d53Speixiaokun    hptw_bypassed(i) := stageResp.bits.bypassed(i) ||
67783d93d53Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
67883d93d53Speixiaokun        io.resp.fire)
67983d93d53Speixiaokun  )
68083d93d53Speixiaokun
68130104977Speixiaokun  val isAllStage = stageResp.bits.req_info.s2xlate === allStage
682c0991f6aSpeixiaokun  val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2
6833ea4388cSHaoyuan Feng  val stage1Hit = (resp_res.l0.hit || resp_res.sp.hit) && isAllStage
684da605600Speixiaokun  val idx = stageResp.bits.req_info.vpn(2, 0)
6853ea4388cSHaoyuan Feng  val stage1Pf = !Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
6866c4dcc2dSLemover  io.resp.bits.req_info   := stageResp.bits.req_info
6876c4dcc2dSLemover  io.resp.bits.isFirst  := stageResp.bits.isFirst
6883ea4388cSHaoyuan Feng  io.resp.bits.hit      := (resp_res.l0.hit || resp_res.sp.hit) && (!isAllStage || isAllStage && stage1Pf)
6893ea4388cSHaoyuan Feng  if (EnableSv48) {
69026175c3fSHaoyuan 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
6913ea4388cSHaoyuan Feng  } else {
69226175c3fSHaoyuan Feng    io.resp.bits.bypassed := ((bypassed(0) && !resp_res.l0.hit) || (bypassed(1) && !resp_res.l1.hit) || (bypassed(2) && !resp_res.l2.hit)) && !isAllStage
6933ea4388cSHaoyuan Feng  }
6943ea4388cSHaoyuan Feng  io.resp.bits.prefetch := resp_res.l0.pre && resp_res.l0.hit || resp_res.sp.pre && resp_res.sp.hit
6953ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l3Hit.map(_ := resp_res.l3.get.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq)
696325f0a4eSpeixiaokun  io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
6973ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
6983ea4388cSHaoyuan 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))
699980ddf4cSpeixiaokun  io.resp.bits.toFsm.stage1Hit := stage1Hit
7008882eb68SXin Tian  if (HasBitmapCheck) {
7018882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.jmp_bitmap_check := resp_res.l0.bitmapCheck.get.jmp_bitmap_check || resp_res.sp.bitmapCheck.get.jmp_bitmap_check
7028882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.toLLPTW := resp_res.l0.bitmapCheck.get.jmp_bitmap_check && (stageResp.bits.req_info.s2xlate === noS2xlate || stageResp.bits.req_info.s2xlate === onlyStage1)
7038882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
7048882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
7058882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
7068882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
7078882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.SPlevel := resp_res.sp.level
7088882eb68SXin Tian  }
709d0de7e4aSpeixiaokun
710325f0a4eSpeixiaokun  io.resp.bits.isHptwReq := stageResp.bits.isHptwReq
7113ea4388cSHaoyuan Feng  if (EnableSv48) {
71226175c3fSHaoyuan 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
7133ea4388cSHaoyuan Feng  } else {
71426175c3fSHaoyuan 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
7153ea4388cSHaoyuan Feng  }
716d0de7e4aSpeixiaokun  io.resp.bits.toHptw.id := stageResp.bits.hptwId
7173ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l3Hit.map(_ := resp_res.l3.get.hit && stageResp.bits.isHptwReq)
718325f0a4eSpeixiaokun  io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq
7193ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq
7203ea4388cSHaoyuan 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)
72182978df9Speixiaokun  io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn
722eb4bf3f2Speixiaokun  io.resp.bits.toHptw.resp.entry.asid := DontCare
72397929664SXiaokun-Pei  io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.vmid)
7243ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l0.hit, 0.U, resp_res.sp.level))
725d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source)
7263ea4388cSHaoyuan 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)
727002c10a4SYanqin Li  io.resp.bits.toHptw.resp.entry.pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(idx), resp_res.sp.pbmt)
728718a93f5SHaoyuan Feng  io.resp.bits.toHptw.resp.entry.n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
7293ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l0.hit, resp_res.l0.perm(idx), resp_res.sp.perm))
7303ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
731d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v
732e0c1f271SHaoyuan Feng  io.resp.bits.toHptw.resp.gaf := false.B
7338882eb68SXin Tian  if (HasBitmapCheck) {
7348882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.jmp_bitmap_check := resp_res.l0.bitmapCheck.get.jmp_bitmap_check || resp_res.sp.bitmapCheck.get.jmp_bitmap_check
7358882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
7368882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
7378882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
7388882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
7398882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.fromSP := resp_res.sp.bitmapCheck.get.jmp_bitmap_check
7408882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.SPlevel := resp_res.sp.level
7418882eb68SXin Tian  }
742d0de7e4aSpeixiaokun
7436979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3))
7446979864eSXiaokun-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
74597929664SXiaokun-Pei  io.resp.bits.stage1.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.vmid))
7463ea4388cSHaoyuan Feng  if (EnableSv48) {
7473ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
7483ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
7493ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U,
7503ea4388cSHaoyuan Feng          Mux(resp_res.l2.hit, 2.U, 3.U))))))
7513ea4388cSHaoyuan Feng  } else {
7523ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
7533ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
7543ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U, 2.U)))))
7553ea4388cSHaoyuan Feng  }
7566979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source))
75763632028SHaoyuan Feng  for (i <- 0 until tlbcontiguous) {
7583ea4388cSHaoyuan Feng    if (EnableSv48) {
7593ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7603ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7613ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7623ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth),
7633ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(gvpnLen - 1, sectortlbwidth)))))
7643ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7653ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7663ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7673ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(sectortlbwidth - 1, 0),
7683ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(sectortlbwidth - 1, 0)))))
7693ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7703ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7713ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7723ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.v,
7733ea4388cSHaoyuan Feng              resp_res.l3.get.v))))
7743ea4388cSHaoyuan Feng    } else {
7753ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7763ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7773ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7783ea4388cSHaoyuan Feng            resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth))))
7793ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7803ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7813ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7823ea4388cSHaoyuan Feng            resp_res.l2.ppn(sectortlbwidth - 1, 0))))
7833ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7843ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7853ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7863ea4388cSHaoyuan Feng            resp_res.l2.v)))
7873ea4388cSHaoyuan Feng    }
788002c10a4SYanqin Li    io.resp.bits.stage1.entry(i).pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(i),
789002c10a4SYanqin Li      Mux(resp_res.sp.hit, resp_res.sp.pbmt,
790002c10a4SYanqin Li        Mux(resp_res.l1.hit, resp_res.l1.pbmt,
791002c10a4SYanqin Li          resp_res.l2.pbmt)))
792718a93f5SHaoyuan Feng    io.resp.bits.stage1.entry(i).n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
79397929664SXiaokun-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))))
7946979864eSXiaokun-Pei    io.resp.bits.stage1.entry(i).pf := !io.resp.bits.stage1.entry(i).v
795e0c1f271SHaoyuan Feng    io.resp.bits.stage1.entry(i).af := false.B
7968882eb68SXin Tian    io.resp.bits.stage1.entry(i).cf := l0cfs(i) // L0 lavel Bitmap Check Failed Vector
79763632028SHaoyuan Feng  }
798da605600Speixiaokun  io.resp.bits.stage1.pteidx := UIntToOH(idx).asBools
7993ea4388cSHaoyuan Feng  io.resp.bits.stage1.not_super := Mux(resp_res.l0.hit, true.B, false.B)
8006962b4ffSHaoyuan Feng  io.resp.bits.stage1.not_merge := false.B
8016c4dcc2dSLemover  io.resp.valid := stageResp.valid
8023ea4388cSHaoyuan Feng  XSError(stageResp.valid && resp_res.l0.hit && resp_res.sp.hit, "normal page and super page both hit")
8036d5ddbceSLemover
8046d5ddbceSLemover  // refill Perf
8053ea4388cSHaoyuan Feng  val l3RefillPerf = if (EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
8063ea4388cSHaoyuan Feng  val l2RefillPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
8073ea4388cSHaoyuan Feng  val l1RefillPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
8083ea4388cSHaoyuan Feng  val l0RefillPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
8095854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
8103ea4388cSHaoyuan Feng  l3RefillPerf.map(_.map(_ := false.B))
8116d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
8123ea4388cSHaoyuan Feng  l1RefillPerf.map(_ := false.B)
8133ea4388cSHaoyuan Feng  l0RefillPerf.map(_ := false.B)
8146d5ddbceSLemover  spRefillPerf.map(_ := false.B)
8156d5ddbceSLemover
8166d5ddbceSLemover  // refill
8173ea4388cSHaoyuan Feng  l1.io.w.req <> DontCare
8183ea4388cSHaoyuan Feng  l0.io.w.req <> DontCare
8193ea4388cSHaoyuan Feng  l1.io.w.req.valid := false.B
8203ea4388cSHaoyuan Feng  l0.io.w.req.valid := false.B
8216d5ddbceSLemover
8226d5ddbceSLemover  val memRdata = refill.ptes
8235854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
8247797f035SbugGenerator  val memSelData = io.refill.bits.sel_pte_dup
8257797f035SbugGenerator  val memPte = memSelData.map(a => a.asTypeOf(new PteBundle))
826dd286b6aSYanqin Li  val mPBMTE = io.csr.mPBMTE
827dd286b6aSYanqin Li  val hPBMTE = io.csr.hPBMTE
828dd286b6aSYanqin Li  val pbmte = Mux(refill.req_info_dup(0).s2xlate === onlyStage1 || refill.req_info_dup(0).s2xlate === allStage, hPBMTE, mPBMTE)
829b848eea5SLemover
8308882eb68SXin Tian  def Tran2D(flushMask: UInt): Vec[UInt] = {
8318882eb68SXin Tian    val tran2D = Wire(Vec(l2tlbParams.l0nSets,UInt(l2tlbParams.l0nWays.W)))
8328882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
8338882eb68SXin Tian      tran2D(i) := flushMask((i + 1) * l2tlbParams.l0nWays - 1, i * l2tlbParams.l0nWays)
8348882eb68SXin Tian    }
8358882eb68SXin Tian    tran2D
8368882eb68SXin Tian  }
8378882eb68SXin Tian  def updateL0BitmapReg(l0BitmapReg: Vec[Vec[Vec[UInt]]], tran2D: Vec[UInt]) = {
8388882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
8398882eb68SXin Tian      for (j <- 0 until l2tlbParams.l0nWays) {
8408882eb68SXin Tian        when (tran2D(i)(j) === 0.U) {
8418882eb68SXin Tian          for (k <- 0 until tlbcontiguous) {
8428882eb68SXin Tian            l0BitmapReg(i)(j)(k) := 0.U
8438882eb68SXin Tian          }
8448882eb68SXin Tian        }
8458882eb68SXin Tian      }
8468882eb68SXin Tian    }
8478882eb68SXin Tian  }
8488882eb68SXin Tian  def TranVec(flushMask: UInt): Vec[UInt] = {
8498882eb68SXin Tian    val vec = Wire(Vec(l2tlbParams.spSize,UInt(1.W)))
8508882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
8518882eb68SXin Tian      vec(i) := flushMask(i)
8528882eb68SXin Tian    }
8538882eb68SXin Tian    vec
8548882eb68SXin Tian  }
8558882eb68SXin Tian  def updateSpBitmapReg(spBitmapReg: Vec[UInt], vec : Vec[UInt]) = {
8568882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
8578882eb68SXin Tian      spBitmapReg(i) := spBitmapReg(i) & vec(i)
8588882eb68SXin Tian    }
8598882eb68SXin Tian  }
8608882eb68SXin Tian
8616d5ddbceSLemover  // TODO: handle sfenceLatch outsize
8623ea4388cSHaoyuan Feng  if (EnableSv48) {
8638b33cd30Sklin02    val l3Refill =
8646962b4ffSHaoyuan Feng      !flush_dup(2) &&
8656962b4ffSHaoyuan Feng      refill.levelOH.l3.get &&
8666962b4ffSHaoyuan Feng      !memPte(2).isLeaf() &&
867e0c1f271SHaoyuan Feng      memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
8688b33cd30Sklin02    val l3RefillIdx = replaceWrapper(l3v.get, ptwl3replace.get.way).suggestName(s"l3_refillIdx")
8698b33cd30Sklin02    val l3RfOH = UIntToOH(l3RefillIdx).asUInt.suggestName(s"l3_rfOH")
8708b33cd30Sklin02    when (l3Refill) {
8718b33cd30Sklin02      l3.get(l3RefillIdx).refill(
8723ea4388cSHaoyuan Feng        refill.req_info_dup(2).vpn,
8733ea4388cSHaoyuan Feng        Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
87497929664SXiaokun-Pei        io.csr_dup(2).hgatp.vmid,
8753ea4388cSHaoyuan Feng        memSelData(2),
8763ea4388cSHaoyuan Feng        3.U,
8773ea4388cSHaoyuan Feng        refill_prefetch_dup(2)
87845f497a4Shappy-lx      )
8798b33cd30Sklin02      ptwl2replace.access(l3RefillIdx)
8808b33cd30Sklin02      l3v.get := l3v.get | l3RfOH
8818b33cd30Sklin02      l3g.get := (l3g.get & ~l3RfOH) | Mux(memPte(2).perm.g, l3RfOH, 0.U)
8828b33cd30Sklin02      l3h.get(l3RefillIdx) := refill_h(2)
8836d5ddbceSLemover
8843ea4388cSHaoyuan Feng      for (i <- 0 until l2tlbParams.l3Size) {
8858b33cd30Sklin02        l3RefillPerf.get(i) := i.U === l3RefillIdx
8866d5ddbceSLemover      }
8873ea4388cSHaoyuan Feng    }
8888b33cd30Sklin02    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")
8898b33cd30Sklin02    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")
8906d5ddbceSLemover  }
8916d5ddbceSLemover
8926962b4ffSHaoyuan Feng  // L2 refill
8938b33cd30Sklin02  val l2Refill =
8946962b4ffSHaoyuan Feng    !flush_dup(2) &&
8956962b4ffSHaoyuan Feng    refill.levelOH.l2 &&
8966962b4ffSHaoyuan Feng    !memPte(2).isLeaf() &&
897e0c1f271SHaoyuan Feng    memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
8988b33cd30Sklin02  val l2RefillIdx = replaceWrapper(l2v, ptwl2replace.way).suggestName(s"l2_refillIdx")
8998b33cd30Sklin02  val l2RfOH = UIntToOH(l2RefillIdx).asUInt.suggestName(s"l2_rfOH")
9008b33cd30Sklin02  when (
9018b33cd30Sklin02    l2Refill
902dd286b6aSYanqin Li  ) {
9038b33cd30Sklin02    l2(l2RefillIdx).refill(
9043ea4388cSHaoyuan Feng      refill.req_info_dup(2).vpn,
9053ea4388cSHaoyuan Feng      Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
90697929664SXiaokun-Pei      io.csr_dup(2).hgatp.vmid,
9073ea4388cSHaoyuan Feng      memSelData(2),
9083ea4388cSHaoyuan Feng      2.U,
9093ea4388cSHaoyuan Feng      refill_prefetch_dup(2)
9103ea4388cSHaoyuan Feng    )
9118b33cd30Sklin02    ptwl2replace.access(l2RefillIdx)
9128b33cd30Sklin02    l2v := l2v | l2RfOH
9138b33cd30Sklin02    l2g := (l2g & ~l2RfOH) | Mux(memPte(2).perm.g, l2RfOH, 0.U)
9148b33cd30Sklin02    l2h(l2RefillIdx) := refill_h(2)
9153ea4388cSHaoyuan Feng
9163ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
9178b33cd30Sklin02      l2RefillPerf(i) := i.U === l2RefillIdx
9183ea4388cSHaoyuan Feng    }
9193ea4388cSHaoyuan Feng  }
9208b33cd30Sklin02  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")
9218b33cd30Sklin02  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")
9223ea4388cSHaoyuan Feng
9236962b4ffSHaoyuan Feng  // L1 refill
9248b33cd30Sklin02  val l1Refill = !flush_dup(1) && refill.levelOH.l1
9258b33cd30Sklin02  val l1RefillIdx = genPtwL1SetIdx(refill.req_info_dup(1).vpn).suggestName(s"l1_refillIdx")
9268b33cd30Sklin02  val l1VictimWay = replaceWrapper(getl1vSet(refill.req_info_dup(1).vpn), ptwl1replace.way(l1RefillIdx)).suggestName(s"l1_victimWay")
9278b33cd30Sklin02  val l1VictimWayOH = UIntToOH(l1VictimWay).suggestName(s"l1_victimWayOH")
9288b33cd30Sklin02  val l1RfvOH = UIntToOH(Cat(l1RefillIdx, l1VictimWay)).asUInt.suggestName(s"l1_rfvOH")
9298b33cd30Sklin02  val l1Wdata = Wire(l1EntryType)
9308b33cd30Sklin02  l1Wdata.gen(
93182978df9Speixiaokun    vpn = refill.req_info_dup(1).vpn,
932cca17e78Speixiaokun    asid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid),
93397929664SXiaokun-Pei    vmid = io.csr_dup(1).hgatp.vmid,
93445f497a4Shappy-lx    data = memRdata,
93545f497a4Shappy-lx    levelUInt = 1.U,
9364ed5afbdSXiaokun-Pei    refill_prefetch_dup(1),
937dd286b6aSYanqin Li    refill.req_info_dup(1).s2xlate,
938e0c1f271SHaoyuan Feng    pbmte,
939e0c1f271SHaoyuan Feng    io.csr_dup(1).vsatp.mode
94045f497a4Shappy-lx  )
9418b33cd30Sklin02  when (l1Refill) {
9423ea4388cSHaoyuan Feng    l1.io.w.apply(
9436d5ddbceSLemover      valid = true.B,
9448b33cd30Sklin02      setIdx = l1RefillIdx,
9458b33cd30Sklin02      data = l1Wdata,
9468b33cd30Sklin02      waymask = l1VictimWayOH
9476d5ddbceSLemover    )
9488b33cd30Sklin02    ptwl1replace.access(l1RefillIdx, l1VictimWay)
9498b33cd30Sklin02    l1v := l1v | l1RfvOH
9508b33cd30Sklin02    l1g := l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U)
9518b33cd30Sklin02    l1h(l1RefillIdx)(l1VictimWay) := refill_h(1)
9526d5ddbceSLemover
9533ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
9548b33cd30Sklin02      l1RefillPerf(i) := i.U === l1VictimWay
9556d5ddbceSLemover    }
9566d5ddbceSLemover  }
9578b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refillIdx:0x${Hexadecimal(l1RefillIdx)} victimWay:${l1VictimWay} victimWayOH:${Binary(l1VictimWayOH)} rfvOH(in UInt):${Cat(l1RefillIdx, l1VictimWay)}\n")
9588b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refilldata:0x${l1Wdata}\n")
9598b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1v:${Binary(l1v)} -> ${Binary(l1v | l1RfvOH)}\n")
9608b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1g:${Binary(l1g)} -> ${Binary(l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U))}\n")
9616d5ddbceSLemover
9626962b4ffSHaoyuan Feng  // L0 refill
963718a93f5SHaoyuan Feng  val l0Refill = !flush_dup(0) && refill.levelOH.l0 && !memPte(0).isNapot(refill.level_dup(0))
9648b33cd30Sklin02  val l0RefillIdx = genPtwL0SetIdx(refill.req_info_dup(0).vpn).suggestName(s"l0_refillIdx")
9658b33cd30Sklin02  val l0VictimWay = replaceWrapper(getl0vSet(refill.req_info_dup(0).vpn), ptwl0replace.way(l0RefillIdx)).suggestName(s"l0_victimWay")
9668b33cd30Sklin02  val l0VictimWayOH = UIntToOH(l0VictimWay).asUInt.suggestName(s"l0_victimWayOH")
9678b33cd30Sklin02  val l0RfvOH = UIntToOH(Cat(l0RefillIdx, l0VictimWay)).suggestName(s"l0_rfvOH")
9688b33cd30Sklin02  val l0Wdata = Wire(l0EntryType)
9698882eb68SXin Tian  // trans the l0 way info, for late wakeup logic
9708882eb68SXin Tian  if (HasBitmapCheck) {
9718882eb68SXin Tian    io.l0_way_info.get := l0VictimWayOH
9728882eb68SXin Tian  }
9738b33cd30Sklin02  l0Wdata.gen(
9743ea4388cSHaoyuan Feng    vpn = refill.req_info_dup(0).vpn,
9753ea4388cSHaoyuan Feng    asid = Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
97697929664SXiaokun-Pei    vmid = io.csr_dup(0).hgatp.vmid,
97745f497a4Shappy-lx    data = memRdata,
9783ea4388cSHaoyuan Feng    levelUInt = 0.U,
9793ea4388cSHaoyuan Feng    refill_prefetch_dup(0),
980dd286b6aSYanqin Li    refill.req_info_dup(0).s2xlate,
981e0c1f271SHaoyuan Feng    pbmte,
982e0c1f271SHaoyuan Feng    io.csr_dup(0).vsatp.mode
98345f497a4Shappy-lx  )
9848b33cd30Sklin02  when (l0Refill) {
9853ea4388cSHaoyuan Feng    l0.io.w.apply(
9866d5ddbceSLemover      valid = true.B,
9878b33cd30Sklin02      setIdx = l0RefillIdx,
9888b33cd30Sklin02      data = l0Wdata,
9898b33cd30Sklin02      waymask = l0VictimWayOH
9906d5ddbceSLemover    )
9918b33cd30Sklin02    ptwl0replace.access(l0RefillIdx, l0VictimWay)
9928b33cd30Sklin02    l0v := l0v | l0RfvOH
9938b33cd30Sklin02    l0g := l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U)
9948b33cd30Sklin02    l0h(l0RefillIdx)(l0VictimWay) := refill_h(0)
9958882eb68SXin Tian    if (HasBitmapCheck) {updateL0BitmapReg(l0BitmapReg, Tran2D(~l0RfvOH))}
9966d5ddbceSLemover
9973ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
9988b33cd30Sklin02      l0RefillPerf(i) := i.U === l0VictimWay
9996d5ddbceSLemover    }
10006d5ddbceSLemover  }
10018b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refillIdx:0x${Hexadecimal(l0RefillIdx)} victimWay:${l0VictimWay} victimWayOH:${Binary(l0VictimWayOH)} rfvOH(in UInt):${Cat(l0RefillIdx, l0VictimWay)}\n")
10028b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refilldata:0x${l0Wdata}\n")
10038b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0v:${Binary(l0v)} -> ${Binary(l0v | l0RfvOH)}\n")
10048b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0g:${Binary(l0g)} -> ${Binary(l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U))}\n")
10057797f035SbugGenerator
10068d8ac704SLemover
10078d8ac704SLemover  // misc entries: super & invalid
10088b33cd30Sklin02  val spRefill =
10096962b4ffSHaoyuan Feng    !flush_dup(0) &&
10100a56a7dcSHaoyuan Feng    (refill.levelOH.sp || (refill.levelOH.l0 && memPte(0).isNapot(refill.level_dup(0)))) &&
1011e0c1f271SHaoyuan Feng    ((memPte(0).isLeaf() && memPte(0).canRefill(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte, io.csr_dup(0).vsatp.mode)) ||
1012e0c1f271SHaoyuan Feng    memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte))
10138b33cd30Sklin02  val spRefillIdx = spreplace.way.suggestName(s"sp_refillIdx") // LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
10148b33cd30Sklin02  val spRfOH = UIntToOH(spRefillIdx).asUInt.suggestName(s"sp_rfOH")
10158b33cd30Sklin02  when (spRefill) {
10168b33cd30Sklin02    sp(spRefillIdx).refill(
10177797f035SbugGenerator      refill.req_info_dup(0).vpn,
1018b188e334Speixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
101997929664SXiaokun-Pei      io.csr_dup(0).hgatp.vmid,
10207797f035SbugGenerator      memSelData(0),
10213ea4388cSHaoyuan Feng      refill.level_dup(0),
10227797f035SbugGenerator      refill_prefetch_dup(0),
1023e0c1f271SHaoyuan Feng      !memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte)
102445f497a4Shappy-lx    )
10258b33cd30Sklin02    spreplace.access(spRefillIdx)
10268b33cd30Sklin02    spv := spv | spRfOH
10278b33cd30Sklin02    spg := spg & ~spRfOH | Mux(memPte(0).perm.g, spRfOH, 0.U)
10288b33cd30Sklin02    sph(spRefillIdx) := refill_h(0)
10298882eb68SXin Tian    if (HasBitmapCheck) {updateSpBitmapReg(spBitmapReg, TranVec(~spRfOH))}
10306d5ddbceSLemover
10315854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
10328b33cd30Sklin02      spRefillPerf(i) := i.U === spRefillIdx
10336d5ddbceSLemover    }
10346d5ddbceSLemover  }
10358b33cd30Sklin02  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")
10368b33cd30Sklin02  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")
10376d5ddbceSLemover
10383ea4388cSHaoyuan Feng  val l1eccFlush = resp_res.l1.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l1eccError, init = false.B)
10393ea4388cSHaoyuan Feng  val l0eccFlush = resp_res.l0.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l0eccError, init = false.B)
10406c4dcc2dSLemover  val eccVpn = stageResp.bits.req_info.vpn
10417196f5a2SLemover
10423ea4388cSHaoyuan Feng  XSError(l1eccFlush, "l2tlb.cache.l1 ecc error. Should not happen at sim stage")
10433ea4388cSHaoyuan Feng  XSError(l0eccFlush, "l2tlb.cache.l0 ecc error. Should not happen at sim stage")
10443ea4388cSHaoyuan Feng  when (l1eccFlush) {
10453ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL1SetIdx(eccVpn))
10463ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l1nWays, a.asUInt) }).asUInt
10473ea4388cSHaoyuan Feng    l1v := l1v & ~flushMask
10483ea4388cSHaoyuan Feng    l1g := l1g & ~flushMask
10497196f5a2SLemover  }
10507196f5a2SLemover
10513ea4388cSHaoyuan Feng  when (l0eccFlush) {
10523ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(eccVpn))
10533ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
10543ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask
10553ea4388cSHaoyuan Feng    l0g := l0g & ~flushMask
10567196f5a2SLemover  }
10577196f5a2SLemover
10583ea4388cSHaoyuan Feng  // sfence for l0
10593ea4388cSHaoyuan Feng  val sfence_valid_l0 = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
10603ea4388cSHaoyuan Feng  when (sfence_valid_l0) {
10613ea4388cSHaoyuan 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
10623ea4388cSHaoyuan Feng    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
10633ea4388cSHaoyuan Feng    when (sfence_dup(0).bits.rs1/*va*/) {
10643ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
10657797f035SbugGenerator        // all va && all asid
10663ea4388cSHaoyuan Feng        l0v := l0v & ~l0hhit
10677797f035SbugGenerator      } .otherwise {
10687797f035SbugGenerator        // all va && specific asid except global
10693ea4388cSHaoyuan Feng        l0v := l0v & (l0g | ~l0hhit)
10707797f035SbugGenerator      }
10717797f035SbugGenerator    } .otherwise {
10723ea4388cSHaoyuan Feng      // val flushMask = UIntToOH(genTlbl1Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
10733ea4388cSHaoyuan Feng      val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(sfence_vpn))
10743ea4388cSHaoyuan Feng      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l0nWays, _.asUInt))).asUInt
10753ea4388cSHaoyuan Feng      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
10767797f035SbugGenerator      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
10777797f035SbugGenerator      flushMask.suggestName(s"sfence_nrs1_flushMask")
10787797f035SbugGenerator
10793ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
10807797f035SbugGenerator        // specific leaf of addr && all asid
10813ea4388cSHaoyuan Feng        l0v := l0v & ~flushMask & ~l0hhit
10827797f035SbugGenerator      } .otherwise {
10837797f035SbugGenerator        // specific leaf of addr && specific asid
10843ea4388cSHaoyuan Feng        l0v := l0v & (~flushMask | l0g | ~l0hhit)
10857797f035SbugGenerator      }
10867797f035SbugGenerator    }
10877797f035SbugGenerator  }
10887797f035SbugGenerator
10893ea4388cSHaoyuan Feng  // hfencev, simple implementation for l0
10903ea4388cSHaoyuan Feng  val hfencev_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hv
10913ea4388cSHaoyuan Feng  when(hfencev_valid_l0) {
10923ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_  === onlyStage1))).asUInt
10933ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all VS-stage l0 pte
1094d0de7e4aSpeixiaokun  }
1095d0de7e4aSpeixiaokun
10963ea4388cSHaoyuan Feng  // hfenceg, simple implementation for l0
10973ea4388cSHaoyuan Feng  val hfenceg_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hg
10983ea4388cSHaoyuan Feng  when(hfenceg_valid_l0) {
10993ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_ === onlyStage2))).asUInt
11003ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all G-stage l0 pte
1101d0de7e4aSpeixiaokun  }
1102d0de7e4aSpeixiaokun
11033ea4388cSHaoyuan Feng  val l2asidhit = VecInit(l2asids.map(_ === sfence_dup(2).bits.id)).asUInt
1104d0de7e4aSpeixiaokun  val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt
1105d0de7e4aSpeixiaokun  val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
1106d0de7e4aSpeixiaokun  when (sfence_valid) {
110797929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
110897929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
11093ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
1110e5da58f0Speixiaokun    val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
11113ea4388cSHaoyuan 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
11127797f035SbugGenerator    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
11137797f035SbugGenerator
11147797f035SbugGenerator    when (sfence_dup(0).bits.rs1/*va*/) {
11157797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11166d5ddbceSLemover        // all va && all asid
11173ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11183ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & VecInit(l2vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt)
1119447c794eSpeixiaokun        spv := spv & ~(sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
11206d5ddbceSLemover      } .otherwise {
11216d5ddbceSLemover        // all va && specific asid except global
11223ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
11233ea4388cSHaoyuan 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)
11245f64f303Speixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
11256d5ddbceSLemover      }
11266d5ddbceSLemover    } .otherwise {
11277797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11286d5ddbceSLemover        // specific leaf of addr && all asid
112997929664SXiaokun-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)
11306d5ddbceSLemover      } .otherwise {
11316d5ddbceSLemover        // specific leaf of addr && specific asid
113297929664SXiaokun-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)
1133d0de7e4aSpeixiaokun      }
1134d0de7e4aSpeixiaokun    }
1135d0de7e4aSpeixiaokun  }
1136d0de7e4aSpeixiaokun
1137d0de7e4aSpeixiaokun  val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv
1138d0de7e4aSpeixiaokun  when (hfencev_valid) {
113997929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
114097929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
11413ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage1)).asUInt
1142cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt
11433ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage1))).asUInt
1144d0de7e4aSpeixiaokun    val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
1145d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1146d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
11473ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11483ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1149447c794eSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1150d0de7e4aSpeixiaokun      }.otherwise {
11513ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
11523ea4388cSHaoyuan Feng        l2v := l2v & ~(~l2g & l2hhit & l2asidhit & l2vmidhit)
1153d0de7e4aSpeixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & spvmidhit)
1154d0de7e4aSpeixiaokun      }
1155d0de7e4aSpeixiaokun    }.otherwise {
1156d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
115797929664SXiaokun-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)
1158d0de7e4aSpeixiaokun      }.otherwise {
115997929664SXiaokun-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)
1160d0de7e4aSpeixiaokun      }
1161d0de7e4aSpeixiaokun    }
1162d0de7e4aSpeixiaokun  }
1163d0de7e4aSpeixiaokun
1164d0de7e4aSpeixiaokun
1165d0de7e4aSpeixiaokun  val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg
1166d0de7e4aSpeixiaokun  when(hfenceg_valid) {
11673ea4388cSHaoyuan Feng    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
1168cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
11693ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage2)).asUInt
1170cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt
11713ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage2))).asUInt
1172887df0f4Speixiaokun    val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen)
1173d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1174d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1175d0de7e4aSpeixiaokun        l1v := l1v & ~l1hhit
11763ea4388cSHaoyuan Feng        l2v := l2v & ~l2hhit
1177d0de7e4aSpeixiaokun        spv := spv & ~sphhit
1178d0de7e4aSpeixiaokun      }.otherwise {
11793ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11803ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1181d0de7e4aSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1182d0de7e4aSpeixiaokun      }
1183d0de7e4aSpeixiaokun    }.otherwise {
1184d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
11858fe4f15fSXiaokun-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)
1186d0de7e4aSpeixiaokun      }.otherwise {
11878fe4f15fSXiaokun-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)
11886d5ddbceSLemover      }
11896d5ddbceSLemover    }
11906d5ddbceSLemover  }
11916d5ddbceSLemover
11923ea4388cSHaoyuan Feng  if (EnableSv48) {
11933ea4388cSHaoyuan Feng    val l3asidhit = VecInit(l3asids.get.map(_ === sfence_dup(2).bits.id)).asUInt
119497929664SXiaokun-Pei    val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
11953ea4388cSHaoyuan 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
11963ea4388cSHaoyuan Feng
11973ea4388cSHaoyuan Feng    when (sfence_valid) {
119897929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
11993ea4388cSHaoyuan 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
12003ea4388cSHaoyuan Feng      val sfence_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
12013ea4388cSHaoyuan Feng
12023ea4388cSHaoyuan Feng      when (sfence_dup(2).bits.rs1/*va*/) {
12033ea4388cSHaoyuan Feng        when (sfence_dup(2).bits.rs2) {
12043ea4388cSHaoyuan Feng          // all va && all asid
12053ea4388cSHaoyuan 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))
12063ea4388cSHaoyuan Feng        } .otherwise {
12073ea4388cSHaoyuan Feng          // all va && specific asid except global
12083ea4388cSHaoyuan 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))
12093ea4388cSHaoyuan Feng        }
12103ea4388cSHaoyuan Feng      }
12113ea4388cSHaoyuan Feng    }
12123ea4388cSHaoyuan Feng
12133ea4388cSHaoyuan Feng    when (hfencev_valid) {
121497929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
12153ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage1)).asUInt
12163ea4388cSHaoyuan Feng      val hfencev_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
12173ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12183ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12193ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12203ea4388cSHaoyuan Feng        }.otherwise {
12213ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(~l3g.get & l3hhit & l3asidhit & l3vmidhit))
12223ea4388cSHaoyuan Feng        }
12233ea4388cSHaoyuan Feng      }
12243ea4388cSHaoyuan Feng    }
12253ea4388cSHaoyuan Feng
12263ea4388cSHaoyuan Feng    when (hfenceg_valid) {
12273ea4388cSHaoyuan Feng      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
12283ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage2)).asUInt
12293ea4388cSHaoyuan Feng      val hfenceg_gvpn = (sfence_dup(2).bits.addr << 2)(sfence_dup(2).bits.addr.getWidth - 1, offLen)
12303ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12313ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12323ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~l3hhit)
12333ea4388cSHaoyuan Feng        }.otherwise {
12343ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12353ea4388cSHaoyuan Feng        }
12363ea4388cSHaoyuan Feng      }
12373ea4388cSHaoyuan Feng    }
12383ea4388cSHaoyuan Feng  }
12393ea4388cSHaoyuan Feng
12407797f035SbugGenerator  def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = {
12412c86e165SZhangZifei    in.ready := !in.valid || out.ready
12422c86e165SZhangZifei    out.valid := in.valid
12432c86e165SZhangZifei    out.bits := in.bits
12441f4a7c0cSLemover    out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) =>
12457797f035SbugGenerator      val bypassed_reg = Reg(Bool())
1246d0de7e4aSpeixiaokun      val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid
12477797f035SbugGenerator      when (inFire) { bypassed_reg := bypassed_wire }
12487797f035SbugGenerator      .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire }
12497797f035SbugGenerator
12507797f035SbugGenerator      b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire))
12511f4a7c0cSLemover    }
12522c86e165SZhangZifei  }
12532c86e165SZhangZifei
12546d5ddbceSLemover  // Perf Count
12553ea4388cSHaoyuan Feng  val resp_l0 = resp_res.l0.hit
12566c4dcc2dSLemover  val resp_sp = resp_res.sp.hit
12573ea4388cSHaoyuan Feng  val resp_l3_pre = if (EnableSv48) Some(resp_res.l3.get.pre) else None
12586c4dcc2dSLemover  val resp_l2_pre = resp_res.l2.pre
12593ea4388cSHaoyuan Feng  val resp_l1_pre = resp_res.l1.pre
12603ea4388cSHaoyuan Feng  val resp_l0_pre = resp_res.l0.pre
12616c4dcc2dSLemover  val resp_sp_pre = resp_res.sp.pre
1262935edac4STang Haojin  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1263bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
12643ea4388cSHaoyuan Feng  if (EnableSv48) {
12653ea4388cSHaoyuan 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)
12663ea4388cSHaoyuan Feng  }
12673ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12683ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12693ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit", base_valid_access_0 && resp_l0)
1270bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
1271bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
1272bc063562SLemover
12733ea4388cSHaoyuan Feng  if (EnableSv48) {
12743ea4388cSHaoyuan 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)
12753ea4388cSHaoyuan Feng  }
12763ea4388cSHaoyuan 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)
12773ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12783ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre", base_valid_access_0 && resp_l0_pre && resp_l0)
1279bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
12803ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1281bc063562SLemover
1282935edac4STang Haojin  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire
1283bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
12843ea4388cSHaoyuan Feng  if (EnableSv48) {
12853ea4388cSHaoyuan 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)
12863ea4388cSHaoyuan Feng  }
12873ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12883ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12893ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit", base_valid_access_1 && resp_l0)
1290bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
1291bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
1292bc063562SLemover
12933ea4388cSHaoyuan Feng  if (EnableSv48) {
12943ea4388cSHaoyuan 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)
12953ea4388cSHaoyuan Feng  }
12963ea4388cSHaoyuan 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)
12973ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12983ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre", base_valid_access_1 && resp_l0_pre && resp_l0)
1299bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
13003ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1301bc063562SLemover
1302935edac4STang Haojin  val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1303bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
13043ea4388cSHaoyuan Feng  if (EnableSv48) {
13053ea4388cSHaoyuan 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)
13063ea4388cSHaoyuan Feng  }
13073ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13083ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13093ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_first", base_valid_access_2 && resp_l0)
1310bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
1311bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
1312bc063562SLemover
13133ea4388cSHaoyuan Feng  if (EnableSv48) {
13143ea4388cSHaoyuan 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)
13153ea4388cSHaoyuan Feng  }
13163ea4388cSHaoyuan 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)
13173ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13183ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre_first", base_valid_access_2 && resp_l0_pre && resp_l0)
1319bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
13203ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1321bc063562SLemover
1322935edac4STang Haojin  val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire
1323bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
13243ea4388cSHaoyuan Feng  if (EnableSv48) {
13253ea4388cSHaoyuan 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)
13263ea4388cSHaoyuan Feng  }
13273ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13283ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13293ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_first", base_valid_access_3 && resp_l0)
1330bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
1331bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
1332bc063562SLemover
13333ea4388cSHaoyuan Feng  if (EnableSv48) {
13343ea4388cSHaoyuan 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)
13353ea4388cSHaoyuan Feng  }
13363ea4388cSHaoyuan 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)
13373ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13383ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre_first", base_valid_access_3 && resp_l0_pre && resp_l0)
1339bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
13403ea4388cSHaoyuan 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)
1341bc063562SLemover
13426d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
13436d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
13443ea4388cSHaoyuan Feng  if (EnableSv48) {
13453ea4388cSHaoyuan Feng    l3AccessPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3AccessIndex${i}", l) }
13463ea4388cSHaoyuan Feng  }
13473ea4388cSHaoyuan Feng  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2AccessIndex${i}", l) }
13483ea4388cSHaoyuan Feng  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1AccessIndex${i}", l) }
13493ea4388cSHaoyuan Feng  l0AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0AccessIndex${i}", l) }
13506d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
13513ea4388cSHaoyuan Feng  if (EnableSv48) {
13523ea4388cSHaoyuan Feng    l3RefillPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3RefillIndex${i}", l) }
13533ea4388cSHaoyuan Feng  }
13543ea4388cSHaoyuan Feng  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2RefillIndex${i}", l) }
13553ea4388cSHaoyuan Feng  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1RefillIndex${i}", l) }
13563ea4388cSHaoyuan Feng  l0RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0RefillIndex${i}", l) }
13576d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
13586d5ddbceSLemover
13593ea4388cSHaoyuan Feng  if (EnableSv48) {
13603ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill", Cat(l3RefillPerf.get).orR)
13613ea4388cSHaoyuan Feng  }
1362bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
13633ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
13643ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill", Cat(l0RefillPerf).orR)
1365bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
13663ea4388cSHaoyuan Feng  if (EnableSv48) {
13673ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf.get).orR && refill_prefetch_dup(0))
13683ea4388cSHaoyuan Feng  }
13697797f035SbugGenerator  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0))
13703ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0))
13713ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill_pre", Cat(l0RefillPerf).orR && refill_prefetch_dup(0))
13727797f035SbugGenerator  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0))
1373bc063562SLemover
13746d5ddbceSLemover  // debug
13757797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n")
13763ea4388cSHaoyuan Feng  if (EnableSv48) {
13773ea4388cSHaoyuan Feng    XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v.get)}\n")
13783ea4388cSHaoyuan Feng  }
13797797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n")
13803ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n")
13813ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0v:${Binary(l0v)}\n")
13823ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0g:${Binary(l0g)}\n")
13837797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n")
13847797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n")
13853ea4388cSHaoyuan Feng  if (EnableSv48) {
13863ea4388cSHaoyuan Feng    XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v.get)}\n")
13873ea4388cSHaoyuan Feng  }
13887797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n")
13893ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n")
13903ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0v:${Binary(l0v)}\n")
13913ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0g:${Binary(l0g)}\n")
13927797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n")
1393cd365d4cSrvcoresjw
1394cd365d4cSrvcoresjw  val perfEvents = Seq(
139556be8e20SYinan Xu    ("access           ", base_valid_access_0             ),
1396cd365d4cSrvcoresjw    ("l2_hit           ", l2Hit                           ),
13973ea4388cSHaoyuan Feng    ("l1_hit           ", l1Hit                           ),
13983ea4388cSHaoyuan Feng    ("l0_hit           ", l0Hit                           ),
1399cd365d4cSrvcoresjw    ("sp_hit           ", spHit                           ),
14003ea4388cSHaoyuan Feng    ("pte_hit          ", l0Hit || spHit                  ),
1401cd365d4cSrvcoresjw    ("rwHarzad         ", io.req.valid && !io.req.ready   ),
1402cd365d4cSrvcoresjw    ("out_blocked      ", io.resp.valid && !io.resp.ready ),
1403cd365d4cSrvcoresjw  )
14041ca0e4f3SYinan Xu  generatePerfEvent()
14056d5ddbceSLemover}
1406