xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableCache.scala (revision 45f497a4abde3fa5930268b418d634554b21b0b8)
16d5ddbceSLemover/***************************************************************************************
26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
46d5ddbceSLemover*
56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
86d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
96d5ddbceSLemover*
106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
136d5ddbceSLemover*
146d5ddbceSLemover* See the Mulan PSL v2 for more details.
156d5ddbceSLemover***************************************************************************************/
166d5ddbceSLemover
176d5ddbceSLemoverpackage xiangshan.cache.mmu
186d5ddbceSLemover
196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
22b848eea5SLemoverimport chisel3.internal.naming.chiselName
236d5ddbceSLemoverimport xiangshan._
246d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
256d5ddbceSLemoverimport utils._
266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
276d5ddbceSLemoverimport freechips.rocketchip.tilelink._
286d5ddbceSLemover
296d5ddbceSLemover/* ptw cache caches the page table of all the three layers
306d5ddbceSLemover * ptw cache resp at next cycle
316d5ddbceSLemover * the cache should not be blocked
326d5ddbceSLemover * when miss queue if full, just block req outside
336d5ddbceSLemover */
346d5ddbceSLemoverclass PtwCacheIO()(implicit p: Parameters) extends PtwBundle {
35*45f497a4Shappy-lx  val req = Flipped(DecoupledIO(new L2TlbInnerBundle()))
36bc063562SLemover  val req_isFirst = Input(Bool()) // only for perf counter
376d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
38*45f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
396d5ddbceSLemover    val hit = Bool()
40bc063562SLemover    val prefetch = Bool() // is the entry fetched by prefetch
416d5ddbceSLemover    val toFsm = new Bundle {
426d5ddbceSLemover      val l1Hit = Bool()
436d5ddbceSLemover      val l2Hit = Bool()
446d5ddbceSLemover      val ppn = UInt(ppnLen.W)
456d5ddbceSLemover    }
466d5ddbceSLemover    val toTlb = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
476d5ddbceSLemover  })
486d5ddbceSLemover  val refill = Flipped(ValidIO(new Bundle {
495854c1edSLemover    val ptes = UInt(blockBits.W)
50*45f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
516d5ddbceSLemover    val level = UInt(log2Up(Level).W)
52b848eea5SLemover    val addr_low = UInt((log2Up(l2tlbParams.blockBytes) - log2Up(XLEN/8)).W)
536d5ddbceSLemover  }))
546d5ddbceSLemover  val sfence = Input(new SfenceBundle)
55*45f497a4Shappy-lx  val csr = Input(new TlbCsrBundle)
566d5ddbceSLemover}
576d5ddbceSLemover
58b848eea5SLemover@chiselName
596d5ddbceSLemoverclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst {
606d5ddbceSLemover  val io = IO(new PtwCacheIO)
616d5ddbceSLemover
627196f5a2SLemover  val ecc = Code.fromString(l2tlbParams.ecc)
637196f5a2SLemover  val l2EntryType = new PTWEntriesWithEcc(ecc, num = PtwL2SectorSize, tagLen = PtwL2TagLen, level = 1, hasPerm = false)
647196f5a2SLemover  val l3EntryType = new PTWEntriesWithEcc(ecc, num = PtwL3SectorSize, tagLen = PtwL3TagLen, level = 2, hasPerm = true)
657196f5a2SLemover
666d5ddbceSLemover  // TODO: four caches make the codes dirty, think about how to deal with it
676d5ddbceSLemover
686d5ddbceSLemover  val sfence = io.sfence
696d5ddbceSLemover  val refill = io.refill.bits
70*45f497a4Shappy-lx  val refill_prefetch = from_pre(io.refill.bits.req_info.source)
716d5ddbceSLemover
726d5ddbceSLemover  val first_valid = io.req.valid
736d5ddbceSLemover  val first_fire = first_valid && io.req.ready
746d5ddbceSLemover  val first_req = io.req.bits
756d5ddbceSLemover  val second_ready = Wire(Bool())
76bc063562SLemover  val second_valid = ValidHold(first_fire && !sfence.valid, io.resp.fire(), sfence.valid)
776d5ddbceSLemover  val second_req = RegEnable(first_req, first_fire)
786d5ddbceSLemover  // NOTE: if ptw cache resp may be blocked, hard to handle refill
796d5ddbceSLemover  // when miss queue is full, please to block itlb and dtlb input
80bc063562SLemover  val second_isFirst = RegEnable(io.req_isFirst, first_fire) // only for perf counter
816d5ddbceSLemover
826d5ddbceSLemover  // when refill, refuce to accept new req
835854c1edSLemover  val rwHarzad = if (sramSinglePort) io.refill.valid else false.B
84bc063562SLemover  io.req.ready := !rwHarzad && second_ready
856d5ddbceSLemover  // NOTE: when write, don't ready, whe
866d5ddbceSLemover  //       when replay, just come in, out make sure resp.fire()
876d5ddbceSLemover
886d5ddbceSLemover  // l1: level 0 non-leaf pte
895854c1edSLemover  val l1 = Reg(Vec(l2tlbParams.l1Size, new PtwEntry(tagLen = PtwL1TagLen)))
905854c1edSLemover  val l1v = RegInit(0.U(l2tlbParams.l1Size.W))
915854c1edSLemover  val l1g = Reg(UInt(l2tlbParams.l1Size.W))
92*45f497a4Shappy-lx  val l1asids = Reg(Vec(l2tlbParams.l1Size, UInt(AsidLength.W)))
936d5ddbceSLemover
946d5ddbceSLemover  // l2: level 1 non-leaf pte
956d5ddbceSLemover  val l2 = Module(new SRAMTemplate(
967196f5a2SLemover    l2EntryType,
975854c1edSLemover    set = l2tlbParams.l2nSets,
985854c1edSLemover    way = l2tlbParams.l2nWays,
995854c1edSLemover    singlePort = sramSinglePort
1006d5ddbceSLemover  ))
1015854c1edSLemover  val l2v = RegInit(0.U((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
1025854c1edSLemover  val l2g = Reg(UInt((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W))
103*45f497a4Shappy-lx  val l2asids = Reg(Vec(l2tlbParams.l2nSets, Vec(l2tlbParams.l2nWays, UInt(AsidLength.W))))
1046d5ddbceSLemover  def getl2vSet(vpn: UInt) = {
1055854c1edSLemover    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
1066d5ddbceSLemover    val set = genPtwL2SetIdx(vpn)
1075854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
1085854c1edSLemover    val l2vVec = l2v.asTypeOf(Vec(l2tlbParams.l2nSets, UInt(l2tlbParams.l2nWays.W)))
1096d5ddbceSLemover    l2vVec(set)
1106d5ddbceSLemover  }
111*45f497a4Shappy-lx  def getl2asidSet(vpn: UInt) = {
112*45f497a4Shappy-lx    require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays))
113*45f497a4Shappy-lx    val set = genPtwL2SetIdx(vpn)
114*45f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l2nSets))
115*45f497a4Shappy-lx    l2asids(set)
116*45f497a4Shappy-lx  }
1176d5ddbceSLemover
1186d5ddbceSLemover  // l3: level 2 leaf pte of 4KB pages
1196d5ddbceSLemover  val l3 = Module(new SRAMTemplate(
1207196f5a2SLemover    l3EntryType,
1215854c1edSLemover    set = l2tlbParams.l3nSets,
1225854c1edSLemover    way = l2tlbParams.l3nWays,
1235854c1edSLemover    singlePort = sramSinglePort
1246d5ddbceSLemover  ))
1255854c1edSLemover  val l3v = RegInit(0.U((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
1265854c1edSLemover  val l3g = Reg(UInt((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W))
127*45f497a4Shappy-lx  val l3asids = Reg(Vec(l2tlbParams.l3nSets, Vec(l2tlbParams.l3nWays, UInt(AsidLength.W))))
1286d5ddbceSLemover  def getl3vSet(vpn: UInt) = {
1295854c1edSLemover    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
1306d5ddbceSLemover    val set = genPtwL3SetIdx(vpn)
1315854c1edSLemover    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
1325854c1edSLemover    val l3vVec = l3v.asTypeOf(Vec(l2tlbParams.l3nSets, UInt(l2tlbParams.l3nWays.W)))
1336d5ddbceSLemover    l3vVec(set)
1346d5ddbceSLemover  }
135*45f497a4Shappy-lx  def getl3asidSet(vpn: UInt) = {
136*45f497a4Shappy-lx    require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays))
137*45f497a4Shappy-lx    val set = genPtwL3SetIdx(vpn)
138*45f497a4Shappy-lx    require(set.getWidth == log2Up(l2tlbParams.l3nSets))
139*45f497a4Shappy-lx    l3asids(set)
140*45f497a4Shappy-lx  }
1416d5ddbceSLemover
1426d5ddbceSLemover  // sp: level 0/1 leaf pte of 1GB/2MB super pages
1435854c1edSLemover  val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true)))
1445854c1edSLemover  val spv = RegInit(0.U(l2tlbParams.spSize.W))
1455854c1edSLemover  val spg = Reg(UInt(l2tlbParams.spSize.W))
146*45f497a4Shappy-lx  val spasids = Reg(Vec(l2tlbParams.spSize, UInt(AsidLength.W)))
1476d5ddbceSLemover
1486d5ddbceSLemover  // Access Perf
1495854c1edSLemover  val l1AccessPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
1505854c1edSLemover  val l2AccessPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
1515854c1edSLemover  val l3AccessPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
1525854c1edSLemover  val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
1536d5ddbceSLemover  l1AccessPerf.map(_ := false.B)
1546d5ddbceSLemover  l2AccessPerf.map(_ := false.B)
1556d5ddbceSLemover  l3AccessPerf.map(_ := false.B)
1566d5ddbceSLemover  spAccessPerf.map(_ := false.B)
1576d5ddbceSLemover
1581af89150SLemover  val cache_read_valid = OneCycleValid(first_fire, sfence.valid)
1596d5ddbceSLemover  // l1
1605854c1edSLemover  val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer, l2tlbParams.l1Size)
161bc063562SLemover  val (l1Hit, l1HitPPN, l1Pre) = {
162*45f497a4Shappy-lx    val hitVecT = l1.zipWithIndex.map { case (e, i) => e.hit(first_req.vpn, io.csr.satp.asid) && l1v(i) }
1636d5ddbceSLemover    val hitVec = hitVecT.map(RegEnable(_, first_fire))
1646d5ddbceSLemover    val hitPPN = ParallelPriorityMux(hitVec zip l1.map(_.ppn))
165bc063562SLemover    val hitPre = ParallelPriorityMux(hitVec zip l1.map(_.prefetch))
1661af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid
1676d5ddbceSLemover
1686d5ddbceSLemover    when (hit) { ptwl1replace.access(OHToUInt(hitVec)) }
1696d5ddbceSLemover
1706d5ddbceSLemover    l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(first_fire)}
1715854c1edSLemover    for (i <- 0 until l2tlbParams.l1Size) {
172*45f497a4Shappy-lx      XSDebug(first_fire, p"[l1] l1(${i.U}) ${l1(i)} hit:${l1(i).hit(first_req.vpn, io.csr.satp.asid)}\n")
1736d5ddbceSLemover    }
1746d5ddbceSLemover    XSDebug(first_fire, p"[l1] l1v:${Binary(l1v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n")
1756d5ddbceSLemover    XSDebug(second_valid, p"[l1] l1Hit:${hit} l1HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n")
1766d5ddbceSLemover
1776d5ddbceSLemover    VecInit(hitVecT).suggestName(s"l1_hitVecT")
1786d5ddbceSLemover    VecInit(hitVec).suggestName(s"l1_hitVec")
1796d5ddbceSLemover
180bc063562SLemover    (hit, hitPPN, hitPre)
1816d5ddbceSLemover  }
1826d5ddbceSLemover
1836d5ddbceSLemover  // l2
1845854c1edSLemover  val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer,l2tlbParams.l2nWays,l2tlbParams.l2nSets)
185bc063562SLemover  val (l2Hit, l2HitPPN, l2Pre, l2eccError) = {
1866d5ddbceSLemover    val ridx = genPtwL2SetIdx(first_req.vpn)
1876d5ddbceSLemover    val vidx = RegEnable(VecInit(getl2vSet(first_req.vpn).asBools), first_fire)
188*45f497a4Shappy-lx    val asids_idx = RegEnable(getl2asidSet(first_req.vpn), first_fire)
1896d5ddbceSLemover    l2.io.r.req.valid := first_fire
1906d5ddbceSLemover    l2.io.r.req.bits.apply(setIdx = ridx)
1916d5ddbceSLemover    val ramDatas = l2.io.r.resp.data
1926d5ddbceSLemover    // val hitVec = VecInit(ramDatas.map{wayData => wayData.hit(first_req.vpn) })
193*45f497a4Shappy-lx    val hitVec = VecInit(ramDatas.zip(vidx).map { case (wayData, v) => wayData.entries.hit(second_req.vpn, io.csr.satp.asid) && v })
1947196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
1957196f5a2SLemover    val hitWayData = hitWayEntry.entries
1967196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
1971af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid && RegNext(l2.io.r.req.ready, init = false.B)
1985854c1edSLemover    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l2nWays).map(_.U))
1996d5ddbceSLemover
2007196f5a2SLemover    val eccError = ecc.decode(Cat(hitWayEcc, hitWayData.asUInt())).error
2017196f5a2SLemover
2026d5ddbceSLemover    ridx.suggestName(s"l2_ridx")
2036d5ddbceSLemover    vidx.suggestName(s"l2_vidx")
2046d5ddbceSLemover    ramDatas.suggestName(s"l2_ramDatas")
2056d5ddbceSLemover    hitVec.suggestName(s"l2_hitVec")
2066d5ddbceSLemover    hitWayData.suggestName(s"l2_hitWayData")
2076d5ddbceSLemover    hitWay.suggestName(s"l2_hitWay")
2086d5ddbceSLemover
2096d5ddbceSLemover    when (hit) { ptwl2replace.access(genPtwL2SetIdx(second_req.vpn), hitWay) }
2106d5ddbceSLemover
2116d5ddbceSLemover    l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(first_fire) }
2126d5ddbceSLemover    XSDebug(first_fire, p"[l2] ridx:0x${Hexadecimal(ridx)}\n")
2135854c1edSLemover    for (i <- 0 until l2tlbParams.l2nWays) {
214*45f497a4Shappy-lx      XSDebug(RegNext(first_fire), p"[l2] ramDatas(${i.U}) ${ramDatas(i)}  l2v:${vidx(i)}  hit:${ramDatas(i).entries.hit(second_req.vpn, io.csr.satp.asid)}\n")
2156d5ddbceSLemover    }
2166d5ddbceSLemover    XSDebug(second_valid, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitWayData.ppns(genPtwL2SectorIdx(second_req.vpn)))} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${Binary(vidx.asUInt)}\n")
2176d5ddbceSLemover
218bc063562SLemover    (hit && !eccError, hitWayData.ppns(genPtwL2SectorIdx(second_req.vpn)), hitWayData.prefetch, hit && eccError)
2196d5ddbceSLemover  }
2206d5ddbceSLemover
2216d5ddbceSLemover  // l3
2225854c1edSLemover  val ptwl3replace = ReplacementPolicy.fromString(l2tlbParams.l3Replacer,l2tlbParams.l3nWays,l2tlbParams.l3nSets)
223bc063562SLemover  val (l3Hit, l3HitData, l3Pre, l3eccError) = {
2246d5ddbceSLemover    val ridx = genPtwL3SetIdx(first_req.vpn)
2256d5ddbceSLemover    val vidx = RegEnable(VecInit(getl3vSet(first_req.vpn).asBools), first_fire)
226*45f497a4Shappy-lx    val asids_idx = RegEnable(getl3asidSet(first_req.vpn), first_fire)
2276d5ddbceSLemover    l3.io.r.req.valid := first_fire
2286d5ddbceSLemover    l3.io.r.req.bits.apply(setIdx = ridx)
2296d5ddbceSLemover    val ramDatas = l3.io.r.resp.data
230*45f497a4Shappy-lx    val hitVec = VecInit(ramDatas.zip(vidx).map{ case (wayData, v) => wayData.entries.hit(second_req.vpn, io.csr.satp.asid) && v })
2317196f5a2SLemover    val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas)
2327196f5a2SLemover    val hitWayData = hitWayEntry.entries
2337196f5a2SLemover    val hitWayEcc = hitWayEntry.ecc
2341af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid && RegNext(l3.io.r.req.ready, init = false.B)
2355854c1edSLemover    val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l3nWays).map(_.U))
2366d5ddbceSLemover
2377196f5a2SLemover    val eccError = ecc.decode(Cat(hitWayEcc, hitWayData.asUInt())).error
2387196f5a2SLemover
2396d5ddbceSLemover    when (hit) { ptwl3replace.access(genPtwL3SetIdx(second_req.vpn), hitWay) }
2406d5ddbceSLemover
2416d5ddbceSLemover    l3AccessPerf.zip(hitVec).map{ case (l, h) => l := h && RegNext(first_fire) }
2426d5ddbceSLemover    XSDebug(first_fire, p"[l3] ridx:0x${Hexadecimal(ridx)}\n")
2435854c1edSLemover    for (i <- 0 until l2tlbParams.l3nWays) {
244*45f497a4Shappy-lx      XSDebug(RegNext(first_fire), p"[l3] ramDatas(${i.U}) ${ramDatas(i)}  l3v:${vidx(i)}  hit:${ramDatas(i).entries.hit(second_req.vpn, io.csr.satp.asid)}\n")
2456d5ddbceSLemover    }
2466d5ddbceSLemover    XSDebug(second_valid, p"[l3] l3Hit:${hit} l3HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${Binary(vidx.asUInt)}\n")
2476d5ddbceSLemover
2486d5ddbceSLemover    ridx.suggestName(s"l3_ridx")
2496d5ddbceSLemover    vidx.suggestName(s"l3_vidx")
2506d5ddbceSLemover    ramDatas.suggestName(s"l3_ramDatas")
2516d5ddbceSLemover    hitVec.suggestName(s"l3_hitVec")
2526d5ddbceSLemover    hitWay.suggestName(s"l3_hitWay")
2536d5ddbceSLemover
254bc063562SLemover    (hit && !eccError, hitWayData, hitWayData.prefetch, hit && eccError)
2556d5ddbceSLemover  }
2566d5ddbceSLemover  val l3HitPPN = l3HitData.ppns(genPtwL3SectorIdx(second_req.vpn))
2576d5ddbceSLemover  val l3HitPerm = l3HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL3SectorSize, new PtePermBundle)))(genPtwL3SectorIdx(second_req.vpn))
2586d5ddbceSLemover
2596d5ddbceSLemover  // super page
2605854c1edSLemover  val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize)
261bc063562SLemover  val (spHit, spHitData, spPre) = {
262*45f497a4Shappy-lx    val hitVecT = sp.zipWithIndex.map { case (e, i) => e.hit(first_req.vpn, io.csr.satp.asid) && spv(i) }
2636d5ddbceSLemover    val hitVec = hitVecT.map(RegEnable(_, first_fire))
2646d5ddbceSLemover    val hitData = ParallelPriorityMux(hitVec zip sp)
2651af89150SLemover    val hit = ParallelOR(hitVec) && cache_read_valid
2666d5ddbceSLemover
2676d5ddbceSLemover    when (hit) { spreplace.access(OHToUInt(hitVec)) }
2686d5ddbceSLemover
2696d5ddbceSLemover    spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && RegNext(first_fire) }
2705854c1edSLemover    for (i <- 0 until l2tlbParams.spSize) {
271*45f497a4Shappy-lx      XSDebug(first_fire, p"[sp] sp(${i.U}) ${sp(i)} hit:${sp(i).hit(first_req.vpn, io.csr.satp.asid)} spv:${spv(i)}\n")
2726d5ddbceSLemover    }
2736d5ddbceSLemover    XSDebug(second_valid, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n")
2746d5ddbceSLemover
2756d5ddbceSLemover    VecInit(hitVecT).suggestName(s"sp_hitVecT")
2766d5ddbceSLemover    VecInit(hitVec).suggestName(s"sp_hitVec")
2776d5ddbceSLemover
278bc063562SLemover    (hit, hitData, hitData.prefetch)
2796d5ddbceSLemover  }
2806d5ddbceSLemover  val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle))
2816d5ddbceSLemover  val spHitLevel = spHitData.level.getOrElse(0.U)
2826d5ddbceSLemover
2836d5ddbceSLemover  val resp = Wire(io.resp.bits.cloneType)
2846d5ddbceSLemover  val resp_latch = RegEnable(resp, io.resp.valid && !io.resp.ready)
285bc063562SLemover  val resp_latch_valid = ValidHold(io.resp.valid && !io.resp.ready, io.resp.fire(), sfence.valid)
286bc063562SLemover  second_ready := !second_valid || io.resp.fire()
287*45f497a4Shappy-lx  resp.req_info   := second_req
2886d5ddbceSLemover  resp.hit      := l3Hit || spHit
289bc063562SLemover  resp.prefetch := l3Pre && l3Hit || spPre && spHit
2906d5ddbceSLemover  resp.toFsm.l1Hit := l1Hit
2916d5ddbceSLemover  resp.toFsm.l2Hit := l2Hit
2926d5ddbceSLemover  resp.toFsm.ppn   := Mux(l2Hit, l2HitPPN, l1HitPPN)
2936d5ddbceSLemover  resp.toTlb.tag   := second_req.vpn
294*45f497a4Shappy-lx  resp.toTlb.asid  := io.csr.satp.asid // DontCare
2956d5ddbceSLemover  resp.toTlb.ppn   := Mux(l3Hit, l3HitPPN, spHitData.ppn)
2966d5ddbceSLemover  resp.toTlb.perm.map(_ := Mux(l3Hit, l3HitPerm, spHitPerm))
2976d5ddbceSLemover  resp.toTlb.level.map(_ := Mux(l3Hit, 2.U, spHitLevel))
298bc063562SLemover  resp.toTlb.prefetch := from_pre(second_req.source)
2996d5ddbceSLemover
3006d5ddbceSLemover  io.resp.valid := second_valid
3016d5ddbceSLemover  io.resp.bits := Mux(resp_latch_valid, resp_latch, resp)
3026d5ddbceSLemover  assert(!(l3Hit && spHit), "normal page and super page both hit")
3036d5ddbceSLemover
3046d5ddbceSLemover  // refill Perf
3055854c1edSLemover  val l1RefillPerf = Wire(Vec(l2tlbParams.l1Size, Bool()))
3065854c1edSLemover  val l2RefillPerf = Wire(Vec(l2tlbParams.l2nWays, Bool()))
3075854c1edSLemover  val l3RefillPerf = Wire(Vec(l2tlbParams.l3nWays, Bool()))
3085854c1edSLemover  val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool()))
3096d5ddbceSLemover  l1RefillPerf.map(_ := false.B)
3106d5ddbceSLemover  l2RefillPerf.map(_ := false.B)
3116d5ddbceSLemover  l3RefillPerf.map(_ := false.B)
3126d5ddbceSLemover  spRefillPerf.map(_ := false.B)
3136d5ddbceSLemover
3146d5ddbceSLemover  // refill
3156d5ddbceSLemover  l2.io.w.req <> DontCare
3166d5ddbceSLemover  l3.io.w.req <> DontCare
3176d5ddbceSLemover  l2.io.w.req.valid := false.B
3186d5ddbceSLemover  l3.io.w.req.valid := false.B
3196d5ddbceSLemover
3205854c1edSLemover  def get_part(data: UInt, index: UInt): UInt = {
3215854c1edSLemover    val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W)))
3225854c1edSLemover    inner_data(index)
3235854c1edSLemover  }
3245854c1edSLemover
3256d5ddbceSLemover  val memRdata = refill.ptes
326b848eea5SLemover  val memSelData = get_part(memRdata, refill.addr_low)
3275854c1edSLemover  val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle))
3286d5ddbceSLemover  val memPte = memSelData.asTypeOf(new PteBundle)
3296d5ddbceSLemover
330b848eea5SLemover  memPte.suggestName("memPte")
331b848eea5SLemover
3326d5ddbceSLemover  // TODO: handle sfenceLatch outsize
333b848eea5SLemover  when (io.refill.valid && !memPte.isPf(refill.level) && !sfence.valid ) {
3346d5ddbceSLemover    when (refill.level === 0.U && !memPte.isLeaf()) {
3355854c1edSLemover      // val refillIdx = LFSR64()(log2Up(l2tlbParams.l1Size)-1,0) // TODO: may be LRU
3366d5ddbceSLemover      val refillIdx = replaceWrapper(l1v, ptwl1replace.way)
3376d5ddbceSLemover      refillIdx.suggestName(s"PtwL1RefillIdx")
3386d5ddbceSLemover      val rfOH = UIntToOH(refillIdx)
339*45f497a4Shappy-lx      l1(refillIdx).refill(
340*45f497a4Shappy-lx        refill.req_info.vpn,
341*45f497a4Shappy-lx        io.csr.satp.asid,
342*45f497a4Shappy-lx        memSelData,
343*45f497a4Shappy-lx        0.U,
344*45f497a4Shappy-lx        refill_prefetch
345*45f497a4Shappy-lx      )
3466d5ddbceSLemover      ptwl1replace.access(refillIdx)
3476d5ddbceSLemover      l1v := l1v | rfOH
3486d5ddbceSLemover      l1g := (l1g & ~rfOH) | Mux(memPte.perm.g, rfOH, 0.U)
3496d5ddbceSLemover
3505854c1edSLemover      for (i <- 0 until l2tlbParams.l1Size) {
3516d5ddbceSLemover        l1RefillPerf(i) := i.U === refillIdx
3526d5ddbceSLemover      }
3536d5ddbceSLemover
354*45f497a4Shappy-lx      XSDebug(p"[l1 refill] refillIdx:${refillIdx} refillEntry:${l1(refillIdx).genPtwEntry(refill.req_info.vpn, io.csr.satp.asid, memSelData, 0.U, refill_prefetch)}\n")
3556d5ddbceSLemover      XSDebug(p"[l1 refill] l1v:${Binary(l1v)}->${Binary(l1v | rfOH)} l1g:${Binary(l1g)}->${Binary((l1g & ~rfOH) | Mux(memPte.perm.g, rfOH, 0.U))}\n")
3566d5ddbceSLemover
3576d5ddbceSLemover      refillIdx.suggestName(s"l1_refillIdx")
3586d5ddbceSLemover      rfOH.suggestName(s"l1_rfOH")
3596d5ddbceSLemover    }
3606d5ddbceSLemover
3616d5ddbceSLemover    when (refill.level === 1.U && !memPte.isLeaf()) {
362*45f497a4Shappy-lx      val refillIdx = genPtwL2SetIdx(refill.req_info.vpn)
363*45f497a4Shappy-lx      val victimWay = replaceWrapper(RegEnable(VecInit(getl2vSet(refill.req_info.vpn).asBools).asUInt, first_fire), ptwl2replace.way(refillIdx))
3646d5ddbceSLemover      val victimWayOH = UIntToOH(victimWay)
3656d5ddbceSLemover      val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
3667196f5a2SLemover      val wdata = Wire(l2EntryType)
367*45f497a4Shappy-lx      wdata.entries := wdata.entries.genEntries(
368*45f497a4Shappy-lx        vpn = refill.req_info.vpn,
369*45f497a4Shappy-lx        asid = io.csr.satp.asid,
370*45f497a4Shappy-lx        data = memRdata,
371*45f497a4Shappy-lx        levelUInt = 1.U,
372*45f497a4Shappy-lx        refill_prefetch
373*45f497a4Shappy-lx      )
3747196f5a2SLemover      wdata.ecc := ecc.encode(wdata.entries.asUInt()) >> wdata.entries.getWidth
3756d5ddbceSLemover      l2.io.w.apply(
3766d5ddbceSLemover        valid = true.B,
3776d5ddbceSLemover        setIdx = refillIdx,
3787196f5a2SLemover        data = wdata,
3796d5ddbceSLemover        waymask = victimWayOH
3806d5ddbceSLemover      )
3816d5ddbceSLemover      ptwl2replace.access(refillIdx, victimWay)
3826d5ddbceSLemover      l2v := l2v | rfvOH
3836d5ddbceSLemover      l2g := l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
3846d5ddbceSLemover
3855854c1edSLemover      for (i <- 0 until l2tlbParams.l2nWays) {
3866d5ddbceSLemover        l2RefillPerf(i) := i.U === victimWay
3876d5ddbceSLemover      }
3886d5ddbceSLemover
3896d5ddbceSLemover      XSDebug(p"[l2 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
390*45f497a4Shappy-lx      XSDebug(p"[l2 refill] refilldata:0x${wdata}\n")
3916d5ddbceSLemover      XSDebug(p"[l2 refill] l2v:${Binary(l2v)} -> ${Binary(l2v | rfvOH)}\n")
3926d5ddbceSLemover      XSDebug(p"[l2 refill] l2g:${Binary(l2g)} -> ${Binary(l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
3936d5ddbceSLemover
3946d5ddbceSLemover      refillIdx.suggestName(s"l2_refillIdx")
3956d5ddbceSLemover      victimWay.suggestName(s"l2_victimWay")
3966d5ddbceSLemover      victimWayOH.suggestName(s"l2_victimWayOH")
3976d5ddbceSLemover      rfvOH.suggestName(s"l2_rfvOH")
3986d5ddbceSLemover    }
3996d5ddbceSLemover
4006d5ddbceSLemover    when (refill.level === 2.U && memPte.isLeaf()) {
401*45f497a4Shappy-lx      val refillIdx = genPtwL3SetIdx(refill.req_info.vpn)
402*45f497a4Shappy-lx      val victimWay = replaceWrapper(RegEnable(VecInit(getl3vSet(refill.req_info.vpn).asBools).asUInt, first_fire), ptwl3replace.way(refillIdx))
4036d5ddbceSLemover      val victimWayOH = UIntToOH(victimWay)
4046d5ddbceSLemover      val rfvOH = UIntToOH(Cat(refillIdx, victimWay))
4057196f5a2SLemover      val wdata = Wire(l3EntryType)
406*45f497a4Shappy-lx      wdata.entries := wdata.entries.genEntries(
407*45f497a4Shappy-lx        vpn = refill.req_info.vpn,
408*45f497a4Shappy-lx        asid = io.csr.satp.asid,
409*45f497a4Shappy-lx        data = memRdata,
410*45f497a4Shappy-lx        levelUInt = 2.U,
411*45f497a4Shappy-lx        refill_prefetch
412*45f497a4Shappy-lx      )
4137196f5a2SLemover      wdata.ecc := ecc.encode(wdata.entries.asUInt()) >> wdata.entries.getWidth
4146d5ddbceSLemover      l3.io.w.apply(
4156d5ddbceSLemover        valid = true.B,
4166d5ddbceSLemover        setIdx = refillIdx,
4177196f5a2SLemover        data = wdata,
4186d5ddbceSLemover        waymask = victimWayOH
4196d5ddbceSLemover      )
4206d5ddbceSLemover      ptwl3replace.access(refillIdx, victimWay)
4216d5ddbceSLemover      l3v := l3v | rfvOH
4226d5ddbceSLemover      l3g := l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U)
4236d5ddbceSLemover
4245854c1edSLemover      for (i <- 0 until l2tlbParams.l3nWays) {
4256d5ddbceSLemover        l3RefillPerf(i) := i.U === victimWay
4266d5ddbceSLemover      }
4276d5ddbceSLemover
4286d5ddbceSLemover      XSDebug(p"[l3 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n")
429*45f497a4Shappy-lx      XSDebug(p"[l3 refill] refilldata:0x${wdata}\n")
4306d5ddbceSLemover      XSDebug(p"[l3 refill] l3v:${Binary(l3v)} -> ${Binary(l3v | rfvOH)}\n")
4316d5ddbceSLemover      XSDebug(p"[l3 refill] l3g:${Binary(l3g)} -> ${Binary(l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n")
4326d5ddbceSLemover
4336d5ddbceSLemover      refillIdx.suggestName(s"l3_refillIdx")
4346d5ddbceSLemover      victimWay.suggestName(s"l3_victimWay")
4356d5ddbceSLemover      victimWayOH.suggestName(s"l3_victimWayOH")
4366d5ddbceSLemover      rfvOH.suggestName(s"l3_rfvOH")
4376d5ddbceSLemover    }
4386d5ddbceSLemover    when ((refill.level === 0.U || refill.level === 1.U) && memPte.isLeaf()) {
4395854c1edSLemover      val refillIdx = spreplace.way// LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU
4406d5ddbceSLemover      val rfOH = UIntToOH(refillIdx)
441*45f497a4Shappy-lx      sp(refillIdx).refill(
442*45f497a4Shappy-lx        refill.req_info.vpn,
443*45f497a4Shappy-lx        io.csr.satp.asid,
444*45f497a4Shappy-lx        memSelData,
445*45f497a4Shappy-lx        refill.level,
446*45f497a4Shappy-lx        refill_prefetch
447*45f497a4Shappy-lx      )
4486d5ddbceSLemover      spreplace.access(refillIdx)
4496d5ddbceSLemover      spv := spv | rfOH
4506d5ddbceSLemover      spg := spg & ~rfOH | Mux(memPte.perm.g, rfOH, 0.U)
4516d5ddbceSLemover
4525854c1edSLemover      for (i <- 0 until l2tlbParams.spSize) {
4536d5ddbceSLemover        spRefillPerf(i) := i.U === refillIdx
4546d5ddbceSLemover      }
4556d5ddbceSLemover
456*45f497a4Shappy-lx      XSDebug(p"[sp refill] refillIdx:${refillIdx} refillEntry:${sp(refillIdx).genPtwEntry(refill.req_info.vpn, io.csr.satp.asid, memSelData, refill.level, refill_prefetch)}\n")
4576d5ddbceSLemover      XSDebug(p"[sp refill] spv:${Binary(spv)}->${Binary(spv | rfOH)} spg:${Binary(spg)}->${Binary(spg & ~rfOH | Mux(memPte.perm.g, rfOH, 0.U))}\n")
4586d5ddbceSLemover
4596d5ddbceSLemover      refillIdx.suggestName(s"sp_refillIdx")
4606d5ddbceSLemover      rfOH.suggestName(s"sp_rfOH")
4616d5ddbceSLemover    }
4626d5ddbceSLemover  }
4636d5ddbceSLemover
4647196f5a2SLemover  val l2eccFlush = RegNext(l2eccError, init = false.B)
4657196f5a2SLemover  val l3eccFlush = RegNext(l3eccError, init = false.B)
4667196f5a2SLemover  val eccVpn = RegNext(second_req.vpn)
4677196f5a2SLemover
4687196f5a2SLemover  assert(!l2eccFlush)
4697196f5a2SLemover  assert(!l3eccFlush)
4707196f5a2SLemover  when (l2eccFlush) {
4717196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL2SetIdx(eccVpn))
4727196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l2nWays, a.asUInt) }).asUInt
4737196f5a2SLemover    l2v := l2v & ~flushMask
4747196f5a2SLemover    l2g := l2g & ~flushMask
4757196f5a2SLemover  }
4767196f5a2SLemover
4777196f5a2SLemover  when (l3eccFlush) {
4787196f5a2SLemover    val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(eccVpn))
4797196f5a2SLemover    val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
4807196f5a2SLemover    l3v := l3v & ~flushMask
4817196f5a2SLemover    l3g := l3g & ~flushMask
4827196f5a2SLemover  }
4837196f5a2SLemover
4846d5ddbceSLemover  // sfence
4856d5ddbceSLemover  when (sfence.valid) {
486*45f497a4Shappy-lx    val l1asidhit = VecInit(l1asids.map(_ === sfence.bits.asid)).asUInt
487*45f497a4Shappy-lx    val spasidhit = VecInit(spasids.map(_ === sfence.bits.asid)).asUInt
488*45f497a4Shappy-lx    val sfence_vpn = sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)
489*45f497a4Shappy-lx
4906d5ddbceSLemover    when (sfence.bits.rs1/*va*/) {
4916d5ddbceSLemover      when (sfence.bits.rs2) {
4926d5ddbceSLemover        // all va && all asid
4936d5ddbceSLemover        l1v := 0.U
4946d5ddbceSLemover        l2v := 0.U
4956d5ddbceSLemover        l3v := 0.U
4966d5ddbceSLemover        spv := 0.U
4976d5ddbceSLemover      } .otherwise {
4986d5ddbceSLemover        // all va && specific asid except global
499*45f497a4Shappy-lx
500*45f497a4Shappy-lx        l1v := l1v & (~l1asidhit | l1g)
5016d5ddbceSLemover        l2v := l2v & l2g
5026d5ddbceSLemover        l3v := l3v & l3g
503*45f497a4Shappy-lx        spv := spv & (~spasidhit | spg)
5046d5ddbceSLemover      }
5056d5ddbceSLemover    } .otherwise {
5066d5ddbceSLemover      // val flushMask = UIntToOH(genTlbL2Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen)))
507*45f497a4Shappy-lx      val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(sfence_vpn))
5085854c1edSLemover      // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l3nWays, _.asUInt))).asUInt
5095854c1edSLemover      val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt
5106d5ddbceSLemover      flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH")
5116d5ddbceSLemover      flushMask.suggestName(s"sfence_nrs1_flushMask")
512*45f497a4Shappy-lx
5136d5ddbceSLemover      when (sfence.bits.rs2) {
5146d5ddbceSLemover        // specific leaf of addr && all asid
5156d5ddbceSLemover        l3v := l3v & ~flushMask
516*45f497a4Shappy-lx        spv := spv & (~VecInit(sp.map(_.hit(sfence_vpn, sfence.bits.asid, ignoreAsid = true))).asUInt | spg)
5176d5ddbceSLemover      } .otherwise {
5186d5ddbceSLemover        // specific leaf of addr && specific asid
5196d5ddbceSLemover        l3v := l3v & (~flushMask | l3g)
520*45f497a4Shappy-lx        spv := spv & (~VecInit(sp.map(_.hit(sfence_vpn, sfence.bits.asid))).asUInt | spg)
5216d5ddbceSLemover      }
5226d5ddbceSLemover    }
5236d5ddbceSLemover  }
5246d5ddbceSLemover
5256d5ddbceSLemover  // Perf Count
526bc063562SLemover  val resp_l3 = DataHoldBypass(l3Hit, io.resp.valid && !resp_latch_valid).asBool()
527bc063562SLemover  val resp_sp = DataHoldBypass(spHit, io.resp.valid && !resp_latch_valid).asBool()
528bc063562SLemover  val resp_l1_pre = DataHoldBypass(l1Pre, io.resp.valid && !resp_latch_valid).asBool()
529bc063562SLemover  val resp_l2_pre = DataHoldBypass(l2Pre, io.resp.valid && !resp_latch_valid).asBool()
530bc063562SLemover  val resp_l3_pre = DataHoldBypass(l3Pre, io.resp.valid && !resp_latch_valid).asBool()
531bc063562SLemover  val resp_sp_pre = DataHoldBypass(spPre, io.resp.valid && !resp_latch_valid).asBool()
532*45f497a4Shappy-lx  val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire()
533bc063562SLemover  XSPerfAccumulate("access", base_valid_access_0)
534bc063562SLemover  XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
535bc063562SLemover  XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
536bc063562SLemover  XSPerfAccumulate("l3_hit", base_valid_access_0 && resp_l3)
537bc063562SLemover  XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp)
538bc063562SLemover  XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit)
539bc063562SLemover
540bc063562SLemover  XSPerfAccumulate("l1_hit_pre", base_valid_access_0 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
541bc063562SLemover  XSPerfAccumulate("l2_hit_pre", base_valid_access_0 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
542bc063562SLemover  XSPerfAccumulate("l3_hit_pre", base_valid_access_0 && resp_l3_pre && resp_l3)
543bc063562SLemover  XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp)
544bc063562SLemover  XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
545bc063562SLemover
546*45f497a4Shappy-lx  val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire()
547bc063562SLemover  XSPerfAccumulate("pre_access", base_valid_access_1)
548bc063562SLemover  XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
549bc063562SLemover  XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
550bc063562SLemover  XSPerfAccumulate("pre_l3_hit", base_valid_access_1 && resp_l3)
551bc063562SLemover  XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp)
552bc063562SLemover  XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit)
553bc063562SLemover
554bc063562SLemover  XSPerfAccumulate("pre_l1_hit_pre", base_valid_access_1 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
555bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre", base_valid_access_1 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
556bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre", base_valid_access_1 && resp_l3_pre && resp_l3)
557bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp)
558bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
559bc063562SLemover
560*45f497a4Shappy-lx  val base_valid_access_2 = second_isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire()
561bc063562SLemover  XSPerfAccumulate("access_first", base_valid_access_2)
562bc063562SLemover  XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
563bc063562SLemover  XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
564bc063562SLemover  XSPerfAccumulate("l3_hit_first", base_valid_access_2 && resp_l3)
565bc063562SLemover  XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp)
566bc063562SLemover  XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit)
567bc063562SLemover
568bc063562SLemover  XSPerfAccumulate("l1_hit_pre_first", base_valid_access_2 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
569bc063562SLemover  XSPerfAccumulate("l2_hit_pre_first", base_valid_access_2 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
570bc063562SLemover  XSPerfAccumulate("l3_hit_pre_first", base_valid_access_2 && resp_l3_pre && resp_l3)
571bc063562SLemover  XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp)
572bc063562SLemover  XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
573bc063562SLemover
574*45f497a4Shappy-lx  val base_valid_access_3 = second_isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire()
575bc063562SLemover  XSPerfAccumulate("pre_access_first", base_valid_access_3)
576bc063562SLemover  XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
577bc063562SLemover  XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
578bc063562SLemover  XSPerfAccumulate("pre_l3_hit_first", base_valid_access_3 && resp_l3)
579bc063562SLemover  XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp)
580bc063562SLemover  XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit)
581bc063562SLemover
582bc063562SLemover  XSPerfAccumulate("pre_l1_hit_pre_first", base_valid_access_3 && resp_l1_pre && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
583bc063562SLemover  XSPerfAccumulate("pre_l2_hit_pre_first", base_valid_access_3 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit)
584bc063562SLemover  XSPerfAccumulate("pre_l3_hit_pre_first", base_valid_access_3 && resp_l3_pre && resp_l3)
585bc063562SLemover  XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp)
586bc063562SLemover  XSPerfAccumulate("pre_pte_hit_pre_first",base_valid_access_3 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit)
587bc063562SLemover
5886d5ddbceSLemover  XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready)
5896d5ddbceSLemover  XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready)
5906d5ddbceSLemover  l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1AccessIndex${i}", l) }
5916d5ddbceSLemover  l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2AccessIndex${i}", l) }
5926d5ddbceSLemover  l3AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3AccessIndex${i}", l) }
5936d5ddbceSLemover  spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) }
5946d5ddbceSLemover  l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1RefillIndex${i}", l) }
5956d5ddbceSLemover  l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2RefillIndex${i}", l) }
5966d5ddbceSLemover  l3RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3RefillIndex${i}", l) }
5976d5ddbceSLemover  spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) }
5986d5ddbceSLemover
599bc063562SLemover  XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR)
600bc063562SLemover  XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR)
601bc063562SLemover  XSPerfAccumulate("l3Refill", Cat(l3RefillPerf).orR)
602bc063562SLemover  XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR)
603*45f497a4Shappy-lx  XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch)
604*45f497a4Shappy-lx  XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch)
605*45f497a4Shappy-lx  XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf).orR && refill_prefetch)
606*45f497a4Shappy-lx  XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch)
607bc063562SLemover
6086d5ddbceSLemover  // debug
6096d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] original v and g vector:\n")
6106d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l1v:${Binary(l1v)}\n")
6116d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l2v:${Binary(l2v)}\n")
6126d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l3v:${Binary(l3v)}\n")
6136d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] l3g:${Binary(l3g)}\n")
6146d5ddbceSLemover  XSDebug(sfence.valid, p"[sfence] spv:${Binary(spv)}\n")
6156d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] new v and g vector:\n")
6166d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l1v:${Binary(l1v)}\n")
6176d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l2v:${Binary(l2v)}\n")
6186d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l3v:${Binary(l3v)}\n")
6196d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] l3g:${Binary(l3g)}\n")
6206d5ddbceSLemover  XSDebug(RegNext(sfence.valid), p"[sfence] spv:${Binary(spv)}\n")
6216d5ddbceSLemover}
622