xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision fc4ea7c0b51a85ee0b03a9de78197604487fbdc9)
16d5ddbceSLemover/***************************************************************************************
28882eb68SXin Tian* Copyright (c) 2021-2025 Beijing Institute of Open Source Chip (BOSC)
3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
58882eb68SXin Tian* Copyright (c) 2024-2025 Institute of Information Engineering, Chinese Academy of Sciences
66d5ddbceSLemover*
76d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
86d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
96d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
106d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
116d5ddbceSLemover*
126d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
136d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
146d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
156d5ddbceSLemover*
166d5ddbceSLemover* See the Mulan PSL v2 for more details.
176d5ddbceSLemover***************************************************************************************/
186d5ddbceSLemover
196d5ddbceSLemoverpackage xiangshan.cache.mmu
206d5ddbceSLemover
218891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
226d5ddbceSLemoverimport chisel3._
236d5ddbceSLemoverimport chisel3.util._
246d5ddbceSLemoverimport xiangshan._
256d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
266d5ddbceSLemoverimport utils._
273c02ee8fSwakafaimport utility._
28abc4432bSHaoyuan Fengimport coupledL2.utils.SplittedSRAM
296d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
306d5ddbceSLemoverimport freechips.rocketchip.tilelink._
314b2c87baS梁森 Liang Senimport utility.mbist.MbistPipeline
326d5ddbceSLemover
336d5ddbceSLemover/* ptw cache caches the page table of all the three layers
346d5ddbceSLemover * ptw cache resp at next cycle
356d5ddbceSLemover * the cache should not be blocked
366d5ddbceSLemover * when miss queue if full, just block req outside
376d5ddbceSLemover */
383889e11eSLemover
393889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle {
403889e11eSLemover  val hit = Bool()
413889e11eSLemover  val pre = Bool()
424c0e0181SXiaokun-Pei  val ppn = UInt(gvpnLen.W)
43002c10a4SYanqin Li  val pbmt = UInt(ptePbmtLen.W)
443889e11eSLemover  val perm = new PtePermBundle()
45718a93f5SHaoyuan Feng  val n = UInt(pteNLen.W)
463889e11eSLemover  val ecc = Bool()
473889e11eSLemover  val level = UInt(2.W)
488d8ac704SLemover  val v = Bool()
498882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
508882eb68SXin Tian    val jmp_bitmap_check = Bool()
518882eb68SXin Tian    val pte = UInt(XLEN.W) // Page Table Entry
528882eb68SXin Tian  })
533889e11eSLemover
54718a93f5SHaoyuan Feng  def apply(hit: Bool, pre: Bool, ppn: UInt, pbmt: UInt = 0.U, n: UInt = 0.U,
55002c10a4SYanqin Li            perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()),
568882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B, jmp_bitmap_check: Bool = false.B,
578882eb68SXin Tian            pte: UInt = 0.U): Unit = {
583889e11eSLemover    this.hit := hit && !ecc
593889e11eSLemover    this.pre := pre
603889e11eSLemover    this.ppn := ppn
61718a93f5SHaoyuan Feng    this.n := n
62002c10a4SYanqin Li    this.pbmt := pbmt
633889e11eSLemover    this.perm := perm
643889e11eSLemover    this.ecc := ecc && hit
653889e11eSLemover    this.level := level
668d8ac704SLemover    this.v := valid
678882eb68SXin Tian    if (HasBitmapCheck) {
688882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
698882eb68SXin Tian      this.bitmapCheck.get.pte := pte
708882eb68SXin Tian    }
713889e11eSLemover  }
723889e11eSLemover}
733889e11eSLemover
7463632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle {
7563632028SHaoyuan Feng  assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
7663632028SHaoyuan Feng  val hit = Bool()
7763632028SHaoyuan Feng  val pre = Bool()
784c0e0181SXiaokun-Pei  val ppn = Vec(tlbcontiguous, UInt(gvpnLen.W))
79002c10a4SYanqin Li  val pbmt = Vec(tlbcontiguous, UInt(ptePbmtLen.W))
8063632028SHaoyuan Feng  val perm = Vec(tlbcontiguous, new PtePermBundle())
8163632028SHaoyuan Feng  val ecc = Bool()
8263632028SHaoyuan Feng  val level = UInt(2.W)
8363632028SHaoyuan Feng  val v = Vec(tlbcontiguous, Bool())
848882eb68SXin Tian  val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
858882eb68SXin Tian    val jmp_bitmap_check = Bool()
868882eb68SXin Tian    val hitway = UInt(l2tlbParams.l0nWays.W)
878882eb68SXin Tian    val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
888882eb68SXin Tian    val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
898882eb68SXin Tian  })
9063632028SHaoyuan Feng
91002c10a4SYanqin Li  def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], pbmt: Vec[UInt] = Vec(tlbcontiguous, 0.U),
92002c10a4SYanqin Li            perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())),
938882eb68SXin Tian            ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B),
948882eb68SXin Tian            jmp_bitmap_check: Bool = false.B,
958882eb68SXin Tian            hitway: UInt = 0.U, ptes: Vec[UInt] , cfs: Vec[Bool]): Unit = {
9663632028SHaoyuan Feng    this.hit := hit && !ecc
9763632028SHaoyuan Feng    this.pre := pre
9863632028SHaoyuan Feng    this.ppn := ppn
99002c10a4SYanqin Li    this.pbmt := pbmt
10063632028SHaoyuan Feng    this.perm := perm
10163632028SHaoyuan Feng    this.ecc := ecc && hit
10263632028SHaoyuan Feng    this.level := level
10363632028SHaoyuan Feng    this.v := valid
1048882eb68SXin Tian    if (HasBitmapCheck) {
1058882eb68SXin Tian      this.bitmapCheck.get.jmp_bitmap_check := jmp_bitmap_check
1068882eb68SXin Tian      this.bitmapCheck.get.hitway := hitway
1078882eb68SXin Tian      this.bitmapCheck.get.ptes := ptes
1088882eb68SXin Tian      this.bitmapCheck.get.cfs := cfs
1098882eb68SXin Tian    }
11063632028SHaoyuan Feng  }
11163632028SHaoyuan Feng}
11263632028SHaoyuan Feng
1133889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle {
1143ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(new PageCachePerPespBundle) else None
1153889e11eSLemover  val l2 = new PageCachePerPespBundle
1163ea4388cSHaoyuan Feng  val l1 = new PageCachePerPespBundle
1173ea4388cSHaoyuan Feng  val l0 = new PageCacheMergePespBundle
1183889e11eSLemover  val sp = new PageCachePerPespBundle
1193889e11eSLemover}
1203889e11eSLemover
1213889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle {
1223889e11eSLemover  val req_info = new L2TlbInnerBundle()
1233889e11eSLemover  val isFirst = Bool()
1243ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Vec(4, Bool()) else Vec(3, Bool())
125325f0a4eSpeixiaokun  val isHptwReq = Bool()
126d0de7e4aSpeixiaokun  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
1273889e11eSLemover}
1283889e11eSLemover
1293889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
1303889e11eSLemover  val req = Flipped(DecoupledIO(new PtwCacheReq()))
1316d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
13245f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
13394133605SLemover    val isFirst = Bool()
1346d5ddbceSLemover    val hit = Bool()
135bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
1361f4a7c0cSLemover    val bypassed = Bool()
1376d5ddbceSLemover    val toFsm = new Bundle {
1383ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
1396d5ddbceSLemover      val l2Hit = Bool()
1403ea4388cSHaoyuan Feng      val l1Hit = Bool()
1414c0e0181SXiaokun-Pei      val ppn = UInt(gvpnLen.W)
14230104977Speixiaokun      val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW
1438882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
1448882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
1458882eb68SXin Tian        val toLLPTW = Bool()
1468882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
1478882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
1488882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
1498882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
1508882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
1518882eb68SXin Tian      })
1526d5ddbceSLemover    }
1536979864eSXiaokun-Pei    val stage1 = new PtwMergeResp()
154325f0a4eSpeixiaokun    val isHptwReq = Bool()
155d0de7e4aSpeixiaokun    val toHptw = new Bundle {
1563ea4388cSHaoyuan Feng      val l3Hit = if (EnableSv48) Some(Bool()) else None
157d0de7e4aSpeixiaokun      val l2Hit = Bool()
1583ea4388cSHaoyuan Feng      val l1Hit = Bool()
159d0de7e4aSpeixiaokun      val ppn = UInt(ppnLen.W)
160d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
161d0de7e4aSpeixiaokun      val resp = new HptwResp() // used if hit
16283d93d53Speixiaokun      val bypassed = Bool()
1638882eb68SXin Tian      val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle {
1648882eb68SXin Tian        val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check
1658882eb68SXin Tian        val hitway = UInt(l2tlbParams.l0nWays.W)
1668882eb68SXin Tian        val pte = UInt(XLEN.W) // Page Table Entry
1678882eb68SXin Tian        val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector
1688882eb68SXin Tian        val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector
1698882eb68SXin Tian        val fromSP = Bool()
1708882eb68SXin Tian        val SPlevel = UInt(log2Up(Level).W)
1718882eb68SXin Tian      })
172d0de7e4aSpeixiaokun    }
1736d5ddbceSLemover  })
1746d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
1755854c1edSLemover    val ptes = UInt(blockBits.W)
1767797f035SbugGenerator    val levelOH = new Bundle {
1777797f035SbugGenerator      // NOTE: levelOH has (Level+1) bits, each stands for page cache entries
1787797f035SbugGenerator      val sp = Bool()
1793ea4388cSHaoyuan Feng      val l0 = Bool()
1807797f035SbugGenerator      val l1 = Bool()
1813ea4388cSHaoyuan Feng      val l2 = Bool()
1823ea4388cSHaoyuan Feng      val l3 = if (EnableSv48) Some(Bool()) else None
1837797f035SbugGenerator      def apply(levelUInt: UInt, valid: Bool) = {
1843ea4388cSHaoyuan Feng        sp := GatedValidRegNext((levelUInt === 1.U || levelUInt === 2.U || levelUInt === 3.U) && valid, false.B)
1853ea4388cSHaoyuan Feng        l0 := GatedValidRegNext((levelUInt === 0.U) & valid, false.B)
1863ea4388cSHaoyuan Feng        l1 := GatedValidRegNext((levelUInt === 1.U) & valid, false.B)
1873ea4388cSHaoyuan Feng        l2 := GatedValidRegNext((levelUInt === 2.U) & valid, false.B)
1883ea4388cSHaoyuan Feng        l3.map(_ := GatedValidRegNext((levelUInt === 3.U) & valid, false.B))
1897797f035SbugGenerator      }
1907797f035SbugGenerator    }
1917797f035SbugGenerator    // duplicate level and sel_pte for each page caches, for better fanout
1927797f035SbugGenerator    val req_info_dup = Vec(3, new L2TlbInnerBundle())
1933ea4388cSHaoyuan Feng    val level_dup = Vec(3, UInt(log2Up(Level + 1).W))
1947797f035SbugGenerator    val sel_pte_dup = Vec(3, UInt(XLEN.W))
1956d5ddbceSLemover  }))
1968882eb68SXin Tian  // when refill l0,save way info for late bitmap wakeup convenient
1978882eb68SXin Tian  // valid at same cycle of refill.levelOH.l0
1988882eb68SXin Tian  val l0_way_info = Option.when(HasBitmapCheck)(Output(UInt(l2tlbParams.l0nWays.W)))
1997797f035SbugGenerator  val sfence_dup = Vec(4, Input(new SfenceBundle()))
2007797f035SbugGenerator  val csr_dup = Vec(3, Input(new TlbCsrBundle()))
2018882eb68SXin Tian  val bitmap_wakeup = Option.when(HasBitmapCheck)(Flipped(ValidIO(new Bundle {
2028882eb68SXin Tian    val setIndex = Input(UInt(PtwL0SetIdxLen.W))
2038882eb68SXin Tian    val tag = Input(UInt(SPTagLen.W))
2048882eb68SXin Tian    val isSp = Input(Bool())
2058882eb68SXin Tian    val way_info = UInt(l2tlbParams.l0nWays.W)
2068882eb68SXin Tian    val pte_index = UInt(sectortlbwidth.W)
2078882eb68SXin Tian    val check_success = Bool()
2088882eb68SXin Tian  })))
2096d5ddbceSLemover}
2106d5ddbceSLemover
2111ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
2126d5ddbceSLemover  val io = IO(new PtwCacheIO)
2137196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
214abc4432bSHaoyuan Feng  val l1EntryType = new PTWEntriesWithEcc(ecc, num = PtwL1SectorSize, tagLen = PtwL1TagLen, level = 1, hasPerm = false, ReservedBits = l2tlbParams.l1ReservedBits)
215abc4432bSHaoyuan Feng  val l0EntryType = new PTWEntriesWithEcc(ecc, num = PtwL0SectorSize, tagLen = PtwL0TagLen, level = 0, hasPerm = true, ReservedBits = l2tlbParams.l0ReservedBits)
2167196f5a2SLemover
2178882eb68SXin Tian  // use two additional regs to record corresponding cache entry whether via bitmap check
2188882eb68SXin Tian  // 32(l0nSets)* 8 (l0nWays) * 8 (tlbcontiguous)
2198882eb68SXin Tian  val l0BitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.l0nSets)(VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W))))))))
2208882eb68SXin Tian  val spBitmapReg = RegInit(VecInit(Seq.fill(l2tlbParams.spSize)(0.U(1.W))))
2218882eb68SXin Tian
2228882eb68SXin Tian  val bitmapEnable = io.csr_dup(0).mbmc.BME === 1.U && io.csr_dup(0).mbmc.CMODE === 0.U
2236d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
2246d5ddbceSLemover
2257797f035SbugGenerator  val sfence_dup = io.sfence_dup
2266d5ddbceSLemover  val refill = io.refill.bits
2277797f035SbugGenerator  val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source))
2284ed5afbdSXiaokun-Pei  val refill_h = io.refill.bits.req_info_dup.map(a => Mux(a.s2xlate === allStage, onlyStage1, a.s2xlate))
229d0de7e4aSpeixiaokun  val flush_dup = sfence_dup.zip(io.csr_dup).map(f => f._1.valid || f._2.satp.changed || f._2.vsatp.changed || f._2.hgatp.changed)
2307797f035SbugGenerator  val flush = flush_dup(0)
2316d5ddbceSLemover
2326d5ddbceSLemover  // when refill, refuce to accept new req
2335854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
2343889e11eSLemover
2353889e11eSLemover  // handle hand signal and req_info
2366c4dcc2dSLemover  // TODO: replace with FlushableQueue
2376c4dcc2dSLemover  val stageReq = Wire(Decoupled(new PtwCacheReq()))         // enq stage & read page cache valid
2386c4dcc2dSLemover  val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp
2396c4dcc2dSLemover  val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc
2406c4dcc2dSLemover  val stageResp = Wire(Decoupled(new PtwCacheReq()))         // deq stage
2417797f035SbugGenerator
2427797f035SbugGenerator  val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush)      // catch ram data
2437797f035SbugGenerator  val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter
2447797f035SbugGenerator  val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool()))
2457797f035SbugGenerator  stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush))  // ecc flush
2467797f035SbugGenerator
2476c4dcc2dSLemover  stageReq <> io.req
2486c4dcc2dSLemover  PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad)
2497797f035SbugGenerator  InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle)
2506c4dcc2dSLemover  PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush)
2517797f035SbugGenerator  InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle)
2526c4dcc2dSLemover  PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush)
2536c4dcc2dSLemover  stageResp.ready := !stageResp.valid || io.resp.ready
2546d5ddbceSLemover
2553ea4388cSHaoyuan Feng  // l3: level 3 non-leaf pte
2563ea4388cSHaoyuan Feng  val l3 = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, new PtwEntry(tagLen = PtwL3TagLen)))) else None
2573ea4388cSHaoyuan Feng  val l3v = if (EnableSv48) Some(RegInit(0.U(l2tlbParams.l3Size.W))) else None
2583ea4388cSHaoyuan Feng  val l3g = if (EnableSv48) Some(Reg(UInt(l2tlbParams.l3Size.W))) else None
2593ea4388cSHaoyuan Feng  val l3asids = if (EnableSv48) Some(l3.get.map(_.asid)) else None
2603ea4388cSHaoyuan Feng  val l3vmids = if (EnableSv48) Some(l3.get.map(_.vmid)) else None
2613ea4388cSHaoyuan Feng  val l3h = if (EnableSv48) Some(Reg(Vec(l2tlbParams.l3Size, UInt(2.W)))) else None
2626d5ddbceSLemover
2633ea4388cSHaoyuan Feng  // l2: level 2 non-leaf pte
2643ea4388cSHaoyuan Feng  val l2 = Reg(Vec(l2tlbParams.l2Size, new PtwEntry(tagLen = PtwL2TagLen)))
2653ea4388cSHaoyuan Feng  val l2v = RegInit(0.U(l2tlbParams.l2Size.W))
2663ea4388cSHaoyuan Feng  val l2g = Reg(UInt(l2tlbParams.l2Size.W))
2673ea4388cSHaoyuan Feng  val l2asids = l2.map(_.asid)
2683ea4388cSHaoyuan Feng  val l2vmids = l2.map(_.vmid)
2693ea4388cSHaoyuan Feng  val l2h = Reg(Vec(l2tlbParams.l2Size, UInt(2.W)))
2703ea4388cSHaoyuan Feng
2713ea4388cSHaoyuan Feng  // l1: level 1 non-leaf pte
272abc4432bSHaoyuan Feng  val l1 = Module(new SplittedSRAM(
2733ea4388cSHaoyuan Feng    l1EntryType,
2743ea4388cSHaoyuan Feng    set = l2tlbParams.l1nSets,
2753ea4388cSHaoyuan Feng    way = l2tlbParams.l1nWays,
276d4265a7fSHaoyuan Feng    waySplit = 1,
277abc4432bSHaoyuan Feng    dataSplit = 4,
278abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
2794b2c87baS梁森 Liang Sen    readMCP2 = false,
2804b2c87baS梁森 Liang Sen    hasMbist = hasMbist
2816d5ddbceSLemover  ))
2824b2c87baS梁森 Liang Sen  val mbistPlL1 = MbistPipeline.PlaceMbistPipeline(1, s"MbistPipePtwL1", hasMbist)
2833ea4388cSHaoyuan Feng  val l1v = RegInit(0.U((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2843ea4388cSHaoyuan Feng  val l1g = Reg(UInt((l2tlbParams.l1nSets * l2tlbParams.l1nWays).W))
2853ea4388cSHaoyuan Feng  val l1h = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(2.W))))
286f9395f72SHaoyuan Feng  val l1asids = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(l2tlbParams.hashAsidWidth.W))))
287f9395f72SHaoyuan Feng  val l1vmids = Reg(Vec(l2tlbParams.l1nSets, Vec(l2tlbParams.l1nWays, UInt(l2tlbParams.hashAsidWidth.W))))
2883ea4388cSHaoyuan Feng  def getl1vSet(vpn: UInt) = {
2893ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2903ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2913ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2923ea4388cSHaoyuan Feng    val l1vVec = l1v.asTypeOf(Vec(l2tlbParams.l1nSets, UInt(l2tlbParams.l1nWays.W)))
2933ea4388cSHaoyuan Feng    l1vVec(set)
2946d5ddbceSLemover  }
2953ea4388cSHaoyuan Feng  def getl1hSet(vpn: UInt) = {
2963ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l1nWays) == log2Down(l2tlbParams.l1nWays))
2973ea4388cSHaoyuan Feng    val set = genPtwL1SetIdx(vpn)
2983ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l1nSets))
2993ea4388cSHaoyuan Feng    l1h(set)
30045f497a4Shappy-lx  }
3016d5ddbceSLemover
3023ea4388cSHaoyuan Feng  // l0: level 0 leaf pte of 4KB pages
303abc4432bSHaoyuan Feng  val l0 = Module(new SplittedSRAM(
3043ea4388cSHaoyuan Feng    l0EntryType,
3053ea4388cSHaoyuan Feng    set = l2tlbParams.l0nSets,
3063ea4388cSHaoyuan Feng    way = l2tlbParams.l0nWays,
307d4265a7fSHaoyuan Feng    waySplit = 2,
308abc4432bSHaoyuan Feng    dataSplit = 4,
309abc4432bSHaoyuan Feng    singlePort = sramSinglePort,
3104b2c87baS梁森 Liang Sen    readMCP2 = false,
3114b2c87baS梁森 Liang Sen    hasMbist = hasMbist
3126d5ddbceSLemover  ))
3134b2c87baS梁森 Liang Sen  val mbistPlL0 = MbistPipeline.PlaceMbistPipeline(1, s"MbistPipePtwL0", hasMbist)
3143ea4388cSHaoyuan Feng  val l0v = RegInit(0.U((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3153ea4388cSHaoyuan Feng  val l0g = Reg(UInt((l2tlbParams.l0nSets * l2tlbParams.l0nWays).W))
3163ea4388cSHaoyuan Feng  val l0h = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(2.W))))
317f9395f72SHaoyuan Feng  val l0asids = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(l2tlbParams.hashAsidWidth.W))))
318f9395f72SHaoyuan Feng  val l0vmids = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(l2tlbParams.hashAsidWidth.W))))
319f9395f72SHaoyuan Feng  val l0vpns = Reg(Vec(l2tlbParams.l0nSets, Vec(l2tlbParams.l0nWays, UInt(l2tlbParams.hashVpnWidth.W))))
3203ea4388cSHaoyuan Feng  def getl0vSet(vpn: UInt) = {
3213ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3223ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3233ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3243ea4388cSHaoyuan Feng    val l0vVec = l0v.asTypeOf(Vec(l2tlbParams.l0nSets, UInt(l2tlbParams.l0nWays.W)))
3253ea4388cSHaoyuan Feng    l0vVec(set)
3266d5ddbceSLemover  }
3273ea4388cSHaoyuan Feng  def getl0hSet(vpn: UInt) = {
3283ea4388cSHaoyuan Feng    require(log2Up(l2tlbParams.l0nWays) == log2Down(l2tlbParams.l0nWays))
3293ea4388cSHaoyuan Feng    val set = genPtwL0SetIdx(vpn)
3303ea4388cSHaoyuan Feng    require(set.getWidth == log2Up(l2tlbParams.l0nSets))
3313ea4388cSHaoyuan Feng    l0h(set)
33245f497a4Shappy-lx  }
3336d5ddbceSLemover
3343ea4388cSHaoyuan Feng  // sp: level 1/2/3 leaf pte of 512GB/1GB/2MB super pages
335718a93f5SHaoyuan Feng  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true, hasNapot = true)))
3365854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
3375854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
3381dd3e32dSHaoyuan Feng  val spasids = sp.map(_.asid)
339d0de7e4aSpeixiaokun  val spvmids = sp.map(_.vmid)
340d61cd5eeSpeixiaokun  val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W)))
3416d5ddbceSLemover
3428882eb68SXin Tian  if (HasBitmapCheck) {
3438882eb68SXin Tian    // wakeup corresponding entry
3448882eb68SXin Tian    when (io.bitmap_wakeup.get.valid) {
3458882eb68SXin Tian      when (io.bitmap_wakeup.get.bits.isSp) {
3468882eb68SXin Tian        for (i <- 0 until l2tlbParams.spSize) {
3478882eb68SXin Tian          when (sp(i).tag === io.bitmap_wakeup.get.bits.tag && spv(i) === 1.U) {
3488882eb68SXin Tian            spBitmapReg(i) := io.bitmap_wakeup.get.bits.check_success
3498882eb68SXin Tian          }
3508882eb68SXin Tian        }
3518882eb68SXin Tian      } .otherwise {
3528882eb68SXin Tian        val wakeup_setindex = io.bitmap_wakeup.get.bits.setIndex
3538882eb68SXin 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
3548882eb68SXin Tian        assert(l0v(wakeup_setindex * l2tlbParams.l0nWays.U + OHToUInt(io.bitmap_wakeup.get.bits.way_info)) === 1.U,
3558882eb68SXin Tian          "Wakeuped entry must be valid!")
3568882eb68SXin Tian      }
3578882eb68SXin Tian    }
3588882eb68SXin Tian  }
3598882eb68SXin Tian
3606d5ddbceSLemover  // Access Perf
3613ea4388cSHaoyuan Feng  val l3AccessPerf = if(EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
3623ea4388cSHaoyuan Feng  val l2AccessPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
3633ea4388cSHaoyuan Feng  val l1AccessPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
3643ea4388cSHaoyuan Feng  val l0AccessPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
3655854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
3663ea4388cSHaoyuan Feng  if (EnableSv48) l3AccessPerf.map(_.map(_ := false.B))
3676d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
3683ea4388cSHaoyuan Feng  l1AccessPerf.map(_ := false.B)
3693ea4388cSHaoyuan Feng  l0AccessPerf.map(_ := false.B)
3706d5ddbceSLemover  spAccessPerf.map(_ := false.B)
3716d5ddbceSLemover
3723889e11eSLemover
3731f4a7c0cSLemover
37482978df9Speixiaokun  def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = {
3753ea4388cSHaoyuan Feng    (vpn1(vpnLen-1, vpnnLen*level+3) === vpn2(vpnLen-1, vpnnLen*level+3))
3761f4a7c0cSLemover  }
3771f4a7c0cSLemover  // NOTE: not actually bypassed, just check if hit, re-access the page cache
378d0de7e4aSpeixiaokun  def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = {
3796f508cb5Speixiaokun    val change_h = MuxLookup(h_search, noS2xlate)(Seq(
380980ddf4cSpeixiaokun      allStage -> onlyStage1,
381980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
382980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
383980ddf4cSpeixiaokun    ))
3844ed5afbdSXiaokun-Pei    val change_refill_h = MuxLookup(io.refill.bits.req_info_dup(0).s2xlate, noS2xlate)(Seq(
3854ed5afbdSXiaokun-Pei      allStage -> onlyStage1,
3864ed5afbdSXiaokun-Pei      onlyStage1 -> onlyStage1,
3874ed5afbdSXiaokun-Pei      onlyStage2 -> onlyStage2
3884ed5afbdSXiaokun-Pei    ))
389d0de7e4aSpeixiaokun    val refill_vpn = io.refill.bits.req_info_dup(0).vpn
3904ed5afbdSXiaokun-Pei    io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === change_refill_h
3911f4a7c0cSLemover  }
3921f4a7c0cSLemover
39382978df9Speixiaokun  val vpn_search = stageReq.bits.req_info.vpn
3946f508cb5Speixiaokun  val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq(
39509280d15Speixiaokun    allStage -> onlyStage1,
39609280d15Speixiaokun    onlyStage1 -> onlyStage1,
39709280d15Speixiaokun    onlyStage2 -> onlyStage2
39809280d15Speixiaokun  ))
3993ea4388cSHaoyuan Feng
4003ea4388cSHaoyuan Feng  // l3
4013ea4388cSHaoyuan Feng  val l3Hit = if(EnableSv48) Some(Wire(Bool())) else None
4023ea4388cSHaoyuan Feng  val l3HitPPN = if(EnableSv48) Some(Wire(UInt(ppnLen.W))) else None
403002c10a4SYanqin Li  val l3HitPbmt = if(EnableSv48) Some(Wire(UInt(ptePbmtLen.W))) else None
4043ea4388cSHaoyuan Feng  val l3Pre = if(EnableSv48) Some(Wire(Bool())) else None
4053ea4388cSHaoyuan Feng  val ptwl3replace = if(EnableSv48) Some(ReplacementPolicy.fromString(l2tlbParams.l3Replacer, l2tlbParams.l3Size)) else None
4063ea4388cSHaoyuan Feng  if (EnableSv48) {
4073ea4388cSHaoyuan Feng    val hitVecT = l3.get.zipWithIndex.map {
40897929664SXiaokun-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)
4093ea4388cSHaoyuan Feng          && l3v.get(i) && h_search === l3h.get(i))
410d0de7e4aSpeixiaokun    }
4116c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4121f4a7c0cSLemover
4133ea4388cSHaoyuan Feng    // stageDelay, but check for l3
4143ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.ppn)), stageDelay_valid_1cycle)
415002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.pbmt)), stageDelay_valid_1cycle)
4163ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l3.get.map(_.prefetch)), stageDelay_valid_1cycle)
4171f4a7c0cSLemover    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4186d5ddbceSLemover
4193ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl3replace.get.access(OHToUInt(hitVec)) }
4206d5ddbceSLemover
4213ea4388cSHaoyuan Feng    l3AccessPerf.get.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4223ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l3Size) {
42397929664SXiaokun-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")
4246d5ddbceSLemover    }
4253ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l3] l3v:${Binary(l3v.get)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4263ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l3] l3Hit:${hit} l3HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4276d5ddbceSLemover
4283ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l3_hitVecT")
4293ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l3_hitVec")
4303ea4388cSHaoyuan Feng
4313ea4388cSHaoyuan Feng    // synchronize with other entries with RegEnable
4323ea4388cSHaoyuan Feng    l3Hit.map(_ := RegEnable(hit, stageDelay(1).fire))
4333ea4388cSHaoyuan Feng    l3HitPPN.map(_ := RegEnable(hitPPN, stageDelay(1).fire))
434002c10a4SYanqin Li    l3HitPbmt.map(_ := RegEnable(hitPbmt, stageDelay(1).fire))
4353ea4388cSHaoyuan Feng    l3Pre.map(_ := RegEnable(hitPre, stageDelay(1).fire))
4363ea4388cSHaoyuan Feng  }
4373ea4388cSHaoyuan Feng
4383ea4388cSHaoyuan Feng  // l2
4393ea4388cSHaoyuan Feng  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer, l2tlbParams.l2Size)
440002c10a4SYanqin Li  val (l2Hit, l2HitPPN, l2HitPbmt, l2Pre) = {
4413ea4388cSHaoyuan Feng    val hitVecT = l2.zipWithIndex.map {
44297929664SXiaokun-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)
4433ea4388cSHaoyuan Feng        && l2v(i) && h_search === l2h(i))
4443ea4388cSHaoyuan Feng    }
4453ea4388cSHaoyuan Feng    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
4463ea4388cSHaoyuan Feng
4473ea4388cSHaoyuan Feng    // stageDelay, but check for l2
4483ea4388cSHaoyuan Feng    val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.ppn)), stageDelay_valid_1cycle)
449002c10a4SYanqin Li    val hitPbmt = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.pbmt)), stageDelay_valid_1cycle)
4503ea4388cSHaoyuan Feng    val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l2.map(_.prefetch)), stageDelay_valid_1cycle)
4513ea4388cSHaoyuan Feng    val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle)
4523ea4388cSHaoyuan Feng
4533ea4388cSHaoyuan Feng    when (hit && stageDelay_valid_1cycle) { ptwl2replace.access(OHToUInt(hitVec)) }
4543ea4388cSHaoyuan Feng
4553ea4388cSHaoyuan Feng    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle}
4563ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
45797929664SXiaokun-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")
4583ea4388cSHaoyuan Feng    }
4593ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l2] l2v:${Binary(l2v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
4603ea4388cSHaoyuan Feng    XSDebug(stageDelay(0).valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
4613ea4388cSHaoyuan Feng
4623ea4388cSHaoyuan Feng    VecInit(hitVecT).suggestName(s"l2_hitVecT")
4633ea4388cSHaoyuan Feng    VecInit(hitVec).suggestName(s"l2_hitVec")
4646d5ddbceSLemover
4656c4dcc2dSLemover    // synchronize with other entries with RegEnable
4666c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
4676c4dcc2dSLemover     RegEnable(hitPPN, stageDelay(1).fire),
468002c10a4SYanqin Li     RegEnable(hitPbmt, stageDelay(1).fire),
4696c4dcc2dSLemover     RegEnable(hitPre, stageDelay(1).fire))
4706d5ddbceSLemover  }
4716d5ddbceSLemover
4723ea4388cSHaoyuan Feng  // l1
4733ea4388cSHaoyuan Feng  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer,l2tlbParams.l1nWays,l2tlbParams.l1nSets)
474002c10a4SYanqin Li  val (l1Hit, l1HitPPN, l1HitPbmt, l1Pre, l1eccError) = {
4753ea4388cSHaoyuan Feng    val ridx = genPtwL1SetIdx(vpn_search)
4763ea4388cSHaoyuan Feng    l1.io.r.req.valid := stageReq.fire
4773ea4388cSHaoyuan Feng    l1.io.r.req.bits.apply(setIdx = ridx)
4783ea4388cSHaoyuan Feng    val vVec_req = getl1vSet(vpn_search)
4793ea4388cSHaoyuan Feng    val hVec_req = getl1hSet(vpn_search)
4806c4dcc2dSLemover
4816c4dcc2dSLemover    // delay one cycle after sram read
48282978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
4836f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
484980ddf4cSpeixiaokun      allStage -> onlyStage1,
485980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
486980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
487980ddf4cSpeixiaokun    ))
4883ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l1.io.r.resp.data, stageDelay_valid_1cycle)
4897797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
490d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
491d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
49297929664SXiaokun-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)})
4936c4dcc2dSLemover
4946c4dcc2dSLemover    // check hit and ecc
49582978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
4966c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
497935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
4986c4dcc2dSLemover
4997797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
5007196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
5017196f5a2SLemover    val hitWayData = hitWayEntry.entries
5026c4dcc2dSLemover    val hit = ParallelOR(hitVec)
5033ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l1nWays).map(_.U(log2Up(l2tlbParams.l1nWays).W)))
504eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
505eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
506eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
507eef81af7SHaoyuan Feng    } else {
508eef81af7SHaoyuan Feng      eccError := false.B
509eef81af7SHaoyuan Feng    }
5107196f5a2SLemover
5113ea4388cSHaoyuan Feng    ridx.suggestName(s"l1_ridx")
5123ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l1_ramDatas")
5133ea4388cSHaoyuan Feng    hitVec.suggestName(s"l1_hitVec")
5143ea4388cSHaoyuan Feng    hitWayData.suggestName(s"l1_hitWayData")
5153ea4388cSHaoyuan Feng    hitWay.suggestName(s"l1_hitWay")
5166d5ddbceSLemover
5173ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl1replace.access(genPtwL1SetIdx(check_vpn), hitWay) }
5186d5ddbceSLemover
5193ea4388cSHaoyuan Feng    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5203ea4388cSHaoyuan Feng    XSDebug(stageDelay_valid_1cycle, p"[l1] ridx:0x${Hexadecimal(ridx)}\n")
5213ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
5223ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l1] ramDatas(${i.U}) ${ramDatas(i)}  l1v:${vVec(i)}  hit:${hit}\n")
5236d5ddbceSLemover    }
5243ea4388cSHaoyuan 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")
5256d5ddbceSLemover
526002c10a4SYanqin Li    (hit, hitWayData.ppns(genPtwL1SectorIdx(check_vpn)), hitWayData.pbmts(genPtwL1SectorIdx(check_vpn)), hitWayData.prefetch, eccError)
5276d5ddbceSLemover  }
5284b2c87baS梁森 Liang Sen  val te = ClockGate.genTeSink
5294b2c87baS梁森 Liang Sen  val l0_masked_clock = ClockGate(te.cgen, stageReq.fire | (!flush_dup(0) && refill.levelOH.l0) | mbistPlL0.map(_.mbist.req).getOrElse(false.B), clock)
5304b2c87baS梁森 Liang Sen  val l1_masked_clock = ClockGate(te.cgen, stageReq.fire | (!flush_dup(1) && refill.levelOH.l1) | mbistPlL1.map(_.mbist.req).getOrElse(false.B), clock)
531b32e9518SHuijin Li  l0.clock := l0_masked_clock
532b32e9518SHuijin Li  l1.clock := l1_masked_clock
5333ea4388cSHaoyuan Feng  // l0
5343ea4388cSHaoyuan Feng  val ptwl0replace = ReplacementPolicy.fromString(l2tlbParams.l0Replacer,l2tlbParams.l0nWays,l2tlbParams.l0nSets)
5358882eb68SXin Tian  val (l0Hit, l0HitData, l0Pre, l0eccError, l0HitWay, l0BitmapCheckResult, l0JmpBitmapCheck) = {
5363ea4388cSHaoyuan Feng    val ridx = genPtwL0SetIdx(vpn_search)
5373ea4388cSHaoyuan Feng    l0.io.r.req.valid := stageReq.fire
5383ea4388cSHaoyuan Feng    l0.io.r.req.bits.apply(setIdx = ridx)
5393ea4388cSHaoyuan Feng    val vVec_req = getl0vSet(vpn_search)
5403ea4388cSHaoyuan Feng    val hVec_req = getl0hSet(vpn_search)
5416c4dcc2dSLemover
5426c4dcc2dSLemover    // delay one cycle after sram read
54382978df9Speixiaokun    val delay_vpn = stageDelay(0).bits.req_info.vpn
5446f508cb5Speixiaokun    val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq(
545980ddf4cSpeixiaokun      allStage -> onlyStage1,
546980ddf4cSpeixiaokun      onlyStage1 -> onlyStage1,
547980ddf4cSpeixiaokun      onlyStage2 -> onlyStage2
548980ddf4cSpeixiaokun    ))
5493ea4388cSHaoyuan Feng    val data_resp = DataHoldBypass(l0.io.r.resp.data, stageDelay_valid_1cycle)
5507797f035SbugGenerator    val vVec_delay = RegEnable(vVec_req, stageReq.fire)
551d0de7e4aSpeixiaokun    val hVec_delay = RegEnable(hVec_req, stageReq.fire)
552d0de7e4aSpeixiaokun    val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) =>
55397929664SXiaokun-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)})
5546c4dcc2dSLemover
5556c4dcc2dSLemover    // check hit and ecc
55682978df9Speixiaokun    val check_vpn = stageCheck(0).bits.req_info.vpn
5576c4dcc2dSLemover    val ramDatas = RegEnable(data_resp, stageDelay(1).fire)
558935edac4STang Haojin    val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools
5596c4dcc2dSLemover
5607797f035SbugGenerator    val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire)
5617196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
5627196f5a2SLemover    val hitWayData = hitWayEntry.entries
5637196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
5643ea4388cSHaoyuan Feng    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l0nWays).map(_.U(log2Up(l2tlbParams.l0nWays).W)))
5658882eb68SXin Tian
5668882eb68SXin Tian    val ishptw = RegEnable(stageDelay(0).bits.isHptwReq,stageDelay(1).fire)
5678882eb68SXin Tian    val s2x_info = RegEnable(stageDelay(0).bits.req_info.s2xlate,stageDelay(1).fire)
5688882eb68SXin Tian    val pte_index = RegEnable(stageDelay(0).bits.req_info.vpn(sectortlbwidth - 1, 0),stageDelay(1).fire)
5698882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
5708882eb68SXin Tian    val hit = WireInit(false.B)
5718882eb68SXin Tian    val l0bitmapreg = WireInit((VecInit(Seq.fill(l2tlbParams.l0nWays)(VecInit(Seq.fill(tlbcontiguous)(0.U(1.W)))))))
5728882eb68SXin Tian    if (HasBitmapCheck) {
5738882eb68SXin Tian      l0bitmapreg := RegEnable(RegNext(l0BitmapReg(ridx)), stageDelay(1).fire)
5748882eb68SXin Tian      // cause llptw will trigger bitmapcheck
5758882eb68SXin Tian      // add a coniditonal logic
5768882eb68SXin Tian      // (s2x_info =/= allStage || ishptw)
5778882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 1.U, ParallelOR(hitVec))
5788882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && l0bitmapreg(hitWay)(pte_index) === 0.U) {
5798882eb68SXin Tian        jmp_bitmap_check := true.B
5808882eb68SXin Tian      }
5818882eb68SXin Tian    } else {
5828882eb68SXin Tian      hit := ParallelOR(hitVec)
5838882eb68SXin Tian    }
584eef81af7SHaoyuan Feng    val eccError = WireInit(false.B)
585eef81af7SHaoyuan Feng    if (l2tlbParams.enablePTWECC) {
586eef81af7SHaoyuan Feng      eccError := hitWayEntry.decode()
587eef81af7SHaoyuan Feng    } else {
588eef81af7SHaoyuan Feng      eccError := false.B
589eef81af7SHaoyuan Feng    }
5906d5ddbceSLemover
5913ea4388cSHaoyuan Feng    when (hit && stageCheck_valid_1cycle) { ptwl0replace.access(genPtwL0SetIdx(check_vpn), hitWay) }
5927196f5a2SLemover
5933ea4388cSHaoyuan Feng    l0AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle }
5943ea4388cSHaoyuan Feng    XSDebug(stageReq.fire, p"[l0] ridx:0x${Hexadecimal(ridx)}\n")
5953ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
5963ea4388cSHaoyuan Feng      XSDebug(stageCheck_valid_1cycle, p"[l0] ramDatas(${i.U}) ${ramDatas(i)}  l0v:${vVec(i)}  hit:${hitVec(i)}\n")
5976d5ddbceSLemover    }
5983ea4388cSHaoyuan Feng    XSDebug(stageCheck_valid_1cycle, p"[l0] l0Hit:${hit} l0HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n")
5996d5ddbceSLemover
6003ea4388cSHaoyuan Feng    ridx.suggestName(s"l0_ridx")
6013ea4388cSHaoyuan Feng    ramDatas.suggestName(s"l0_ramDatas")
6023ea4388cSHaoyuan Feng    hitVec.suggestName(s"l0_hitVec")
6033ea4388cSHaoyuan Feng    hitWay.suggestName(s"l0_hitWay")
6046d5ddbceSLemover
6058882eb68SXin Tian    (hit, hitWayData, hitWayData.prefetch, eccError, UIntToOH(hitWay), l0bitmapreg(hitWay), jmp_bitmap_check)
6066d5ddbceSLemover  }
6073ea4388cSHaoyuan Feng  val l0HitPPN = l0HitData.ppns
608002c10a4SYanqin Li  val l0HitPbmt = l0HitData.pbmts
6093ea4388cSHaoyuan Feng  val l0HitPerm = l0HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL0SectorSize, new PtePermBundle)))
610e0c1f271SHaoyuan Feng  val l0HitValid = VecInit(l0HitData.onlypf.map(!_))
6118882eb68SXin Tian  val l0Ptes = WireInit(VecInit(Seq.fill(tlbcontiguous)(0.U(XLEN.W)))) // L0 lavel Page Table Entry Vector
6128882eb68SXin Tian  val l0cfs = WireInit(VecInit(Seq.fill(tlbcontiguous)(false.B))) // L0 lavel Bitmap Check Failed Vector
6138882eb68SXin Tian  if (HasBitmapCheck) {
6148882eb68SXin Tian    for (i <- 0 until tlbcontiguous) {
6158882eb68SXin Tian      l0Ptes(i) := Cat(l0HitData.pbmts(i).asUInt,l0HitPPN(i), 0.U(2.W),l0HitPerm(i).asUInt,l0HitValid(i).asUInt)
6168882eb68SXin Tian      l0cfs(i) := !l0BitmapCheckResult(i)
6178882eb68SXin Tian    }
6188882eb68SXin Tian  }
6196d5ddbceSLemover
6206d5ddbceSLemover  // super page
6215854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
6228882eb68SXin Tian  val (spHit, spHitData, spPre, spValid, spJmpBitmapCheck) = {
623718a93f5SHaoyuan 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) }
6246c4dcc2dSLemover    val hitVec = hitVecT.map(RegEnable(_, stageReq.fire))
6256d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
6268882eb68SXin Tian    val ishptw = RegEnable(stageReq.bits.isHptwReq, stageReq.fire)
6278882eb68SXin Tian    val s2x_info = RegEnable(stageReq.bits.req_info.s2xlate, stageReq.fire)
6288882eb68SXin Tian    val jmp_bitmap_check  = WireInit(false.B)
6298882eb68SXin Tian    val hit = WireInit(false.B)
6308882eb68SXin Tian    if (HasBitmapCheck) {
6318882eb68SXin Tian      hit := Mux(bitmapEnable && (s2x_info =/= allStage || ishptw), ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 1.U, ParallelOR(hitVec))
6328882eb68SXin Tian      when (bitmapEnable && (s2x_info =/= allStage || ishptw) && ParallelOR(hitVec) && spBitmapReg(OHToUInt(hitVec)) === 0.U) {
6338882eb68SXin Tian        jmp_bitmap_check := true.B
6348882eb68SXin Tian      }
6358882eb68SXin Tian    } else {
6368882eb68SXin Tian      hit := ParallelOR(hitVec)
6378882eb68SXin Tian    }
6386d5ddbceSLemover
6396c4dcc2dSLemover    when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) }
6406d5ddbceSLemover
6416c4dcc2dSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle }
6425854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
64397929664SXiaokun-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")
6446d5ddbceSLemover    }
6456c4dcc2dSLemover    XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
6466d5ddbceSLemover
6476d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
6486d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
6496d5ddbceSLemover
6506c4dcc2dSLemover    (RegEnable(hit, stageDelay(1).fire),
6516c4dcc2dSLemover     RegEnable(hitData, stageDelay(1).fire),
6526c4dcc2dSLemover     RegEnable(hitData.prefetch, stageDelay(1).fire),
6538882eb68SXin Tian     RegEnable(hitData.v, stageDelay(1).fire),
6548882eb68SXin Tian     RegEnable(jmp_bitmap_check, stageDelay(1).fire))
6556d5ddbceSLemover  }
6566d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
6576d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
6588882eb68SXin Tian  val spPte = Cat(spHitData.pbmt.asUInt,spHitData.ppn, 0.U(2.W), spHitPerm.asUInt,spHitData.v.asUInt) // Super-page Page Table Entry
6596d5ddbceSLemover
6606c4dcc2dSLemover  val check_res = Wire(new PageCacheRespBundle)
6617fbc7393SYanqin Li  check_res.l3.map(_.apply(l3Hit.get, l3Pre.get, l3HitPPN.get, l3HitPbmt.get))
662002c10a4SYanqin Li  check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, l2HitPbmt)
663002c10a4SYanqin Li  check_res.l1.apply(l1Hit, l1Pre, l1HitPPN, l1HitPbmt, ecc = l1eccError)
6648882eb68SXin Tian  check_res.l0.apply(l0Hit, l0Pre, l0HitPPN, l0HitPbmt, l0HitPerm, l0eccError, valid = l0HitValid, jmp_bitmap_check = l0JmpBitmapCheck, hitway = l0HitWay, ptes = l0Ptes, cfs = l0cfs)
6658882eb68SXin Tian  check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitData.pbmt, spHitData.n.getOrElse(0.U), spHitPerm, false.B, spHitLevel, spValid, spJmpBitmapCheck, spPte)
6666d5ddbceSLemover
6676c4dcc2dSLemover  val resp_res = Reg(new PageCacheRespBundle)
6686c4dcc2dSLemover  when (stageCheck(1).fire) { resp_res := check_res }
6693889e11eSLemover
6701f4a7c0cSLemover  // stageResp bypass
6713ea4388cSHaoyuan Feng  val bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
6721f4a7c0cSLemover  bypassed.indices.foreach(i =>
6731f4a7c0cSLemover    bypassed(i) := stageResp.bits.bypassed(i) ||
6746967f5d5Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
6751f4a7c0cSLemover        OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid)
6761f4a7c0cSLemover  )
6771f4a7c0cSLemover
67883d93d53Speixiaokun  // stageResp bypass to hptw
6793ea4388cSHaoyuan Feng  val hptw_bypassed = if (EnableSv48) Wire(Vec(4, Bool())) else Wire(Vec(3, Bool()))
68083d93d53Speixiaokun  hptw_bypassed.indices.foreach(i =>
68183d93d53Speixiaokun    hptw_bypassed(i) := stageResp.bits.bypassed(i) ||
68283d93d53Speixiaokun      ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate),
68383d93d53Speixiaokun        io.resp.fire)
68483d93d53Speixiaokun  )
68583d93d53Speixiaokun
68630104977Speixiaokun  val isAllStage = stageResp.bits.req_info.s2xlate === allStage
687c0991f6aSpeixiaokun  val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2
6883ea4388cSHaoyuan Feng  val stage1Hit = (resp_res.l0.hit || resp_res.sp.hit) && isAllStage
689da605600Speixiaokun  val idx = stageResp.bits.req_info.vpn(2, 0)
6903ea4388cSHaoyuan Feng  val stage1Pf = !Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
6916c4dcc2dSLemover  io.resp.bits.req_info   := stageResp.bits.req_info
6926c4dcc2dSLemover  io.resp.bits.isFirst  := stageResp.bits.isFirst
6933ea4388cSHaoyuan Feng  io.resp.bits.hit      := (resp_res.l0.hit || resp_res.sp.hit) && (!isAllStage || isAllStage && stage1Pf)
6943ea4388cSHaoyuan Feng  if (EnableSv48) {
69526175c3fSHaoyuan 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
6963ea4388cSHaoyuan Feng  } else {
69726175c3fSHaoyuan Feng    io.resp.bits.bypassed := ((bypassed(0) && !resp_res.l0.hit) || (bypassed(1) && !resp_res.l1.hit) || (bypassed(2) && !resp_res.l2.hit)) && !isAllStage
6983ea4388cSHaoyuan Feng  }
6993ea4388cSHaoyuan Feng  io.resp.bits.prefetch := resp_res.l0.pre && resp_res.l0.hit || resp_res.sp.pre && resp_res.sp.hit
7003ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l3Hit.map(_ := resp_res.l3.get.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq)
701325f0a4eSpeixiaokun  io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
7023ea4388cSHaoyuan Feng  io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq
7033ea4388cSHaoyuan 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))
704980ddf4cSpeixiaokun  io.resp.bits.toFsm.stage1Hit := stage1Hit
7058882eb68SXin Tian  if (HasBitmapCheck) {
7068882eb68SXin 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
7078882eb68SXin 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)
7088882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
7098882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
7108882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
7118882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
7128882eb68SXin Tian    io.resp.bits.toFsm.bitmapCheck.get.SPlevel := resp_res.sp.level
7138882eb68SXin Tian  }
714d0de7e4aSpeixiaokun
715325f0a4eSpeixiaokun  io.resp.bits.isHptwReq := stageResp.bits.isHptwReq
7163ea4388cSHaoyuan Feng  if (EnableSv48) {
71726175c3fSHaoyuan 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
7183ea4388cSHaoyuan Feng  } else {
71926175c3fSHaoyuan 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
7203ea4388cSHaoyuan Feng  }
721d0de7e4aSpeixiaokun  io.resp.bits.toHptw.id := stageResp.bits.hptwId
7223ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l3Hit.map(_ := resp_res.l3.get.hit && stageResp.bits.isHptwReq)
723325f0a4eSpeixiaokun  io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq
7243ea4388cSHaoyuan Feng  io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq
7253ea4388cSHaoyuan 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)
72682978df9Speixiaokun  io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn
727eb4bf3f2Speixiaokun  io.resp.bits.toHptw.resp.entry.asid := DontCare
72897929664SXiaokun-Pei  io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.vmid)
7293ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l0.hit, 0.U, resp_res.sp.level))
730d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source)
7313ea4388cSHaoyuan 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)
732002c10a4SYanqin Li  io.resp.bits.toHptw.resp.entry.pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(idx), resp_res.sp.pbmt)
733718a93f5SHaoyuan Feng  io.resp.bits.toHptw.resp.entry.n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
7343ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l0.hit, resp_res.l0.perm(idx), resp_res.sp.perm))
7353ea4388cSHaoyuan Feng  io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l0.hit, resp_res.l0.v(idx), resp_res.sp.v)
736d0de7e4aSpeixiaokun  io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v
737e0c1f271SHaoyuan Feng  io.resp.bits.toHptw.resp.gaf := false.B
7388882eb68SXin Tian  if (HasBitmapCheck) {
7398882eb68SXin 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
7408882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.hitway := resp_res.l0.bitmapCheck.get.hitway
7418882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.pte := resp_res.sp.bitmapCheck.get.pte
7428882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.ptes := resp_res.l0.bitmapCheck.get.ptes
7438882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.cfs := resp_res.l0.bitmapCheck.get.cfs
7448882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.fromSP := resp_res.sp.bitmapCheck.get.jmp_bitmap_check
7458882eb68SXin Tian    io.resp.bits.toHptw.bitmapCheck.get.SPlevel := resp_res.sp.level
7468882eb68SXin Tian  }
747d0de7e4aSpeixiaokun
7486979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3))
7496979864eSXiaokun-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
75097929664SXiaokun-Pei  io.resp.bits.stage1.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.vmid))
7513ea4388cSHaoyuan Feng  if (EnableSv48) {
7523ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
7533ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
7543ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U,
7553ea4388cSHaoyuan Feng          Mux(resp_res.l2.hit, 2.U, 3.U))))))
7563ea4388cSHaoyuan Feng  } else {
7573ea4388cSHaoyuan Feng    io.resp.bits.stage1.entry.map(_.level.map(_ := Mux(resp_res.l0.hit, 0.U,
7583ea4388cSHaoyuan Feng      Mux(resp_res.sp.hit, resp_res.sp.level,
7593ea4388cSHaoyuan Feng        Mux(resp_res.l1.hit, 1.U, 2.U)))))
7603ea4388cSHaoyuan Feng  }
7616979864eSXiaokun-Pei  io.resp.bits.stage1.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source))
76263632028SHaoyuan Feng  for (i <- 0 until tlbcontiguous) {
7633ea4388cSHaoyuan Feng    if (EnableSv48) {
7643ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7653ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7663ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7673ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth),
7683ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(gvpnLen - 1, sectortlbwidth)))))
7693ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7703ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7713ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7723ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.ppn(sectortlbwidth - 1, 0),
7733ea4388cSHaoyuan Feng              resp_res.l3.get.ppn(sectortlbwidth - 1, 0)))))
7743ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7753ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7763ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7773ea4388cSHaoyuan Feng            Mux(resp_res.l2.hit, resp_res.l2.v,
7783ea4388cSHaoyuan Feng              resp_res.l3.get.v))))
7793ea4388cSHaoyuan Feng    } else {
7803ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(gvpnLen - 1, sectortlbwidth),
7813ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(gvpnLen - 1, sectortlbwidth),
7823ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(gvpnLen - 1, sectortlbwidth),
7833ea4388cSHaoyuan Feng            resp_res.l2.ppn(gvpnLen - 1, sectortlbwidth))))
7843ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).ppn_low := Mux(resp_res.l0.hit, resp_res.l0.ppn(i)(sectortlbwidth - 1, 0),
7853ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.ppn(sectortlbwidth - 1, 0),
7863ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.ppn(sectortlbwidth - 1, 0),
7873ea4388cSHaoyuan Feng            resp_res.l2.ppn(sectortlbwidth - 1, 0))))
7883ea4388cSHaoyuan Feng      io.resp.bits.stage1.entry(i).v := Mux(resp_res.l0.hit, resp_res.l0.v(i),
7893ea4388cSHaoyuan Feng        Mux(resp_res.sp.hit, resp_res.sp.v,
7903ea4388cSHaoyuan Feng          Mux(resp_res.l1.hit, resp_res.l1.v,
7913ea4388cSHaoyuan Feng            resp_res.l2.v)))
7923ea4388cSHaoyuan Feng    }
793002c10a4SYanqin Li    io.resp.bits.stage1.entry(i).pbmt := Mux(resp_res.l0.hit, resp_res.l0.pbmt(i),
794002c10a4SYanqin Li      Mux(resp_res.sp.hit, resp_res.sp.pbmt,
795002c10a4SYanqin Li        Mux(resp_res.l1.hit, resp_res.l1.pbmt,
796002c10a4SYanqin Li          resp_res.l2.pbmt)))
797718a93f5SHaoyuan Feng    io.resp.bits.stage1.entry(i).n.map(_ := Mux(resp_res.sp.hit, resp_res.sp.n, 0.U))
79897929664SXiaokun-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))))
7996979864eSXiaokun-Pei    io.resp.bits.stage1.entry(i).pf := !io.resp.bits.stage1.entry(i).v
800e0c1f271SHaoyuan Feng    io.resp.bits.stage1.entry(i).af := false.B
8018882eb68SXin Tian    io.resp.bits.stage1.entry(i).cf := l0cfs(i) // L0 lavel Bitmap Check Failed Vector
80263632028SHaoyuan Feng  }
803da605600Speixiaokun  io.resp.bits.stage1.pteidx := UIntToOH(idx).asBools
8043ea4388cSHaoyuan Feng  io.resp.bits.stage1.not_super := Mux(resp_res.l0.hit, true.B, false.B)
8056962b4ffSHaoyuan Feng  io.resp.bits.stage1.not_merge := false.B
8066c4dcc2dSLemover  io.resp.valid := stageResp.valid
8073ea4388cSHaoyuan Feng  XSError(stageResp.valid && resp_res.l0.hit && resp_res.sp.hit, "normal page and super page both hit")
8086d5ddbceSLemover
8096d5ddbceSLemover  // refill Perf
8103ea4388cSHaoyuan Feng  val l3RefillPerf = if (EnableSv48) Some(Wire(Vec(l2tlbParams.l3Size, Bool()))) else None
8113ea4388cSHaoyuan Feng  val l2RefillPerf = Wire(Vec(l2tlbParams.l2Size, Bool()))
8123ea4388cSHaoyuan Feng  val l1RefillPerf = Wire(Vec(l2tlbParams.l1nWays, Bool()))
8133ea4388cSHaoyuan Feng  val l0RefillPerf = Wire(Vec(l2tlbParams.l0nWays, Bool()))
8145854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
8153ea4388cSHaoyuan Feng  l3RefillPerf.map(_.map(_ := false.B))
8166d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
8173ea4388cSHaoyuan Feng  l1RefillPerf.map(_ := false.B)
8183ea4388cSHaoyuan Feng  l0RefillPerf.map(_ := false.B)
8196d5ddbceSLemover  spRefillPerf.map(_ := false.B)
8206d5ddbceSLemover
8216d5ddbceSLemover  // refill
8223ea4388cSHaoyuan Feng  l1.io.w.req <> DontCare
8233ea4388cSHaoyuan Feng  l0.io.w.req <> DontCare
8243ea4388cSHaoyuan Feng  l1.io.w.req.valid := false.B
8253ea4388cSHaoyuan Feng  l0.io.w.req.valid := false.B
8266d5ddbceSLemover
8276d5ddbceSLemover  val memRdata = refill.ptes
8285854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
8297797f035SbugGenerator  val memSelData = io.refill.bits.sel_pte_dup
8307797f035SbugGenerator  val memPte = memSelData.map(a => a.asTypeOf(new PteBundle))
831dd286b6aSYanqin Li  val mPBMTE = io.csr.mPBMTE
832dd286b6aSYanqin Li  val hPBMTE = io.csr.hPBMTE
833dd286b6aSYanqin Li  val pbmte = Mux(refill.req_info_dup(0).s2xlate === onlyStage1 || refill.req_info_dup(0).s2xlate === allStage, hPBMTE, mPBMTE)
834b848eea5SLemover
8358882eb68SXin Tian  def Tran2D(flushMask: UInt): Vec[UInt] = {
8368882eb68SXin Tian    val tran2D = Wire(Vec(l2tlbParams.l0nSets,UInt(l2tlbParams.l0nWays.W)))
8378882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
8388882eb68SXin Tian      tran2D(i) := flushMask((i + 1) * l2tlbParams.l0nWays - 1, i * l2tlbParams.l0nWays)
8398882eb68SXin Tian    }
8408882eb68SXin Tian    tran2D
8418882eb68SXin Tian  }
8428882eb68SXin Tian  def updateL0BitmapReg(l0BitmapReg: Vec[Vec[Vec[UInt]]], tran2D: Vec[UInt]) = {
8438882eb68SXin Tian    for (i <- 0 until l2tlbParams.l0nSets) {
8448882eb68SXin Tian      for (j <- 0 until l2tlbParams.l0nWays) {
8458882eb68SXin Tian        when (tran2D(i)(j) === 0.U) {
8468882eb68SXin Tian          for (k <- 0 until tlbcontiguous) {
8478882eb68SXin Tian            l0BitmapReg(i)(j)(k) := 0.U
8488882eb68SXin Tian          }
8498882eb68SXin Tian        }
8508882eb68SXin Tian      }
8518882eb68SXin Tian    }
8528882eb68SXin Tian  }
8538882eb68SXin Tian  def TranVec(flushMask: UInt): Vec[UInt] = {
8548882eb68SXin Tian    val vec = Wire(Vec(l2tlbParams.spSize,UInt(1.W)))
8558882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
8568882eb68SXin Tian      vec(i) := flushMask(i)
8578882eb68SXin Tian    }
8588882eb68SXin Tian    vec
8598882eb68SXin Tian  }
8608882eb68SXin Tian  def updateSpBitmapReg(spBitmapReg: Vec[UInt], vec : Vec[UInt]) = {
8618882eb68SXin Tian    for (i <- 0 until l2tlbParams.spSize) {
8628882eb68SXin Tian      spBitmapReg(i) := spBitmapReg(i) & vec(i)
8638882eb68SXin Tian    }
8648882eb68SXin Tian  }
8658882eb68SXin Tian
8666d5ddbceSLemover  // TODO: handle sfenceLatch outsize
8673ea4388cSHaoyuan Feng  if (EnableSv48) {
8688b33cd30Sklin02    val l3Refill =
8696962b4ffSHaoyuan Feng      !flush_dup(2) &&
8706962b4ffSHaoyuan Feng      refill.levelOH.l3.get &&
8716962b4ffSHaoyuan Feng      !memPte(2).isLeaf() &&
872*fc4ea7c0SHaoyuan Feng      memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).hgatp.mode)
8738b33cd30Sklin02    val l3RefillIdx = replaceWrapper(l3v.get, ptwl3replace.get.way).suggestName(s"l3_refillIdx")
8748b33cd30Sklin02    val l3RfOH = UIntToOH(l3RefillIdx).asUInt.suggestName(s"l3_rfOH")
8758b33cd30Sklin02    when (l3Refill) {
8768b33cd30Sklin02      l3.get(l3RefillIdx).refill(
8773ea4388cSHaoyuan Feng        refill.req_info_dup(2).vpn,
8783ea4388cSHaoyuan Feng        Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
87997929664SXiaokun-Pei        io.csr_dup(2).hgatp.vmid,
8803ea4388cSHaoyuan Feng        memSelData(2),
8813ea4388cSHaoyuan Feng        3.U,
8823ea4388cSHaoyuan Feng        refill_prefetch_dup(2)
88345f497a4Shappy-lx      )
8848b33cd30Sklin02      ptwl2replace.access(l3RefillIdx)
8858b33cd30Sklin02      l3v.get := l3v.get | l3RfOH
8868b33cd30Sklin02      l3g.get := (l3g.get & ~l3RfOH) | Mux(memPte(2).perm.g, l3RfOH, 0.U)
8878b33cd30Sklin02      l3h.get(l3RefillIdx) := refill_h(2)
8886d5ddbceSLemover
8893ea4388cSHaoyuan Feng      for (i <- 0 until l2tlbParams.l3Size) {
8908b33cd30Sklin02        l3RefillPerf.get(i) := i.U === l3RefillIdx
8916d5ddbceSLemover      }
8923ea4388cSHaoyuan Feng    }
8938b33cd30Sklin02    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")
8948b33cd30Sklin02    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")
8956d5ddbceSLemover  }
8966d5ddbceSLemover
8976962b4ffSHaoyuan Feng  // L2 refill
8988b33cd30Sklin02  val l2Refill =
8996962b4ffSHaoyuan Feng    !flush_dup(2) &&
9006962b4ffSHaoyuan Feng    refill.levelOH.l2 &&
9016962b4ffSHaoyuan Feng    !memPte(2).isLeaf() &&
902*fc4ea7c0SHaoyuan Feng    memPte(2).canRefill(refill.level_dup(2), refill.req_info_dup(2).s2xlate, pbmte, io.csr_dup(2).hgatp.mode)
9038b33cd30Sklin02  val l2RefillIdx = replaceWrapper(l2v, ptwl2replace.way).suggestName(s"l2_refillIdx")
9048b33cd30Sklin02  val l2RfOH = UIntToOH(l2RefillIdx).asUInt.suggestName(s"l2_rfOH")
9058b33cd30Sklin02  when (
9068b33cd30Sklin02    l2Refill
907dd286b6aSYanqin Li  ) {
9088b33cd30Sklin02    l2(l2RefillIdx).refill(
9093ea4388cSHaoyuan Feng      refill.req_info_dup(2).vpn,
9103ea4388cSHaoyuan Feng      Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid),
91197929664SXiaokun-Pei      io.csr_dup(2).hgatp.vmid,
9123ea4388cSHaoyuan Feng      memSelData(2),
9133ea4388cSHaoyuan Feng      2.U,
9143ea4388cSHaoyuan Feng      refill_prefetch_dup(2)
9153ea4388cSHaoyuan Feng    )
9168b33cd30Sklin02    ptwl2replace.access(l2RefillIdx)
9178b33cd30Sklin02    l2v := l2v | l2RfOH
9188b33cd30Sklin02    l2g := (l2g & ~l2RfOH) | Mux(memPte(2).perm.g, l2RfOH, 0.U)
9198b33cd30Sklin02    l2h(l2RefillIdx) := refill_h(2)
9203ea4388cSHaoyuan Feng
9213ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l2Size) {
9228b33cd30Sklin02      l2RefillPerf(i) := i.U === l2RefillIdx
9233ea4388cSHaoyuan Feng    }
9243ea4388cSHaoyuan Feng  }
9258b33cd30Sklin02  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")
9268b33cd30Sklin02  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")
9273ea4388cSHaoyuan Feng
9286962b4ffSHaoyuan Feng  // L1 refill
9298b33cd30Sklin02  val l1Refill = !flush_dup(1) && refill.levelOH.l1
9308b33cd30Sklin02  val l1RefillIdx = genPtwL1SetIdx(refill.req_info_dup(1).vpn).suggestName(s"l1_refillIdx")
9318b33cd30Sklin02  val l1VictimWay = replaceWrapper(getl1vSet(refill.req_info_dup(1).vpn), ptwl1replace.way(l1RefillIdx)).suggestName(s"l1_victimWay")
9328b33cd30Sklin02  val l1VictimWayOH = UIntToOH(l1VictimWay).suggestName(s"l1_victimWayOH")
9338b33cd30Sklin02  val l1RfvOH = UIntToOH(Cat(l1RefillIdx, l1VictimWay)).asUInt.suggestName(s"l1_rfvOH")
9348b33cd30Sklin02  val l1Wdata = Wire(l1EntryType)
935f9395f72SHaoyuan Feng  val l1Wvpn = refill.req_info_dup(1).vpn
936f9395f72SHaoyuan Feng  val l1Wasid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid)
9378b33cd30Sklin02  l1Wdata.gen(
938f9395f72SHaoyuan Feng    vpn = l1Wvpn,
939f9395f72SHaoyuan Feng    asid = l1Wasid,
94097929664SXiaokun-Pei    vmid = io.csr_dup(1).hgatp.vmid,
94145f497a4Shappy-lx    data = memRdata,
94245f497a4Shappy-lx    levelUInt = 1.U,
9434ed5afbdSXiaokun-Pei    refill_prefetch_dup(1),
944dd286b6aSYanqin Li    refill.req_info_dup(1).s2xlate,
945e0c1f271SHaoyuan Feng    pbmte,
946*fc4ea7c0SHaoyuan Feng    io.csr_dup(1).hgatp.mode
94745f497a4Shappy-lx  )
9488b33cd30Sklin02  when (l1Refill) {
9493ea4388cSHaoyuan Feng    l1.io.w.apply(
9506d5ddbceSLemover      valid = true.B,
9518b33cd30Sklin02      setIdx = l1RefillIdx,
9528b33cd30Sklin02      data = l1Wdata,
9538b33cd30Sklin02      waymask = l1VictimWayOH
9546d5ddbceSLemover    )
9558b33cd30Sklin02    ptwl1replace.access(l1RefillIdx, l1VictimWay)
9568b33cd30Sklin02    l1v := l1v | l1RfvOH
9578b33cd30Sklin02    l1g := l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U)
9588b33cd30Sklin02    l1h(l1RefillIdx)(l1VictimWay) := refill_h(1)
959f9395f72SHaoyuan Feng    l1asids(l1RefillIdx)(l1VictimWay) := XORFold(l1Wasid, l2tlbParams.hashAsidWidth)
960f9395f72SHaoyuan Feng    l1vmids(l1RefillIdx)(l1VictimWay) := XORFold(io.csr_dup(1).hgatp.vmid, l2tlbParams.hashAsidWidth)
9616d5ddbceSLemover
9623ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l1nWays) {
9638b33cd30Sklin02      l1RefillPerf(i) := i.U === l1VictimWay
9646d5ddbceSLemover    }
9656d5ddbceSLemover  }
9668b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refillIdx:0x${Hexadecimal(l1RefillIdx)} victimWay:${l1VictimWay} victimWayOH:${Binary(l1VictimWayOH)} rfvOH(in UInt):${Cat(l1RefillIdx, l1VictimWay)}\n")
9678b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] refilldata:0x${l1Wdata}\n")
9688b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1v:${Binary(l1v)} -> ${Binary(l1v | l1RfvOH)}\n")
9698b33cd30Sklin02  XSDebug(l1Refill, p"[l1 refill] l1g:${Binary(l1g)} -> ${Binary(l1g & ~l1RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l1RfvOH, 0.U))}\n")
9706d5ddbceSLemover
9716962b4ffSHaoyuan Feng  // L0 refill
972718a93f5SHaoyuan Feng  val l0Refill = !flush_dup(0) && refill.levelOH.l0 && !memPte(0).isNapot(refill.level_dup(0))
9738b33cd30Sklin02  val l0RefillIdx = genPtwL0SetIdx(refill.req_info_dup(0).vpn).suggestName(s"l0_refillIdx")
9748b33cd30Sklin02  val l0VictimWay = replaceWrapper(getl0vSet(refill.req_info_dup(0).vpn), ptwl0replace.way(l0RefillIdx)).suggestName(s"l0_victimWay")
9758b33cd30Sklin02  val l0VictimWayOH = UIntToOH(l0VictimWay).asUInt.suggestName(s"l0_victimWayOH")
9768b33cd30Sklin02  val l0RfvOH = UIntToOH(Cat(l0RefillIdx, l0VictimWay)).suggestName(s"l0_rfvOH")
9778b33cd30Sklin02  val l0Wdata = Wire(l0EntryType)
9788882eb68SXin Tian  // trans the l0 way info, for late wakeup logic
9798882eb68SXin Tian  if (HasBitmapCheck) {
9808882eb68SXin Tian    io.l0_way_info.get := l0VictimWayOH
9818882eb68SXin Tian  }
982f9395f72SHaoyuan Feng  val l0Wvpn = refill.req_info_dup(0).vpn
983f9395f72SHaoyuan Feng  val l0Wasid = Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid)
9848b33cd30Sklin02  l0Wdata.gen(
985f9395f72SHaoyuan Feng    vpn = l0Wvpn,
986f9395f72SHaoyuan Feng    asid = l0Wasid,
98797929664SXiaokun-Pei    vmid = io.csr_dup(0).hgatp.vmid,
98845f497a4Shappy-lx    data = memRdata,
9893ea4388cSHaoyuan Feng    levelUInt = 0.U,
9903ea4388cSHaoyuan Feng    refill_prefetch_dup(0),
991dd286b6aSYanqin Li    refill.req_info_dup(0).s2xlate,
992e0c1f271SHaoyuan Feng    pbmte,
993*fc4ea7c0SHaoyuan Feng    io.csr_dup(0).hgatp.mode
99445f497a4Shappy-lx  )
9958b33cd30Sklin02  when (l0Refill) {
9963ea4388cSHaoyuan Feng    l0.io.w.apply(
9976d5ddbceSLemover      valid = true.B,
9988b33cd30Sklin02      setIdx = l0RefillIdx,
9998b33cd30Sklin02      data = l0Wdata,
10008b33cd30Sklin02      waymask = l0VictimWayOH
10016d5ddbceSLemover    )
10028b33cd30Sklin02    ptwl0replace.access(l0RefillIdx, l0VictimWay)
10038b33cd30Sklin02    l0v := l0v | l0RfvOH
10048b33cd30Sklin02    l0g := l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U)
10058b33cd30Sklin02    l0h(l0RefillIdx)(l0VictimWay) := refill_h(0)
10068882eb68SXin Tian    if (HasBitmapCheck) {updateL0BitmapReg(l0BitmapReg, Tran2D(~l0RfvOH))}
1007f9395f72SHaoyuan Feng    l0asids(l0RefillIdx)(l0VictimWay) := XORFold(l0Wasid, l2tlbParams.hashAsidWidth)
1008f9395f72SHaoyuan Feng    l0vmids(l0RefillIdx)(l0VictimWay) := XORFold(io.csr_dup(0).hgatp.vmid, l2tlbParams.hashAsidWidth)
1009f9395f72SHaoyuan Feng    l0vpns(l0RefillIdx)(l0VictimWay) := XORFold(l0Wvpn(vpnLen - 1, vpnLen - PtwL0TagLen), l2tlbParams.hashVpnWidth)
10106d5ddbceSLemover
10113ea4388cSHaoyuan Feng    for (i <- 0 until l2tlbParams.l0nWays) {
10128b33cd30Sklin02      l0RefillPerf(i) := i.U === l0VictimWay
10136d5ddbceSLemover    }
10146d5ddbceSLemover  }
10158b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refillIdx:0x${Hexadecimal(l0RefillIdx)} victimWay:${l0VictimWay} victimWayOH:${Binary(l0VictimWayOH)} rfvOH(in UInt):${Cat(l0RefillIdx, l0VictimWay)}\n")
10168b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] refilldata:0x${l0Wdata}\n")
10178b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0v:${Binary(l0v)} -> ${Binary(l0v | l0RfvOH)}\n")
10188b33cd30Sklin02  XSDebug(l0Refill, p"[l0 refill] l0g:${Binary(l0g)} -> ${Binary(l0g & ~l0RfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, l0RfvOH, 0.U))}\n")
10197797f035SbugGenerator
10208d8ac704SLemover
10218d8ac704SLemover  // misc entries: super & invalid
10228b33cd30Sklin02  val spRefill =
10236962b4ffSHaoyuan Feng    !flush_dup(0) &&
10240a56a7dcSHaoyuan Feng    (refill.levelOH.sp || (refill.levelOH.l0 && memPte(0).isNapot(refill.level_dup(0)))) &&
1025*fc4ea7c0SHaoyuan Feng    ((memPte(0).isLeaf() && memPte(0).canRefill(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte, io.csr_dup(0).hgatp.mode)) ||
1026e0c1f271SHaoyuan Feng    memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte))
10278b33cd30Sklin02  val spRefillIdx = spreplace.way.suggestName(s"sp_refillIdx") // LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
10288b33cd30Sklin02  val spRfOH = UIntToOH(spRefillIdx).asUInt.suggestName(s"sp_rfOH")
10298b33cd30Sklin02  when (spRefill) {
10308b33cd30Sklin02    sp(spRefillIdx).refill(
10317797f035SbugGenerator      refill.req_info_dup(0).vpn,
1032b188e334Speixiaokun      Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid),
103397929664SXiaokun-Pei      io.csr_dup(0).hgatp.vmid,
10347797f035SbugGenerator      memSelData(0),
10353ea4388cSHaoyuan Feng      refill.level_dup(0),
10367797f035SbugGenerator      refill_prefetch_dup(0),
1037e0c1f271SHaoyuan Feng      !memPte(0).onlyPf(refill.level_dup(0), refill.req_info_dup(0).s2xlate, pbmte)
103845f497a4Shappy-lx    )
10398b33cd30Sklin02    spreplace.access(spRefillIdx)
10408b33cd30Sklin02    spv := spv | spRfOH
10418b33cd30Sklin02    spg := spg & ~spRfOH | Mux(memPte(0).perm.g, spRfOH, 0.U)
10428b33cd30Sklin02    sph(spRefillIdx) := refill_h(0)
10438882eb68SXin Tian    if (HasBitmapCheck) {updateSpBitmapReg(spBitmapReg, TranVec(~spRfOH))}
10446d5ddbceSLemover
10455854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
10468b33cd30Sklin02      spRefillPerf(i) := i.U === spRefillIdx
10476d5ddbceSLemover    }
10486d5ddbceSLemover  }
10498b33cd30Sklin02  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")
10508b33cd30Sklin02  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")
10516d5ddbceSLemover
10523ea4388cSHaoyuan Feng  val l1eccFlush = resp_res.l1.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l1eccError, init = false.B)
10533ea4388cSHaoyuan Feng  val l0eccFlush = resp_res.l0.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l0eccError, init = false.B)
10546c4dcc2dSLemover  val eccVpn = stageResp.bits.req_info.vpn
10557196f5a2SLemover
10563ea4388cSHaoyuan Feng  XSError(l1eccFlush, "l2tlb.cache.l1 ecc error. Should not happen at sim stage")
10573ea4388cSHaoyuan Feng  XSError(l0eccFlush, "l2tlb.cache.l0 ecc error. Should not happen at sim stage")
10583ea4388cSHaoyuan Feng  when (l1eccFlush) {
10593ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL1SetIdx(eccVpn))
10603ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l1nWays, a.asUInt) }).asUInt
10613ea4388cSHaoyuan Feng    l1v := l1v & ~flushMask
10623ea4388cSHaoyuan Feng    l1g := l1g & ~flushMask
10637196f5a2SLemover  }
10647196f5a2SLemover
10653ea4388cSHaoyuan Feng  when (l0eccFlush) {
10663ea4388cSHaoyuan Feng    val flushSetIdxOH = UIntToOH(genPtwL0SetIdx(eccVpn))
10673ea4388cSHaoyuan Feng    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l0nWays, a.asUInt) }).asUInt
10683ea4388cSHaoyuan Feng    l0v := l0v & ~flushMask
10693ea4388cSHaoyuan Feng    l0g := l0g & ~flushMask
10707196f5a2SLemover  }
10717196f5a2SLemover
1072f9395f72SHaoyuan Feng  // sfence logic
1073f9395f72SHaoyuan Feng  val l0hashAsid = XORFold(sfence_dup(0).bits.id, l2tlbParams.hashAsidWidth)
1074f9395f72SHaoyuan Feng  val l1hashAsid = XORFold(sfence_dup(1).bits.id, l2tlbParams.hashAsidWidth)
1075f9395f72SHaoyuan Feng  val l0asidhit = VecInit(l0asids.flatMap(_.map(_ === l0hashAsid))).asUInt
1076f9395f72SHaoyuan Feng  val l1asidhit = VecInit(l1asids.flatMap(_.map(_ === l1hashAsid))).asUInt
10773ea4388cSHaoyuan Feng  val l2asidhit = VecInit(l2asids.map(_ === sfence_dup(2).bits.id)).asUInt
1078d0de7e4aSpeixiaokun  val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt
1079f9395f72SHaoyuan Feng
1080d0de7e4aSpeixiaokun  val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv
1081d0de7e4aSpeixiaokun  when (sfence_valid) {
1082f9395f72SHaoyuan Feng    val l0hashVmid = XORFold(io.csr_dup(0).hgatp.vmid, l2tlbParams.hashAsidWidth)
1083f9395f72SHaoyuan Feng    val l1hashVmid = XORFold(io.csr_dup(1).hgatp.vmid, l2tlbParams.hashAsidWidth)
1084f9395f72SHaoyuan Feng    val l0vmidhit = VecInit(l0vmids.flatMap(_.map(_ === l0hashVmid))).asUInt
1085f9395f72SHaoyuan Feng    val l1vmidhit = VecInit(l1vmids.flatMap(_.map(_ === l1hashVmid))).asUInt
108697929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
108797929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
1088f9395f72SHaoyuan Feng
1089f9395f72SHaoyuan 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
1090f9395f72SHaoyuan 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
10913ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map{a => io.csr_dup(2).priv.virt && a === onlyStage1 || !io.csr_dup(2).priv.virt && a === noS2xlate}).asUInt
1092e5da58f0Speixiaokun    val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt
1093f9395f72SHaoyuan Feng    val l0virthit = l0hhit & VecInit(l0vmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt
1094f9395f72SHaoyuan Feng    val l1virthit = l1hhit & VecInit(l1vmidhit.asBools.map{a => io.csr_dup(1).priv.virt && a || !io.csr_dup(1).priv.virt}).asUInt
1095f9395f72SHaoyuan Feng    val l2virthit = l2hhit & VecInit(l2vmidhit.asBools.map{a => io.csr_dup(2).priv.virt && a || !io.csr_dup(2).priv.virt}).asUInt
1096f9395f72SHaoyuan Feng    val spvirthit = sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt
1097f9395f72SHaoyuan Feng
10987797f035SbugGenerator    val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
1099f9395f72SHaoyuan Feng    val l0hashVpn = XORFold(sfence_vpn(vpnLen - 1, vpnLen - PtwL0TagLen), l2tlbParams.hashVpnWidth)
1100f9395f72SHaoyuan Feng    val l0vpnhit = VecInit(l0vpns.flatMap(_.map(_ === l0hashVpn))).asUInt
1101f9395f72SHaoyuan Feng    val l0flushSetIdx = UIntToOH(genPtwL0SetIdx(sfence_vpn))
1102f9395f72SHaoyuan Feng    val l0flushMask = VecInit(l0flushSetIdx.asBools.map{a => Fill(l2tlbParams.l0nWays, a.asUInt)}).asUInt
11037797f035SbugGenerator
11047797f035SbugGenerator    when (sfence_dup(0).bits.rs1/*va*/) {
11057797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11066d5ddbceSLemover        // all va && all asid
1107f9395f72SHaoyuan Feng        l0v := l0v & ~l0virthit
1108f9395f72SHaoyuan Feng        l1v := l1v & ~l1virthit
1109f9395f72SHaoyuan Feng        l2v := l2v & ~l2virthit
1110f9395f72SHaoyuan Feng        spv := spv & ~spvirthit
11116d5ddbceSLemover      } .otherwise {
11126d5ddbceSLemover        // all va && specific asid except global
1113f9395f72SHaoyuan Feng        l0v := l0v & ~(l0virthit & ~l0g & l0asidhit)
1114f9395f72SHaoyuan Feng        l1v := l1v & ~(l1virthit & ~l1g & l1asidhit)
1115f9395f72SHaoyuan Feng        l2v := l2v & ~(l2virthit & ~l2g & l2asidhit)
1116f9395f72SHaoyuan Feng        spv := spv & ~(spvirthit & ~spg & spasidhit)
11176d5ddbceSLemover      }
11186d5ddbceSLemover    } .otherwise {
11197797f035SbugGenerator      when (sfence_dup(0).bits.rs2) {
11206d5ddbceSLemover        // specific leaf of addr && all asid
1121f9395f72SHaoyuan Feng        l0v := l0v & ~(l0virthit & l0vpnhit & l0flushMask)
112297929664SXiaokun-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)
11236d5ddbceSLemover      } .otherwise {
11246d5ddbceSLemover        // specific leaf of addr && specific asid
1125f9395f72SHaoyuan Feng        l0v := l0v & ~(l0virthit & ~l0g & l0asidhit & l0vpnhit & l0flushMask)
112697929664SXiaokun-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)
1127d0de7e4aSpeixiaokun      }
1128d0de7e4aSpeixiaokun    }
1129d0de7e4aSpeixiaokun  }
1130d0de7e4aSpeixiaokun
1131d0de7e4aSpeixiaokun  val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv
1132d0de7e4aSpeixiaokun  when (hfencev_valid) {
1133f9395f72SHaoyuan Feng    val l0hashVmid = XORFold(io.csr_dup(0).hgatp.vmid, l2tlbParams.hashAsidWidth)
1134f9395f72SHaoyuan Feng    val l1hashVmid = XORFold(io.csr_dup(1).hgatp.vmid, l2tlbParams.hashAsidWidth)
1135f9395f72SHaoyuan Feng    val l0vmidhit = VecInit(l0vmids.flatMap(_.map(_ === l0hashVmid))).asUInt
1136f9395f72SHaoyuan Feng    val l1vmidhit = VecInit(l1vmids.flatMap(_.map(_ === l1hashVmid))).asUInt
113797929664SXiaokun-Pei    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
113897929664SXiaokun-Pei    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.vmid)).asUInt
1139f9395f72SHaoyuan Feng
1140f9395f72SHaoyuan Feng    val l0hhit = VecInit(l0h.flatMap(_.map(_ === onlyStage1))).asUInt
1141f9395f72SHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage1))).asUInt
11423ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage1)).asUInt
1143cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt
1144f9395f72SHaoyuan Feng
1145d0de7e4aSpeixiaokun    val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen)
1146f9395f72SHaoyuan Feng    val l0hashVpn = XORFold(hfencev_vpn(vpnLen - 1, vpnLen - PtwL0TagLen), l2tlbParams.hashVpnWidth)
1147f9395f72SHaoyuan Feng    val l0vpnhit = VecInit(l0vpns.flatMap(_.map(_ === l0hashVpn))).asUInt
1148f9395f72SHaoyuan Feng    val l0flushSetIdx = UIntToOH(genPtwL0SetIdx(hfencev_vpn))
1149f9395f72SHaoyuan Feng    val l0flushMask = VecInit(l0flushSetIdx.asBools.map{a => Fill(l2tlbParams.l0nWays, a.asUInt)}).asUInt
1150f9395f72SHaoyuan Feng
1151d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1152d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1153f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit)
1154f9395f72SHaoyuan Feng        l1v := l1v & ~(l1hhit & l1vmidhit)
11553ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1156447c794eSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1157d0de7e4aSpeixiaokun      }.otherwise {
1158f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit & ~l0g & l0asidhit)
1159f9395f72SHaoyuan Feng        l1v := l1v & ~(l1hhit & l1vmidhit & ~l1g & l1asidhit)
1160f9395f72SHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit & ~l2g & l2asidhit)
1161f9395f72SHaoyuan Feng        spv := spv & ~(sphhit & spvmidhit & ~spg & spasidhit)
1162d0de7e4aSpeixiaokun      }
1163d0de7e4aSpeixiaokun    }.otherwise {
1164d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1165f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit & l0vpnhit & l0flushMask)
116697929664SXiaokun-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)
1167d0de7e4aSpeixiaokun      }.otherwise {
1168f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit & ~l0g & l0asidhit & l0vpnhit & l0flushMask)
116997929664SXiaokun-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)
1170d0de7e4aSpeixiaokun      }
1171d0de7e4aSpeixiaokun    }
1172d0de7e4aSpeixiaokun  }
1173d0de7e4aSpeixiaokun
1174d0de7e4aSpeixiaokun
1175d0de7e4aSpeixiaokun  val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg
1176d0de7e4aSpeixiaokun  when(hfenceg_valid) {
1177f9395f72SHaoyuan Feng    val l0hashVmid = XORFold(sfence_dup(0).bits.id, l2tlbParams.hashAsidWidth)
1178f9395f72SHaoyuan Feng    val l1hashVmid = XORFold(sfence_dup(1).bits.id, l2tlbParams.hashAsidWidth)
1179f9395f72SHaoyuan Feng    val l0vmidhit = VecInit(l0vmids.flatMap(_.map(_ === l0hashVmid))).asUInt
1180f9395f72SHaoyuan Feng    val l1vmidhit = VecInit(l1vmids.flatMap(_.map(_ === l1hashVmid))).asUInt
11813ea4388cSHaoyuan Feng    val l2vmidhit = VecInit(l2vmids.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
1182cca17e78Speixiaokun    val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt
1183f9395f72SHaoyuan Feng
1184f9395f72SHaoyuan Feng    val l0hhit = VecInit(l0h.flatMap(_.map(_ === onlyStage2))).asUInt
1185f9395f72SHaoyuan Feng    val l1hhit = VecInit(l1h.flatMap(_.map(_ === onlyStage2))).asUInt
11863ea4388cSHaoyuan Feng    val l2hhit = VecInit(l2h.map(_ === onlyStage2)).asUInt
1187cca17e78Speixiaokun    val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt
1188f9395f72SHaoyuan Feng
1189887df0f4Speixiaokun    val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen)
1190f9395f72SHaoyuan Feng    val l0hashVpn = XORFold(hfenceg_gvpn(vpnLen - 1, vpnLen - PtwL0TagLen), l2tlbParams.hashVpnWidth)
1191f9395f72SHaoyuan Feng    val l0vpnhit = VecInit(l0vpns.flatMap(_.map(_ === l0hashVpn))).asUInt
1192f9395f72SHaoyuan Feng    val l0flushSetIdx = UIntToOH(genPtwL0SetIdx(hfenceg_gvpn))
1193f9395f72SHaoyuan Feng    val l0flushMask = VecInit(l0flushSetIdx.asBools.map{a => Fill(l2tlbParams.l0nWays, a.asUInt)}).asUInt
1194f9395f72SHaoyuan Feng
1195d0de7e4aSpeixiaokun    when(sfence_dup(0).bits.rs1) {
1196d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1197f9395f72SHaoyuan Feng        l0v := l0v & ~l0hhit
1198d0de7e4aSpeixiaokun        l1v := l1v & ~l1hhit
11993ea4388cSHaoyuan Feng        l2v := l2v & ~l2hhit
1200d0de7e4aSpeixiaokun        spv := spv & ~sphhit
1201d0de7e4aSpeixiaokun      }.otherwise {
1202f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit)
1203f9395f72SHaoyuan Feng        l1v := l1v & ~(l1hhit & l1vmidhit)
12043ea4388cSHaoyuan Feng        l2v := l2v & ~(l2hhit & l2vmidhit)
1205d0de7e4aSpeixiaokun        spv := spv & ~(sphhit & spvmidhit)
1206d0de7e4aSpeixiaokun      }
1207d0de7e4aSpeixiaokun    }.otherwise {
1208d0de7e4aSpeixiaokun      when(sfence_dup(0).bits.rs2) {
1209f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vpnhit & l0flushMask)
12108fe4f15fSXiaokun-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)
1211d0de7e4aSpeixiaokun      }.otherwise {
1212f9395f72SHaoyuan Feng        l0v := l0v & ~(l0hhit & l0vmidhit & l0vpnhit & l0flushMask)
12138fe4f15fSXiaokun-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)
12146d5ddbceSLemover      }
12156d5ddbceSLemover    }
12166d5ddbceSLemover  }
12176d5ddbceSLemover
12183ea4388cSHaoyuan Feng  if (EnableSv48) {
12193ea4388cSHaoyuan Feng    val l3asidhit = VecInit(l3asids.get.map(_ === sfence_dup(2).bits.id)).asUInt
122097929664SXiaokun-Pei    val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
12213ea4388cSHaoyuan 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
12223ea4388cSHaoyuan Feng
12233ea4388cSHaoyuan Feng    when (sfence_valid) {
122497929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
12253ea4388cSHaoyuan 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
12263ea4388cSHaoyuan Feng      val sfence_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
12273ea4388cSHaoyuan Feng
12283ea4388cSHaoyuan Feng      when (sfence_dup(2).bits.rs1/*va*/) {
12293ea4388cSHaoyuan Feng        when (sfence_dup(2).bits.rs2) {
12303ea4388cSHaoyuan Feng          // all va && all asid
12313ea4388cSHaoyuan 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))
12323ea4388cSHaoyuan Feng        } .otherwise {
12333ea4388cSHaoyuan Feng          // all va && specific asid except global
12343ea4388cSHaoyuan 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))
12353ea4388cSHaoyuan Feng        }
12363ea4388cSHaoyuan Feng      }
12373ea4388cSHaoyuan Feng    }
12383ea4388cSHaoyuan Feng
12393ea4388cSHaoyuan Feng    when (hfencev_valid) {
124097929664SXiaokun-Pei      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === io.csr_dup(2).hgatp.vmid)).asUInt
12413ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage1)).asUInt
12423ea4388cSHaoyuan Feng      val hfencev_vpn = sfence_dup(2).bits.addr(sfence_dup(2).bits.addr.getWidth-1, offLen)
12433ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12443ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12453ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12463ea4388cSHaoyuan Feng        }.otherwise {
12473ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(~l3g.get & l3hhit & l3asidhit & l3vmidhit))
12483ea4388cSHaoyuan Feng        }
12493ea4388cSHaoyuan Feng      }
12503ea4388cSHaoyuan Feng    }
12513ea4388cSHaoyuan Feng
12523ea4388cSHaoyuan Feng    when (hfenceg_valid) {
12533ea4388cSHaoyuan Feng      val l3vmidhit = VecInit(l3vmids.get.map(_.getOrElse(0.U) === sfence_dup(2).bits.id)).asUInt
12543ea4388cSHaoyuan Feng      val l3hhit = VecInit(l3h.get.map(_ === onlyStage2)).asUInt
12553ea4388cSHaoyuan Feng      val hfenceg_gvpn = (sfence_dup(2).bits.addr << 2)(sfence_dup(2).bits.addr.getWidth - 1, offLen)
12563ea4388cSHaoyuan Feng      when(sfence_dup(2).bits.rs1) {
12573ea4388cSHaoyuan Feng        when(sfence_dup(2).bits.rs2) {
12583ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~l3hhit)
12593ea4388cSHaoyuan Feng        }.otherwise {
12603ea4388cSHaoyuan Feng          l3v.map(_ := l3v.get & ~(l3hhit & l3vmidhit))
12613ea4388cSHaoyuan Feng        }
12623ea4388cSHaoyuan Feng      }
12633ea4388cSHaoyuan Feng    }
12643ea4388cSHaoyuan Feng  }
12653ea4388cSHaoyuan Feng
12667797f035SbugGenerator  def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = {
12672c86e165SZhangZifei    in.ready := !in.valid || out.ready
12682c86e165SZhangZifei    out.valid := in.valid
12692c86e165SZhangZifei    out.bits := in.bits
12701f4a7c0cSLemover    out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) =>
12717797f035SbugGenerator      val bypassed_reg = Reg(Bool())
1272d0de7e4aSpeixiaokun      val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid
12737797f035SbugGenerator      when (inFire) { bypassed_reg := bypassed_wire }
12747797f035SbugGenerator      .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire }
12757797f035SbugGenerator
12767797f035SbugGenerator      b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire))
12771f4a7c0cSLemover    }
12782c86e165SZhangZifei  }
12792c86e165SZhangZifei
12806d5ddbceSLemover  // Perf Count
12813ea4388cSHaoyuan Feng  val resp_l0 = resp_res.l0.hit
12826c4dcc2dSLemover  val resp_sp = resp_res.sp.hit
12833ea4388cSHaoyuan Feng  val resp_l3_pre = if (EnableSv48) Some(resp_res.l3.get.pre) else None
12846c4dcc2dSLemover  val resp_l2_pre = resp_res.l2.pre
12853ea4388cSHaoyuan Feng  val resp_l1_pre = resp_res.l1.pre
12863ea4388cSHaoyuan Feng  val resp_l0_pre = resp_res.l0.pre
12876c4dcc2dSLemover  val resp_sp_pre = resp_res.sp.pre
1288935edac4STang Haojin  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1289bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
12903ea4388cSHaoyuan Feng  if (EnableSv48) {
12913ea4388cSHaoyuan 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)
12923ea4388cSHaoyuan Feng  }
12933ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12943ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
12953ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit", base_valid_access_0 && resp_l0)
1296bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
1297bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
1298bc063562SLemover
12993ea4388cSHaoyuan Feng  if (EnableSv48) {
13003ea4388cSHaoyuan 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)
13013ea4388cSHaoyuan Feng  }
13023ea4388cSHaoyuan 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)
13033ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13043ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre", base_valid_access_0 && resp_l0_pre && resp_l0)
1305bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
13063ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1307bc063562SLemover
1308935edac4STang Haojin  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire
1309bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
13103ea4388cSHaoyuan Feng  if (EnableSv48) {
13113ea4388cSHaoyuan 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)
13123ea4388cSHaoyuan Feng  }
13133ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13143ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13153ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit", base_valid_access_1 && resp_l0)
1316bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
1317bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
1318bc063562SLemover
13193ea4388cSHaoyuan Feng  if (EnableSv48) {
13203ea4388cSHaoyuan 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)
13213ea4388cSHaoyuan Feng  }
13223ea4388cSHaoyuan 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)
13233ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13243ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre", base_valid_access_1 && resp_l0_pre && resp_l0)
1325bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
13263ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1327bc063562SLemover
1328935edac4STang Haojin  val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire
1329bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
13303ea4388cSHaoyuan Feng  if (EnableSv48) {
13313ea4388cSHaoyuan 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)
13323ea4388cSHaoyuan Feng  }
13333ea4388cSHaoyuan Feng  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13343ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13353ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_first", base_valid_access_2 && resp_l0)
1336bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
1337bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
1338bc063562SLemover
13393ea4388cSHaoyuan Feng  if (EnableSv48) {
13403ea4388cSHaoyuan 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)
13413ea4388cSHaoyuan Feng  }
13423ea4388cSHaoyuan 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)
13433ea4388cSHaoyuan Feng  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13443ea4388cSHaoyuan Feng  XSPerfAccumulate("l0_hit_pre_first", base_valid_access_2 && resp_l0_pre && resp_l0)
1345bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
13463ea4388cSHaoyuan Feng  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l0_pre && resp_l0 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
1347bc063562SLemover
1348935edac4STang Haojin  val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire
1349bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
13503ea4388cSHaoyuan Feng  if (EnableSv48) {
13513ea4388cSHaoyuan 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)
13523ea4388cSHaoyuan Feng  }
13533ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13543ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13553ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_first", base_valid_access_3 && resp_l0)
1356bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
1357bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
1358bc063562SLemover
13593ea4388cSHaoyuan Feng  if (EnableSv48) {
13603ea4388cSHaoyuan 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)
13613ea4388cSHaoyuan Feng  }
13623ea4388cSHaoyuan 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)
13633ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.hit)
13643ea4388cSHaoyuan Feng  XSPerfAccumulate("pre_l0_hit_pre_first", base_valid_access_3 && resp_l0_pre && resp_l0)
1365bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
13663ea4388cSHaoyuan 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)
1367bc063562SLemover
13686d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
13696d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
13703ea4388cSHaoyuan Feng  if (EnableSv48) {
13713ea4388cSHaoyuan Feng    l3AccessPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3AccessIndex${i}", l) }
13723ea4388cSHaoyuan Feng  }
13733ea4388cSHaoyuan Feng  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2AccessIndex${i}", l) }
13743ea4388cSHaoyuan Feng  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1AccessIndex${i}", l) }
13753ea4388cSHaoyuan Feng  l0AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0AccessIndex${i}", l) }
13766d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
13773ea4388cSHaoyuan Feng  if (EnableSv48) {
13783ea4388cSHaoyuan Feng    l3RefillPerf.get.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l3RefillIndex${i}", l) }
13793ea4388cSHaoyuan Feng  }
13803ea4388cSHaoyuan Feng  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l2RefillIndex${i}", l) }
13813ea4388cSHaoyuan Feng  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l1RefillIndex${i}", l) }
13823ea4388cSHaoyuan Feng  l0RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"l0RefillIndex${i}", l) }
13836d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
13846d5ddbceSLemover
13853ea4388cSHaoyuan Feng  if (EnableSv48) {
13863ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill", Cat(l3RefillPerf.get).orR)
13873ea4388cSHaoyuan Feng  }
1388bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
13893ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
13903ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill", Cat(l0RefillPerf).orR)
1391bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
13923ea4388cSHaoyuan Feng  if (EnableSv48) {
13933ea4388cSHaoyuan Feng    XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf.get).orR && refill_prefetch_dup(0))
13943ea4388cSHaoyuan Feng  }
13957797f035SbugGenerator  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0))
13963ea4388cSHaoyuan Feng  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0))
13973ea4388cSHaoyuan Feng  XSPerfAccumulate("l0Refill_pre", Cat(l0RefillPerf).orR && refill_prefetch_dup(0))
13987797f035SbugGenerator  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0))
1399bc063562SLemover
14006d5ddbceSLemover  // debug
14017797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n")
14023ea4388cSHaoyuan Feng  if (EnableSv48) {
14033ea4388cSHaoyuan Feng    XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v.get)}\n")
14043ea4388cSHaoyuan Feng  }
14057797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n")
14063ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n")
14073ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0v:${Binary(l0v)}\n")
14083ea4388cSHaoyuan Feng  XSDebug(sfence_dup(0).valid, p"[sfence] l0g:${Binary(l0g)}\n")
14097797f035SbugGenerator  XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n")
14107797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n")
14113ea4388cSHaoyuan Feng  if (EnableSv48) {
14123ea4388cSHaoyuan Feng    XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v.get)}\n")
14133ea4388cSHaoyuan Feng  }
14147797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n")
14153ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n")
14163ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0v:${Binary(l0v)}\n")
14173ea4388cSHaoyuan Feng  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l0g:${Binary(l0g)}\n")
14187797f035SbugGenerator  XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n")
1419cd365d4cSrvcoresjw
1420cd365d4cSrvcoresjw  val perfEvents = Seq(
142156be8e20SYinan Xu    ("access           ", base_valid_access_0             ),
1422cd365d4cSrvcoresjw    ("l2_hit           ", l2Hit                           ),
14233ea4388cSHaoyuan Feng    ("l1_hit           ", l1Hit                           ),
14243ea4388cSHaoyuan Feng    ("l0_hit           ", l0Hit                           ),
1425cd365d4cSrvcoresjw    ("sp_hit           ", spHit                           ),
14263ea4388cSHaoyuan Feng    ("pte_hit          ", l0Hit || spHit                  ),
1427cd365d4cSrvcoresjw    ("rwHarzad         ", io.req.valid && !io.req.ready   ),
1428cd365d4cSrvcoresjw    ("out_blocked      ", io.resp.valid && !io.resp.ready ),
1429cd365d4cSrvcoresjw  )
14301ca0e4f3SYinan Xu  generatePerfEvent()
14316d5ddbceSLemover}
1432