xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 8882eb685de93177da606ee717b5ec8e459a768a)
16d5ddbceSLemover/***************************************************************************************
2*8882eb68SXin 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
5*8882eb68SXin 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._
316d5ddbceSLemover
326d5ddbceSLemover/* ptw cache caches the page table of all the three layers
336d5ddbceSLemover * ptw cache resp at next cycle
346d5ddbceSLemover * the cache should not be blocked
356d5ddbceSLemover * when miss queue if full, just block req outside
366d5ddbceSLemover */
373889e11eSLemover
383889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
393889e11eSLemover  val hit = Bool()
403889e11eSLemover  val pre = Bool()
414c0e0181SXiaokun-Pei  val ppn = UInt(gvpnLen.W)
42002c10a4SYanqin Li  val pbmt = UInt(ptePbmtLen.W)
433889e11eSLemover  val perm = new PtePermBundle()
44718a93f5SHaoyuan Feng  val n = UInt(pteNLen.W)
453889e11eSLemover  val ecc = Bool()
463889e11eSLemover  val level = UInt(2.W)
478d8ac704SLemover  val v = Bool()
48*8882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
49*8882eb68SXin Tian    val jmp_bitmap_check = Bool()
50*8882eb68SXin Tian    val pte = UInt(XLEN.W) // Page Table Entry
51*8882eb68SXin Tian  })
523889e11eSLemover
53718a93f5SHaoyuan Feng  def apply(hit: Bool, pre: Bool, ppn: UInt, pbmt: UInt = 0.U, n: UInt = 0.U,
54002c10a4SYanqin Li            perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
55*8882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B, jmp_bitmap_check: Bool = false.B,
56*8882eb68SXin Tian            pte: UInt = 0.U): Unit = {
573889e11eSLemover    this.hit := hit && !ecc
583889e11eSLemover    this.pre := pre
593889e11eSLemover    this.ppn := ppn
60718a93f5SHaoyuan Feng    this.n := n
61002c10a4SYanqin Li    this.pbmt := pbmt
623889e11eSLemover    this.perm := perm
633889e11eSLemover    this.ecc := ecc && hit
643889e11eSLemover    this.level := level
658d8ac704SLemover    this.v := valid
66*8882eb68SXin Tian    if (HasBitmapCheck) {
67*8882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
68*8882eb68SXin Tian      this.bitmapCheck.get.pte := pte
69*8882eb68SXin Tian    }
703889e11eSLemover  }
713889e11eSLemover}
723889e11eSLemover
7363632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle {
7463632028SHaoyuan Feng  assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
7563632028SHaoyuan Feng  val hit = Bool()
7663632028SHaoyuan Feng  val pre = Bool()
774c0e0181SXiaokun-Pei  val ppn = Vec(tlbcontiguous, UInt(gvpnLen.W))
78002c10a4SYanqin Li  val pbmt = Vec(tlbcontiguous, UInt(ptePbmtLen.W))
7963632028SHaoyuan Feng  val perm = Vec(tlbcontiguous, new PtePermBundle())
8063632028SHaoyuan Feng  val ecc = Bool()
8163632028SHaoyuan Feng  val level = UInt(2.W)
8263632028SHaoyuan Feng  val v = Vec(tlbcontiguous, Bool())
83*8882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
84*8882eb68SXin Tian    val jmp_bitmap_check = Bool()
85*8882eb68SXin Tian    val hitway = UInt(l2tlbParams.l0nWays.W)
86*8882eb68SXin Tian    val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
87*8882eb68SXin Tian    val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
88*8882eb68SXin Tian  })
8963632028SHaoyuan Feng
90002c10a4SYanqin Li  def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], pbmt: Vec[UInt] = Vec(tlbcontiguous, 0.U),
91002c10a4SYanqin Li            perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())),
92*8882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B),
93*8882eb68SXin Tian            jmp_bitmap_check: Bool = false.B,
94*8882eb68SXin Tian            hitway: UInt = 0.U, ptes: Vec[UInt] , cfs: Vec[Bool]): Unit = {
9563632028SHaoyuan Feng    this.hit := hit && !ecc
9663632028SHaoyuan Feng    this.pre := pre
9763632028SHaoyuan Feng    this.ppn := ppn
98002c10a4SYanqin Li    this.pbmt := pbmt
9963632028SHaoyuan Feng    this.perm := perm
10063632028SHaoyuan Feng    this.ecc := ecc && hit
10163632028SHaoyuan Feng    this.level := level
10263632028SHaoyuan Feng    this.v := valid
103*8882eb68SXin Tian    if (HasBitmapCheck) {
104*8882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
105*8882eb68SXin Tian      this.bitmapCheck.get.hitway := hitway
106*8882eb68SXin Tian      this.bitmapCheck.get.ptes := ptes
107*8882eb68SXin Tian      this.bitmapCheck.get.cfs := cfs
108*8882eb68SXin Tian    }
10963632028SHaoyuan Feng  }
11063632028SHaoyuan Feng}
11163632028SHaoyuan Feng
1123889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
1133ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(new PageCachePerPespBundle) else None
1143889e11eSLemover  val l2 = new PageCachePerPespBundle
1153ea4388cSHaoyuan Feng  val l1 = new PageCachePerPespBundle
1163ea4388cSHaoyuan Feng  val l0 = new PageCacheMergePespBundle
1173889e11eSLemover  val sp = new PageCachePerPespBundle
1183889e11eSLemover}
1193889e11eSLemover
1203889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
1213889e11eSLemover  val req_info = new L2TlbInnerBundle()
1223889e11eSLemover  val isFirst = Bool()
1233ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Vec(4, Bool()) else Vec(3, Bool())
124325f0a4eSpeixiaokun  val isHptwReq = Bool()
125d0de7e4aSpeixiaokun  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
1263889e11eSLemover}
1273889e11eSLemover
1283889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
1293889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
1306d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
13145f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
13294133605SLemover    val isFirst = Bool()
1336d5ddbceSLemover    val hit = Bool()
134bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
1351f4a7c0cSLemover    val bypassed = Bool()
1366d5ddbceSLemover    val toFsm = new Bundle {
1373ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
1386d5ddbceSLemover      val l2Hit = Bool()
1393ea4388cSHaoyuan Feng      val l1Hit = Bool()
1404c0e0181SXiaokun-Pei      val ppn = UInt(gvpnLen.W)
14130104977Speixiaokun      val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW
142*8882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
143*8882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
144*8882eb68SXin Tian        val toLLPTW = Bool()
145*8882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
146*8882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
147*8882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
148*8882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
149*8882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
150*8882eb68SXin Tian      })
1516d5ddbceSLemover    }
1526979864eSXiaokun-Pei    val stage1 = new PtwMergeResp()
153325f0a4eSpeixiaokun    val isHptwReq = Bool()
154d0de7e4aSpeixiaokun    val toHptw = new Bundle {
1553ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
156d0de7e4aSpeixiaokun      val l2Hit = Bool()
1573ea4388cSHaoyuan Feng      val l1Hit = Bool()
158d0de7e4aSpeixiaokun      val ppn = UInt(ppnLen.W)
159d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
160d0de7e4aSpeixiaokun      val resp = new HptwResp() // used if hit
16183d93d53Speixiaokun      val bypassed = Bool()
162*8882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
163*8882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
164*8882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
165*8882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
166*8882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
167*8882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
168*8882eb68SXin Tian        val fromSP = Bool()
169*8882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
170*8882eb68SXin Tian      })
171d0de7e4aSpeixiaokun    }
1726d5ddbceSLemover  })
1736d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
1745854c1edSLemover    val ptes = UInt(blockBits.W)
1757797f035SbugGenerator    val levelOH = new Bundle {
1767797f035SbugGenerator      // NOTE: levelOH has (Level+1) bits, each stands for page cache entries
1777797f035SbugGenerator      val sp = Bool()
1783ea4388cSHaoyuan Feng      val l0 = Bool()
1797797f035SbugGenerator      val l1 = Bool()
1803ea4388cSHaoyuan Feng      val l2 = Bool()
1813ea4388cSHaoyuan Feng      val l3 = if (EnableSv48) Some(Bool()) else None
1827797f035SbugGenerator      def apply(levelUInt: UInt, valid: Bool) = {
1833ea4388cSHaoyuan Feng        sp := GatedValidRegNext((levelUInt === 1.U || levelUInt === 2.U || levelUInt === 3.U) && valid, false.B)
1843ea4388cSHaoyuan Feng        l0 := GatedValidRegNext((levelUInt === 0.U) & valid, false.B)
1853ea4388cSHaoyuan Feng        l1 := GatedValidRegNext((levelUInt === 1.U) & valid, false.B)
1863ea4388cSHaoyuan Feng        l2 := GatedValidRegNext((levelUInt === 2.U) & valid, false.B)
1873ea4388cSHaoyuan Feng        l3.map(_ := GatedValidRegNext((levelUInt === 3.U) & valid, false.B))
1887797f035SbugGenerator      }
1897797f035SbugGenerator    }
1907797f035SbugGenerator    // duplicate level and sel_pte for each page caches, for better fanout
1917797f035SbugGenerator    val req_info_dup = Vec(3, new L2TlbInnerBundle())
1923ea4388cSHaoyuan Feng    val level_dup = Vec(3, UInt(log2Up(Level + 1).W))
1937797f035SbugGenerator    val sel_pte_dup = Vec(3, UInt(XLEN.W))
1946d5ddbceSLemover  }))
195*8882eb68SXin Tian  // when refill l0,save way info for late bitmap wakeup convenient
196*8882eb68SXin Tian  // valid at same cycle of refill.levelOH.l0
197*8882eb68SXin Tian  val l0_way_info = Option.when(HasBitmapCheck)(Output(UInt(l2tlbParams.l0nWays.W)))
1987797f035SbugGenerator  val sfence_dup = Vec(4, Input(new SfenceBundle()))
1997797f035SbugGenerator  val csr_dup = Vec(3, Input(new TlbCsrBundle()))
200*8882eb68SXin Tian  val bitmap_wakeup = Option.when(HasBitmapCheck)(Flipped(ValidIO(new Bundle {
201*8882eb68SXin Tian    val setIndex = Input(UInt(PtwL0SetIdxLen.W))
202*8882eb68SXin Tian    val tag = Input(UInt(SPTagLen.W))
203*8882eb68SXin Tian    val isSp = Input(Bool())
204*8882eb68SXin Tian    val way_info = UInt(l2tlbParams.l0nWays.W)
205*8882eb68SXin Tian    val pte_index = UInt(sectortlbwidth.W)
206*8882eb68SXin Tian    val check_success = Bool()
207*8882eb68SXin Tian  })))
2086d5ddbceSLemover}
2096d5ddbceSLemover
2101ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
2116d5ddbceSLemover  val io = IO(new PtwCacheIO)
2127196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
213abc4432bSHaoyuan Feng  val l1EntryType = new PTWEntriesWithEcc(ecc, num = PtwL1SectorSize, tagLen = PtwL1TagLen, level = 1, hasPerm = false, ReservedBits = l2tlbParams.l1ReservedBits)
214abc4432bSHaoyuan Feng  val l0EntryType = new PTWEntriesWithEcc(ecc, num = PtwL0SectorSize, tagLen = PtwL0TagLen, level = 0, hasPerm = true, ReservedBits = l2tlbParams.l0ReservedBits)
2157196f5a2SLemover
216*8882eb68SXin Tian  // use two additional regs to record corresponding cache entry whether via bitmap check
217*8882eb68SXin Tian  // 32(l0nSets)* 8 (l0nWays) * 8 (tlbcontiguous)
218*8882eb68SXin Tian  val l0BitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.l0nSets)(VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W))))))))
219*8882eb68SXin Tian  val spBitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.spSize)(0.U(1.W))))
220*8882eb68SXin Tian
221*8882eb68SXin Tian  val bitmapEnable = io.csr_dup(0).mbmc.BME === 1.U && io.csr_dup(0).mbmc.CMODE === 0.U
2226d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
2236d5ddbceSLemover
2247797f035SbugGenerator  val sfence_dup = io.sfence_dup
2256d5ddbceSLemover  val refill = io.refill.bits
2267797f035SbugGenerator  val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source))
2274ed5afbdSXiaokun-Pei  val refill_h = io.refill.bits.req_info_dup.map(a => Mux(a.s2xlate === allStage, onlyStage1, a.s2xlate))
228d0de7e4aSpeixiaokun  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)
2297797f035SbugGenerator  val flush = flush_dup(0)
2306d5ddbceSLemover
2316d5ddbceSLemover  // when refill, refuce to accept new req
2325854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
2333889e11eSLemover
2343889e11eSLemover  // handle hand signal and req_info
2356c4dcc2dSLemover  // TODO: replace with FlushableQueue
2366c4dcc2dSLemover  val stageReq = Wire(Decoupled(new PtwCacheReq()))         // enq stage & read page cache valid
2376c4dcc2dSLemover  val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp
2386c4dcc2dSLemover  val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc
2396c4dcc2dSLemover  val stageResp = Wire(Decoupled(new PtwCacheReq()))         // deq stage
2407797f035SbugGenerator
2417797f035SbugGenerator  val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush)      // catch ram data
2427797f035SbugGenerator  val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter
2437797f035SbugGenerator  val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool()))
2447797f035SbugGenerator  stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush))  // ecc flush
2457797f035SbugGenerator
2466c4dcc2dSLemover  stageReq <> io.req
2476c4dcc2dSLemover  PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad)
2487797f035SbugGenerator  InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle)
2496c4dcc2dSLemover  PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush)
2507797f035SbugGenerator  InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle)
2516c4dcc2dSLemover  PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush)
2526c4dcc2dSLemover  stageResp.ready := !stageResp.valid || io.resp.ready
2536d5ddbceSLemover
2543ea4388cSHaoyuan Feng  // l3: level 3 non-leaf pte
2553ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, new PtwEntry(tagLen = PtwL3TagLen)))) else None
2563ea4388cSHaoyuan Feng  val l3v = if (EnableSv48) Some(RegInit(0.U(l2tlbParams.l3Size.W))) else None
2573ea4388cSHaoyuan Feng  val l3g = if (EnableSv48) Some(Reg(UInt(l2tlbParams.l3Size.W))) else None
2583ea4388cSHaoyuan Feng  val l3asids = if (EnableSv48) Some(l3.get.map(_.asid)) else None
2593ea4388cSHaoyuan Feng  val l3vmids = if (EnableSv48) Some(l3.get.map(_.vmid)) else None
2603ea4388cSHaoyuan Feng  val l3h = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, UInt(2.W)))) else None
2616d5ddbceSLemover
2623ea4388cSHaoyuan Feng  // l2: level 2 non-leaf pte
2633ea4388cSHaoyuan Feng  val l2 = Reg(Vec(l2tlbParams.l2Size, new PtwEntry(tagLen = PtwL2TagLen)))
2643ea4388cSHaoyuan Feng  val l2v = RegInit(0.U(l2tlbParams.l2Size.W))
2653ea4388cSHaoyuan Feng  val l2g = Reg(UInt(l2tlbParams.l2Size.W))
2663ea4388cSHaoyuan Feng  val l2asids = l2.map(_.asid)
2673ea4388cSHaoyuan Feng  val l2vmids = l2.map(_.vmid)
2683ea4388cSHaoyuan Feng  val l2h = Reg(Vec(l2tlbParams.l2Size, UInt(2.W)))
2693ea4388cSHaoyuan Feng
2703ea4388cSHaoyuan Feng  // l1: level 1 non-leaf pte
271abc4432bSHaoyuan Feng  val l1 = Module(new SplittedSRAM(
2723ea4388cSHaoyuan Feng    l1EntryType,
2733ea4388cSHaoyuan Feng    set = l2tlbParams.l1nSets,
2743ea4388cSHaoyuan Feng    way = l2tlbParams.l1nWays,
275d4265a7fSHaoyuan Feng    waySplit = 1,
276abc4432bSHaoyuan Feng    dataSplit = 4,
277abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
278abc4432bSHaoyuan Feng    readMCP2 = false
2796d5ddbceSLemover  ))
2803ea4388cSHaoyuan Feng  val l1v = RegInit(0.U((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2813ea4388cSHaoyuan Feng  val l1g = Reg(UInt((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2823ea4388cSHaoyuan Feng  val l1h = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(2.W))))
2833ea4388cSHaoyuan Feng  def getl1vSet(vpn: UInt) = {
2843ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2853ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2863ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2873ea4388cSHaoyuan Feng    val l1vVec = l1v.asTypeOf(Vec(l2tlbParams.l1nSets, UInt(l2tlbParams.l1nWays.W)))
2883ea4388cSHaoyuan Feng    l1vVec(set)
2896d5ddbceSLemover  }
2903ea4388cSHaoyuan Feng  def getl1hSet(vpn: UInt) = {
2913ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2923ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2933ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2943ea4388cSHaoyuan Feng    l1h(set)
29545f497a4Shappy-lx  }
2966d5ddbceSLemover
2973ea4388cSHaoyuan Feng  // l0: level 0 leaf pte of 4KB pages
298abc4432bSHaoyuan Feng  val l0 = Module(new SplittedSRAM(
2993ea4388cSHaoyuan Feng    l0EntryType,
3003ea4388cSHaoyuan Feng    set = l2tlbParams.l0nSets,
3013ea4388cSHaoyuan Feng    way = l2tlbParams.l0nWays,
302d4265a7fSHaoyuan Feng    waySplit = 2,
303abc4432bSHaoyuan Feng    dataSplit = 4,
304abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
305abc4432bSHaoyuan Feng    readMCP2 = false
3066d5ddbceSLemover  ))
3073ea4388cSHaoyuan Feng  val l0v = RegInit(0.U((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3083ea4388cSHaoyuan Feng  val l0g = Reg(UInt((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3093ea4388cSHaoyuan Feng  val l0h = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(2.W))))
3103ea4388cSHaoyuan Feng  def getl0vSet(vpn: UInt) = {
3113ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3123ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3133ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3143ea4388cSHaoyuan Feng    val l0vVec = l0v.asTypeOf(Vec(l2tlbParams.l0nSets, UInt(l2tlbParams.l0nWays.W)))
3153ea4388cSHaoyuan Feng    l0vVec(set)
3166d5ddbceSLemover  }
3173ea4388cSHaoyuan Feng  def getl0hSet(vpn: UInt) = {
3183ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3193ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3203ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3213ea4388cSHaoyuan Feng    l0h(set)
32245f497a4Shappy-lx  }
3236d5ddbceSLemover
3243ea4388cSHaoyuan Feng  // sp: level 1/2/3 leaf pte of 512GB/1GB/2MB super pages
325718a93f5SHaoyuan Feng  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true, hasNapot = true)))
3265854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
3275854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
3281dd3e32dSHaoyuan Feng  val spasids = sp.map(_.asid)
329d0de7e4aSpeixiaokun  val spvmids = sp.map(_.vmid)
330d61cd5eeSpeixiaokun  val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W)))
3316d5ddbceSLemover
332*8882eb68SXin Tian  if (HasBitmapCheck) {
333*8882eb68SXin Tian    // wakeup corresponding entry
334*8882eb68SXin Tian    when (io.bitmap_wakeup.get.valid) {
335*8882eb68SXin Tian      when (io.bitmap_wakeup.get.bits.isSp) {
336*8882eb68SXin Tian        for (i <- 0 until l2tlbParams.spSize) {
337*8882eb68SXin Tian          when (sp(i).tag === io.bitmap_wakeup.get.bits.tag && spv(i) === 1.U) {
338*8882eb68SXin Tian            spBitmapReg(i) := io.bitmap_wakeup.get.bits.check_success
339*8882eb68SXin Tian          }
340*8882eb68SXin Tian        }
341*8882eb68SXin Tian      } .otherwise {
342*8882eb68SXin Tian        val wakeup_setindex = io.bitmap_wakeup.get.bits.setIndex
343*8882eb68SXin 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
344*8882eb68SXin Tian        assert(l0v(wakeup_setindex * l2tlbParams.l0nWays.U + OHToUInt(io.bitmap_wakeup.get.bits.way_info)) === 1.U,
345*8882eb68SXin Tian          "Wakeuped entry must be valid!")
346*8882eb68SXin Tian      }
347*8882eb68SXin Tian    }
348*8882eb68SXin Tian  }
349*8882eb68SXin Tian
3506d5ddbceSLemover  // Access Perf
3513ea4388cSHaoyuan Feng  val l3AccessPerf = if(EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
3523ea4388cSHaoyuan Feng  val l2AccessPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
3533ea4388cSHaoyuan Feng  val l1AccessPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
3543ea4388cSHaoyuan Feng  val l0AccessPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
3555854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
3563ea4388cSHaoyuan Feng  if (EnableSv48) l3AccessPerf.map(_.map(_ := false.B))
3576d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
3583ea4388cSHaoyuan Feng  l1AccessPerf.map(_ := false.B)
3593ea4388cSHaoyuan Feng  l0AccessPerf.map(_ := false.B)
3606d5ddbceSLemover  spAccessPerf.map(_ := false.B)
3616d5ddbceSLemover
3623889e11eSLemover
3631f4a7c0cSLemover
36482978df9Speixiaokun  def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = {
3653ea4388cSHaoyuan Feng    (vpn1(vpnLen-1, vpnnLen*level+3) === vpn2(vpnLen-1, vpnnLen*level+3))
3661f4a7c0cSLemover  }
3671f4a7c0cSLemover  // NOTE: not actually bypassed, just check if hit, re-access the page cache
368d0de7e4aSpeixiaokun  def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = {
3696f508cb5Speixiaokun    val change_h = MuxLookup(h_search, noS2xlate)(Seq(
370980ddf4cSpeixiaokun      allStage -> onlyStage1,
371980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
372980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
373980ddf4cSpeixiaokun    ))
3744ed5afbdSXiaokun-Pei    val change_refill_h = MuxLookup(io.refill.bits.req_info_dup(0).s2xlate, noS2xlate)(Seq(
3754ed5afbdSXiaokun-Pei      allStage -> onlyStage1,
3764ed5afbdSXiaokun-Pei      onlyStage1 -> onlyStage1,
3774ed5afbdSXiaokun-Pei      onlyStage2 -> onlyStage2
3784ed5afbdSXiaokun-Pei    ))
379d0de7e4aSpeixiaokun    val refill_vpn = io.refill.bits.req_info_dup(0).vpn
3804ed5afbdSXiaokun-Pei    io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === change_refill_h
3811f4a7c0cSLemover  }
3821f4a7c0cSLemover
38382978df9Speixiaokun  val vpn_search = stageReq.bits.req_info.vpn
3846f508cb5Speixiaokun  val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq(
38509280d15Speixiaokun    allStage -> onlyStage1,
38609280d15Speixiaokun    onlyStage1 -> onlyStage1,
38709280d15Speixiaokun    onlyStage2 -> onlyStage2
38809280d15Speixiaokun  ))
3893ea4388cSHaoyuan Feng
3903ea4388cSHaoyuan Feng  // l3
3913ea4388cSHaoyuan Feng  val l3Hit = if(EnableSv48) Some(Wire(Bool())) else None
3923ea4388cSHaoyuan Feng  val l3HitPPN = if(EnableSv48) Some(Wire(UInt(ppnLen.W))) else None
393002c10a4SYanqin Li  val l3HitPbmt = if(EnableSv48) Some(Wire(UInt(ptePbmtLen.W))) else None
3943ea4388cSHaoyuan Feng  val l3Pre = if(EnableSv48) Some(Wire(Bool())) else None
3953ea4388cSHaoyuan Feng  val ptwl3replace = if(EnableSv48) Some(ReplacementPolicy.fromString(l2tlbParams.l3Replacer, l2tlbParams.l3Size)) else None
3963ea4388cSHaoyuan Feng  if (EnableSv48) {
3973ea4388cSHaoyuan Feng    val hitVecT = l3.get.zipWithIndex.map {
39897929664SXiaokun-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)
3993ea4388cSHaoyuan Feng          && l3v.get(i) && h_search === l3h.get(i))
400d0de7e4aSpeixiaokun    }
4016c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4021f4a7c0cSLemover
4033ea4388cSHaoyuan Feng    // stageDelay, but check for l3
4043ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.ppn)), stageDelay_valid_1cycle)
405002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.pbmt)), stageDelay_valid_1cycle)
4063ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.prefetch)), stageDelay_valid_1cycle)
4071f4a7c0cSLemover    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4086d5ddbceSLemover
4093ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl3replace.get.access(OHToUInt(hitVec)) }
4106d5ddbceSLemover
4113ea4388cSHaoyuan Feng    l3AccessPerf.get.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4123ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l3Size) {
41397929664SXiaokun-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")
4146d5ddbceSLemover    }
4153ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l3] l3v:${Binary(l3v.get)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4163ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l3] l3Hit:${hit} l3HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4176d5ddbceSLemover
4183ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l3_hitVecT")
4193ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l3_hitVec")
4203ea4388cSHaoyuan Feng
4213ea4388cSHaoyuan Feng    // synchronize with other entries with RegEnable
4223ea4388cSHaoyuan Feng    l3Hit.map(_ := RegEnable(hit, stageDelay(1).fire))
4233ea4388cSHaoyuan Feng    l3HitPPN.map(_ := RegEnable(hitPPN, stageDelay(1).fire))
424002c10a4SYanqin Li    l3HitPbmt.map(_ := RegEnable(hitPbmt, stageDelay(1).fire))
4253ea4388cSHaoyuan Feng    l3Pre.map(_ := RegEnable(hitPre, stageDelay(1).fire))
4263ea4388cSHaoyuan Feng  }
4273ea4388cSHaoyuan Feng
4283ea4388cSHaoyuan Feng  // l2
4293ea4388cSHaoyuan Feng  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer, l2tlbParams.l2Size)
430002c10a4SYanqin Li  val (l2Hit, l2HitPPN, l2HitPbmt, l2Pre) = {
4313ea4388cSHaoyuan Feng    val hitVecT = l2.zipWithIndex.map {
43297929664SXiaokun-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)
4333ea4388cSHaoyuan Feng        && l2v(i) && h_search === l2h(i))
4343ea4388cSHaoyuan Feng    }
4353ea4388cSHaoyuan Feng    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4363ea4388cSHaoyuan Feng
4373ea4388cSHaoyuan Feng    // stageDelay, but check for l2
4383ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.ppn)), stageDelay_valid_1cycle)
439002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.pbmt)), stageDelay_valid_1cycle)
4403ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.prefetch)), stageDelay_valid_1cycle)
4413ea4388cSHaoyuan Feng    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4423ea4388cSHaoyuan Feng
4433ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl2replace.access(OHToUInt(hitVec)) }
4443ea4388cSHaoyuan Feng
4453ea4388cSHaoyuan Feng    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4463ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
44797929664SXiaokun-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")
4483ea4388cSHaoyuan Feng    }
4493ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l2] l2v:${Binary(l2v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4503ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4513ea4388cSHaoyuan Feng
4523ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l2_hitVecT")
4533ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l2_hitVec")
4546d5ddbceSLemover
4556c4dcc2dSLemover    // synchronize with other entries with RegEnable
4566c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
4576c4dcc2dSLemover     RegEnable(hitPPN, stageDelay(1).fire),
458002c10a4SYanqin Li     RegEnable(hitPbmt, stageDelay(1).fire),
4596c4dcc2dSLemover     RegEnable(hitPre, stageDelay(1).fire))
4606d5ddbceSLemover  }
4616d5ddbceSLemover
4623ea4388cSHaoyuan Feng  // l1
4633ea4388cSHaoyuan Feng  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer,l2tlbParams.l1nWays,l2tlbParams.l1nSets)
464002c10a4SYanqin Li  val (l1Hit, l1HitPPN, l1HitPbmt, l1Pre, l1eccError) = {
4653ea4388cSHaoyuan Feng    val ridx = genPtwL1SetIdx(vpn_search)
4663ea4388cSHaoyuan Feng    l1.io.r.req.valid := stageReq.fire
4673ea4388cSHaoyuan Feng    l1.io.r.req.bits.apply(setIdx = ridx)
4683ea4388cSHaoyuan Feng    val vVec_req = getl1vSet(vpn_search)
4693ea4388cSHaoyuan Feng    val hVec_req = getl1hSet(vpn_search)
4706c4dcc2dSLemover
4716c4dcc2dSLemover    // delay one cycle after sram read
47282978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
4736f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
474980ddf4cSpeixiaokun      allStage -> onlyStage1,
475980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
476980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
477980ddf4cSpeixiaokun    ))
4783ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l1.io.r.resp.data, stageDelay_valid_1cycle)
4797797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
480d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
481d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
48297929664SXiaokun-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)})
4836c4dcc2dSLemover
4846c4dcc2dSLemover    // check hit and ecc
48582978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
4866c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
487935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
4886c4dcc2dSLemover
4897797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
4907196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
4917196f5a2SLemover    val hitWayData = hitWayEntry.entries
4926c4dcc2dSLemover    val hit = ParallelOR(hitVec)
4933ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l1nWays).map(_.U(log2Up(l2tlbParams.l1nWays).W)))
494eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
495eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
496eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
497eef81af7SHaoyuan Feng    } else {
498eef81af7SHaoyuan Feng      eccError := false.B
499eef81af7SHaoyuan Feng    }
5007196f5a2SLemover
5013ea4388cSHaoyuan Feng    ridx.suggestName(s"l1_ridx")
5023ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l1_ramDatas")
5033ea4388cSHaoyuan Feng    hitVec.suggestName(s"l1_hitVec")
5043ea4388cSHaoyuan Feng    hitWayData.suggestName(s"l1_hitWayData")
5053ea4388cSHaoyuan Feng    hitWay.suggestName(s"l1_hitWay")
5066d5ddbceSLemover
5073ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl1replace.access(genPtwL1SetIdx(check_vpn), hitWay) }
5086d5ddbceSLemover
5093ea4388cSHaoyuan Feng    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5103ea4388cSHaoyuan Feng    XSDebug(stageDelay_valid_1cycle, p"[l1] ridx:0x${Hexadecimal(ridx)}\n")
5113ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
5123ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l1] ramDatas(${i.U}) ${ramDatas(i)}  l1v:${vVec(i)}  hit:${hit}\n")
5136d5ddbceSLemover    }
5143ea4388cSHaoyuan 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")
5156d5ddbceSLemover
516002c10a4SYanqin Li    (hit, hitWayData.ppns(genPtwL1SectorIdx(check_vpn)), hitWayData.pbmts(genPtwL1SectorIdx(check_vpn)), hitWayData.prefetch, eccError)
5176d5ddbceSLemover  }
5186d5ddbceSLemover
519b32e9518SHuijin Li  val l0_masked_clock = ClockGate(false.B, stageReq.fire | (!flush_dup(0) && refill.levelOH.l0), clock)
520b32e9518SHuijin Li  val l1_masked_clock = ClockGate(false.B, stageReq.fire | (!flush_dup(1) && refill.levelOH.l1), clock)
521b32e9518SHuijin Li  l0.clock := l0_masked_clock
522b32e9518SHuijin Li  l1.clock := l1_masked_clock
5233ea4388cSHaoyuan Feng  // l0
5243ea4388cSHaoyuan Feng  val ptwl0replace = ReplacementPolicy.fromString(l2tlbParams.l0Replacer,l2tlbParams.l0nWays,l2tlbParams.l0nSets)
525*8882eb68SXin Tian  val (l0Hit, l0HitData, l0Pre, l0eccError, l0HitWay, l0BitmapCheckResult, l0JmpBitmapCheck) = {
5263ea4388cSHaoyuan Feng    val ridx = genPtwL0SetIdx(vpn_search)
5273ea4388cSHaoyuan Feng    l0.io.r.req.valid := stageReq.fire
5283ea4388cSHaoyuan Feng    l0.io.r.req.bits.apply(setIdx = ridx)
5293ea4388cSHaoyuan Feng    val vVec_req = getl0vSet(vpn_search)
5303ea4388cSHaoyuan Feng    val hVec_req = getl0hSet(vpn_search)
5316c4dcc2dSLemover
5326c4dcc2dSLemover    // delay one cycle after sram read
53382978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
5346f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
535980ddf4cSpeixiaokun      allStage -> onlyStage1,
536980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
537980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
538980ddf4cSpeixiaokun    ))
5393ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l0.io.r.resp.data, stageDelay_valid_1cycle)
5407797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
541d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
542d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
54397929664SXiaokun-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)})
5446c4dcc2dSLemover
5456c4dcc2dSLemover    // check hit and ecc
54682978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
5476c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
548935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
5496c4dcc2dSLemover
5507797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
5517196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
5527196f5a2SLemover    val hitWayData = hitWayEntry.entries
5537196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
5543ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l0nWays).map(_.U(log2Up(l2tlbParams.l0nWays).W)))
555*8882eb68SXin Tian
556*8882eb68SXin Tian    val ishptw = RegEnable(stageDelay(0).bits.isHptwReq,stageDelay(1).fire)
557*8882eb68SXin Tian    val s2x_info = RegEnable(stageDelay(0).bits.req_info.s2xlate,stageDelay(1).fire)
558*8882eb68SXin Tian    val pte_index = RegEnable(stageDelay(0).bits.req_info.vpn(sectortlbwidth - 1, 0),stageDelay(1).fire)
559*8882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
560*8882eb68SXin Tian    val hit = WireInit(false.B)
561*8882eb68SXin Tian    val l0bitmapreg = WireInit((VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W)))))))
562*8882eb68SXin Tian    if (HasBitmapCheck) {
563*8882eb68SXin Tian      l0bitmapreg := RegEnable(RegNext(l0BitmapReg(ridx)), stageDelay(1).fire)
564*8882eb68SXin Tian      // cause llptw will trigger bitmapcheck
565*8882eb68SXin Tian      // add a coniditonal logic
566*8882eb68SXin Tian      // (s2x_info =/= allStage || ishptw)
567*8882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 1.U, ParallelOR(hitVec))
568*8882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 0.U) {
569*8882eb68SXin Tian        jmp_bitmap_check := true.B
570*8882eb68SXin Tian      }
571*8882eb68SXin Tian    } else {
572*8882eb68SXin Tian      hit := ParallelOR(hitVec)
573*8882eb68SXin Tian    }
574eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
575eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
576eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
577eef81af7SHaoyuan Feng    } else {
578eef81af7SHaoyuan Feng      eccError := false.B
579eef81af7SHaoyuan Feng    }
5806d5ddbceSLemover
5813ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl0replace.access(genPtwL0SetIdx(check_vpn), hitWay) }
5827196f5a2SLemover
5833ea4388cSHaoyuan Feng    l0AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5843ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l0] ridx:0x${Hexadecimal(ridx)}\n")
5853ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
5863ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l0] ramDatas(${i.U}) ${ramDatas(i)}  l0v:${vVec(i)}  hit:${hitVec(i)}\n")
5876d5ddbceSLemover    }
5883ea4388cSHaoyuan Feng    XSDebug(stageCheck_valid_1cycle, p"[l0] l0Hit:${hit} l0HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n")
5896d5ddbceSLemover
5903ea4388cSHaoyuan Feng    ridx.suggestName(s"l0_ridx")
5913ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l0_ramDatas")
5923ea4388cSHaoyuan Feng    hitVec.suggestName(s"l0_hitVec")
5933ea4388cSHaoyuan Feng    hitWay.suggestName(s"l0_hitWay")
5946d5ddbceSLemover
595*8882eb68SXin Tian    (hit, hitWayData, hitWayData.prefetch, eccError, UIntToOH(hitWay), l0bitmapreg(hitWay), jmp_bitmap_check)
5966d5ddbceSLemover  }
5973ea4388cSHaoyuan Feng  val l0HitPPN = l0HitData.ppns
598002c10a4SYanqin Li  val l0HitPbmt = l0HitData.pbmts
5993ea4388cSHaoyuan Feng  val l0HitPerm = l0HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL0SectorSize, new PtePermBundle)))
600e0c1f271SHaoyuan Feng  val l0HitValid = VecInit(l0HitData.onlypf.map(!_))
601*8882eb68SXin Tian  val l0Ptes = WireInit(VecInit(Seq.fill(tlbcontiguous)(0.U(XLEN.W)))) // L0 lavel Page Table Entry Vector
602*8882eb68SXin Tian  val l0cfs = WireInit(VecInit(Seq.fill(tlbcontiguous)(false.B))) // L0 lavel Bitmap Check Failed Vector
603*8882eb68SXin Tian  if (HasBitmapCheck) {
604*8882eb68SXin Tian    for (i <- 0 until tlbcontiguous) {
605*8882eb68SXin Tian      l0Ptes(i) := Cat(l0HitData.pbmts(i).asUInt,l0HitPPN(i), 0.U(2.W),l0HitPerm(i).asUInt,l0HitValid(i).asUInt)
606*8882eb68SXin Tian      l0cfs(i) := !l0BitmapCheckResult(i)
607*8882eb68SXin Tian    }
608*8882eb68SXin Tian  }
6096d5ddbceSLemover
6106d5ddbceSLemover  // super page
6115854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
612*8882eb68SXin Tian  val (spHit, spHitData, spPre, spValid, spJmpBitmapCheck) = {
613718a93f5SHaoyuan 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) }
6146c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
6156d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
616*8882eb68SXin Tian    val ishptw = RegEnable(stageReq.bits.isHptwReq, stageReq.fire)
617*8882eb68SXin Tian    val s2x_info = RegEnable(stageReq.bits.req_info.s2xlate, stageReq.fire)
618*8882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
619*8882eb68SXin Tian    val hit = WireInit(false.B)
620*8882eb68SXin Tian    if (HasBitmapCheck) {
621*8882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 1.U, ParallelOR(hitVec))
622*8882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 0.U) {
623*8882eb68SXin Tian        jmp_bitmap_check := true.B
624*8882eb68SXin Tian      }
625*8882eb68SXin Tian    } else {
626*8882eb68SXin Tian      hit := ParallelOR(hitVec)
627*8882eb68SXin Tian    }
6286d5ddbceSLemover
6296c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) }
6306d5ddbceSLemover
6316c4dcc2dSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle }
6325854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
63397929664SXiaokun-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")
6346d5ddbceSLemover    }
6356c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
6366d5ddbceSLemover
6376d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
6386d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
6396d5ddbceSLemover
6406c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
6416c4dcc2dSLemover     RegEnable(hitData, stageDelay(1).fire),
6426c4dcc2dSLemover     RegEnable(hitData.prefetch, stageDelay(1).fire),
643*8882eb68SXin Tian     RegEnable(hitData.v, stageDelay(1).fire),
644*8882eb68SXin Tian     RegEnable(jmp_bitmap_check, stageDelay(1).fire))
6456d5ddbceSLemover  }
6466d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
6476d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
648*8882eb68SXin Tian  val spPte = Cat(spHitData.pbmt.asUInt,spHitData.ppn, 0.U(2.W), spHitPerm.asUInt,spHitData.v.asUInt) // Super-page Page Table Entry
6496d5ddbceSLemover
6506c4dcc2dSLemover  val check_res = Wire(new PageCacheRespBundle)
6517fbc7393SYanqin Li  check_res.l3.map(_.apply(l3Hit.get, l3Pre.get, l3HitPPN.get, l3HitPbmt.get))
652002c10a4SYanqin Li  check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, l2HitPbmt)
653002c10a4SYanqin Li  check_res.l1.apply(l1Hit, l1Pre, l1HitPPN, l1HitPbmt, ecc = l1eccError)
654*8882eb68SXin Tian  check_res.l0.apply(l0Hit, l0Pre, l0HitPPN, l0HitPbmt, l0HitPerm, l0eccError, valid = l0HitValid, jmp_bitmap_check = l0JmpBitmapCheck, hitway = l0HitWay, ptes = l0Ptes, cfs = l0cfs)
655*8882eb68SXin Tian  check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitData.pbmt, spHitData.n.getOrElse(0.U), spHitPerm, false.B, spHitLevel, spValid, spJmpBitmapCheck, spPte)
6566d5ddbceSLemover
6576c4dcc2dSLemover  val resp_res = Reg(new PageCacheRespBundle)
6586c4dcc2dSLemover  when (stageCheck(1).fire) { resp_res := check_res }
6593889e11eSLemover
6601f4a7c0cSLemover  // stageResp bypass
6613ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
6621f4a7c0cSLemover  bypassed.indices.foreach(i =>
6631f4a7c0cSLemover    bypassed(i) := stageResp.bits.bypassed(i) ||
6646967f5d5Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
6651f4a7c0cSLemover        OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid)
6661f4a7c0cSLemover  )
6671f4a7c0cSLemover
66883d93d53Speixiaokun  // stageResp bypass to hptw
6693ea4388cSHaoyuan Feng  val hptw_bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
67083d93d53Speixiaokun  hptw_bypassed.indices.foreach(i =>
67183d93d53Speixiaokun    hptw_bypassed(i) := stageResp.bits.bypassed(i) ||
67283d93d53Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
67383d93d53Speixiaokun        io.resp.fire)
67483d93d53Speixiaokun  )
67583d93d53Speixiaokun
67630104977Speixiaokun  val isAllStage = stageResp.bits.req_info.s2xlate === allStage
677c0991f6aSpeixiaokun  val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2
6783ea4388cSHaoyuan Feng  val stage1Hit = (resp_res.l0.hit || resp_res.sp.hit) && isAllStage
679da605600Speixiaokun  val idx = stageResp.bits.req_info.vpn(2, 0)
6803ea4388cSHaoyuan Feng  val stage1Pf = !Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
6816c4dcc2dSLemover  io.resp.bits.req_info   := stageResp.bits.req_info
6826c4dcc2dSLemover  io.resp.bits.isFirst  := stageResp.bits.isFirst
6833ea4388cSHaoyuan Feng  io.resp.bits.hit      := (resp_res.l0.hit || resp_res.sp.hit) && (!isAllStage || isAllStage && stage1Pf)
6843ea4388cSHaoyuan Feng  if (EnableSv48) {
68526175c3fSHaoyuan 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
6863ea4388cSHaoyuan Feng  } else {
68726175c3fSHaoyuan Feng    io.resp.bits.bypassed := ((bypassed(0) && !resp_res.l0.hit) || (bypassed(1) && !resp_res.l1.hit) || (bypassed(2) && !resp_res.l2.hit)) && !isAllStage
6883ea4388cSHaoyuan Feng  }
6893ea4388cSHaoyuan Feng  io.resp.bits.prefetch := resp_res.l0.pre && resp_res.l0.hit || resp_res.sp.pre && resp_res.sp.hit
6903ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l3Hit.map(_ := resp_res.l3.get.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq)
691325f0a4eSpeixiaokun  io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
6923ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
6933ea4388cSHaoyuan 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))
694980ddf4cSpeixiaokun  io.resp.bits.toFsm.stage1Hit := stage1Hit
695*8882eb68SXin Tian  if (HasBitmapCheck) {
696*8882eb68SXin 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
697*8882eb68SXin 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)
698*8882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
699*8882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
700*8882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
701*8882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
702*8882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.SPlevel := resp_res.sp.level
703*8882eb68SXin Tian  }
704d0de7e4aSpeixiaokun
705325f0a4eSpeixiaokun  io.resp.bits.isHptwReq := stageResp.bits.isHptwReq
7063ea4388cSHaoyuan Feng  if (EnableSv48) {
70726175c3fSHaoyuan 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
7083ea4388cSHaoyuan Feng  } else {
70926175c3fSHaoyuan 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
7103ea4388cSHaoyuan Feng  }
711d0de7e4aSpeixiaokun  io.resp.bits.toHptw.id := stageResp.bits.hptwId
7123ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l3Hit.map(_ := resp_res.l3.get.hit && stageResp.bits.isHptwReq)
713325f0a4eSpeixiaokun  io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq
7143ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq
7153ea4388cSHaoyuan 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)
71682978df9Speixiaokun  io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn
717eb4bf3f2Speixiaokun  io.resp.bits.toHptw.resp.entry.asid := DontCare
71897929664SXiaokun-Pei  io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.vmid)
7193ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l0.hit, 0.U, resp_res.sp.level))
720d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source)
7213ea4388cSHaoyuan 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)
722002c10a4SYanqin Li  io.resp.bits.toHptw.resp.entry.pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(idx), resp_res.sp.pbmt)
723718a93f5SHaoyuan Feng  io.resp.bits.toHptw.resp.entry.n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
7243ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l0.hit, resp_res.l0.perm(idx), resp_res.sp.perm))
7253ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
726d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v
727e0c1f271SHaoyuan Feng  io.resp.bits.toHptw.resp.gaf := false.B
728*8882eb68SXin Tian  if (HasBitmapCheck) {
729*8882eb68SXin 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
730*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
731*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
732*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
733*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
734*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.fromSP := resp_res.sp.bitmapCheck.get.jmp_bitmap_check
735*8882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.SPlevel := resp_res.sp.level
736*8882eb68SXin Tian  }
737d0de7e4aSpeixiaokun
7386979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3))
7396979864eSXiaokun-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
74097929664SXiaokun-Pei  io.resp.bits.stage1.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.vmid))
7413ea4388cSHaoyuan Feng  if (EnableSv48) {
7423ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
7433ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
7443ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U,
7453ea4388cSHaoyuan Feng          Mux(resp_res.l2.hit, 2.U, 3.U))))))
7463ea4388cSHaoyuan Feng  } else {
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, 2.U)))))
7503ea4388cSHaoyuan Feng  }
7516979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source))
75263632028SHaoyuan Feng  for (i <- 0 until tlbcontiguous) {
7533ea4388cSHaoyuan Feng    if (EnableSv48) {
7543ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7553ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7563ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7573ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth),
7583ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(gvpnLen - 1, sectortlbwidth)))))
7593ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7603ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7613ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7623ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(sectortlbwidth - 1, 0),
7633ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(sectortlbwidth - 1, 0)))))
7643ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7653ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7663ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7673ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.v,
7683ea4388cSHaoyuan Feng              resp_res.l3.get.v))))
7693ea4388cSHaoyuan Feng    } else {
7703ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7713ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7723ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7733ea4388cSHaoyuan Feng            resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth))))
7743ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7753ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7763ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7773ea4388cSHaoyuan Feng            resp_res.l2.ppn(sectortlbwidth - 1, 0))))
7783ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7793ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7803ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7813ea4388cSHaoyuan Feng            resp_res.l2.v)))
7823ea4388cSHaoyuan Feng    }
783002c10a4SYanqin Li    io.resp.bits.stage1.entry(i).pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(i),
784002c10a4SYanqin Li      Mux(resp_res.sp.hit, resp_res.sp.pbmt,
785002c10a4SYanqin Li        Mux(resp_res.l1.hit, resp_res.l1.pbmt,
786002c10a4SYanqin Li          resp_res.l2.pbmt)))
787718a93f5SHaoyuan Feng    io.resp.bits.stage1.entry(i).n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
78897929664SXiaokun-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))))
7896979864eSXiaokun-Pei    io.resp.bits.stage1.entry(i).pf := !io.resp.bits.stage1.entry(i).v
790e0c1f271SHaoyuan Feng    io.resp.bits.stage1.entry(i).af := false.B
791*8882eb68SXin Tian    io.resp.bits.stage1.entry(i).cf := l0cfs(i) // L0 lavel Bitmap Check Failed Vector
79263632028SHaoyuan Feng  }
793da605600Speixiaokun  io.resp.bits.stage1.pteidx := UIntToOH(idx).asBools
7943ea4388cSHaoyuan Feng  io.resp.bits.stage1.not_super := Mux(resp_res.l0.hit, true.B, false.B)
7956962b4ffSHaoyuan Feng  io.resp.bits.stage1.not_merge := false.B
7966c4dcc2dSLemover  io.resp.valid := stageResp.valid
7973ea4388cSHaoyuan Feng  XSError(stageResp.valid && resp_res.l0.hit && resp_res.sp.hit, "normal page and super page both hit")
7986d5ddbceSLemover
7996d5ddbceSLemover  // refill Perf
8003ea4388cSHaoyuan Feng  val l3RefillPerf = if (EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
8013ea4388cSHaoyuan Feng  val l2RefillPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
8023ea4388cSHaoyuan Feng  val l1RefillPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
8033ea4388cSHaoyuan Feng  val l0RefillPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
8045854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
8053ea4388cSHaoyuan Feng  l3RefillPerf.map(_.map(_ := false.B))
8066d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
8073ea4388cSHaoyuan Feng  l1RefillPerf.map(_ := false.B)
8083ea4388cSHaoyuan Feng  l0RefillPerf.map(_ := false.B)
8096d5ddbceSLemover  spRefillPerf.map(_ := false.B)
8106d5ddbceSLemover
8116d5ddbceSLemover  // refill
8123ea4388cSHaoyuan Feng  l1.io.w.req <> DontCare
8133ea4388cSHaoyuan Feng  l0.io.w.req <> DontCare
8143ea4388cSHaoyuan Feng  l1.io.w.req.valid := false.B
8153ea4388cSHaoyuan Feng  l0.io.w.req.valid := false.B
8166d5ddbceSLemover
8176d5ddbceSLemover  val memRdata = refill.ptes
8185854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
8197797f035SbugGenerator  val memSelData = io.refill.bits.sel_pte_dup
8207797f035SbugGenerator  val memPte = memSelData.map(a => a.asTypeOf(new PteBundle))
821dd286b6aSYanqin Li  val mPBMTE = io.csr.mPBMTE
822dd286b6aSYanqin Li  val hPBMTE = io.csr.hPBMTE
823dd286b6aSYanqin Li  val pbmte = Mux(refill.req_info_dup(0).s2xlate === onlyStage1 || refill.req_info_dup(0).s2xlate === allStage, hPBMTE, mPBMTE)
824b848eea5SLemover
825*8882eb68SXin Tian  def Tran2D(flushMask: UInt): Vec[UInt] = {
826*8882eb68SXin Tian    val tran2D = Wire(Vec(l2tlbParams.l0nSets,UInt(l2tlbParams.l0nWays.W)))
827*8882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
828*8882eb68SXin Tian      tran2D(i) := flushMask((i + 1) * l2tlbParams.l0nWays - 1, i * l2tlbParams.l0nWays)
829*8882eb68SXin Tian    }
830*8882eb68SXin Tian    tran2D
831*8882eb68SXin Tian  }
832*8882eb68SXin Tian  def updateL0BitmapReg(l0BitmapReg: Vec[Vec[Vec[UInt]]], tran2D: Vec[UInt]) = {
833*8882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
834*8882eb68SXin Tian      for (j <- 0 until l2tlbParams.l0nWays) {
835*8882eb68SXin Tian        when (tran2D(i)(j) === 0.U) {
836*8882eb68SXin Tian          for (k <- 0 until tlbcontiguous) {
837*8882eb68SXin Tian            l0BitmapReg(i)(j)(k) := 0.U
838*8882eb68SXin Tian          }
839*8882eb68SXin Tian        }
840*8882eb68SXin Tian      }
841*8882eb68SXin Tian    }
842*8882eb68SXin Tian  }
843*8882eb68SXin Tian  def TranVec(flushMask: UInt): Vec[UInt] = {
844*8882eb68SXin Tian    val vec = Wire(Vec(l2tlbParams.spSize,UInt(1.W)))
845*8882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
846*8882eb68SXin Tian      vec(i) := flushMask(i)
847*8882eb68SXin Tian    }
848*8882eb68SXin Tian    vec
849*8882eb68SXin Tian  }
850*8882eb68SXin Tian  def updateSpBitmapReg(spBitmapReg: Vec[UInt], vec : Vec[UInt]) = {
851*8882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
852*8882eb68SXin Tian      spBitmapReg(i) := spBitmapReg(i) & vec(i)
853*8882eb68SXin Tian    }
854*8882eb68SXin Tian  }
855*8882eb68SXin Tian
8566d5ddbceSLemover  // TODO: handle sfenceLatch outsize
8573ea4388cSHaoyuan Feng  if (EnableSv48) {
8588b33cd30Sklin02    val l3Refill =
8596962b4ffSHaoyuan Feng      !flush_dup(2) &&
8606962b4ffSHaoyuan Feng      refill.levelOH.l3.get &&
8616962b4ffSHaoyuan Feng      !memPte(2).isLeaf() &&
862e0c1f271SHaoyuan Feng      memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
8638b33cd30Sklin02    val l3RefillIdx = replaceWrapper(l3v.get, ptwl3replace.get.way).suggestName(s"l3_refillIdx")
8648b33cd30Sklin02    val l3RfOH = UIntToOH(l3RefillIdx).asUInt.suggestName(s"l3_rfOH")
8658b33cd30Sklin02    when (l3Refill) {
8668b33cd30Sklin02      l3.get(l3RefillIdx).refill(
8673ea4388cSHaoyuan Feng        refill.req_info_dup(2).vpn,
8683ea4388cSHaoyuan Feng        Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
86997929664SXiaokun-Pei        io.csr_dup(2).hgatp.vmid,
8703ea4388cSHaoyuan Feng        memSelData(2),
8713ea4388cSHaoyuan Feng        3.U,
8723ea4388cSHaoyuan Feng        refill_prefetch_dup(2)
87345f497a4Shappy-lx      )
8748b33cd30Sklin02      ptwl2replace.access(l3RefillIdx)
8758b33cd30Sklin02      l3v.get := l3v.get | l3RfOH
8768b33cd30Sklin02      l3g.get := (l3g.get & ~l3RfOH) | Mux(memPte(2).perm.g, l3RfOH, 0.U)
8778b33cd30Sklin02      l3h.get(l3RefillIdx) := refill_h(2)
8786d5ddbceSLemover
8793ea4388cSHaoyuan Feng      for (i <- 0 until l2tlbParams.l3Size) {
8808b33cd30Sklin02        l3RefillPerf.get(i) := i.U === l3RefillIdx
8816d5ddbceSLemover      }
8823ea4388cSHaoyuan Feng    }
8838b33cd30Sklin02    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")
8848b33cd30Sklin02    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")
8856d5ddbceSLemover  }
8866d5ddbceSLemover
8876962b4ffSHaoyuan Feng  // L2 refill
8888b33cd30Sklin02  val l2Refill =
8896962b4ffSHaoyuan Feng    !flush_dup(2) &&
8906962b4ffSHaoyuan Feng    refill.levelOH.l2 &&
8916962b4ffSHaoyuan Feng    !memPte(2).isLeaf() &&
892e0c1f271SHaoyuan Feng    memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).vsatp.mode)
8938b33cd30Sklin02  val l2RefillIdx = replaceWrapper(l2v, ptwl2replace.way).suggestName(s"l2_refillIdx")
8948b33cd30Sklin02  val l2RfOH = UIntToOH(l2RefillIdx).asUInt.suggestName(s"l2_rfOH")
8958b33cd30Sklin02  when (
8968b33cd30Sklin02    l2Refill
897dd286b6aSYanqin Li  ) {
8988b33cd30Sklin02    l2(l2RefillIdx).refill(
8993ea4388cSHaoyuan Feng      refill.req_info_dup(2).vpn,
9003ea4388cSHaoyuan Feng      Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
90197929664SXiaokun-Pei      io.csr_dup(2).hgatp.vmid,
9023ea4388cSHaoyuan Feng      memSelData(2),
9033ea4388cSHaoyuan Feng      2.U,
9043ea4388cSHaoyuan Feng      refill_prefetch_dup(2)
9053ea4388cSHaoyuan Feng    )
9068b33cd30Sklin02    ptwl2replace.access(l2RefillIdx)
9078b33cd30Sklin02    l2v := l2v | l2RfOH
9088b33cd30Sklin02    l2g := (l2g & ~l2RfOH) | Mux(memPte(2).perm.g, l2RfOH, 0.U)
9098b33cd30Sklin02    l2h(l2RefillIdx) := refill_h(2)
9103ea4388cSHaoyuan Feng
9113ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
9128b33cd30Sklin02      l2RefillPerf(i) := i.U === l2RefillIdx
9133ea4388cSHaoyuan Feng    }
9143ea4388cSHaoyuan Feng  }
9158b33cd30Sklin02  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")
9168b33cd30Sklin02  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")
9173ea4388cSHaoyuan Feng
9186962b4ffSHaoyuan Feng  // L1 refill
9198b33cd30Sklin02  val l1Refill = !flush_dup(1) && refill.levelOH.l1
9208b33cd30Sklin02  val l1RefillIdx = genPtwL1SetIdx(refill.req_info_dup(1).vpn).suggestName(s"l1_refillIdx")
9218b33cd30Sklin02  val l1VictimWay = replaceWrapper(getl1vSet(refill.req_info_dup(1).vpn), ptwl1replace.way(l1RefillIdx)).suggestName(s"l1_victimWay")
9228b33cd30Sklin02  val l1VictimWayOH = UIntToOH(l1VictimWay).suggestName(s"l1_victimWayOH")
9238b33cd30Sklin02  val l1RfvOH = UIntToOH(Cat(l1RefillIdx, l1VictimWay)).asUInt.suggestName(s"l1_rfvOH")
9248b33cd30Sklin02  val l1Wdata = Wire(l1EntryType)
9258b33cd30Sklin02  l1Wdata.gen(
92682978df9Speixiaokun    vpn = refill.req_info_dup(1).vpn,
927cca17e78Speixiaokun    asid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid),
92897929664SXiaokun-Pei    vmid = io.csr_dup(1).hgatp.vmid,
92945f497a4Shappy-lx    data = memRdata,
93045f497a4Shappy-lx    levelUInt = 1.U,
9314ed5afbdSXiaokun-Pei    refill_prefetch_dup(1),
932dd286b6aSYanqin Li    refill.req_info_dup(1).s2xlate,
933e0c1f271SHaoyuan Feng    pbmte,
934e0c1f271SHaoyuan Feng    io.csr_dup(1).vsatp.mode
93545f497a4Shappy-lx  )
9368b33cd30Sklin02  when (l1Refill) {
9373ea4388cSHaoyuan Feng    l1.io.w.apply(
9386d5ddbceSLemover      valid = true.B,
9398b33cd30Sklin02      setIdx = l1RefillIdx,
9408b33cd30Sklin02      data = l1Wdata,
9418b33cd30Sklin02      waymask = l1VictimWayOH
9426d5ddbceSLemover    )
9438b33cd30Sklin02    ptwl1replace.access(l1RefillIdx, l1VictimWay)
9448b33cd30Sklin02    l1v := l1v | l1RfvOH
9458b33cd30Sklin02    l1g := l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U)
9468b33cd30Sklin02    l1h(l1RefillIdx)(l1VictimWay) := refill_h(1)
9476d5ddbceSLemover
9483ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
9498b33cd30Sklin02      l1RefillPerf(i) := i.U === l1VictimWay
9506d5ddbceSLemover    }
9516d5ddbceSLemover  }
9528b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refillIdx:0x${Hexadecimal(l1RefillIdx)} victimWay:${l1VictimWay} victimWayOH:${Binary(l1VictimWayOH)} rfvOH(in UInt):${Cat(l1RefillIdx, l1VictimWay)}\n")
9538b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refilldata:0x${l1Wdata}\n")
9548b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1v:${Binary(l1v)} -> ${Binary(l1v | l1RfvOH)}\n")
9558b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1g:${Binary(l1g)} -> ${Binary(l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U))}\n")
9566d5ddbceSLemover
9576962b4ffSHaoyuan Feng  // L0 refill
958718a93f5SHaoyuan Feng  val l0Refill = !flush_dup(0) && refill.levelOH.l0 && !memPte(0).isNapot(refill.level_dup(0))
9598b33cd30Sklin02  val l0RefillIdx = genPtwL0SetIdx(refill.req_info_dup(0).vpn).suggestName(s"l0_refillIdx")
9608b33cd30Sklin02  val l0VictimWay = replaceWrapper(getl0vSet(refill.req_info_dup(0).vpn), ptwl0replace.way(l0RefillIdx)).suggestName(s"l0_victimWay")
9618b33cd30Sklin02  val l0VictimWayOH = UIntToOH(l0VictimWay).asUInt.suggestName(s"l0_victimWayOH")
9628b33cd30Sklin02  val l0RfvOH = UIntToOH(Cat(l0RefillIdx, l0VictimWay)).suggestName(s"l0_rfvOH")
9638b33cd30Sklin02  val l0Wdata = Wire(l0EntryType)
964*8882eb68SXin Tian  // trans the l0 way info, for late wakeup logic
965*8882eb68SXin Tian  if (HasBitmapCheck) {
966*8882eb68SXin Tian    io.l0_way_info.get := l0VictimWayOH
967*8882eb68SXin Tian  }
9688b33cd30Sklin02  l0Wdata.gen(
9693ea4388cSHaoyuan Feng    vpn = refill.req_info_dup(0).vpn,
9703ea4388cSHaoyuan Feng    asid = Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
97197929664SXiaokun-Pei    vmid = io.csr_dup(0).hgatp.vmid,
97245f497a4Shappy-lx    data = memRdata,
9733ea4388cSHaoyuan Feng    levelUInt = 0.U,
9743ea4388cSHaoyuan Feng    refill_prefetch_dup(0),
975dd286b6aSYanqin Li    refill.req_info_dup(0).s2xlate,
976e0c1f271SHaoyuan Feng    pbmte,
977e0c1f271SHaoyuan Feng    io.csr_dup(0).vsatp.mode
97845f497a4Shappy-lx  )
9798b33cd30Sklin02  when (l0Refill) {
9803ea4388cSHaoyuan Feng    l0.io.w.apply(
9816d5ddbceSLemover      valid = true.B,
9828b33cd30Sklin02      setIdx = l0RefillIdx,
9838b33cd30Sklin02      data = l0Wdata,
9848b33cd30Sklin02      waymask = l0VictimWayOH
9856d5ddbceSLemover    )
9868b33cd30Sklin02    ptwl0replace.access(l0RefillIdx, l0VictimWay)
9878b33cd30Sklin02    l0v := l0v | l0RfvOH
9888b33cd30Sklin02    l0g := l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U)
9898b33cd30Sklin02    l0h(l0RefillIdx)(l0VictimWay) := refill_h(0)
990*8882eb68SXin Tian    if (HasBitmapCheck) {updateL0BitmapReg(l0BitmapReg, Tran2D(~l0RfvOH))}
9916d5ddbceSLemover
9923ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
9938b33cd30Sklin02      l0RefillPerf(i) := i.U === l0VictimWay
9946d5ddbceSLemover    }
9956d5ddbceSLemover  }
9968b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refillIdx:0x${Hexadecimal(l0RefillIdx)} victimWay:${l0VictimWay} victimWayOH:${Binary(l0VictimWayOH)} rfvOH(in UInt):${Cat(l0RefillIdx, l0VictimWay)}\n")
9978b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refilldata:0x${l0Wdata}\n")
9988b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0v:${Binary(l0v)} -> ${Binary(l0v | l0RfvOH)}\n")
9998b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0g:${Binary(l0g)} -> ${Binary(l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U))}\n")
10007797f035SbugGenerator
10018d8ac704SLemover
10028d8ac704SLemover  // misc entries: super & invalid
10038b33cd30Sklin02  val spRefill =
10046962b4ffSHaoyuan Feng    !flush_dup(0) &&
10050a56a7dcSHaoyuan Feng    (refill.levelOH.sp || (refill.levelOH.l0 && memPte(0).isNapot(refill.level_dup(0)))) &&
1006e0c1f271SHaoyuan Feng    ((memPte(0).isLeaf() && memPte(0).canRefill(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte, io.csr_dup(0).vsatp.mode)) ||
1007e0c1f271SHaoyuan Feng    memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte))
10088b33cd30Sklin02  val spRefillIdx = spreplace.way.suggestName(s"sp_refillIdx") // LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
10098b33cd30Sklin02  val spRfOH = UIntToOH(spRefillIdx).asUInt.suggestName(s"sp_rfOH")
10108b33cd30Sklin02  when (spRefill) {
10118b33cd30Sklin02    sp(spRefillIdx).refill(
10127797f035SbugGenerator      refill.req_info_dup(0).vpn,
1013b188e334Speixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
101497929664SXiaokun-Pei      io.csr_dup(0).hgatp.vmid,
10157797f035SbugGenerator      memSelData(0),
10163ea4388cSHaoyuan Feng      refill.level_dup(0),
10177797f035SbugGenerator      refill_prefetch_dup(0),
1018e0c1f271SHaoyuan Feng      !memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte)
101945f497a4Shappy-lx    )
10208b33cd30Sklin02    spreplace.access(spRefillIdx)
10218b33cd30Sklin02    spv := spv | spRfOH
10228b33cd30Sklin02    spg := spg & ~spRfOH | Mux(memPte(0).perm.g, spRfOH, 0.U)
10238b33cd30Sklin02    sph(spRefillIdx) := refill_h(0)
1024*8882eb68SXin Tian    if (HasBitmapCheck) {updateSpBitmapReg(spBitmapReg, TranVec(~spRfOH))}
10256d5ddbceSLemover
10265854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
10278b33cd30Sklin02      spRefillPerf(i) := i.U === spRefillIdx
10286d5ddbceSLemover    }
10296d5ddbceSLemover  }
10308b33cd30Sklin02  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")
10318b33cd30Sklin02  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")
10326d5ddbceSLemover
10333ea4388cSHaoyuan Feng  val l1eccFlush = resp_res.l1.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l1eccError, init = false.B)
10343ea4388cSHaoyuan Feng  val l0eccFlush = resp_res.l0.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l0eccError, init = false.B)
10356c4dcc2dSLemover  val eccVpn = stageResp.bits.req_info.vpn
10367196f5a2SLemover
10373ea4388cSHaoyuan Feng  XSError(l1eccFlush, "l2tlb.cache.l1 ecc error. Should not happen at sim stage")
10383ea4388cSHaoyuan Feng  XSError(l0eccFlush, "l2tlb.cache.l0 ecc error. Should not happen at sim stage")
10393ea4388cSHaoyuan Feng  when (l1eccFlush) {
10403ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL1SetIdx(eccVpn))
10413ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l1nWays, a.asUInt) }).asUInt
10423ea4388cSHaoyuan Feng    l1v := l1v & ~flushMask
10433ea4388cSHaoyuan Feng    l1g := l1g & ~flushMask
10447196f5a2SLemover  }
10457196f5a2SLemover
10463ea4388cSHaoyuan Feng  when (l0eccFlush) {
10473ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(eccVpn))
10483ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
10493ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask
10503ea4388cSHaoyuan Feng    l0g := l0g & ~flushMask
10517196f5a2SLemover  }
10527196f5a2SLemover
10533ea4388cSHaoyuan Feng  // sfence for l0
10543ea4388cSHaoyuan Feng  val sfence_valid_l0 = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
10553ea4388cSHaoyuan Feng  when (sfence_valid_l0) {
10563ea4388cSHaoyuan 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
10573ea4388cSHaoyuan Feng    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
10583ea4388cSHaoyuan Feng    when (sfence_dup(0).bits.rs1/*va*/) {
10593ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
10607797f035SbugGenerator        // all va && all asid
10613ea4388cSHaoyuan Feng        l0v := l0v & ~l0hhit
10627797f035SbugGenerator      } .otherwise {
10637797f035SbugGenerator        // all va && specific asid except global
10643ea4388cSHaoyuan Feng        l0v := l0v & (l0g | ~l0hhit)
10657797f035SbugGenerator      }
10667797f035SbugGenerator    } .otherwise {
10673ea4388cSHaoyuan Feng      // val flushMask = UIntToOH(genTlbl1Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
10683ea4388cSHaoyuan Feng      val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(sfence_vpn))
10693ea4388cSHaoyuan Feng      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l0nWays, _.asUInt))).asUInt
10703ea4388cSHaoyuan Feng      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
10717797f035SbugGenerator      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
10727797f035SbugGenerator      flushMask.suggestName(s"sfence_nrs1_flushMask")
10737797f035SbugGenerator
10743ea4388cSHaoyuan Feng      when (sfence_dup(0).bits.rs2) {
10757797f035SbugGenerator        // specific leaf of addr && all asid
10763ea4388cSHaoyuan Feng        l0v := l0v & ~flushMask & ~l0hhit
10777797f035SbugGenerator      } .otherwise {
10787797f035SbugGenerator        // specific leaf of addr && specific asid
10793ea4388cSHaoyuan Feng        l0v := l0v & (~flushMask | l0g | ~l0hhit)
10807797f035SbugGenerator      }
10817797f035SbugGenerator    }
10827797f035SbugGenerator  }
10837797f035SbugGenerator
10843ea4388cSHaoyuan Feng  // hfencev, simple implementation for l0
10853ea4388cSHaoyuan Feng  val hfencev_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hv
10863ea4388cSHaoyuan Feng  when(hfencev_valid_l0) {
10873ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_  === onlyStage1))).asUInt
10883ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all VS-stage l0 pte
1089d0de7e4aSpeixiaokun  }
1090d0de7e4aSpeixiaokun
10913ea4388cSHaoyuan Feng  // hfenceg, simple implementation for l0
10923ea4388cSHaoyuan Feng  val hfenceg_valid_l0 = sfence_dup(0).valid && sfence_dup(0).bits.hg
10933ea4388cSHaoyuan Feng  when(hfenceg_valid_l0) {
10943ea4388cSHaoyuan Feng    val flushMask = VecInit(l0h.flatMap(_.map(_ === onlyStage2))).asUInt
10953ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask // all G-stage l0 pte
1096d0de7e4aSpeixiaokun  }
1097d0de7e4aSpeixiaokun
10983ea4388cSHaoyuan Feng  val l2asidhit = VecInit(l2asids.map(_ === sfence_dup(2).bits.id)).asUInt
1099d0de7e4aSpeixiaokun  val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt
1100d0de7e4aSpeixiaokun  val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
1101d0de7e4aSpeixiaokun  when (sfence_valid) {
110297929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
110397929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
11043ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
1105e5da58f0Speixiaokun    val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
11063ea4388cSHaoyuan 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
11077797f035SbugGenerator    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
11087797f035SbugGenerator
11097797f035SbugGenerator    when (sfence_dup(0).bits.rs1/*va*/) {
11107797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11116d5ddbceSLemover        // all va && all asid
11123ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11133ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & VecInit(l2vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt)
1114447c794eSpeixiaokun        spv := spv & ~(sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
11156d5ddbceSLemover      } .otherwise {
11166d5ddbceSLemover        // all va && specific asid except global
11173ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
11183ea4388cSHaoyuan 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)
11195f64f303Speixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt)
11206d5ddbceSLemover      }
11216d5ddbceSLemover    } .otherwise {
11227797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11236d5ddbceSLemover        // specific leaf of addr && all asid
112497929664SXiaokun-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)
11256d5ddbceSLemover      } .otherwise {
11266d5ddbceSLemover        // specific leaf of addr && specific asid
112797929664SXiaokun-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)
1128d0de7e4aSpeixiaokun      }
1129d0de7e4aSpeixiaokun    }
1130d0de7e4aSpeixiaokun  }
1131d0de7e4aSpeixiaokun
1132d0de7e4aSpeixiaokun  val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv
1133d0de7e4aSpeixiaokun  when (hfencev_valid) {
113497929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
113597929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
11363ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage1)).asUInt
1137cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt
11383ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage1))).asUInt
1139d0de7e4aSpeixiaokun    val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
1140d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1141d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
11423ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11433ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1144447c794eSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1145d0de7e4aSpeixiaokun      }.otherwise {
11463ea4388cSHaoyuan Feng        l1v := l1v & (l1g | ~l1hhit)
11473ea4388cSHaoyuan Feng        l2v := l2v & ~(~l2g & l2hhit & l2asidhit & l2vmidhit)
1148d0de7e4aSpeixiaokun        spv := spv & ~(~spg & sphhit & spasidhit & spvmidhit)
1149d0de7e4aSpeixiaokun      }
1150d0de7e4aSpeixiaokun    }.otherwise {
1151d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
115297929664SXiaokun-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)
1153d0de7e4aSpeixiaokun      }.otherwise {
115497929664SXiaokun-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)
1155d0de7e4aSpeixiaokun      }
1156d0de7e4aSpeixiaokun    }
1157d0de7e4aSpeixiaokun  }
1158d0de7e4aSpeixiaokun
1159d0de7e4aSpeixiaokun
1160d0de7e4aSpeixiaokun  val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg
1161d0de7e4aSpeixiaokun  when(hfenceg_valid) {
11623ea4388cSHaoyuan Feng    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
1163cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
11643ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage2)).asUInt
1165cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt
11663ea4388cSHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage2))).asUInt
1167887df0f4Speixiaokun    val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen)
1168d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1169d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1170d0de7e4aSpeixiaokun        l1v := l1v & ~l1hhit
11713ea4388cSHaoyuan Feng        l2v := l2v & ~l2hhit
1172d0de7e4aSpeixiaokun        spv := spv & ~sphhit
1173d0de7e4aSpeixiaokun      }.otherwise {
11743ea4388cSHaoyuan Feng        l1v := l1v & ~l1hhit
11753ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1176d0de7e4aSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1177d0de7e4aSpeixiaokun      }
1178d0de7e4aSpeixiaokun    }.otherwise {
1179d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
11808fe4f15fSXiaokun-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)
1181d0de7e4aSpeixiaokun      }.otherwise {
11828fe4f15fSXiaokun-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)
11836d5ddbceSLemover      }
11846d5ddbceSLemover    }
11856d5ddbceSLemover  }
11866d5ddbceSLemover
11873ea4388cSHaoyuan Feng  if (EnableSv48) {
11883ea4388cSHaoyuan Feng    val l3asidhit = VecInit(l3asids.get.map(_ === sfence_dup(2).bits.id)).asUInt
118997929664SXiaokun-Pei    val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
11903ea4388cSHaoyuan 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
11913ea4388cSHaoyuan Feng
11923ea4388cSHaoyuan Feng    when (sfence_valid) {
119397929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
11943ea4388cSHaoyuan 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
11953ea4388cSHaoyuan Feng      val sfence_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
11963ea4388cSHaoyuan Feng
11973ea4388cSHaoyuan Feng      when (sfence_dup(2).bits.rs1/*va*/) {
11983ea4388cSHaoyuan Feng        when (sfence_dup(2).bits.rs2) {
11993ea4388cSHaoyuan Feng          // all va && all asid
12003ea4388cSHaoyuan 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))
12013ea4388cSHaoyuan Feng        } .otherwise {
12023ea4388cSHaoyuan Feng          // all va && specific asid except global
12033ea4388cSHaoyuan 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))
12043ea4388cSHaoyuan Feng        }
12053ea4388cSHaoyuan Feng      }
12063ea4388cSHaoyuan Feng    }
12073ea4388cSHaoyuan Feng
12083ea4388cSHaoyuan Feng    when (hfencev_valid) {
120997929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
12103ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage1)).asUInt
12113ea4388cSHaoyuan Feng      val hfencev_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
12123ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12133ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12143ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12153ea4388cSHaoyuan Feng        }.otherwise {
12163ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(~l3g.get & l3hhit & l3asidhit & l3vmidhit))
12173ea4388cSHaoyuan Feng        }
12183ea4388cSHaoyuan Feng      }
12193ea4388cSHaoyuan Feng    }
12203ea4388cSHaoyuan Feng
12213ea4388cSHaoyuan Feng    when (hfenceg_valid) {
12223ea4388cSHaoyuan Feng      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
12233ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage2)).asUInt
12243ea4388cSHaoyuan Feng      val hfenceg_gvpn = (sfence_dup(2).bits.addr << 2)(sfence_dup(2).bits.addr.getWidth - 1, offLen)
12253ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12263ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12273ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~l3hhit)
12283ea4388cSHaoyuan Feng        }.otherwise {
12293ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12303ea4388cSHaoyuan Feng        }
12313ea4388cSHaoyuan Feng      }
12323ea4388cSHaoyuan Feng    }
12333ea4388cSHaoyuan Feng  }
12343ea4388cSHaoyuan Feng
12357797f035SbugGenerator  def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = {
12362c86e165SZhangZifei    in.ready := !in.valid || out.ready
12372c86e165SZhangZifei    out.valid := in.valid
12382c86e165SZhangZifei    out.bits := in.bits
12391f4a7c0cSLemover    out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) =>
12407797f035SbugGenerator      val bypassed_reg = Reg(Bool())
1241d0de7e4aSpeixiaokun      val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid
12427797f035SbugGenerator      when (inFire) { bypassed_reg := bypassed_wire }
12437797f035SbugGenerator      .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire }
12447797f035SbugGenerator
12457797f035SbugGenerator      b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire))
12461f4a7c0cSLemover    }
12472c86e165SZhangZifei  }
12482c86e165SZhangZifei
12496d5ddbceSLemover  // Perf Count
12503ea4388cSHaoyuan Feng  val resp_l0 = resp_res.l0.hit
12516c4dcc2dSLemover  val resp_sp = resp_res.sp.hit
12523ea4388cSHaoyuan Feng  val resp_l3_pre = if (EnableSv48) Some(resp_res.l3.get.pre) else None
12536c4dcc2dSLemover  val resp_l2_pre = resp_res.l2.pre
12543ea4388cSHaoyuan Feng  val resp_l1_pre = resp_res.l1.pre
12553ea4388cSHaoyuan Feng  val resp_l0_pre = resp_res.l0.pre
12566c4dcc2dSLemover  val resp_sp_pre = resp_res.sp.pre
1257935edac4STang Haojin  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1258bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
12593ea4388cSHaoyuan Feng  if (EnableSv48) {
12603ea4388cSHaoyuan 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)
12613ea4388cSHaoyuan Feng  }
12623ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12633ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12643ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit", base_valid_access_0 && resp_l0)
1265bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
1266bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
1267bc063562SLemover
12683ea4388cSHaoyuan Feng  if (EnableSv48) {
12693ea4388cSHaoyuan 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)
12703ea4388cSHaoyuan Feng  }
12713ea4388cSHaoyuan 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)
12723ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12733ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre", base_valid_access_0 && resp_l0_pre && resp_l0)
1274bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
12753ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1276bc063562SLemover
1277935edac4STang Haojin  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire
1278bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
12793ea4388cSHaoyuan Feng  if (EnableSv48) {
12803ea4388cSHaoyuan 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)
12813ea4388cSHaoyuan Feng  }
12823ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12833ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12843ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit", base_valid_access_1 && resp_l0)
1285bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
1286bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
1287bc063562SLemover
12883ea4388cSHaoyuan Feng  if (EnableSv48) {
12893ea4388cSHaoyuan 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)
12903ea4388cSHaoyuan Feng  }
12913ea4388cSHaoyuan 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)
12923ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12933ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre", base_valid_access_1 && resp_l0_pre && resp_l0)
1294bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
12953ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1296bc063562SLemover
1297935edac4STang Haojin  val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1298bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
12993ea4388cSHaoyuan Feng  if (EnableSv48) {
13003ea4388cSHaoyuan 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)
13013ea4388cSHaoyuan Feng  }
13023ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13033ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13043ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_first", base_valid_access_2 && resp_l0)
1305bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
1306bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
1307bc063562SLemover
13083ea4388cSHaoyuan Feng  if (EnableSv48) {
13093ea4388cSHaoyuan 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)
13103ea4388cSHaoyuan Feng  }
13113ea4388cSHaoyuan 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)
13123ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13133ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre_first", base_valid_access_2 && resp_l0_pre && resp_l0)
1314bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
13153ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1316bc063562SLemover
1317935edac4STang Haojin  val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire
1318bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
13193ea4388cSHaoyuan Feng  if (EnableSv48) {
13203ea4388cSHaoyuan 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)
13213ea4388cSHaoyuan Feng  }
13223ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13233ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13243ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_first", base_valid_access_3 && resp_l0)
1325bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
1326bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
1327bc063562SLemover
13283ea4388cSHaoyuan Feng  if (EnableSv48) {
13293ea4388cSHaoyuan 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)
13303ea4388cSHaoyuan Feng  }
13313ea4388cSHaoyuan 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)
13323ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13333ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre_first", base_valid_access_3 && resp_l0_pre && resp_l0)
1334bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
13353ea4388cSHaoyuan 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)
1336bc063562SLemover
13376d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
13386d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
13393ea4388cSHaoyuan Feng  if (EnableSv48) {
13403ea4388cSHaoyuan Feng    l3AccessPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3AccessIndex${i}", l) }
13413ea4388cSHaoyuan Feng  }
13423ea4388cSHaoyuan Feng  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2AccessIndex${i}", l) }
13433ea4388cSHaoyuan Feng  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1AccessIndex${i}", l) }
13443ea4388cSHaoyuan Feng  l0AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0AccessIndex${i}", l) }
13456d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
13463ea4388cSHaoyuan Feng  if (EnableSv48) {
13473ea4388cSHaoyuan Feng    l3RefillPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3RefillIndex${i}", l) }
13483ea4388cSHaoyuan Feng  }
13493ea4388cSHaoyuan Feng  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2RefillIndex${i}", l) }
13503ea4388cSHaoyuan Feng  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1RefillIndex${i}", l) }
13513ea4388cSHaoyuan Feng  l0RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0RefillIndex${i}", l) }
13526d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
13536d5ddbceSLemover
13543ea4388cSHaoyuan Feng  if (EnableSv48) {
13553ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill", Cat(l3RefillPerf.get).orR)
13563ea4388cSHaoyuan Feng  }
1357bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
13583ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
13593ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill", Cat(l0RefillPerf).orR)
1360bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
13613ea4388cSHaoyuan Feng  if (EnableSv48) {
13623ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf.get).orR && refill_prefetch_dup(0))
13633ea4388cSHaoyuan Feng  }
13647797f035SbugGenerator  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0))
13653ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0))
13663ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill_pre", Cat(l0RefillPerf).orR && refill_prefetch_dup(0))
13677797f035SbugGenerator  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0))
1368bc063562SLemover
13696d5ddbceSLemover  // debug
13707797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n")
13713ea4388cSHaoyuan Feng  if (EnableSv48) {
13723ea4388cSHaoyuan Feng    XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v.get)}\n")
13733ea4388cSHaoyuan Feng  }
13747797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n")
13753ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n")
13763ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0v:${Binary(l0v)}\n")
13773ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0g:${Binary(l0g)}\n")
13787797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n")
13797797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n")
13803ea4388cSHaoyuan Feng  if (EnableSv48) {
13813ea4388cSHaoyuan Feng    XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v.get)}\n")
13823ea4388cSHaoyuan Feng  }
13837797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n")
13843ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n")
13853ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0v:${Binary(l0v)}\n")
13863ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0g:${Binary(l0g)}\n")
13877797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n")
1388cd365d4cSrvcoresjw
1389cd365d4cSrvcoresjw  val perfEvents = Seq(
139056be8e20SYinan Xu    ("access           ", base_valid_access_0             ),
1391cd365d4cSrvcoresjw    ("l2_hit           ", l2Hit                           ),
13923ea4388cSHaoyuan Feng    ("l1_hit           ", l1Hit                           ),
13933ea4388cSHaoyuan Feng    ("l0_hit           ", l0Hit                           ),
1394cd365d4cSrvcoresjw    ("sp_hit           ", spHit                           ),
13953ea4388cSHaoyuan Feng    ("pte_hit          ", l0Hit || spHit                  ),
1396cd365d4cSrvcoresjw    ("rwHarzad         ", io.req.valid && !io.req.ready   ),
1397cd365d4cSrvcoresjw    ("out_blocked      ", io.resp.valid && !io.resp.ready ),
1398cd365d4cSrvcoresjw  )
13991ca0e4f3SYinan Xu  generatePerfEvent()
14006d5ddbceSLemover}
1401