16d5ddbceSLemover/*************************************************************************************** 26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 46d5ddbceSLemover* 56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 86d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 96d5ddbceSLemover* 106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 136d5ddbceSLemover* 146d5ddbceSLemover* See the Mulan PSL v2 for more details. 156d5ddbceSLemover***************************************************************************************/ 166d5ddbceSLemover 176d5ddbceSLemoverpackage xiangshan.cache.mmu 186d5ddbceSLemover 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 206d5ddbceSLemoverimport chisel3._ 216d5ddbceSLemoverimport chisel3.util._ 226d5ddbceSLemoverimport xiangshan._ 236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 246d5ddbceSLemoverimport utils._ 253c02ee8fSwakafaimport utility._ 266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 276d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 286d5ddbceSLemover 296d5ddbceSLemover/* ptw cache caches the page table of all the three layers 306d5ddbceSLemover * ptw cache resp at next cycle 316d5ddbceSLemover * the cache should not be blocked 326d5ddbceSLemover * when miss queue if full, just block req outside 336d5ddbceSLemover */ 343889e11eSLemover 353889e11eSLemoverclass PageCachePerPespBundle(implicit p: Parameters) extends PtwBundle { 363889e11eSLemover val hit = Bool() 373889e11eSLemover val pre = Bool() 38d61cd5eeSpeixiaokun val ppn = if (HasHExtension) UInt((vpnLen max ppnLen).W) else UInt(ppnLen.W) 393889e11eSLemover val perm = new PtePermBundle() 403889e11eSLemover val ecc = Bool() 413889e11eSLemover val level = UInt(2.W) 428d8ac704SLemover val v = Bool() 433889e11eSLemover 443889e11eSLemover def apply(hit: Bool, pre: Bool, ppn: UInt, perm: PtePermBundle = 0.U.asTypeOf(new PtePermBundle()), 458d8ac704SLemover ecc: Bool = false.B, level: UInt = 0.U, valid: Bool = true.B) { 463889e11eSLemover this.hit := hit && !ecc 473889e11eSLemover this.pre := pre 483889e11eSLemover this.ppn := ppn 493889e11eSLemover this.perm := perm 503889e11eSLemover this.ecc := ecc && hit 513889e11eSLemover this.level := level 528d8ac704SLemover this.v := valid 533889e11eSLemover } 543889e11eSLemover} 553889e11eSLemover 5663632028SHaoyuan Fengclass PageCacheMergePespBundle(implicit p: Parameters) extends PtwBundle { 5763632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 5863632028SHaoyuan Feng val hit = Bool() 5963632028SHaoyuan Feng val pre = Bool() 60d61cd5eeSpeixiaokun val ppn = Vec(tlbcontiguous, if(HasHExtension) UInt((vpnLen max ppnLen).W) else UInt(ppnLen.W)) 6163632028SHaoyuan Feng val perm = Vec(tlbcontiguous, new PtePermBundle()) 6263632028SHaoyuan Feng val ecc = Bool() 6363632028SHaoyuan Feng val level = UInt(2.W) 6463632028SHaoyuan Feng val v = Vec(tlbcontiguous, Bool()) 6563632028SHaoyuan Feng 6663632028SHaoyuan Feng def apply(hit: Bool, pre: Bool, ppn: Vec[UInt], perm: Vec[PtePermBundle] = Vec(tlbcontiguous, 0.U.asTypeOf(new PtePermBundle())), 6763632028SHaoyuan Feng ecc: Bool = false.B, level: UInt = 0.U, valid: Vec[Bool] = Vec(tlbcontiguous, true.B)) { 6863632028SHaoyuan Feng this.hit := hit && !ecc 6963632028SHaoyuan Feng this.pre := pre 7063632028SHaoyuan Feng this.ppn := ppn 7163632028SHaoyuan Feng this.perm := perm 7263632028SHaoyuan Feng this.ecc := ecc && hit 7363632028SHaoyuan Feng this.level := level 7463632028SHaoyuan Feng this.v := valid 7563632028SHaoyuan Feng } 7663632028SHaoyuan Feng} 7763632028SHaoyuan Feng 783889e11eSLemoverclass PageCacheRespBundle(implicit p: Parameters) extends PtwBundle { 793889e11eSLemover val l1 = new PageCachePerPespBundle 803889e11eSLemover val l2 = new PageCachePerPespBundle 8163632028SHaoyuan Feng val l3 = new PageCacheMergePespBundle 823889e11eSLemover val sp = new PageCachePerPespBundle 833889e11eSLemover} 843889e11eSLemover 853889e11eSLemoverclass PtwCacheReq(implicit p: Parameters) extends PtwBundle { 863889e11eSLemover val req_info = new L2TlbInnerBundle() 873889e11eSLemover val isFirst = Bool() 881f4a7c0cSLemover val bypassed = Vec(3, Bool()) 89325f0a4eSpeixiaokun val isHptwReq = Bool() 90d0de7e4aSpeixiaokun val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W) 913889e11eSLemover} 923889e11eSLemover 933889e11eSLemoverclass PtwCacheIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 943889e11eSLemover val req = Flipped(DecoupledIO(new PtwCacheReq())) 956d5ddbceSLemover val resp = DecoupledIO(new Bundle { 9645f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 9794133605SLemover val isFirst = Bool() 986d5ddbceSLemover val hit = Bool() 99bc063562SLemover val prefetch = Bool() // is the entry fetched by prefetch 1001f4a7c0cSLemover val bypassed = Bool() 1016d5ddbceSLemover val toFsm = new Bundle { 1026d5ddbceSLemover val l1Hit = Bool() 1036d5ddbceSLemover val l2Hit = Bool() 104d61cd5eeSpeixiaokun val ppn = if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W) 10530104977Speixiaokun val stage1Hit = Bool() // find stage 1 pte in cache, but need to search stage 2 pte in cache at PTW 1066d5ddbceSLemover } 10763632028SHaoyuan Feng val toTlb = new PtwMergeResp() 108325f0a4eSpeixiaokun val isHptwReq = Bool() 109d0de7e4aSpeixiaokun val toHptw = new Bundle { 110d0de7e4aSpeixiaokun val l1Hit = Bool() 111d0de7e4aSpeixiaokun val l2Hit = Bool() 112d0de7e4aSpeixiaokun val ppn = UInt(ppnLen.W) 113d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 114d0de7e4aSpeixiaokun val resp = new HptwResp() // used if hit 11583d93d53Speixiaokun val bypassed = Bool() 116d0de7e4aSpeixiaokun } 1176d5ddbceSLemover }) 1186d5ddbceSLemover val refill = Flipped(ValidIO(new Bundle { 1195854c1edSLemover val ptes = UInt(blockBits.W) 1207797f035SbugGenerator val levelOH = new Bundle { 1217797f035SbugGenerator // NOTE: levelOH has (Level+1) bits, each stands for page cache entries 1227797f035SbugGenerator val sp = Bool() 1237797f035SbugGenerator val l3 = Bool() 1247797f035SbugGenerator val l2 = Bool() 1257797f035SbugGenerator val l1 = Bool() 1267797f035SbugGenerator def apply(levelUInt: UInt, valid: Bool) = { 1277797f035SbugGenerator sp := RegNext((levelUInt === 0.U || levelUInt === 1.U) && valid, false.B) 1287797f035SbugGenerator l3 := RegNext((levelUInt === 2.U) & valid, false.B) 1297797f035SbugGenerator l2 := RegNext((levelUInt === 1.U) & valid, false.B) 1307797f035SbugGenerator l1 := RegNext((levelUInt === 0.U) & valid, false.B) 1317797f035SbugGenerator } 1327797f035SbugGenerator } 1337797f035SbugGenerator // duplicate level and sel_pte for each page caches, for better fanout 1347797f035SbugGenerator val req_info_dup = Vec(3, new L2TlbInnerBundle()) 1357797f035SbugGenerator val level_dup = Vec(3, UInt(log2Up(Level).W)) 1367797f035SbugGenerator val sel_pte_dup = Vec(3, UInt(XLEN.W)) 1376d5ddbceSLemover })) 1387797f035SbugGenerator val sfence_dup = Vec(4, Input(new SfenceBundle())) 1397797f035SbugGenerator val csr_dup = Vec(3, Input(new TlbCsrBundle())) 1406d5ddbceSLemover} 1416d5ddbceSLemover 1421ca0e4f3SYinan Xuclass PtwCache()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 1436d5ddbceSLemover val io = IO(new PtwCacheIO) 1447196f5a2SLemover val ecc = Code.fromString(l2tlbParams.ecc) 1457196f5a2SLemover val l2EntryType = new PTWEntriesWithEcc(ecc, num = PtwL2SectorSize, tagLen = PtwL2TagLen, level = 1, hasPerm = false) 1467196f5a2SLemover val l3EntryType = new PTWEntriesWithEcc(ecc, num = PtwL3SectorSize, tagLen = PtwL3TagLen, level = 2, hasPerm = true) 1477196f5a2SLemover 1486d5ddbceSLemover // TODO: four caches make the codes dirty, think about how to deal with it 1496d5ddbceSLemover 1507797f035SbugGenerator val sfence_dup = io.sfence_dup 1516d5ddbceSLemover val refill = io.refill.bits 1527797f035SbugGenerator val refill_prefetch_dup = io.refill.bits.req_info_dup.map(a => from_pre(a.source)) 153d0de7e4aSpeixiaokun val flush_dup = sfence_dup.zip(io.csr_dup).map(f => f._1.valid || f._2.satp.changed || f._2.vsatp.changed || f._2.hgatp.changed) 1547797f035SbugGenerator val flush = flush_dup(0) 1556d5ddbceSLemover 1566d5ddbceSLemover // when refill, refuce to accept new req 1575854c1edSLemover val rwHarzad = if (sramSinglePort) io.refill.valid else false.B 1583889e11eSLemover 1593889e11eSLemover // handle hand signal and req_info 1606c4dcc2dSLemover // TODO: replace with FlushableQueue 1616c4dcc2dSLemover val stageReq = Wire(Decoupled(new PtwCacheReq())) // enq stage & read page cache valid 1626c4dcc2dSLemover val stageDelay = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // page cache resp 1636c4dcc2dSLemover val stageCheck = Wire(Vec(2, Decoupled(new PtwCacheReq()))) // check hit & check ecc 1646c4dcc2dSLemover val stageResp = Wire(Decoupled(new PtwCacheReq())) // deq stage 1657797f035SbugGenerator 1667797f035SbugGenerator val stageDelay_valid_1cycle = OneCycleValid(stageReq.fire, flush) // catch ram data 1677797f035SbugGenerator val stageCheck_valid_1cycle = OneCycleValid(stageDelay(1).fire, flush) // replace & perf counter 1687797f035SbugGenerator val stageResp_valid_1cycle_dup = Wire(Vec(2, Bool())) 1697797f035SbugGenerator stageResp_valid_1cycle_dup.map(_ := OneCycleValid(stageCheck(1).fire, flush)) // ecc flush 1707797f035SbugGenerator 1716c4dcc2dSLemover stageReq <> io.req 1726c4dcc2dSLemover PipelineConnect(stageReq, stageDelay(0), stageDelay(1).ready, flush, rwHarzad) 1737797f035SbugGenerator InsideStageConnect(stageDelay(0), stageDelay(1), stageDelay_valid_1cycle) 1746c4dcc2dSLemover PipelineConnect(stageDelay(1), stageCheck(0), stageCheck(1).ready, flush) 1757797f035SbugGenerator InsideStageConnect(stageCheck(0), stageCheck(1), stageCheck_valid_1cycle) 1766c4dcc2dSLemover PipelineConnect(stageCheck(1), stageResp, io.resp.ready, flush) 1776c4dcc2dSLemover stageResp.ready := !stageResp.valid || io.resp.ready 1786d5ddbceSLemover 1796d5ddbceSLemover // l1: level 0 non-leaf pte 1805854c1edSLemover val l1 = Reg(Vec(l2tlbParams.l1Size, new PtwEntry(tagLen = PtwL1TagLen))) 1815854c1edSLemover val l1v = RegInit(0.U(l2tlbParams.l1Size.W)) 1825854c1edSLemover val l1g = Reg(UInt(l2tlbParams.l1Size.W)) 1831dd3e32dSHaoyuan Feng val l1asids = l1.map(_.asid) 184d0de7e4aSpeixiaokun val l1vmids = l1.map(_.vmid) 185*875ae3b4SXiaokun-Pei val l1h = Reg(Vec(l2tlbParams.l1Size, UInt(2.W))) 1866d5ddbceSLemover 1876d5ddbceSLemover // l2: level 1 non-leaf pte 1886d5ddbceSLemover val l2 = Module(new SRAMTemplate( 1897196f5a2SLemover l2EntryType, 1905854c1edSLemover set = l2tlbParams.l2nSets, 1915854c1edSLemover way = l2tlbParams.l2nWays, 1925854c1edSLemover singlePort = sramSinglePort 1936d5ddbceSLemover )) 1945854c1edSLemover val l2v = RegInit(0.U((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W)) 1955854c1edSLemover val l2g = Reg(UInt((l2tlbParams.l2nSets * l2tlbParams.l2nWays).W)) 196d0de7e4aSpeixiaokun val l2h = Reg(Vec(l2tlbParams.l2nSets, Vec(l2tlbParams.l2nWays, UInt(2.W)))) 1976d5ddbceSLemover def getl2vSet(vpn: UInt) = { 1985854c1edSLemover require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays)) 1996d5ddbceSLemover val set = genPtwL2SetIdx(vpn) 2005854c1edSLemover require(set.getWidth == log2Up(l2tlbParams.l2nSets)) 2015854c1edSLemover val l2vVec = l2v.asTypeOf(Vec(l2tlbParams.l2nSets, UInt(l2tlbParams.l2nWays.W))) 2026d5ddbceSLemover l2vVec(set) 2036d5ddbceSLemover } 204d0de7e4aSpeixiaokun def getl2hSet(vpn: UInt) = { 20545f497a4Shappy-lx require(log2Up(l2tlbParams.l2nWays) == log2Down(l2tlbParams.l2nWays)) 20645f497a4Shappy-lx val set = genPtwL2SetIdx(vpn) 20745f497a4Shappy-lx require(set.getWidth == log2Up(l2tlbParams.l2nSets)) 208d0de7e4aSpeixiaokun l2h(set) 20945f497a4Shappy-lx } 2106d5ddbceSLemover 211d0de7e4aSpeixiaokun 212d0de7e4aSpeixiaokun 2136d5ddbceSLemover // l3: level 2 leaf pte of 4KB pages 2146d5ddbceSLemover val l3 = Module(new SRAMTemplate( 2157196f5a2SLemover l3EntryType, 2165854c1edSLemover set = l2tlbParams.l3nSets, 2175854c1edSLemover way = l2tlbParams.l3nWays, 2185854c1edSLemover singlePort = sramSinglePort 2196d5ddbceSLemover )) 2205854c1edSLemover val l3v = RegInit(0.U((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W)) 2215854c1edSLemover val l3g = Reg(UInt((l2tlbParams.l3nSets * l2tlbParams.l3nWays).W)) 222d0de7e4aSpeixiaokun val l3h = Reg(Vec(l2tlbParams.l3nSets, Vec(l2tlbParams.l3nWays, UInt(2.W)))) 2236d5ddbceSLemover def getl3vSet(vpn: UInt) = { 2245854c1edSLemover require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays)) 2256d5ddbceSLemover val set = genPtwL3SetIdx(vpn) 2265854c1edSLemover require(set.getWidth == log2Up(l2tlbParams.l3nSets)) 2275854c1edSLemover val l3vVec = l3v.asTypeOf(Vec(l2tlbParams.l3nSets, UInt(l2tlbParams.l3nWays.W))) 2286d5ddbceSLemover l3vVec(set) 2296d5ddbceSLemover } 230d0de7e4aSpeixiaokun def getl3hSet(vpn: UInt) = { 23145f497a4Shappy-lx require(log2Up(l2tlbParams.l3nWays) == log2Down(l2tlbParams.l3nWays)) 23245f497a4Shappy-lx val set = genPtwL3SetIdx(vpn) 23345f497a4Shappy-lx require(set.getWidth == log2Up(l2tlbParams.l3nSets)) 234d0de7e4aSpeixiaokun l3h(set) 23545f497a4Shappy-lx } 2366d5ddbceSLemover 2376d5ddbceSLemover // sp: level 0/1 leaf pte of 1GB/2MB super pages 2385854c1edSLemover val sp = Reg(Vec(l2tlbParams.spSize, new PtwEntry(tagLen = SPTagLen, hasPerm = true, hasLevel = true))) 2395854c1edSLemover val spv = RegInit(0.U(l2tlbParams.spSize.W)) 2405854c1edSLemover val spg = Reg(UInt(l2tlbParams.spSize.W)) 2411dd3e32dSHaoyuan Feng val spasids = sp.map(_.asid) 242d0de7e4aSpeixiaokun val spvmids = sp.map(_.vmid) 243d61cd5eeSpeixiaokun val sph = Reg(Vec(l2tlbParams.spSize, UInt(2.W))) 2446d5ddbceSLemover 2456d5ddbceSLemover // Access Perf 2465854c1edSLemover val l1AccessPerf = Wire(Vec(l2tlbParams.l1Size, Bool())) 2475854c1edSLemover val l2AccessPerf = Wire(Vec(l2tlbParams.l2nWays, Bool())) 2485854c1edSLemover val l3AccessPerf = Wire(Vec(l2tlbParams.l3nWays, Bool())) 2495854c1edSLemover val spAccessPerf = Wire(Vec(l2tlbParams.spSize, Bool())) 2506d5ddbceSLemover l1AccessPerf.map(_ := false.B) 2516d5ddbceSLemover l2AccessPerf.map(_ := false.B) 2526d5ddbceSLemover l3AccessPerf.map(_ := false.B) 2536d5ddbceSLemover spAccessPerf.map(_ := false.B) 2546d5ddbceSLemover 2553889e11eSLemover 2561f4a7c0cSLemover 25782978df9Speixiaokun def vpn_match(vpn1: UInt, vpn2: UInt, level: Int) = { 25882978df9Speixiaokun (vpn1(vpnLen-1, vpnnLen*(2-level)+3) === vpn2(vpnLen-1, vpnnLen*(2-level)+3)) 2591f4a7c0cSLemover } 2601f4a7c0cSLemover // NOTE: not actually bypassed, just check if hit, re-access the page cache 261d0de7e4aSpeixiaokun def refill_bypass(vpn: UInt, level: Int, h_search: UInt) = { 2626f508cb5Speixiaokun val change_h = MuxLookup(h_search, noS2xlate)(Seq( 263980ddf4cSpeixiaokun allStage -> onlyStage1, 264980ddf4cSpeixiaokun onlyStage1 -> onlyStage1, 265980ddf4cSpeixiaokun onlyStage2 -> onlyStage2 266980ddf4cSpeixiaokun )) 267d0de7e4aSpeixiaokun val refill_vpn = io.refill.bits.req_info_dup(0).vpn 268980ddf4cSpeixiaokun io.refill.valid && (level.U === io.refill.bits.level_dup(0)) && vpn_match(refill_vpn, vpn, level) && change_h === io.refill.bits.req_info_dup(0).s2xlate 2691f4a7c0cSLemover } 2701f4a7c0cSLemover 27182978df9Speixiaokun val vpn_search = stageReq.bits.req_info.vpn 2726f508cb5Speixiaokun val h_search = MuxLookup(stageReq.bits.req_info.s2xlate, noS2xlate)(Seq( 27309280d15Speixiaokun allStage -> onlyStage1, 27409280d15Speixiaokun onlyStage1 -> onlyStage1, 27509280d15Speixiaokun onlyStage2 -> onlyStage2 27609280d15Speixiaokun )) 2776d5ddbceSLemover // l1 2785854c1edSLemover val ptwl1replace = ReplacementPolicy.fromString(l2tlbParams.l1Replacer, l2tlbParams.l1Size) 279bc063562SLemover val (l1Hit, l1HitPPN, l1Pre) = { 280d0de7e4aSpeixiaokun val hitVecT = l1.zipWithIndex.map { 281b188e334Speixiaokun case (e, i) => (e.hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate) 282d0de7e4aSpeixiaokun && l1v(i) && h_search === l1h(i)) 283d0de7e4aSpeixiaokun } 2846c4dcc2dSLemover val hitVec = hitVecT.map(RegEnable(_, stageReq.fire)) 2851f4a7c0cSLemover 2861f4a7c0cSLemover // stageDelay, but check for l1 2879c503409SLemover val hitPPN = DataHoldBypass(ParallelPriorityMux(hitVec zip l1.map(_.ppn)), stageDelay_valid_1cycle) 2889c503409SLemover val hitPre = DataHoldBypass(ParallelPriorityMux(hitVec zip l1.map(_.prefetch)), stageDelay_valid_1cycle) 2891f4a7c0cSLemover val hit = DataHoldBypass(ParallelOR(hitVec), stageDelay_valid_1cycle) 2906d5ddbceSLemover 2916c4dcc2dSLemover when (hit && stageDelay_valid_1cycle) { ptwl1replace.access(OHToUInt(hitVec)) } 2926d5ddbceSLemover 2936c4dcc2dSLemover l1AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageDelay_valid_1cycle} 2945854c1edSLemover for (i <- 0 until l2tlbParams.l1Size) { 295b188e334Speixiaokun XSDebug(stageReq.fire, p"[l1] l1(${i.U}) ${l1(i)} hit:${l1(i).hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate)}\n") 2966d5ddbceSLemover } 2976c4dcc2dSLemover XSDebug(stageReq.fire, p"[l1] l1v:${Binary(l1v)} hitVecT:${Binary(VecInit(hitVecT).asUInt)}\n") 2986c4dcc2dSLemover XSDebug(stageDelay(0).valid, p"[l1] l1Hit:${hit} l1HitPPN:0x${Hexadecimal(hitPPN)} hitVec:${VecInit(hitVec).asUInt}\n") 2996d5ddbceSLemover 3006d5ddbceSLemover VecInit(hitVecT).suggestName(s"l1_hitVecT") 3016d5ddbceSLemover VecInit(hitVec).suggestName(s"l1_hitVec") 3026d5ddbceSLemover 3036c4dcc2dSLemover // synchronize with other entries with RegEnable 3046c4dcc2dSLemover (RegEnable(hit, stageDelay(1).fire), 3056c4dcc2dSLemover RegEnable(hitPPN, stageDelay(1).fire), 3066c4dcc2dSLemover RegEnable(hitPre, stageDelay(1).fire)) 3076d5ddbceSLemover } 3086d5ddbceSLemover 3096d5ddbceSLemover // l2 3105854c1edSLemover val ptwl2replace = ReplacementPolicy.fromString(l2tlbParams.l2Replacer,l2tlbParams.l2nWays,l2tlbParams.l2nSets) 311bc063562SLemover val (l2Hit, l2HitPPN, l2Pre, l2eccError) = { 312d0de7e4aSpeixiaokun val ridx = genPtwL2SetIdx(vpn_search) 3136c4dcc2dSLemover l2.io.r.req.valid := stageReq.fire 3146d5ddbceSLemover l2.io.r.req.bits.apply(setIdx = ridx) 315d0de7e4aSpeixiaokun val vVec_req = getl2vSet(vpn_search) 316d0de7e4aSpeixiaokun val hVec_req = getl2hSet(vpn_search) 3176c4dcc2dSLemover 3186c4dcc2dSLemover // delay one cycle after sram read 31982978df9Speixiaokun val delay_vpn = stageDelay(0).bits.req_info.vpn 3206f508cb5Speixiaokun val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq( 321980ddf4cSpeixiaokun allStage -> onlyStage1, 322980ddf4cSpeixiaokun onlyStage1 -> onlyStage1, 323980ddf4cSpeixiaokun onlyStage2 -> onlyStage2 324980ddf4cSpeixiaokun )) 3256c4dcc2dSLemover val data_resp = DataHoldBypass(l2.io.r.resp.data, stageDelay_valid_1cycle) 3267797f035SbugGenerator val vVec_delay = RegEnable(vVec_req, stageReq.fire) 327d0de7e4aSpeixiaokun val hVec_delay = RegEnable(hVec_req, stageReq.fire) 328d0de7e4aSpeixiaokun val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) => 329b188e334Speixiaokun wayData.entries.hit(delay_vpn, io.csr_dup(1).satp.asid, io.csr_dup(1).vsatp.asid, io.csr_dup(1).hgatp.asid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)}) 3306c4dcc2dSLemover 3316c4dcc2dSLemover // check hit and ecc 33282978df9Speixiaokun val check_vpn = stageCheck(0).bits.req_info.vpn 3336c4dcc2dSLemover val ramDatas = RegEnable(data_resp, stageDelay(1).fire) 334935edac4STang Haojin val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools 3356c4dcc2dSLemover 3367797f035SbugGenerator val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire) 3377196f5a2SLemover val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas) 3387196f5a2SLemover val hitWayData = hitWayEntry.entries 3396c4dcc2dSLemover val hit = ParallelOR(hitVec) 340f3034303SHaoyuan Feng val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l2nWays).map(_.U(log2Up(l2tlbParams.l2nWays).W))) 3413889e11eSLemover val eccError = hitWayEntry.decode() 3427196f5a2SLemover 3436d5ddbceSLemover ridx.suggestName(s"l2_ridx") 3446d5ddbceSLemover ramDatas.suggestName(s"l2_ramDatas") 3456d5ddbceSLemover hitVec.suggestName(s"l2_hitVec") 3466d5ddbceSLemover hitWayData.suggestName(s"l2_hitWayData") 3476d5ddbceSLemover hitWay.suggestName(s"l2_hitWay") 3486d5ddbceSLemover 3496c4dcc2dSLemover when (hit && stageCheck_valid_1cycle) { ptwl2replace.access(genPtwL2SetIdx(check_vpn), hitWay) } 3506d5ddbceSLemover 3516c4dcc2dSLemover l2AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle } 3526c4dcc2dSLemover XSDebug(stageDelay_valid_1cycle, p"[l2] ridx:0x${Hexadecimal(ridx)}\n") 3535854c1edSLemover for (i <- 0 until l2tlbParams.l2nWays) { 3546c4dcc2dSLemover XSDebug(stageCheck_valid_1cycle, p"[l2] ramDatas(${i.U}) ${ramDatas(i)} l2v:${vVec(i)} hit:${hit}\n") 3556d5ddbceSLemover } 3566c4dcc2dSLemover XSDebug(stageCheck_valid_1cycle, p"[l2] l2Hit:${hit} l2HitPPN:0x${Hexadecimal(hitWayData.ppns(genPtwL2SectorIdx(check_vpn)))} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} vidx:${vVec}\n") 3576d5ddbceSLemover 3586c4dcc2dSLemover (hit, hitWayData.ppns(genPtwL2SectorIdx(check_vpn)), hitWayData.prefetch, eccError) 3596d5ddbceSLemover } 3606d5ddbceSLemover 3616d5ddbceSLemover // l3 3625854c1edSLemover val ptwl3replace = ReplacementPolicy.fromString(l2tlbParams.l3Replacer,l2tlbParams.l3nWays,l2tlbParams.l3nSets) 363bc063562SLemover val (l3Hit, l3HitData, l3Pre, l3eccError) = { 364d0de7e4aSpeixiaokun val ridx = genPtwL3SetIdx(vpn_search) 3656c4dcc2dSLemover l3.io.r.req.valid := stageReq.fire 3666d5ddbceSLemover l3.io.r.req.bits.apply(setIdx = ridx) 367d0de7e4aSpeixiaokun val vVec_req = getl3vSet(vpn_search) 368d0de7e4aSpeixiaokun val hVec_req = getl3hSet(vpn_search) 3696c4dcc2dSLemover 3706c4dcc2dSLemover // delay one cycle after sram read 37182978df9Speixiaokun val delay_vpn = stageDelay(0).bits.req_info.vpn 3726f508cb5Speixiaokun val delay_h = MuxLookup(stageDelay(0).bits.req_info.s2xlate, noS2xlate)(Seq( 373980ddf4cSpeixiaokun allStage -> onlyStage1, 374980ddf4cSpeixiaokun onlyStage1 -> onlyStage1, 375980ddf4cSpeixiaokun onlyStage2 -> onlyStage2 376980ddf4cSpeixiaokun )) 3776c4dcc2dSLemover val data_resp = DataHoldBypass(l3.io.r.resp.data, stageDelay_valid_1cycle) 3787797f035SbugGenerator val vVec_delay = RegEnable(vVec_req, stageReq.fire) 379d0de7e4aSpeixiaokun val hVec_delay = RegEnable(hVec_req, stageReq.fire) 380d0de7e4aSpeixiaokun val hitVec_delay = VecInit(data_resp.zip(vVec_delay.asBools).zip(hVec_delay).map { case ((wayData, v), h) => 381b188e334Speixiaokun wayData.entries.hit(delay_vpn, io.csr_dup(2).satp.asid, io.csr_dup(2).vsatp.asid, io.csr_dup(2).hgatp.asid, s2xlate = delay_h =/= noS2xlate) && v && (delay_h === h)}) 3826c4dcc2dSLemover 3836c4dcc2dSLemover // check hit and ecc 38482978df9Speixiaokun val check_vpn = stageCheck(0).bits.req_info.vpn 3856c4dcc2dSLemover val ramDatas = RegEnable(data_resp, stageDelay(1).fire) 386935edac4STang Haojin val vVec = RegEnable(vVec_delay, stageDelay(1).fire).asBools 3876c4dcc2dSLemover 3887797f035SbugGenerator val hitVec = RegEnable(hitVec_delay, stageDelay(1).fire) 3897196f5a2SLemover val hitWayEntry = ParallelPriorityMux(hitVec zip ramDatas) 3907196f5a2SLemover val hitWayData = hitWayEntry.entries 3917196f5a2SLemover val hitWayEcc = hitWayEntry.ecc 3926c4dcc2dSLemover val hit = ParallelOR(hitVec) 393f3034303SHaoyuan Feng val hitWay = ParallelPriorityMux(hitVec zip (0 until l2tlbParams.l3nWays).map(_.U(log2Up(l2tlbParams.l3nWays).W))) 3943889e11eSLemover val eccError = hitWayEntry.decode() 3956d5ddbceSLemover 3966c4dcc2dSLemover when (hit && stageCheck_valid_1cycle) { ptwl3replace.access(genPtwL3SetIdx(check_vpn), hitWay) } 3977196f5a2SLemover 3986c4dcc2dSLemover l3AccessPerf.zip(hitVec).map{ case (l, h) => l := h && stageCheck_valid_1cycle } 3996c4dcc2dSLemover XSDebug(stageReq.fire, p"[l3] ridx:0x${Hexadecimal(ridx)}\n") 4005854c1edSLemover for (i <- 0 until l2tlbParams.l3nWays) { 4016c4dcc2dSLemover XSDebug(stageCheck_valid_1cycle, p"[l3] ramDatas(${i.U}) ${ramDatas(i)} l3v:${vVec(i)} hit:${hitVec(i)}\n") 4026d5ddbceSLemover } 4036c4dcc2dSLemover XSDebug(stageCheck_valid_1cycle, p"[l3] l3Hit:${hit} l3HitData:${hitWayData} hitVec:${Binary(hitVec.asUInt)} hitWay:${hitWay} v:${vVec}\n") 4046d5ddbceSLemover 4056d5ddbceSLemover ridx.suggestName(s"l3_ridx") 4066d5ddbceSLemover ramDatas.suggestName(s"l3_ramDatas") 4076d5ddbceSLemover hitVec.suggestName(s"l3_hitVec") 4086d5ddbceSLemover hitWay.suggestName(s"l3_hitWay") 4096d5ddbceSLemover 4103889e11eSLemover (hit, hitWayData, hitWayData.prefetch, eccError) 4116d5ddbceSLemover } 41263632028SHaoyuan Feng val l3HitPPN = l3HitData.ppns 41363632028SHaoyuan Feng val l3HitPerm = l3HitData.perms.getOrElse(0.U.asTypeOf(Vec(PtwL3SectorSize, new PtePermBundle))) 41463632028SHaoyuan Feng val l3HitValid = l3HitData.vs 4156d5ddbceSLemover 4166d5ddbceSLemover // super page 4175854c1edSLemover val spreplace = ReplacementPolicy.fromString(l2tlbParams.spReplacer, l2tlbParams.spSize) 4188d8ac704SLemover val (spHit, spHitData, spPre, spValid) = { 419b188e334Speixiaokun val hitVecT = sp.zipWithIndex.map { case (e, i) => e.hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate) && spv(i) && (sph(i) === h_search) } 4206c4dcc2dSLemover val hitVec = hitVecT.map(RegEnable(_, stageReq.fire)) 4216d5ddbceSLemover val hitData = ParallelPriorityMux(hitVec zip sp) 4226c4dcc2dSLemover val hit = ParallelOR(hitVec) 4236d5ddbceSLemover 4246c4dcc2dSLemover when (hit && stageDelay_valid_1cycle) { spreplace.access(OHToUInt(hitVec)) } 4256d5ddbceSLemover 4266c4dcc2dSLemover spAccessPerf.zip(hitVec).map{ case (s, h) => s := h && stageDelay_valid_1cycle } 4275854c1edSLemover for (i <- 0 until l2tlbParams.spSize) { 428b188e334Speixiaokun XSDebug(stageReq.fire, p"[sp] sp(${i.U}) ${sp(i)} hit:${sp(i).hit(vpn_search, io.csr_dup(0).satp.asid, io.csr_dup(0).vsatp.asid, io.csr_dup(0).hgatp.asid, s2xlate = h_search =/= noS2xlate)} spv:${spv(i)}\n") 4296d5ddbceSLemover } 4306c4dcc2dSLemover XSDebug(stageDelay_valid_1cycle, p"[sp] spHit:${hit} spHitData:${hitData} hitVec:${Binary(VecInit(hitVec).asUInt)}\n") 4316d5ddbceSLemover 4326d5ddbceSLemover VecInit(hitVecT).suggestName(s"sp_hitVecT") 4336d5ddbceSLemover VecInit(hitVec).suggestName(s"sp_hitVec") 4346d5ddbceSLemover 4356c4dcc2dSLemover (RegEnable(hit, stageDelay(1).fire), 4366c4dcc2dSLemover RegEnable(hitData, stageDelay(1).fire), 4376c4dcc2dSLemover RegEnable(hitData.prefetch, stageDelay(1).fire), 438935edac4STang Haojin RegEnable(hitData.v, stageDelay(1).fire)) 4396d5ddbceSLemover } 4406d5ddbceSLemover val spHitPerm = spHitData.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)) 4416d5ddbceSLemover val spHitLevel = spHitData.level.getOrElse(0.U) 4426d5ddbceSLemover 4436c4dcc2dSLemover val check_res = Wire(new PageCacheRespBundle) 4446c4dcc2dSLemover check_res.l1.apply(l1Hit, l1Pre, l1HitPPN) 4456c4dcc2dSLemover check_res.l2.apply(l2Hit, l2Pre, l2HitPPN, ecc = l2eccError) 4461f4a7c0cSLemover check_res.l3.apply(l3Hit, l3Pre, l3HitPPN, l3HitPerm, l3eccError, valid = l3HitValid) 4476c4dcc2dSLemover check_res.sp.apply(spHit, spPre, spHitData.ppn, spHitPerm, false.B, spHitLevel, spValid) 4486d5ddbceSLemover 4496c4dcc2dSLemover val resp_res = Reg(new PageCacheRespBundle) 4506c4dcc2dSLemover when (stageCheck(1).fire) { resp_res := check_res } 4513889e11eSLemover 4521f4a7c0cSLemover // stageResp bypass 4531f4a7c0cSLemover val bypassed = Wire(Vec(3, Bool())) 4541f4a7c0cSLemover bypassed.indices.foreach(i => 4551f4a7c0cSLemover bypassed(i) := stageResp.bits.bypassed(i) || 4566967f5d5Speixiaokun ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate), 4571f4a7c0cSLemover OneCycleValid(stageCheck(1).fire, false.B) || io.refill.valid) 4581f4a7c0cSLemover ) 4591f4a7c0cSLemover 46083d93d53Speixiaokun // stageResp bypass to hptw 46183d93d53Speixiaokun val hptw_bypassed = Wire(Vec(3, Bool())) 46283d93d53Speixiaokun hptw_bypassed.indices.foreach(i => 46383d93d53Speixiaokun hptw_bypassed(i) := stageResp.bits.bypassed(i) || 46483d93d53Speixiaokun ValidHoldBypass(refill_bypass(stageResp.bits.req_info.vpn, i, stageResp.bits.req_info.s2xlate), 46583d93d53Speixiaokun io.resp.fire) 46683d93d53Speixiaokun ) 46783d93d53Speixiaokun 46830104977Speixiaokun val isAllStage = stageResp.bits.req_info.s2xlate === allStage 469c0991f6aSpeixiaokun val isOnlyStage2 = stageResp.bits.req_info.s2xlate === onlyStage2 470980ddf4cSpeixiaokun val stage1Hit = (resp_res.l3.hit || resp_res.sp.hit) && isAllStage 4716c4dcc2dSLemover io.resp.bits.req_info := stageResp.bits.req_info 4726c4dcc2dSLemover io.resp.bits.isFirst := stageResp.bits.isFirst 47330104977Speixiaokun io.resp.bits.hit := (resp_res.l3.hit || resp_res.sp.hit) && !isAllStage 4746967f5d5Speixiaokun io.resp.bits.bypassed := (bypassed(2) || (bypassed(1) && !resp_res.l2.hit) || (bypassed(0) && !resp_res.l1.hit)) && !isAllStage 4756c4dcc2dSLemover io.resp.bits.prefetch := resp_res.l3.pre && resp_res.l3.hit || resp_res.sp.pre && resp_res.sp.hit 476325f0a4eSpeixiaokun io.resp.bits.toFsm.l1Hit := resp_res.l1.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq 477325f0a4eSpeixiaokun io.resp.bits.toFsm.l2Hit := resp_res.l2.hit && !stage1Hit && !isOnlyStage2 && !stageResp.bits.isHptwReq 4786c4dcc2dSLemover io.resp.bits.toFsm.ppn := Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l1.ppn) 479980ddf4cSpeixiaokun io.resp.bits.toFsm.stage1Hit := stage1Hit 480d0de7e4aSpeixiaokun 481325f0a4eSpeixiaokun io.resp.bits.isHptwReq := stageResp.bits.isHptwReq 48283d93d53Speixiaokun io.resp.bits.toHptw.bypassed := (hptw_bypassed(2) || (hptw_bypassed(1) && !resp_res.l2.hit) || (hptw_bypassed(0) && !resp_res.l1.hit)) && stageResp.bits.isHptwReq 483d0de7e4aSpeixiaokun io.resp.bits.toHptw.id := stageResp.bits.hptwId 484325f0a4eSpeixiaokun io.resp.bits.toHptw.l1Hit := resp_res.l1.hit && stageResp.bits.isHptwReq 485325f0a4eSpeixiaokun io.resp.bits.toHptw.l2Hit := resp_res.l2.hit && stageResp.bits.isHptwReq 486d0de7e4aSpeixiaokun io.resp.bits.toHptw.ppn := Mux(resp_res.l2.hit, resp_res.l2.ppn, resp_res.l1.ppn) 48782978df9Speixiaokun val idx = stageResp.bits.req_info.vpn(2, 0) 48882978df9Speixiaokun io.resp.bits.toHptw.resp.entry.tag := stageResp.bits.req_info.vpn 489eb4bf3f2Speixiaokun io.resp.bits.toHptw.resp.entry.asid := DontCare 490d61cd5eeSpeixiaokun io.resp.bits.toHptw.resp.entry.vmid.map(_ := io.csr_dup(0).hgatp.asid) 491d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.entry.level.map(_ := Mux(resp_res.l3.hit, 2.U, resp_res.sp.level)) 492d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.entry.prefetch := from_pre(stageResp.bits.req_info.source) 493d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.entry.ppn := Mux(resp_res.l3.hit, resp_res.l3.ppn(idx), resp_res.sp.ppn) 494d61cd5eeSpeixiaokun io.resp.bits.toHptw.resp.entry.perm.map(_ := Mux(resp_res.l3.hit, resp_res.l3.perm(idx), resp_res.sp.perm)) 495d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.entry.v := Mux(resp_res.l3.hit, resp_res.l3.v(idx), resp_res.sp.v) 496d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.gpf := !io.resp.bits.toHptw.resp.entry.v 497d0de7e4aSpeixiaokun io.resp.bits.toHptw.resp.gaf := false.B 498d0de7e4aSpeixiaokun 49982978df9Speixiaokun io.resp.bits.toTlb.entry.map(_.tag := stageResp.bits.req_info.vpn(vpnLen - 1, 3)) 500eb4bf3f2Speixiaokun io.resp.bits.toTlb.entry.map(_.asid := Mux(stageResp.bits.req_info.hasS2xlate(), io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid)) // DontCare 501eb4bf3f2Speixiaokun io.resp.bits.toTlb.entry.map(_.vmid.map(_ := io.csr_dup(0).hgatp.asid)) 50263632028SHaoyuan Feng io.resp.bits.toTlb.entry.map(_.level.map(_ := Mux(resp_res.l3.hit, 2.U, resp_res.sp.level))) 50363632028SHaoyuan Feng io.resp.bits.toTlb.entry.map(_.prefetch := from_pre(stageResp.bits.req_info.source)) 50463632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 50563632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).ppn := Mux(resp_res.l3.hit, resp_res.l3.ppn(i)(ppnLen - 1, sectortlbwidth), resp_res.sp.ppn(ppnLen - 1, sectortlbwidth)) 50663632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).ppn_low := Mux(resp_res.l3.hit, resp_res.l3.ppn(i)(sectortlbwidth - 1, 0), resp_res.sp.ppn(sectortlbwidth - 1, 0)) 50763632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).perm.map(_ := Mux(resp_res.l3.hit, resp_res.l3.perm(i), resp_res.sp.perm)) 50863632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).v := Mux(resp_res.l3.hit, resp_res.l3.v(i), resp_res.sp.v) 50963632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).pf := !io.resp.bits.toTlb.entry(i).v 51063632028SHaoyuan Feng io.resp.bits.toTlb.entry(i).af := false.B 51163632028SHaoyuan Feng } 51263632028SHaoyuan Feng io.resp.bits.toTlb.pteidx := UIntToOH(stageResp.bits.req_info.vpn(2, 0)).asBools 51363632028SHaoyuan Feng io.resp.bits.toTlb.not_super := Mux(resp_res.l3.hit, true.B, false.B) 5146c4dcc2dSLemover io.resp.valid := stageResp.valid 5156c4dcc2dSLemover XSError(stageResp.valid && resp_res.l3.hit && resp_res.sp.hit, "normal page and super page both hit") 5161f4a7c0cSLemover XSError(stageResp.valid && io.resp.bits.hit && bypassed(2), "page cache, bypassed but hit") 5176d5ddbceSLemover 5186d5ddbceSLemover // refill Perf 5195854c1edSLemover val l1RefillPerf = Wire(Vec(l2tlbParams.l1Size, Bool())) 5205854c1edSLemover val l2RefillPerf = Wire(Vec(l2tlbParams.l2nWays, Bool())) 5215854c1edSLemover val l3RefillPerf = Wire(Vec(l2tlbParams.l3nWays, Bool())) 5225854c1edSLemover val spRefillPerf = Wire(Vec(l2tlbParams.spSize, Bool())) 5236d5ddbceSLemover l1RefillPerf.map(_ := false.B) 5246d5ddbceSLemover l2RefillPerf.map(_ := false.B) 5256d5ddbceSLemover l3RefillPerf.map(_ := false.B) 5266d5ddbceSLemover spRefillPerf.map(_ := false.B) 5276d5ddbceSLemover 5286d5ddbceSLemover // refill 5296d5ddbceSLemover l2.io.w.req <> DontCare 5306d5ddbceSLemover l3.io.w.req <> DontCare 5316d5ddbceSLemover l2.io.w.req.valid := false.B 5326d5ddbceSLemover l3.io.w.req.valid := false.B 5336d5ddbceSLemover 5346d5ddbceSLemover val memRdata = refill.ptes 5355854c1edSLemover val memPtes = (0 until (l2tlbParams.blockBytes/(XLEN/8))).map(i => memRdata((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle)) 5367797f035SbugGenerator val memSelData = io.refill.bits.sel_pte_dup 5377797f035SbugGenerator val memPte = memSelData.map(a => a.asTypeOf(new PteBundle)) 538b848eea5SLemover 5396d5ddbceSLemover // TODO: handle sfenceLatch outsize 5400d94d540SHaoyuan Feng when (!flush_dup(0) && refill.levelOH.l1 && !memPte(0).isLeaf() && !memPte(0).isPf(refill.level_dup(0)) && !memPte(0).isAf()) { 5415854c1edSLemover // val refillIdx = LFSR64()(log2Up(l2tlbParams.l1Size)-1,0) // TODO: may be LRU 5426d5ddbceSLemover val refillIdx = replaceWrapper(l1v, ptwl1replace.way) 5436d5ddbceSLemover refillIdx.suggestName(s"PtwL1RefillIdx") 5446d5ddbceSLemover val rfOH = UIntToOH(refillIdx) 54545f497a4Shappy-lx l1(refillIdx).refill( 54682978df9Speixiaokun refill.req_info_dup(0).vpn, 547980ddf4cSpeixiaokun Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid), 548d0de7e4aSpeixiaokun io.csr_dup(0).hgatp.asid, 5497797f035SbugGenerator memSelData(0), 55045f497a4Shappy-lx 0.U, 5517797f035SbugGenerator refill_prefetch_dup(0) 55245f497a4Shappy-lx ) 5536d5ddbceSLemover ptwl1replace.access(refillIdx) 5546d5ddbceSLemover l1v := l1v | rfOH 5557797f035SbugGenerator l1g := (l1g & ~rfOH) | Mux(memPte(0).perm.g, rfOH, 0.U) 556d0de7e4aSpeixiaokun l1h(refillIdx) := refill.req_info_dup(0).s2xlate 5576d5ddbceSLemover 5585854c1edSLemover for (i <- 0 until l2tlbParams.l1Size) { 5596d5ddbceSLemover l1RefillPerf(i) := i.U === refillIdx 5606d5ddbceSLemover } 5616d5ddbceSLemover 562b188e334Speixiaokun XSDebug(p"[l1 refill] refillIdx:${refillIdx} refillEntry:${l1(refillIdx).genPtwEntry(refill.req_info_dup(0).vpn, Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid), memSelData(0), 0.U, prefetch = refill_prefetch_dup(0))}\n") 5637797f035SbugGenerator XSDebug(p"[l1 refill] l1v:${Binary(l1v)}->${Binary(l1v | rfOH)} l1g:${Binary(l1g)}->${Binary((l1g & ~rfOH) | Mux(memPte(0).perm.g, rfOH, 0.U))}\n") 5646d5ddbceSLemover 5656d5ddbceSLemover refillIdx.suggestName(s"l1_refillIdx") 5666d5ddbceSLemover rfOH.suggestName(s"l1_rfOH") 5676d5ddbceSLemover } 5686d5ddbceSLemover 5690d94d540SHaoyuan Feng when (!flush_dup(1) && refill.levelOH.l2 && !memPte(1).isLeaf() && !memPte(1).isPf(refill.level_dup(1)) && !memPte(1).isAf()) { 5707797f035SbugGenerator val refillIdx = genPtwL2SetIdx(refill.req_info_dup(1).vpn) 5717797f035SbugGenerator val victimWay = replaceWrapper(getl2vSet(refill.req_info_dup(1).vpn), ptwl2replace.way(refillIdx)) 5726d5ddbceSLemover val victimWayOH = UIntToOH(victimWay) 5736d5ddbceSLemover val rfvOH = UIntToOH(Cat(refillIdx, victimWay)) 5747196f5a2SLemover val wdata = Wire(l2EntryType) 5753889e11eSLemover wdata.gen( 57682978df9Speixiaokun vpn = refill.req_info_dup(1).vpn, 577cca17e78Speixiaokun asid = Mux(refill.req_info_dup(1).s2xlate =/= noS2xlate, io.csr_dup(1).vsatp.asid, io.csr_dup(1).satp.asid), 578d0de7e4aSpeixiaokun vmid = io.csr_dup(1).hgatp.asid, 57945f497a4Shappy-lx data = memRdata, 58045f497a4Shappy-lx levelUInt = 1.U, 5817797f035SbugGenerator refill_prefetch_dup(1) 58245f497a4Shappy-lx ) 5836d5ddbceSLemover l2.io.w.apply( 5846d5ddbceSLemover valid = true.B, 5856d5ddbceSLemover setIdx = refillIdx, 5867196f5a2SLemover data = wdata, 5876d5ddbceSLemover waymask = victimWayOH 5886d5ddbceSLemover ) 5896d5ddbceSLemover ptwl2replace.access(refillIdx, victimWay) 5906d5ddbceSLemover l2v := l2v | rfvOH 5916d5ddbceSLemover l2g := l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U) 592980ddf4cSpeixiaokun l2h(refillIdx)(victimWay) := refill.req_info_dup(1).s2xlate 5936d5ddbceSLemover 5945854c1edSLemover for (i <- 0 until l2tlbParams.l2nWays) { 5956d5ddbceSLemover l2RefillPerf(i) := i.U === victimWay 5966d5ddbceSLemover } 5976d5ddbceSLemover 5986d5ddbceSLemover XSDebug(p"[l2 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n") 59945f497a4Shappy-lx XSDebug(p"[l2 refill] refilldata:0x${wdata}\n") 6006d5ddbceSLemover XSDebug(p"[l2 refill] l2v:${Binary(l2v)} -> ${Binary(l2v | rfvOH)}\n") 6016d5ddbceSLemover XSDebug(p"[l2 refill] l2g:${Binary(l2g)} -> ${Binary(l2g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n") 6026d5ddbceSLemover 6036d5ddbceSLemover refillIdx.suggestName(s"l2_refillIdx") 6046d5ddbceSLemover victimWay.suggestName(s"l2_victimWay") 6056d5ddbceSLemover victimWayOH.suggestName(s"l2_victimWayOH") 6066d5ddbceSLemover rfvOH.suggestName(s"l2_rfvOH") 6076d5ddbceSLemover } 6086d5ddbceSLemover 6090d94d540SHaoyuan Feng when (!flush_dup(2) && refill.levelOH.l3 && !memPte(2).isAf()) { 6107797f035SbugGenerator val refillIdx = genPtwL3SetIdx(refill.req_info_dup(2).vpn) 6117797f035SbugGenerator val victimWay = replaceWrapper(getl3vSet(refill.req_info_dup(2).vpn), ptwl3replace.way(refillIdx)) 6126d5ddbceSLemover val victimWayOH = UIntToOH(victimWay) 6136d5ddbceSLemover val rfvOH = UIntToOH(Cat(refillIdx, victimWay)) 6147196f5a2SLemover val wdata = Wire(l3EntryType) 6153889e11eSLemover wdata.gen( 61682978df9Speixiaokun vpn = refill.req_info_dup(2).vpn, 617cca17e78Speixiaokun asid = Mux(refill.req_info_dup(2).s2xlate =/= noS2xlate, io.csr_dup(2).vsatp.asid, io.csr_dup(2).satp.asid), 618d0de7e4aSpeixiaokun vmid = io.csr_dup(2).hgatp.asid, 61945f497a4Shappy-lx data = memRdata, 62045f497a4Shappy-lx levelUInt = 2.U, 6217797f035SbugGenerator refill_prefetch_dup(2) 62245f497a4Shappy-lx ) 6236d5ddbceSLemover l3.io.w.apply( 6246d5ddbceSLemover valid = true.B, 6256d5ddbceSLemover setIdx = refillIdx, 6267196f5a2SLemover data = wdata, 6276d5ddbceSLemover waymask = victimWayOH 6286d5ddbceSLemover ) 6296d5ddbceSLemover ptwl3replace.access(refillIdx, victimWay) 6306d5ddbceSLemover l3v := l3v | rfvOH 6316d5ddbceSLemover l3g := l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U) 632d0de7e4aSpeixiaokun l3h(refillIdx)(victimWay) := refill.req_info_dup(2).s2xlate 6336d5ddbceSLemover 6345854c1edSLemover for (i <- 0 until l2tlbParams.l3nWays) { 6356d5ddbceSLemover l3RefillPerf(i) := i.U === victimWay 6366d5ddbceSLemover } 6376d5ddbceSLemover 6386d5ddbceSLemover XSDebug(p"[l3 refill] refillIdx:0x${Hexadecimal(refillIdx)} victimWay:${victimWay} victimWayOH:${Binary(victimWayOH)} rfvOH(in UInt):${Cat(refillIdx, victimWay)}\n") 63945f497a4Shappy-lx XSDebug(p"[l3 refill] refilldata:0x${wdata}\n") 6406d5ddbceSLemover XSDebug(p"[l3 refill] l3v:${Binary(l3v)} -> ${Binary(l3v | rfvOH)}\n") 6416d5ddbceSLemover XSDebug(p"[l3 refill] l3g:${Binary(l3g)} -> ${Binary(l3g & ~rfvOH | Mux(Cat(memPtes.map(_.perm.g)).andR, rfvOH, 0.U))}\n") 6426d5ddbceSLemover 6436d5ddbceSLemover refillIdx.suggestName(s"l3_refillIdx") 6446d5ddbceSLemover victimWay.suggestName(s"l3_victimWay") 6456d5ddbceSLemover victimWayOH.suggestName(s"l3_victimWayOH") 6466d5ddbceSLemover rfvOH.suggestName(s"l3_rfvOH") 6476d5ddbceSLemover } 6487797f035SbugGenerator 6498d8ac704SLemover 6508d8ac704SLemover // misc entries: super & invalid 6510d94d540SHaoyuan Feng when (!flush_dup(0) && refill.levelOH.sp && (memPte(0).isLeaf() || memPte(0).isPf(refill.level_dup(0))) && !memPte(0).isAf()) { 6525854c1edSLemover val refillIdx = spreplace.way// LFSR64()(log2Up(l2tlbParams.spSize)-1,0) // TODO: may be LRU 6536d5ddbceSLemover val rfOH = UIntToOH(refillIdx) 65445f497a4Shappy-lx sp(refillIdx).refill( 6557797f035SbugGenerator refill.req_info_dup(0).vpn, 656b188e334Speixiaokun Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid), 657d0de7e4aSpeixiaokun io.csr_dup(0).hgatp.asid, 6587797f035SbugGenerator memSelData(0), 6597797f035SbugGenerator refill.level_dup(2), 6607797f035SbugGenerator refill_prefetch_dup(0), 6617797f035SbugGenerator !memPte(0).isPf(refill.level_dup(0)), 66245f497a4Shappy-lx ) 6636d5ddbceSLemover spreplace.access(refillIdx) 6646d5ddbceSLemover spv := spv | rfOH 6657797f035SbugGenerator spg := spg & ~rfOH | Mux(memPte(0).perm.g, rfOH, 0.U) 666d0de7e4aSpeixiaokun sph(refillIdx) := refill.req_info_dup(0).s2xlate 6676d5ddbceSLemover 6685854c1edSLemover for (i <- 0 until l2tlbParams.spSize) { 6696d5ddbceSLemover spRefillPerf(i) := i.U === refillIdx 6706d5ddbceSLemover } 6716d5ddbceSLemover 672b188e334Speixiaokun XSDebug(p"[sp refill] refillIdx:${refillIdx} refillEntry:${sp(refillIdx).genPtwEntry(refill.req_info_dup(0).vpn, Mux(refill.req_info_dup(0).s2xlate =/= noS2xlate, io.csr_dup(0).vsatp.asid, io.csr_dup(0).satp.asid), memSelData(0), refill.level_dup(0), refill_prefetch_dup(0))}\n") 6737797f035SbugGenerator XSDebug(p"[sp refill] spv:${Binary(spv)}->${Binary(spv | rfOH)} spg:${Binary(spg)}->${Binary(spg & ~rfOH | Mux(memPte(0).perm.g, rfOH, 0.U))}\n") 6746d5ddbceSLemover 6756d5ddbceSLemover refillIdx.suggestName(s"sp_refillIdx") 6766d5ddbceSLemover rfOH.suggestName(s"sp_rfOH") 6776d5ddbceSLemover } 6786d5ddbceSLemover 6797797f035SbugGenerator val l2eccFlush = resp_res.l2.ecc && stageResp_valid_1cycle_dup(0) // RegNext(l2eccError, init = false.B) 6807797f035SbugGenerator val l3eccFlush = resp_res.l3.ecc && stageResp_valid_1cycle_dup(1) // RegNext(l3eccError, init = false.B) 6816c4dcc2dSLemover val eccVpn = stageResp.bits.req_info.vpn 6827196f5a2SLemover 6836c4dcc2dSLemover XSError(l2eccFlush, "l2tlb.cache.l2 ecc error. Should not happen at sim stage") 6846c4dcc2dSLemover XSError(l3eccFlush, "l2tlb.cache.l3 ecc error. Should not happen at sim stage") 6857196f5a2SLemover when (l2eccFlush) { 6867196f5a2SLemover val flushSetIdxOH = UIntToOH(genPtwL2SetIdx(eccVpn)) 6877196f5a2SLemover val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l2nWays, a.asUInt) }).asUInt 6887196f5a2SLemover l2v := l2v & ~flushMask 6897196f5a2SLemover l2g := l2g & ~flushMask 6907196f5a2SLemover } 6917196f5a2SLemover 6927196f5a2SLemover when (l3eccFlush) { 6937196f5a2SLemover val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(eccVpn)) 6947196f5a2SLemover val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt 6957196f5a2SLemover l3v := l3v & ~flushMask 6967196f5a2SLemover l3g := l3g & ~flushMask 6977196f5a2SLemover } 6987196f5a2SLemover 699a1d4b4bfSpeixiaokun // sfence for l3 700d0de7e4aSpeixiaokun val sfence_valid_l3 = sfence_dup(3).valid && !sfence_dup(3).bits.hg && !sfence_dup(3).bits.hv 701a1d4b4bfSpeixiaokun when (sfence_valid_l3) { 702a1d4b4bfSpeixiaokun val l3hhit = VecInit(l3h.flatMap(_.map(_ === onlyStage1 && io.csr_dup(0).priv.virt || !io.csr_dup(0).priv.virt))).asUInt 7037797f035SbugGenerator val sfence_vpn = sfence_dup(3).bits.addr(sfence_dup(3).bits.addr.getWidth-1, offLen) 7047797f035SbugGenerator when (sfence_dup(3).bits.rs1/*va*/) { 7057797f035SbugGenerator when (sfence_dup(3).bits.rs2) { 7067797f035SbugGenerator // all va && all asid 707a1d4b4bfSpeixiaokun l3v := l3v & ~l3hhit 7087797f035SbugGenerator } .otherwise { 7097797f035SbugGenerator // all va && specific asid except global 710a1d4b4bfSpeixiaokun l3v := l3v & l3g & ~l3hhit 7117797f035SbugGenerator } 7127797f035SbugGenerator } .otherwise { 7137797f035SbugGenerator // val flushMask = UIntToOH(genTlbL2Idx(sfence.bits.addr(sfence.bits.addr.getWidth-1, offLen))) 7147797f035SbugGenerator val flushSetIdxOH = UIntToOH(genPtwL3SetIdx(sfence_vpn)) 7157797f035SbugGenerator // val flushMask = VecInit(flushSetIdxOH.asBools.map(Fill(l2tlbParams.l3nWays, _.asUInt))).asUInt 7167797f035SbugGenerator val flushMask = VecInit(flushSetIdxOH.asBools.map { a => Fill(l2tlbParams.l3nWays, a.asUInt) }).asUInt 7177797f035SbugGenerator flushSetIdxOH.suggestName(s"sfence_nrs1_flushSetIdxOH") 7187797f035SbugGenerator flushMask.suggestName(s"sfence_nrs1_flushMask") 7197797f035SbugGenerator 7207797f035SbugGenerator when (sfence_dup(3).bits.rs2) { 7217797f035SbugGenerator // specific leaf of addr && all asid 722a1d4b4bfSpeixiaokun l3v := l3v & ~flushMask & ~l3hhit 7237797f035SbugGenerator } .otherwise { 7247797f035SbugGenerator // specific leaf of addr && specific asid 725a1d4b4bfSpeixiaokun l3v := l3v & (~flushMask | l3g | ~l3hhit) 7267797f035SbugGenerator } 7277797f035SbugGenerator } 7287797f035SbugGenerator } 7297797f035SbugGenerator 7305f64f303Speixiaokun // hfencev, simple implementation for l3 731d0de7e4aSpeixiaokun val hfencev_valid_l3 = sfence_dup(3).valid && sfence_dup(3).bits.hv 732a1d4b4bfSpeixiaokun when(hfencev_valid_l3) { 733cca17e78Speixiaokun val flushMask = VecInit(l3h.flatMap(_.map(_ === onlyStage1))).asUInt 734d0de7e4aSpeixiaokun l3v := l3v & ~flushMask // all VS-stage l3 pte 735d0de7e4aSpeixiaokun } 736d0de7e4aSpeixiaokun 737d0de7e4aSpeixiaokun // hfenceg, simple implementation for l3 738d0de7e4aSpeixiaokun val hfenceg_valid_l3 = sfence_dup(3).valid && sfence_dup(3).bits.hg 739d0de7e4aSpeixiaokun when(hfenceg_valid_l3) { 740cca17e78Speixiaokun val flushMask = VecInit(l3h.flatMap(_.map(_ === onlyStage2))).asUInt 741d0de7e4aSpeixiaokun l3v := l3v & ~flushMask // all G-stage l3 pte 742d0de7e4aSpeixiaokun } 743d0de7e4aSpeixiaokun 744d0de7e4aSpeixiaokun 745d0de7e4aSpeixiaokun val l1asidhit = VecInit(l1asids.map(_ === sfence_dup(0).bits.id)).asUInt 746d0de7e4aSpeixiaokun val spasidhit = VecInit(spasids.map(_ === sfence_dup(0).bits.id)).asUInt 747d0de7e4aSpeixiaokun val sfence_valid = sfence_dup(0).valid && !sfence_dup(0).bits.hg && !sfence_dup(0).bits.hv 748d0de7e4aSpeixiaokun when (sfence_valid) { 749cca17e78Speixiaokun val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt 750cca17e78Speixiaokun val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt 751e5da58f0Speixiaokun val l1hhit = VecInit(l1h.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt 752e5da58f0Speixiaokun val sphhit = VecInit(sph.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate}).asUInt 753e5da58f0Speixiaokun val l2hhit = VecInit(l2h.flatMap(_.map{a => io.csr_dup(0).priv.virt && a === onlyStage1 || !io.csr_dup(0).priv.virt && a === noS2xlate})).asUInt 7547797f035SbugGenerator val sfence_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen) 7557797f035SbugGenerator 7567797f035SbugGenerator when (sfence_dup(0).bits.rs1/*va*/) { 7577797f035SbugGenerator when (sfence_dup(0).bits.rs2) { 7586d5ddbceSLemover // all va && all asid 759d0de7e4aSpeixiaokun l2v := l2v & ~l2hhit 7605f64f303Speixiaokun l1v := l1v & ~(l1hhit & VecInit(l1vmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt) 761447c794eSpeixiaokun spv := spv & ~(sphhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt) 7626d5ddbceSLemover } .otherwise { 7636d5ddbceSLemover // all va && specific asid except global 764d0de7e4aSpeixiaokun l2v := l2v & (l2g | ~l2hhit) 7655f64f303Speixiaokun l1v := l1v & ~(~l1g & l1hhit & l1asidhit & VecInit(l1vmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt) 7665f64f303Speixiaokun spv := spv & ~(~spg & sphhit & spasidhit & VecInit(spvmidhit.asBools.map{a => io.csr_dup(0).priv.virt && a || !io.csr_dup(0).priv.virt}).asUInt) 7676d5ddbceSLemover } 7686d5ddbceSLemover } .otherwise { 7697797f035SbugGenerator when (sfence_dup(0).bits.rs2) { 7706d5ddbceSLemover // specific leaf of addr && all asid 771a0c90508Speixiaokun spv := spv & ~(sphhit & VecInit(sp.map(_.hit(sfence_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, ignoreAsid = true, s2xlate = io.csr_dup(0).priv.virt))).asUInt) 7726d5ddbceSLemover } .otherwise { 7736d5ddbceSLemover // specific leaf of addr && specific asid 774a0c90508Speixiaokun spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(sfence_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, s2xlate = io.csr_dup(0).priv.virt))).asUInt) 775d0de7e4aSpeixiaokun } 776d0de7e4aSpeixiaokun } 777d0de7e4aSpeixiaokun } 778d0de7e4aSpeixiaokun 779d0de7e4aSpeixiaokun val hfencev_valid = sfence_dup(0).valid && sfence_dup(0).bits.hv 780d0de7e4aSpeixiaokun when (hfencev_valid) { 781cca17e78Speixiaokun val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt 782cca17e78Speixiaokun val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === io.csr_dup(0).hgatp.asid)).asUInt 783cca17e78Speixiaokun val l1hhit = VecInit(l1h.map(_ === onlyStage1)).asUInt 784cca17e78Speixiaokun val sphhit = VecInit(sph.map(_ === onlyStage1)).asUInt 785cca17e78Speixiaokun val l2hhit = VecInit(l2h.flatMap(_.map(_ === onlyStage1))).asUInt 786d0de7e4aSpeixiaokun val hfencev_vpn = sfence_dup(0).bits.addr(sfence_dup(0).bits.addr.getWidth-1, offLen) 787d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs1) { 788d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs2) { 789d0de7e4aSpeixiaokun l2v := l2v & ~l2hhit 790d0de7e4aSpeixiaokun l1v := l1v & ~(l1hhit & l1vmidhit) 791447c794eSpeixiaokun spv := spv & ~(sphhit & spvmidhit) 792d0de7e4aSpeixiaokun }.otherwise { 793d0de7e4aSpeixiaokun l2v := l2v & (l2g | ~l2hhit) 794d0de7e4aSpeixiaokun l1v := l1v & ~(~l1g & l1hhit & l1asidhit & l1vmidhit) 795d0de7e4aSpeixiaokun spv := spv & ~(~spg & sphhit & spasidhit & spvmidhit) 796d0de7e4aSpeixiaokun } 797d0de7e4aSpeixiaokun }.otherwise { 798d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs2) { 799a0c90508Speixiaokun spv := spv & ~(sphhit & VecInit(sp.map(_.hit(hfencev_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, ignoreAsid = true, s2xlate = true.B))).asUInt) 800d0de7e4aSpeixiaokun }.otherwise { 801a0c90508Speixiaokun spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(hfencev_vpn, sfence_dup(0).bits.id, sfence_dup(0).bits.id, io.csr_dup(0).hgatp.asid, s2xlate = true.B))).asUInt) 802d0de7e4aSpeixiaokun } 803d0de7e4aSpeixiaokun } 804d0de7e4aSpeixiaokun } 805d0de7e4aSpeixiaokun 806d0de7e4aSpeixiaokun 807d0de7e4aSpeixiaokun val hfenceg_valid = sfence_dup(0).valid && sfence_dup(0).bits.hg 808d0de7e4aSpeixiaokun when(hfenceg_valid) { 809cca17e78Speixiaokun val l1vmidhit = VecInit(l1vmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt 810cca17e78Speixiaokun val spvmidhit = VecInit(spvmids.map(_.getOrElse(0.U) === sfence_dup(0).bits.id)).asUInt 811cca17e78Speixiaokun val l1hhit = VecInit(l1h.map(_ === onlyStage2)).asUInt 812cca17e78Speixiaokun val sphhit = VecInit(sph.map(_ === onlyStage2)).asUInt 813cca17e78Speixiaokun val l2hhit = VecInit(l2h.flatMap(_.map(_ === onlyStage2))).asUInt 814887df0f4Speixiaokun val hfenceg_gvpn = (sfence_dup(0).bits.addr << 2)(sfence_dup(0).bits.addr.getWidth - 1, offLen) 815d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs1) { 816d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs2) { 817d0de7e4aSpeixiaokun l2v := l2v & ~l2hhit 818d0de7e4aSpeixiaokun l1v := l1v & ~l1hhit 819d0de7e4aSpeixiaokun spv := spv & ~sphhit 820d0de7e4aSpeixiaokun }.otherwise { 821d0de7e4aSpeixiaokun l2v := l2v & ~l2hhit 822d0de7e4aSpeixiaokun l1v := l1v & ~(l1hhit & l1vmidhit) 823d0de7e4aSpeixiaokun spv := spv & ~(sphhit & spvmidhit) 824d0de7e4aSpeixiaokun } 825d0de7e4aSpeixiaokun }.otherwise { 826d0de7e4aSpeixiaokun when(sfence_dup(0).bits.rs2) { 827a0c90508Speixiaokun spv := spv & ~(sphhit & VecInit(sp.map(_.hit(hfenceg_gvpn, 0.U, 0.U, sfence_dup(0).bits.id, ignoreAsid = true, s2xlate = true.B))).asUInt) 828d0de7e4aSpeixiaokun }.otherwise { 829a0c90508Speixiaokun spv := spv & ~(~spg & sphhit & VecInit(sp.map(_.hit(hfenceg_gvpn, 0.U, 0.U, sfence_dup(0).bits.id, ignoreAsid = true, s2xlate = false.B))).asUInt) 8306d5ddbceSLemover } 8316d5ddbceSLemover } 8326d5ddbceSLemover } 8336d5ddbceSLemover 8347797f035SbugGenerator def InsideStageConnect(in: DecoupledIO[PtwCacheReq], out: DecoupledIO[PtwCacheReq], inFire: Bool): Unit = { 8352c86e165SZhangZifei in.ready := !in.valid || out.ready 8362c86e165SZhangZifei out.valid := in.valid 8372c86e165SZhangZifei out.bits := in.bits 8381f4a7c0cSLemover out.bits.bypassed.zip(in.bits.bypassed).zipWithIndex.map{ case (b, i) => 8397797f035SbugGenerator val bypassed_reg = Reg(Bool()) 840d0de7e4aSpeixiaokun val bypassed_wire = refill_bypass(in.bits.req_info.vpn, i, in.bits.req_info.s2xlate) && io.refill.valid 8417797f035SbugGenerator when (inFire) { bypassed_reg := bypassed_wire } 8427797f035SbugGenerator .elsewhen (io.refill.valid) { bypassed_reg := bypassed_reg || bypassed_wire } 8437797f035SbugGenerator 8447797f035SbugGenerator b._1 := b._2 || (bypassed_wire || (bypassed_reg && !inFire)) 8451f4a7c0cSLemover } 8462c86e165SZhangZifei } 8472c86e165SZhangZifei 8486d5ddbceSLemover // Perf Count 8496c4dcc2dSLemover val resp_l3 = resp_res.l3.hit 8506c4dcc2dSLemover val resp_sp = resp_res.sp.hit 8516c4dcc2dSLemover val resp_l1_pre = resp_res.l1.pre 8526c4dcc2dSLemover val resp_l2_pre = resp_res.l2.pre 8536c4dcc2dSLemover val resp_l3_pre = resp_res.l3.pre 8546c4dcc2dSLemover val resp_sp_pre = resp_res.sp.pre 855935edac4STang Haojin val base_valid_access_0 = !from_pre(io.resp.bits.req_info.source) && io.resp.fire 856bc063562SLemover XSPerfAccumulate("access", base_valid_access_0) 857bc063562SLemover XSPerfAccumulate("l1_hit", base_valid_access_0 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 858bc063562SLemover XSPerfAccumulate("l2_hit", base_valid_access_0 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 859bc063562SLemover XSPerfAccumulate("l3_hit", base_valid_access_0 && resp_l3) 860bc063562SLemover XSPerfAccumulate("sp_hit", base_valid_access_0 && resp_sp) 861bc063562SLemover XSPerfAccumulate("pte_hit",base_valid_access_0 && io.resp.bits.hit) 862bc063562SLemover 863bc063562SLemover 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) 864bc063562SLemover XSPerfAccumulate("l2_hit_pre", base_valid_access_0 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 865bc063562SLemover XSPerfAccumulate("l3_hit_pre", base_valid_access_0 && resp_l3_pre && resp_l3) 866bc063562SLemover XSPerfAccumulate("sp_hit_pre", base_valid_access_0 && resp_sp_pre && resp_sp) 867bc063562SLemover XSPerfAccumulate("pte_hit_pre",base_valid_access_0 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit) 868bc063562SLemover 869935edac4STang Haojin val base_valid_access_1 = from_pre(io.resp.bits.req_info.source) && io.resp.fire 870bc063562SLemover XSPerfAccumulate("pre_access", base_valid_access_1) 871bc063562SLemover XSPerfAccumulate("pre_l1_hit", base_valid_access_1 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 872bc063562SLemover XSPerfAccumulate("pre_l2_hit", base_valid_access_1 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 873bc063562SLemover XSPerfAccumulate("pre_l3_hit", base_valid_access_1 && resp_l3) 874bc063562SLemover XSPerfAccumulate("pre_sp_hit", base_valid_access_1 && resp_sp) 875bc063562SLemover XSPerfAccumulate("pre_pte_hit",base_valid_access_1 && io.resp.bits.hit) 876bc063562SLemover 877bc063562SLemover 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) 878bc063562SLemover XSPerfAccumulate("pre_l2_hit_pre", base_valid_access_1 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 879bc063562SLemover XSPerfAccumulate("pre_l3_hit_pre", base_valid_access_1 && resp_l3_pre && resp_l3) 880bc063562SLemover XSPerfAccumulate("pre_sp_hit_pre", base_valid_access_1 && resp_sp_pre && resp_sp) 881bc063562SLemover XSPerfAccumulate("pre_pte_hit_pre",base_valid_access_1 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit) 882bc063562SLemover 883935edac4STang Haojin val base_valid_access_2 = stageResp.bits.isFirst && !from_pre(io.resp.bits.req_info.source) && io.resp.fire 884bc063562SLemover XSPerfAccumulate("access_first", base_valid_access_2) 885bc063562SLemover XSPerfAccumulate("l1_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 886bc063562SLemover XSPerfAccumulate("l2_hit_first", base_valid_access_2 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 887bc063562SLemover XSPerfAccumulate("l3_hit_first", base_valid_access_2 && resp_l3) 888bc063562SLemover XSPerfAccumulate("sp_hit_first", base_valid_access_2 && resp_sp) 889bc063562SLemover XSPerfAccumulate("pte_hit_first",base_valid_access_2 && io.resp.bits.hit) 890bc063562SLemover 891bc063562SLemover 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) 892bc063562SLemover XSPerfAccumulate("l2_hit_pre_first", base_valid_access_2 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 893bc063562SLemover XSPerfAccumulate("l3_hit_pre_first", base_valid_access_2 && resp_l3_pre && resp_l3) 894bc063562SLemover XSPerfAccumulate("sp_hit_pre_first", base_valid_access_2 && resp_sp_pre && resp_sp) 895bc063562SLemover XSPerfAccumulate("pte_hit_pre_first",base_valid_access_2 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit) 896bc063562SLemover 897935edac4STang Haojin val base_valid_access_3 = stageResp.bits.isFirst && from_pre(io.resp.bits.req_info.source) && io.resp.fire 898bc063562SLemover XSPerfAccumulate("pre_access_first", base_valid_access_3) 899bc063562SLemover XSPerfAccumulate("pre_l1_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l1Hit && !io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 900bc063562SLemover XSPerfAccumulate("pre_l2_hit_first", base_valid_access_3 && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 901bc063562SLemover XSPerfAccumulate("pre_l3_hit_first", base_valid_access_3 && resp_l3) 902bc063562SLemover XSPerfAccumulate("pre_sp_hit_first", base_valid_access_3 && resp_sp) 903bc063562SLemover XSPerfAccumulate("pre_pte_hit_first", base_valid_access_3 && io.resp.bits.hit) 904bc063562SLemover 905bc063562SLemover 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) 906bc063562SLemover XSPerfAccumulate("pre_l2_hit_pre_first", base_valid_access_3 && resp_l2_pre && io.resp.bits.toFsm.l2Hit && !io.resp.bits.hit) 907bc063562SLemover XSPerfAccumulate("pre_l3_hit_pre_first", base_valid_access_3 && resp_l3_pre && resp_l3) 908bc063562SLemover XSPerfAccumulate("pre_sp_hit_pre_first", base_valid_access_3 && resp_sp_pre && resp_sp) 909bc063562SLemover XSPerfAccumulate("pre_pte_hit_pre_first",base_valid_access_3 && (resp_l3_pre && resp_l3 || resp_sp_pre && resp_sp) && io.resp.bits.hit) 910bc063562SLemover 9116d5ddbceSLemover XSPerfAccumulate("rwHarzad", io.req.valid && !io.req.ready) 9126d5ddbceSLemover XSPerfAccumulate("out_blocked", io.resp.valid && !io.resp.ready) 9136d5ddbceSLemover l1AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1AccessIndex${i}", l) } 9146d5ddbceSLemover l2AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2AccessIndex${i}", l) } 9156d5ddbceSLemover l3AccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3AccessIndex${i}", l) } 9166d5ddbceSLemover spAccessPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPAccessIndex${i}", l) } 9176d5ddbceSLemover l1RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L1RefillIndex${i}", l) } 9186d5ddbceSLemover l2RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L2RefillIndex${i}", l) } 9196d5ddbceSLemover l3RefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"L3RefillIndex${i}", l) } 9206d5ddbceSLemover spRefillPerf.zipWithIndex.map{ case (l, i) => XSPerfAccumulate(s"SPRefillIndex${i}", l) } 9216d5ddbceSLemover 922bc063562SLemover XSPerfAccumulate("l1Refill", Cat(l1RefillPerf).orR) 923bc063562SLemover XSPerfAccumulate("l2Refill", Cat(l2RefillPerf).orR) 924bc063562SLemover XSPerfAccumulate("l3Refill", Cat(l3RefillPerf).orR) 925bc063562SLemover XSPerfAccumulate("spRefill", Cat(spRefillPerf).orR) 9267797f035SbugGenerator XSPerfAccumulate("l1Refill_pre", Cat(l1RefillPerf).orR && refill_prefetch_dup(0)) 9277797f035SbugGenerator XSPerfAccumulate("l2Refill_pre", Cat(l2RefillPerf).orR && refill_prefetch_dup(0)) 9287797f035SbugGenerator XSPerfAccumulate("l3Refill_pre", Cat(l3RefillPerf).orR && refill_prefetch_dup(0)) 9297797f035SbugGenerator XSPerfAccumulate("spRefill_pre", Cat(spRefillPerf).orR && refill_prefetch_dup(0)) 930bc063562SLemover 9316d5ddbceSLemover // debug 9327797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] original v and g vector:\n") 9337797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] l1v:${Binary(l1v)}\n") 9347797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] l2v:${Binary(l2v)}\n") 9357797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] l3v:${Binary(l3v)}\n") 9367797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] l3g:${Binary(l3g)}\n") 9377797f035SbugGenerator XSDebug(sfence_dup(0).valid, p"[sfence] spv:${Binary(spv)}\n") 9387797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] new v and g vector:\n") 9397797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l1v:${Binary(l1v)}\n") 9407797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l2v:${Binary(l2v)}\n") 9417797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3v:${Binary(l3v)}\n") 9427797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] l3g:${Binary(l3g)}\n") 9437797f035SbugGenerator XSDebug(RegNext(sfence_dup(0).valid), p"[sfence] spv:${Binary(spv)}\n") 944cd365d4cSrvcoresjw 945cd365d4cSrvcoresjw val perfEvents = Seq( 94656be8e20SYinan Xu ("access ", base_valid_access_0 ), 947cd365d4cSrvcoresjw ("l1_hit ", l1Hit ), 948cd365d4cSrvcoresjw ("l2_hit ", l2Hit ), 949cd365d4cSrvcoresjw ("l3_hit ", l3Hit ), 950cd365d4cSrvcoresjw ("sp_hit ", spHit ), 951cd365d4cSrvcoresjw ("pte_hit ", l3Hit || spHit ), 952cd365d4cSrvcoresjw ("rwHarzad ", io.req.valid && !io.req.ready ), 953cd365d4cSrvcoresjw ("out_blocked ", io.resp.valid && !io.resp.ready), 954cd365d4cSrvcoresjw ) 9551ca0e4f3SYinan Xu generatePerfEvent() 9566d5ddbceSLemover} 957