192e3bfefSLemover/*************************************************************************************** 28882eb68SXin Tian* Copyright (c) 2021-2025 Beijing Institute of Open Source Chip (BOSC) 38882eb68SXin Tian* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 492e3bfefSLemover* Copyright (c) 2020-2021 Peng Cheng Laboratory 58882eb68SXin Tian* Copyright (c) 2024-2025 Institute of Information Engineering, Chinese Academy of Sciences 692e3bfefSLemover* 792e3bfefSLemover* XiangShan is licensed under Mulan PSL v2. 892e3bfefSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 992e3bfefSLemover* You may obtain a copy of Mulan PSL v2 at: 1092e3bfefSLemover* http://license.coscl.org.cn/MulanPSL2 1192e3bfefSLemover* 1292e3bfefSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1392e3bfefSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1492e3bfefSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1592e3bfefSLemover* 1692e3bfefSLemover* See the Mulan PSL v2 for more details. 1792e3bfefSLemover***************************************************************************************/ 1892e3bfefSLemover 1992e3bfefSLemoverpackage xiangshan.cache.mmu 2092e3bfefSLemover 218891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2292e3bfefSLemoverimport chisel3._ 2392e3bfefSLemoverimport chisel3.experimental.ExtModule 2492e3bfefSLemoverimport chisel3.util._ 2592e3bfefSLemoverimport xiangshan._ 2692e3bfefSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 2792e3bfefSLemoverimport utils._ 283c02ee8fSwakafaimport utility._ 2992e3bfefSLemoverimport freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp} 3092e3bfefSLemoverimport freechips.rocketchip.tilelink._ 3192e3bfefSLemoverimport xiangshan.backend.fu.{PMP, PMPChecker, PMPReqBundle, PMPRespBundle} 3292e3bfefSLemoverimport xiangshan.backend.fu.util.HasCSRConst 339c26bab7SHaoyuan Fengimport difftest._ 3492e3bfefSLemover 3592e3bfefSLemoverclass L2TLB()(implicit p: Parameters) extends LazyModule with HasPtwConst { 3695e60e55STang Haojin override def shouldBeInlined: Boolean = false 3792e3bfefSLemover 3892e3bfefSLemover val node = TLClientNode(Seq(TLMasterPortParameters.v1( 3992e3bfefSLemover clients = Seq(TLMasterParameters.v1( 4092e3bfefSLemover "ptw", 4192e3bfefSLemover sourceId = IdRange(0, MemReqWidth) 42d2b20d1aSTang Haojin )), 43d2b20d1aSTang Haojin requestFields = Seq(ReqSourceField()) 4492e3bfefSLemover ))) 4592e3bfefSLemover 4692e3bfefSLemover lazy val module = new L2TLBImp(this) 4792e3bfefSLemover} 4892e3bfefSLemover 4992e3bfefSLemoverclass L2TLBImp(outer: L2TLB)(implicit p: Parameters) extends PtwModule(outer) with HasCSRConst with HasPerfEvents { 5092e3bfefSLemover 5192e3bfefSLemover val (mem, edge) = outer.node.out.head 5292e3bfefSLemover 5392e3bfefSLemover val io = IO(new L2TLBIO) 5492e3bfefSLemover val difftestIO = IO(new Bundle() { 5592e3bfefSLemover val ptwResp = Output(Bool()) 5692e3bfefSLemover val ptwAddr = Output(UInt(64.W)) 5792e3bfefSLemover val ptwData = Output(Vec(4, UInt(64.W))) 5892e3bfefSLemover }) 5992e3bfefSLemover 6092e3bfefSLemover /* Ptw processes multiple requests 6192e3bfefSLemover * Divide Ptw procedure into two stages: cache access ; mem access if cache miss 6292e3bfefSLemover * miss queue itlb dtlb 6392e3bfefSLemover * | | | 6492e3bfefSLemover * ------arbiter------ 6592e3bfefSLemover * | 6692e3bfefSLemover * l1 - l2 - l3 - sp 6792e3bfefSLemover * | 6892e3bfefSLemover * ------------------------------------------- 6992e3bfefSLemover * miss | queue | hit 7092e3bfefSLemover * [][][][][][] | 7192e3bfefSLemover * | | 7292e3bfefSLemover * state machine accessing mem | 7392e3bfefSLemover * | | 7492e3bfefSLemover * ---------------arbiter--------------------- 7592e3bfefSLemover * | | 7692e3bfefSLemover * itlb dtlb 7792e3bfefSLemover */ 7892e3bfefSLemover 7992e3bfefSLemover difftestIO <> DontCare 8092e3bfefSLemover 817797f035SbugGenerator val sfence_tmp = DelayN(io.sfence, 1) 827797f035SbugGenerator val csr_tmp = DelayN(io.csr.tlb, 1) 838882eb68SXin Tian val sfence_dup = Seq.fill(if (HasBitmapCheck) 11 else 9)(RegNext(sfence_tmp)) 848882eb68SXin Tian val csr_dup = Seq.fill(if (HasBitmapCheck) 10 else 8)(RegNext(csr_tmp)) // TODO: add csr_modified? 857797f035SbugGenerator val satp = csr_dup(0).satp 86d0de7e4aSpeixiaokun val vsatp = csr_dup(0).vsatp 87d0de7e4aSpeixiaokun val hgatp = csr_dup(0).hgatp 887797f035SbugGenerator val priv = csr_dup(0).priv 89dd286b6aSYanqin Li val mPBMTE = csr_dup(0).mPBMTE 90dd286b6aSYanqin Li val hPBMTE = csr_dup(0).hPBMTE 91d0de7e4aSpeixiaokun val flush = sfence_dup(0).valid || satp.changed || vsatp.changed || hgatp.changed 9292e3bfefSLemover 9392e3bfefSLemover val pmp = Module(new PMP()) 948882eb68SXin Tian val pmp_check = VecInit(Seq.fill(if (HasBitmapCheck) 4 else 3)(Module(new PMPChecker(lgMaxSize = 3, sameCycle = true)).io)) 9592e3bfefSLemover pmp.io.distribute_csr := io.csr.distribute_csr 968882eb68SXin Tian if (HasBitmapCheck) { 978882eb68SXin Tian pmp_check.foreach(_.check_env.apply(csr_dup(0).mbmc.CMODE.asBool, ModeS, pmp.io.pmp, pmp.io.pma)) 988882eb68SXin Tian } else { 9992e3bfefSLemover pmp_check.foreach(_.check_env.apply(ModeS, pmp.io.pmp, pmp.io.pma)) 1008882eb68SXin Tian } 1018882eb68SXin Tian 1028882eb68SXin Tian // add bitmapcheck 1038882eb68SXin Tian val bitmap = Option.when(HasBitmapCheck)(Module(new Bitmap)) 1048882eb68SXin Tian val bitmapcache = Option.when(HasBitmapCheck)(Module(new BitmapCache)) 1058882eb68SXin Tian 1068882eb68SXin Tian if (HasBitmapCheck) { 1078882eb68SXin Tian bitmap.foreach { Bitmap => 1088882eb68SXin Tian Bitmap.io.csr := csr_dup(8) 1098882eb68SXin Tian Bitmap.io.sfence := sfence_dup(9) 1108882eb68SXin Tian bitmapcache.foreach { BitmapCache => 1118882eb68SXin Tian // connect bitmap and bitmapcache 1128882eb68SXin Tian BitmapCache.io.req <> Bitmap.io.cache.req 1138882eb68SXin Tian Bitmap.io.cache.resp <> BitmapCache.io.resp 1148882eb68SXin Tian BitmapCache.io.refill <> Bitmap.io.refill 1158882eb68SXin Tian BitmapCache.io.csr := csr_dup(9) 1168882eb68SXin Tian BitmapCache.io.sfence := sfence_dup(10) 1178882eb68SXin Tian } 1188882eb68SXin Tian } 1198882eb68SXin Tian } 12092e3bfefSLemover 12192e3bfefSLemover val missQueue = Module(new L2TlbMissQueue) 12292e3bfefSLemover val cache = Module(new PtwCache) 12392e3bfefSLemover val ptw = Module(new PTW) 124d0de7e4aSpeixiaokun val hptw = Module(new HPTW) 12592e3bfefSLemover val llptw = Module(new LLPTW) 1267797f035SbugGenerator val blockmq = Module(new BlockHelper(3)) 12792e3bfefSLemover val arb1 = Module(new Arbiter(new PtwReq, PtwWidth)) 1286967f5d5Speixiaokun val arb2 = Module(new Arbiter(new L2TlbWithHptwIdBundle, ((if (l2tlbParams.enablePrefetch) 4 else 3) + (if(HasHExtension) 1 else 0)))) 129d0de7e4aSpeixiaokun val hptw_req_arb = Module(new Arbiter(new Bundle { 130d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 131eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 13297929664SXiaokun-Pei val gvpn = UInt(gvpnLen.W) 133d0de7e4aSpeixiaokun }, 2)) 134d0de7e4aSpeixiaokun val hptw_resp_arb = Module(new Arbiter(new Bundle { 135d0de7e4aSpeixiaokun val resp = new HptwResp() 136d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 137d0de7e4aSpeixiaokun }, 2)) 138d0de7e4aSpeixiaokun val outArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle { 139d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 140eb4bf3f2Speixiaokun val s1 = new PtwSectorResp () 141eb4bf3f2Speixiaokun val s2 = new HptwResp() 142d0de7e4aSpeixiaokun }, 1)).io) 143d0de7e4aSpeixiaokun val mergeArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle { 144d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 145eb4bf3f2Speixiaokun val s1 = new PtwMergeResp() 146eb4bf3f2Speixiaokun val s2 = new HptwResp() 147d0de7e4aSpeixiaokun }, 3)).io) 14892e3bfefSLemover val outArbCachePort = 0 14992e3bfefSLemover val outArbFsmPort = 1 15092e3bfefSLemover val outArbMqPort = 2 15192e3bfefSLemover 1528882eb68SXin Tian if (HasBitmapCheck) { 1538882eb68SXin Tian // connect ptwcache and bitmap sleep-wakeup port 1548882eb68SXin Tian cache.io.bitmap_wakeup.get <> bitmap.get.io.wakeup 1558882eb68SXin Tian } 1568882eb68SXin Tian 157d0de7e4aSpeixiaokun // hptw arb input port 158d0de7e4aSpeixiaokun val InHptwArbPTWPort = 0 159d0de7e4aSpeixiaokun val InHptwArbLLPTWPort = 1 160d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).valid := ptw.io.hptw.req.valid 161d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.gvpn := ptw.io.hptw.req.bits.gvpn 162d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.id := ptw.io.hptw.req.bits.id 163c3d5cfb3Speixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.source := ptw.io.hptw.req.bits.source 164d0de7e4aSpeixiaokun ptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbPTWPort).ready 165d0de7e4aSpeixiaokun 166d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).valid := llptw.io.hptw.req.valid 167d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.gvpn := llptw.io.hptw.req.bits.gvpn 168d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.id := llptw.io.hptw.req.bits.id 169eb4bf3f2Speixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.source := llptw.io.hptw.req.bits.source 170d0de7e4aSpeixiaokun llptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbLLPTWPort).ready 171d0de7e4aSpeixiaokun 1729c503409SLemover // arb2 input port 1737f6221c5Speixiaokun val InArbHPTWPort = 0 1747f6221c5Speixiaokun val InArbPTWPort = 1 1757f6221c5Speixiaokun val InArbMissQueuePort = 2 1767f6221c5Speixiaokun val InArbTlbPort = 3 1777f6221c5Speixiaokun val InArbPrefetchPort = 4 17892e3bfefSLemover // NOTE: when cache out but miss and ptw doesnt accept, 17992e3bfefSLemover arb1.io.in <> VecInit(io.tlb.map(_.req(0))) 180eb4bf3f2Speixiaokun 181416c2536SHaoyuan Feng val tlbCounter = RegInit(0.U(log2Ceil(MissQueueSize + 1).W)) 182416c2536SHaoyuan Feng val reqVec = WireInit(VecInit(Seq.fill(PtwWidth)(false.B))) 183416c2536SHaoyuan Feng val respVec = WireInit(VecInit(Seq.fill(PtwWidth)(false.B))) 184416c2536SHaoyuan Feng 185416c2536SHaoyuan Feng for (i <- 0 until PtwWidth) { 186416c2536SHaoyuan Feng when (io.tlb(i).req(0).fire) { 187416c2536SHaoyuan Feng reqVec(i) := true.B 188416c2536SHaoyuan Feng } 189416c2536SHaoyuan Feng when (io.tlb(i).resp.fire) { 190416c2536SHaoyuan Feng respVec(i) := true.B 191416c2536SHaoyuan Feng } 192416c2536SHaoyuan Feng } 193416c2536SHaoyuan Feng 194b575c4e8SHaoyuan Feng when (flush) { 195b575c4e8SHaoyuan Feng tlbCounter := 0.U 196b575c4e8SHaoyuan Feng } .otherwise { 197416c2536SHaoyuan Feng tlbCounter := tlbCounter + PopCount(reqVec) - PopCount(respVec) 198b575c4e8SHaoyuan Feng } 199416c2536SHaoyuan Feng XSError(!(tlbCounter >= 0.U && tlbCounter <= MissQueueSize.U), s"l2tlb full!") 20092e3bfefSLemover 2019c503409SLemover arb2.io.in(InArbPTWPort).valid := ptw.io.llptw.valid 2026967f5d5Speixiaokun arb2.io.in(InArbPTWPort).bits.req_info := ptw.io.llptw.bits.req_info 203325f0a4eSpeixiaokun arb2.io.in(InArbPTWPort).bits.isHptwReq := false.B 2047f6221c5Speixiaokun arb2.io.in(InArbPTWPort).bits.isLLptw := false.B 2056967f5d5Speixiaokun arb2.io.in(InArbPTWPort).bits.hptwId := DontCare 2069c503409SLemover ptw.io.llptw.ready := arb2.io.in(InArbPTWPort).ready 20783d93d53Speixiaokun block_decoupled(missQueue.io.out, arb2.io.in(InArbMissQueuePort), Mux(missQueue.io.out.bits.isLLptw, !llptw.io.in.ready, !ptw.io.req.ready)) 2087797f035SbugGenerator 209416c2536SHaoyuan Feng arb2.io.in(InArbTlbPort).valid := arb1.io.out.fire 2106967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.vpn := arb1.io.out.bits.vpn 2116967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.s2xlate := arb1.io.out.bits.s2xlate 2126967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.source := arb1.io.chosen 213325f0a4eSpeixiaokun arb2.io.in(InArbTlbPort).bits.isHptwReq := false.B 2147f6221c5Speixiaokun arb2.io.in(InArbTlbPort).bits.isLLptw := false.B 2156967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.hptwId := DontCare 216416c2536SHaoyuan Feng // 1. arb1 and arb2 are both comb logic, so ready can work just the same cycle 217416c2536SHaoyuan Feng // 2. arb1 can send one req at most in a cycle, so do not need to write 218416c2536SHaoyuan Feng // "tlbCounter <= (MissQueueSize - 2).U" 219416c2536SHaoyuan Feng arb1.io.out.ready := arb2.io.in(InArbTlbPort).ready && tlbCounter < MissQueueSize.U 220d0de7e4aSpeixiaokun 221d0de7e4aSpeixiaokun arb2.io.in(InArbHPTWPort).valid := hptw_req_arb.io.out.valid 2226967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.vpn := hptw_req_arb.io.out.bits.gvpn 2236967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.s2xlate := onlyStage2 2246967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.source := hptw_req_arb.io.out.bits.source 225325f0a4eSpeixiaokun arb2.io.in(InArbHPTWPort).bits.isHptwReq := true.B 2267f6221c5Speixiaokun arb2.io.in(InArbHPTWPort).bits.isLLptw := false.B 2276967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.hptwId := hptw_req_arb.io.out.bits.id 228eb4bf3f2Speixiaokun hptw_req_arb.io.out.ready := arb2.io.in(InArbHPTWPort).ready 229c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 23092e3bfefSLemover if (l2tlbParams.enablePrefetch) { 23192e3bfefSLemover val prefetch = Module(new L2TlbPrefetch()) 23292e3bfefSLemover val recv = cache.io.resp 23392e3bfefSLemover // NOTE: 1. prefetch doesn't gen prefetch 2. req from mq doesn't gen prefetch 23492e3bfefSLemover // NOTE: 1. miss req gen prefetch 2. hit but prefetched gen prefetch 235935edac4STang Haojin prefetch.io.in.valid := recv.fire && !from_pre(recv.bits.req_info.source) && (!recv.bits.hit || 23692e3bfefSLemover recv.bits.prefetch) && recv.bits.isFirst 23792e3bfefSLemover prefetch.io.in.bits.vpn := recv.bits.req_info.vpn 2387797f035SbugGenerator prefetch.io.sfence := sfence_dup(0) 2397797f035SbugGenerator prefetch.io.csr := csr_dup(0) 24092e3bfefSLemover arb2.io.in(InArbPrefetchPort) <> prefetch.io.out 2415afdf73cSHaoyuan Feng 242c686adcdSYinan Xu val isWriteL2TlbPrefetchTable = Constantin.createRecord(s"isWriteL2TlbPrefetchTable$hartId") 243c686adcdSYinan Xu val L2TlbPrefetchTable = ChiselDB.createTable(s"L2TlbPrefetch_hart$hartId", new L2TlbPrefetchDB) 2445afdf73cSHaoyuan Feng val L2TlbPrefetchDB = Wire(new L2TlbPrefetchDB) 2456967f5d5Speixiaokun L2TlbPrefetchDB.vpn := prefetch.io.out.bits.req_info.vpn 246da3bf434SMaxpicca-Li L2TlbPrefetchTable.log(L2TlbPrefetchDB, isWriteL2TlbPrefetchTable.orR && prefetch.io.out.fire, "L2TlbPrefetch", clock, reset) 24792e3bfefSLemover } 24892e3bfefSLemover arb2.io.out.ready := cache.io.req.ready 24992e3bfefSLemover 2508882eb68SXin Tian // Instructs requests from cache need go to LLPTW for processing 2518882eb68SXin Tian val toFsm_toLLPTW = if (HasBitmapCheck) cache.io.resp.bits.toFsm.bitmapCheck.get.toLLPTW else false.B 2527797f035SbugGenerator 2536967f5d5Speixiaokun val mq_arb = Module(new Arbiter(new L2TlbWithHptwIdBundle, 2)) 2547797f035SbugGenerator mq_arb.io.in(0).valid := cache.io.resp.valid && !cache.io.resp.bits.hit && 25583d93d53Speixiaokun !from_pre(cache.io.resp.bits.req_info.source) && !cache.io.resp.bits.isHptwReq && // hptw reqs are not sent to missqueue 256325f0a4eSpeixiaokun (cache.io.resp.bits.bypassed || ( 2578882eb68SXin Tian (((!cache.io.resp.bits.toFsm.l1Hit && !toFsm_toLLPTW) || cache.io.resp.bits.toFsm.stage1Hit) && !cache.io.resp.bits.isHptwReq && (cache.io.resp.bits.isFirst || !ptw.io.req.ready)) // send to ptw, is first or ptw is busy; 2588882eb68SXin Tian || ((cache.io.resp.bits.toFsm.l1Hit || toFsm_toLLPTW) && !llptw.io.in.ready) // send to llptw, llptw is full 259325f0a4eSpeixiaokun )) 260325f0a4eSpeixiaokun 2616967f5d5Speixiaokun mq_arb.io.in(0).bits.req_info := cache.io.resp.bits.req_info 26283d93d53Speixiaokun mq_arb.io.in(0).bits.isHptwReq := false.B 26383d93d53Speixiaokun mq_arb.io.in(0).bits.hptwId := DontCare 2648882eb68SXin Tian mq_arb.io.in(0).bits.isLLptw := cache.io.resp.bits.toFsm.l1Hit || toFsm_toLLPTW 2656967f5d5Speixiaokun mq_arb.io.in(1).bits.req_info := llptw.io.cache.bits 266325f0a4eSpeixiaokun mq_arb.io.in(1).bits.isHptwReq := false.B 2676967f5d5Speixiaokun mq_arb.io.in(1).bits.hptwId := DontCare 2687f6221c5Speixiaokun mq_arb.io.in(1).bits.isLLptw := false.B 2696967f5d5Speixiaokun mq_arb.io.in(1).valid := llptw.io.cache.valid 2706967f5d5Speixiaokun llptw.io.cache.ready := mq_arb.io.in(1).ready 2717797f035SbugGenerator missQueue.io.in <> mq_arb.io.out 2727797f035SbugGenerator missQueue.io.sfence := sfence_dup(6) 2737797f035SbugGenerator missQueue.io.csr := csr_dup(5) 2747797f035SbugGenerator 2757797f035SbugGenerator blockmq.io.start := missQueue.io.out.fire 276935edac4STang Haojin blockmq.io.enable := ptw.io.req.fire 2777797f035SbugGenerator 278d0de7e4aSpeixiaokun llptw.io.in.valid := cache.io.resp.valid && 279d0de7e4aSpeixiaokun !cache.io.resp.bits.hit && 2808882eb68SXin Tian (toFsm_toLLPTW || cache.io.resp.bits.toFsm.l1Hit) && 281d0de7e4aSpeixiaokun !cache.io.resp.bits.bypassed && 282325f0a4eSpeixiaokun !cache.io.resp.bits.isHptwReq 2839c503409SLemover llptw.io.in.bits.req_info := cache.io.resp.bits.req_info 2849c503409SLemover llptw.io.in.bits.ppn := cache.io.resp.bits.toFsm.ppn 2858882eb68SXin Tian if (HasBitmapCheck) { 2868882eb68SXin Tian llptw.io.in.bits.bitmapCheck.get.jmp_bitmap_check := cache.io.resp.bits.toFsm.bitmapCheck.get.jmp_bitmap_check 2878882eb68SXin Tian llptw.io.in.bits.bitmapCheck.get.ptes := cache.io.resp.bits.toFsm.bitmapCheck.get.ptes 2888882eb68SXin Tian llptw.io.in.bits.bitmapCheck.get.cfs := cache.io.resp.bits.toFsm.bitmapCheck.get.cfs 2898882eb68SXin Tian llptw.io.in.bits.bitmapCheck.get.hitway := cache.io.resp.bits.toFsm.bitmapCheck.get.hitway 2908882eb68SXin Tian } 2917797f035SbugGenerator llptw.io.sfence := sfence_dup(1) 2927797f035SbugGenerator llptw.io.csr := csr_dup(1) 2930ede9a33SXiaokun-Pei val llptw_stage1 = Reg(Vec(l2tlbParams.llptwsize, new PtwMergeResp())) 2940ede9a33SXiaokun-Pei when(llptw.io.in.fire){ 2950ede9a33SXiaokun-Pei llptw_stage1(llptw.io.mem.enq_ptr) := cache.io.resp.bits.stage1 2960ede9a33SXiaokun-Pei } 29792e3bfefSLemover 298416c2536SHaoyuan Feng cache.io.req.valid := arb2.io.out.fire 2996967f5d5Speixiaokun cache.io.req.bits.req_info := arb2.io.out.bits.req_info 300325f0a4eSpeixiaokun cache.io.req.bits.isFirst := (arb2.io.chosen =/= InArbMissQueuePort.U && !arb2.io.out.bits.isHptwReq) 301325f0a4eSpeixiaokun cache.io.req.bits.isHptwReq := arb2.io.out.bits.isHptwReq 3026967f5d5Speixiaokun cache.io.req.bits.hptwId := arb2.io.out.bits.hptwId 3031f4a7c0cSLemover cache.io.req.bits.bypassed.map(_ := false.B) 3047797f035SbugGenerator cache.io.sfence := sfence_dup(2) 3057797f035SbugGenerator cache.io.csr := csr_dup(2) 3067797f035SbugGenerator cache.io.sfence_dup.zip(sfence_dup.drop(2).take(4)).map(s => s._1 := s._2) 3077797f035SbugGenerator cache.io.csr_dup.zip(csr_dup.drop(2).take(3)).map(c => c._1 := c._2) 3084c4af37cSpeixiaokun cache.io.resp.ready := MuxCase(mq_arb.io.in(0).ready || ptw.io.req.ready, Seq( 30983d93d53Speixiaokun (!cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq) -> hptw.io.req.ready, 31083d93d53Speixiaokun (cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq) -> hptw_resp_arb.io.in(HptwRespArbCachePort).ready, 3114c4af37cSpeixiaokun cache.io.resp.bits.hit -> outReady(cache.io.resp.bits.req_info.source, outArbCachePort), 3128882eb68SXin Tian ((toFsm_toLLPTW || cache.io.resp.bits.toFsm.l1Hit) && !cache.io.resp.bits.bypassed && llptw.io.in.ready) -> llptw.io.in.ready, 31383d93d53Speixiaokun (cache.io.resp.bits.bypassed || cache.io.resp.bits.isFirst) -> mq_arb.io.in(0).ready 3144c4af37cSpeixiaokun )) 31592e3bfefSLemover 31692e3bfefSLemover // NOTE: missQueue req has higher priority 3173ea4388cSHaoyuan Feng ptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && !cache.io.resp.bits.toFsm.l1Hit && 3187797f035SbugGenerator !cache.io.resp.bits.bypassed && 319d0de7e4aSpeixiaokun !cache.io.resp.bits.isFirst && 3208882eb68SXin Tian !cache.io.resp.bits.isHptwReq && 3218882eb68SXin Tian !toFsm_toLLPTW 32292e3bfefSLemover ptw.io.req.bits.req_info := cache.io.resp.bits.req_info 3233ea4388cSHaoyuan Feng if (EnableSv48) { 3243ea4388cSHaoyuan Feng ptw.io.req.bits.l3Hit.get := cache.io.resp.bits.toFsm.l3Hit.get 3253ea4388cSHaoyuan Feng } 3263ea4388cSHaoyuan Feng ptw.io.req.bits.l2Hit := cache.io.resp.bits.toFsm.l2Hit 32792e3bfefSLemover ptw.io.req.bits.ppn := cache.io.resp.bits.toFsm.ppn 32830104977Speixiaokun ptw.io.req.bits.stage1Hit := cache.io.resp.bits.toFsm.stage1Hit 3296979864eSXiaokun-Pei ptw.io.req.bits.stage1 := cache.io.resp.bits.stage1 3308882eb68SXin Tian if (HasBitmapCheck) { 3318882eb68SXin Tian ptw.io.req.bits.bitmapCheck.get.jmp_bitmap_check := cache.io.resp.bits.toFsm.bitmapCheck.get.jmp_bitmap_check 3328882eb68SXin Tian ptw.io.req.bits.bitmapCheck.get.pte := cache.io.resp.bits.toFsm.bitmapCheck.get.pte 3338882eb68SXin Tian ptw.io.req.bits.bitmapCheck.get.cfs := cache.io.resp.bits.toFsm.bitmapCheck.get.cfs 3348882eb68SXin Tian ptw.io.req.bits.bitmapCheck.get.SPlevel := cache.io.resp.bits.toFsm.bitmapCheck.get.SPlevel 3358882eb68SXin Tian } 3367797f035SbugGenerator ptw.io.sfence := sfence_dup(7) 3377797f035SbugGenerator ptw.io.csr := csr_dup(6) 33892e3bfefSLemover ptw.io.resp.ready := outReady(ptw.io.resp.bits.source, outArbFsmPort) 33992e3bfefSLemover 34083d93d53Speixiaokun hptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq 34182978df9Speixiaokun hptw.io.req.bits.gvpn := cache.io.resp.bits.req_info.vpn 342d0de7e4aSpeixiaokun hptw.io.req.bits.id := cache.io.resp.bits.toHptw.id 343c3d5cfb3Speixiaokun hptw.io.req.bits.source := cache.io.resp.bits.req_info.source 3443ea4388cSHaoyuan Feng if (EnableSv48) { 3453ea4388cSHaoyuan Feng hptw.io.req.bits.l3Hit.get := cache.io.resp.bits.toHptw.l3Hit.get 3463ea4388cSHaoyuan Feng } 347d0de7e4aSpeixiaokun hptw.io.req.bits.l2Hit := cache.io.resp.bits.toHptw.l2Hit 3483ea4388cSHaoyuan Feng hptw.io.req.bits.l1Hit := cache.io.resp.bits.toHptw.l1Hit 349979f601eSpeixiaokun hptw.io.req.bits.ppn := cache.io.resp.bits.toHptw.ppn 35083d93d53Speixiaokun hptw.io.req.bits.bypassed := cache.io.resp.bits.toHptw.bypassed 3518882eb68SXin Tian if (HasBitmapCheck) { 3528882eb68SXin Tian hptw.io.req.bits.bitmapCheck.get <> cache.io.resp.bits.toHptw.bitmapCheck.get 3538882eb68SXin Tian } 354d0de7e4aSpeixiaokun hptw.io.sfence := sfence_dup(8) 355d0de7e4aSpeixiaokun hptw.io.csr := csr_dup(7) 35692e3bfefSLemover // mem req 35792e3bfefSLemover def blockBytes_align(addr: UInt) = { 35892e3bfefSLemover Cat(addr(PAddrBits - 1, log2Up(l2tlbParams.blockBytes)), 0.U(log2Up(l2tlbParams.blockBytes).W)) 35992e3bfefSLemover } 36092e3bfefSLemover def addr_low_from_vpn(vpn: UInt) = { 36192e3bfefSLemover vpn(log2Ceil(l2tlbParams.blockBytes)-log2Ceil(XLEN/8)-1, 0) 36292e3bfefSLemover } 36392e3bfefSLemover def addr_low_from_paddr(paddr: UInt) = { 36492e3bfefSLemover paddr(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 36592e3bfefSLemover } 366d0de7e4aSpeixiaokun def from_llptw(id: UInt) = { 367d0de7e4aSpeixiaokun id < l2tlbParams.llptwsize.U 368d0de7e4aSpeixiaokun } 369d0de7e4aSpeixiaokun def from_ptw(id: UInt) = { 370d0de7e4aSpeixiaokun id === l2tlbParams.llptwsize.U 371d0de7e4aSpeixiaokun } 372d0de7e4aSpeixiaokun def from_hptw(id: UInt) = { 373d0de7e4aSpeixiaokun id === l2tlbParams.llptwsize.U + 1.U 37492e3bfefSLemover } 3758882eb68SXin Tian def from_bitmap(id: UInt) = { 3768882eb68SXin Tian (id > l2tlbParams.llptwsize.U + 1.U) && (id < MemReqWidth.U) 3778882eb68SXin Tian } 37892e3bfefSLemover val waiting_resp = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B))) 37992e3bfefSLemover val flush_latch = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B))) 38083d93d53Speixiaokun val hptw_bypassed = RegInit(false.B) 38192e3bfefSLemover for (i <- waiting_resp.indices) { 38292e3bfefSLemover assert(!flush_latch(i) || waiting_resp(i)) // when sfence_latch wait for mem resp, waiting_resp should be true 38392e3bfefSLemover } 38492e3bfefSLemover 38592e3bfefSLemover val llptw_out = llptw.io.out 38692e3bfefSLemover val llptw_mem = llptw.io.mem 38797929664SXiaokun-Pei llptw_mem.flush_latch := flush_latch.take(l2tlbParams.llptwsize) 38892e3bfefSLemover llptw_mem.req_mask := waiting_resp.take(l2tlbParams.llptwsize) 389d61cd5eeSpeixiaokun ptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize) 390d61cd5eeSpeixiaokun hptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize + 1) 3918882eb68SXin Tian if (HasBitmapCheck) { 3928882eb68SXin Tian bitmap.get.io.mem.req_mask := waiting_resp.slice(MemReqWidth - (l2tlbParams.llptwsize + 2), MemReqWidth) 3938882eb68SXin Tian } 3948882eb68SXin Tian val mem_arb = Module(new Arbiter(new L2TlbMemReqBundle(), if (HasBitmapCheck) 4 else 3)) 39592e3bfefSLemover mem_arb.io.in(0) <> ptw.io.mem.req 39692e3bfefSLemover mem_arb.io.in(1) <> llptw_mem.req 397d0de7e4aSpeixiaokun mem_arb.io.in(2) <> hptw.io.mem.req 3988882eb68SXin Tian if (HasBitmapCheck) { 3998882eb68SXin Tian mem_arb.io.in(3) <> bitmap.get.io.mem.req 4008882eb68SXin Tian } 40192e3bfefSLemover mem_arb.io.out.ready := mem.a.ready && !flush 40292e3bfefSLemover 40327ba10c1SXiaokun-Pei // // assert, should not send mem access at same addr for twice. 40427ba10c1SXiaokun-Pei // val last_resp_vpn = RegEnable(cache.io.refill.bits.req_info_dup(0).vpn, cache.io.refill.valid) 40527ba10c1SXiaokun-Pei // val last_resp_s2xlate = RegEnable(cache.io.refill.bits.req_info_dup(0).s2xlate, cache.io.refill.valid) 40627ba10c1SXiaokun-Pei // val last_resp_level = RegEnable(cache.io.refill.bits.level_dup(0), cache.io.refill.valid) 40727ba10c1SXiaokun-Pei // val last_resp_v = RegInit(false.B) 40827ba10c1SXiaokun-Pei // val last_has_invalid = !Cat(cache.io.refill.bits.ptes.asTypeOf(Vec(blockBits/XLEN, UInt(XLEN.W))).map(a => a(0))).andR || cache.io.refill.bits.sel_pte_dup(0).asTypeOf(new PteBundle).isAf() 40927ba10c1SXiaokun-Pei // when (cache.io.refill.valid) { last_resp_v := !last_has_invalid} 41027ba10c1SXiaokun-Pei // when (flush) { last_resp_v := false.B } 41127ba10c1SXiaokun-Pei // XSError(last_resp_v && cache.io.refill.valid && 41227ba10c1SXiaokun-Pei // (cache.io.refill.bits.req_info_dup(0).vpn === last_resp_vpn) && 41327ba10c1SXiaokun-Pei // (cache.io.refill.bits.level_dup(0) === last_resp_level) && 41427ba10c1SXiaokun-Pei // (cache.io.refill.bits.req_info_dup(0).s2xlate === last_resp_s2xlate), 41527ba10c1SXiaokun-Pei // "l2tlb should not access mem at same addr for twice") 41627ba10c1SXiaokun-Pei // // ATTENTION: this may wrongly assert when: a ptes is l2, last part is valid, 41727ba10c1SXiaokun-Pei // // but the current part is invalid, so one more mem access happened 41827ba10c1SXiaokun-Pei // // If this happened, remove the assert. 4191f4a7c0cSLemover 42092e3bfefSLemover val req_addr_low = Reg(Vec(MemReqWidth, UInt((log2Up(l2tlbParams.blockBytes)-log2Up(XLEN/8)).W))) 42192e3bfefSLemover 422935edac4STang Haojin when (llptw.io.in.fire) { 42392e3bfefSLemover // when enq miss queue, set the req_addr_low to receive the mem resp data part 42492e3bfefSLemover req_addr_low(llptw_mem.enq_ptr) := addr_low_from_vpn(llptw.io.in.bits.req_info.vpn) 42592e3bfefSLemover } 426935edac4STang Haojin when (mem_arb.io.out.fire) { 42792e3bfefSLemover req_addr_low(mem_arb.io.out.bits.id) := addr_low_from_paddr(mem_arb.io.out.bits.addr) 42892e3bfefSLemover waiting_resp(mem_arb.io.out.bits.id) := true.B 42983d93d53Speixiaokun hptw_bypassed := from_hptw(mem_arb.io.out.bits.id) && mem_arb.io.out.bits.hptw_bypassed 43092e3bfefSLemover } 43192e3bfefSLemover // mem read 43292e3bfefSLemover val memRead = edge.Get( 43392e3bfefSLemover fromSource = mem_arb.io.out.bits.id, 43492e3bfefSLemover // toAddress = memAddr(log2Up(CacheLineSize / 2 / 8) - 1, 0), 43592e3bfefSLemover toAddress = blockBytes_align(mem_arb.io.out.bits.addr), 43692e3bfefSLemover lgSize = log2Up(l2tlbParams.blockBytes).U 43792e3bfefSLemover )._2 43892e3bfefSLemover mem.a.bits := memRead 43992e3bfefSLemover mem.a.valid := mem_arb.io.out.valid && !flush 440d2b20d1aSTang Haojin mem.a.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.PTW.id.U) 44192e3bfefSLemover mem.d.ready := true.B 44292e3bfefSLemover // mem -> data buffer 44397929664SXiaokun-Pei val refill_data = RegInit(VecInit.fill(blockBits / l1BusDataWidth)(0.U(l1BusDataWidth.W))) 444935edac4STang Haojin val refill_helper = edge.firstlastHelper(mem.d.bits, mem.d.fire) 44592e3bfefSLemover val mem_resp_done = refill_helper._3 446d0de7e4aSpeixiaokun val mem_resp_from_llptw = from_llptw(mem.d.bits.source) 447d0de7e4aSpeixiaokun val mem_resp_from_ptw = from_ptw(mem.d.bits.source) 448d0de7e4aSpeixiaokun val mem_resp_from_hptw = from_hptw(mem.d.bits.source) 4498882eb68SXin Tian val mem_resp_from_bitmap = from_bitmap(mem.d.bits.source) 45092e3bfefSLemover when (mem.d.valid) { 451d0de7e4aSpeixiaokun assert(mem.d.bits.source < MemReqWidth.U) 45292e3bfefSLemover refill_data(refill_helper._4) := mem.d.bits.data 45392e3bfefSLemover } 4547797f035SbugGenerator // refill_data_tmp is the wire fork of refill_data, but one cycle earlier 4557797f035SbugGenerator val refill_data_tmp = WireInit(refill_data) 4567797f035SbugGenerator refill_data_tmp(refill_helper._4) := mem.d.bits.data 4577797f035SbugGenerator 45892e3bfefSLemover // save only one pte for each id 45992e3bfefSLemover // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache) 4608882eb68SXin Tian val resp_pte = VecInit((0 until (if (HasBitmapCheck) MemReqWidth / 2 else MemReqWidth)).map(i => 46197929664SXiaokun-Pei if (i == l2tlbParams.llptwsize + 1) {RegEnable(get_part(refill_data_tmp, req_addr_low(i)), 0.U.asTypeOf(get_part(refill_data_tmp, req_addr_low(i))), mem_resp_done && mem_resp_from_hptw) } 46297929664SXiaokun-Pei else if (i == l2tlbParams.llptwsize) {RegEnable(get_part(refill_data_tmp, req_addr_low(i)), 0.U.asTypeOf(get_part(refill_data_tmp, req_addr_low(i))), mem_resp_done && mem_resp_from_ptw) } 46397929664SXiaokun-Pei else { Mux(llptw_mem.buffer_it(i), get_part(refill_data, req_addr_low(i)), RegEnable(get_part(refill_data, req_addr_low(i)), 0.U.asTypeOf(get_part(refill_data, req_addr_low(i))), llptw_mem.buffer_it(i))) } 4647797f035SbugGenerator // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle 46592e3bfefSLemover )) 46692e3bfefSLemover 46763632028SHaoyuan Feng // save eight ptes for each id when sector tlb 46863632028SHaoyuan Feng // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache) 4698882eb68SXin Tian val resp_pte_sector = VecInit((0 until (if (HasBitmapCheck) MemReqWidth / 2 else MemReqWidth)).map(i => 47097929664SXiaokun-Pei if (i == l2tlbParams.llptwsize + 1) {RegEnable(refill_data_tmp, 0.U.asTypeOf(refill_data_tmp), mem_resp_done && mem_resp_from_hptw) } 47197929664SXiaokun-Pei else if (i == l2tlbParams.llptwsize) {RegEnable(refill_data_tmp, 0.U.asTypeOf(refill_data_tmp), mem_resp_done && mem_resp_from_ptw) } 47297929664SXiaokun-Pei else { Mux(llptw_mem.buffer_it(i), refill_data, RegEnable(refill_data, 0.U.asTypeOf(refill_data), llptw_mem.buffer_it(i))) } 47363632028SHaoyuan Feng // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle 47463632028SHaoyuan Feng )) 47563632028SHaoyuan Feng 4768882eb68SXin Tian if (HasBitmapCheck) { 4778882eb68SXin Tian // add bitmap arb 4788882eb68SXin Tian bitmap.foreach { Bitmap => 4798882eb68SXin Tian val bitmap_arb = Module(new Arbiter(new bitmapReqBundle(), 3)) 4808882eb68SXin Tian bitmap_arb.io.in(0) <> ptw.io.bitmap.get.req 4818882eb68SXin Tian bitmap_arb.io.in(1) <> llptw.io.bitmap.get.req 4828882eb68SXin Tian bitmap_arb.io.in(2) <> hptw.io.bitmap.get.req 4838882eb68SXin Tian bitmap_arb.io.out.ready := Bitmap.io.req.ready 4848882eb68SXin Tian 4858882eb68SXin Tian Bitmap.io.req <> bitmap_arb.io.out 4868882eb68SXin Tian 4878882eb68SXin Tian // connect bitmap resp to PTW 4888882eb68SXin Tian val bitmapresp_to_llptw = from_llptw(Bitmap.io.resp.bits.id) 4898882eb68SXin Tian val bitmapresp_to_hptw = from_hptw(Bitmap.io.resp.bits.id) 4908882eb68SXin Tian val bitmapresp_to_ptw = from_ptw(Bitmap.io.resp.bits.id) 4918882eb68SXin Tian 4928882eb68SXin Tian Bitmap.io.resp.ready := (llptw.io.bitmap.get.resp.ready && bitmapresp_to_llptw) || (hptw.io.bitmap.get.resp.ready && bitmapresp_to_hptw) || (ptw.io.bitmap.get.resp.ready && bitmapresp_to_ptw) 4938882eb68SXin Tian 4948882eb68SXin Tian // bitmap -> llptw ptw hptw 4958882eb68SXin Tian llptw.io.bitmap.get.resp.valid := Bitmap.io.resp.valid && bitmapresp_to_llptw 4968882eb68SXin Tian hptw.io.bitmap.get.resp.valid := Bitmap.io.resp.valid && bitmapresp_to_hptw 4978882eb68SXin Tian ptw.io.bitmap.get.resp.valid := Bitmap.io.resp.valid && bitmapresp_to_ptw 4988882eb68SXin Tian 4998882eb68SXin Tian // add ptw、hptw、llptw with bitmap resp connect 5008882eb68SXin Tian ptw.io.bitmap.get.resp.bits := Bitmap.io.resp.bits 5018882eb68SXin Tian hptw.io.bitmap.get.resp.bits := Bitmap.io.resp.bits 5028882eb68SXin Tian llptw.io.bitmap.get.resp.bits := Bitmap.io.resp.bits 5038882eb68SXin Tian 5048882eb68SXin Tian // mem -> bitmap 5058882eb68SXin Tian Bitmap.io.mem.resp.valid := mem_resp_done && mem_resp_from_bitmap 5068882eb68SXin Tian Bitmap.io.mem.resp.bits.id := DataHoldBypass(mem.d.bits.source, mem.d.valid) 5078882eb68SXin Tian Bitmap.io.mem.resp.bits.value := DataHoldBypass(refill_data_tmp.asUInt, mem.d.valid) 5088882eb68SXin Tian } 5098882eb68SXin Tian 5108882eb68SXin Tian // ptwcache -> hptw llptw 5118882eb68SXin Tian hptw.io.l0_way_info.get := cache.io.l0_way_info.get 5128882eb68SXin Tian llptw.io.l0_way_info.get := cache.io.l0_way_info.get 5138882eb68SXin Tian } 5148882eb68SXin Tian 515d0de7e4aSpeixiaokun // mem -> llptw 516d0de7e4aSpeixiaokun llptw_mem.resp.valid := mem_resp_done && mem_resp_from_llptw 5177797f035SbugGenerator llptw_mem.resp.bits.id := DataHoldBypass(mem.d.bits.source, mem.d.valid) 518ce5f4200SGuanghui Hu llptw_mem.resp.bits.value := DataHoldBypass(refill_data_tmp.asUInt, mem.d.valid) 51992e3bfefSLemover // mem -> ptw 520d0de7e4aSpeixiaokun ptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_ptw 521d61cd5eeSpeixiaokun ptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize) 522d0de7e4aSpeixiaokun // mem -> hptw 523d0de7e4aSpeixiaokun hptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_hptw 524d61cd5eeSpeixiaokun hptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize + 1) 52592e3bfefSLemover // mem -> cache 526d0de7e4aSpeixiaokun val refill_from_llptw = mem_resp_from_llptw 527d0de7e4aSpeixiaokun val refill_from_ptw = mem_resp_from_ptw 528d0de7e4aSpeixiaokun val refill_from_hptw = mem_resp_from_hptw 5293ea4388cSHaoyuan Feng val refill_level = Mux(refill_from_llptw, 0.U, Mux(refill_from_ptw, RegEnable(ptw.io.refill.level, 0.U, ptw.io.mem.req.fire), RegEnable(hptw.io.refill.level, 0.U, hptw.io.mem.req.fire))) 530591ae1c5SXin Tian val refill_valid = mem_resp_done && (if (HasBitmapCheck) !mem_resp_from_bitmap else true.B) && !flush && !flush_latch(mem.d.bits.source) && !(from_hptw(mem.d.bits.source) && hptw_bypassed) 5317797f035SbugGenerator 5325adc4829SYanqin Li cache.io.refill.valid := GatedValidRegNext(refill_valid, false.B) 53392e3bfefSLemover cache.io.refill.bits.ptes := refill_data.asUInt 534d0de7e4aSpeixiaokun cache.io.refill.bits.req_info_dup.map(_ := RegEnable(Mux(refill_from_llptw, llptw_mem.refill, Mux(refill_from_ptw, ptw.io.refill.req_info, hptw.io.refill.req_info)), refill_valid)) 5357797f035SbugGenerator cache.io.refill.bits.level_dup.map(_ := RegEnable(refill_level, refill_valid)) 5367797f035SbugGenerator cache.io.refill.bits.levelOH(refill_level, refill_valid) 5375adc4829SYanqin Li cache.io.refill.bits.sel_pte_dup.map(_ := RegEnable(sel_data(refill_data_tmp.asUInt, req_addr_low(mem.d.bits.source)), refill_valid)) 53892e3bfefSLemover 5399c26bab7SHaoyuan Feng if (env.EnableDifftest) { 5409c26bab7SHaoyuan Feng val difftest_ptw_addr = RegInit(VecInit(Seq.fill(MemReqWidth)(0.U(PAddrBits.W)))) 5419c26bab7SHaoyuan Feng when (mem.a.valid) { 5429c26bab7SHaoyuan Feng difftest_ptw_addr(mem.a.bits.source) := mem.a.bits.address 5439c26bab7SHaoyuan Feng } 5449c26bab7SHaoyuan Feng 545a0c65233SYinan Xu val difftest = DifftestModule(new DiffRefillEvent, dontCare = true) 546254e4960SHaoyuan Feng difftest.coreid := io.hartId 5477d45a146SYinan Xu difftest.index := 2.U 5487d45a146SYinan Xu difftest.valid := cache.io.refill.valid 5495adc4829SYanqin Li difftest.addr := difftest_ptw_addr(RegEnable(mem.d.bits.source, mem.d.valid)) 5507d45a146SYinan Xu difftest.data := refill_data.asTypeOf(difftest.data) 551935edac4STang Haojin difftest.idtfr := DontCare 5529c26bab7SHaoyuan Feng } 5539c26bab7SHaoyuan Feng 5545ab1b84dSHaoyuan Feng if (env.EnableDifftest) { 5555ab1b84dSHaoyuan Feng for (i <- 0 until PtwWidth) { 5567d45a146SYinan Xu val difftest = DifftestModule(new DiffL2TLBEvent) 557b436d3b6Speixiaokun difftest.coreid := io.hartId 558d61cd5eeSpeixiaokun difftest.valid := io.tlb(i).resp.fire && !io.tlb(i).resp.bits.s1.af && !io.tlb(i).resp.bits.s2.gaf 5597d45a146SYinan Xu difftest.index := i.U 56087d0ba30Speixiaokun difftest.vpn := Cat(io.tlb(i).resp.bits.s1.entry.tag, 0.U(sectortlbwidth.W)) 561002c10a4SYanqin Li difftest.pbmt := io.tlb(i).resp.bits.s1.entry.pbmt 562002c10a4SYanqin Li difftest.g_pbmt := io.tlb(i).resp.bits.s2.entry.pbmt 56363632028SHaoyuan Feng for (j <- 0 until tlbcontiguous) { 564b436d3b6Speixiaokun difftest.ppn(j) := Cat(io.tlb(i).resp.bits.s1.entry.ppn, io.tlb(i).resp.bits.s1.ppn_low(j)) 565b436d3b6Speixiaokun difftest.valididx(j) := io.tlb(i).resp.bits.s1.valididx(j) 56687d0ba30Speixiaokun difftest.pteidx(j) := io.tlb(i).resp.bits.s1.pteidx(j) 56763632028SHaoyuan Feng } 56887d0ba30Speixiaokun difftest.perm := io.tlb(i).resp.bits.s1.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt 56987d0ba30Speixiaokun difftest.level := io.tlb(i).resp.bits.s1.entry.level.getOrElse(0.U.asUInt) 57087d0ba30Speixiaokun difftest.pf := io.tlb(i).resp.bits.s1.pf 57187d0ba30Speixiaokun difftest.satp := Cat(io.csr.tlb.satp.mode, io.csr.tlb.satp.asid, io.csr.tlb.satp.ppn) 57287d0ba30Speixiaokun difftest.vsatp := Cat(io.csr.tlb.vsatp.mode, io.csr.tlb.vsatp.asid, io.csr.tlb.vsatp.ppn) 57397929664SXiaokun-Pei difftest.hgatp := Cat(io.csr.tlb.hgatp.mode, io.csr.tlb.hgatp.vmid, io.csr.tlb.hgatp.ppn) 57487d0ba30Speixiaokun difftest.gvpn := io.tlb(i).resp.bits.s2.entry.tag 57587d0ba30Speixiaokun difftest.g_perm := io.tlb(i).resp.bits.s2.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt 57687d0ba30Speixiaokun difftest.g_level := io.tlb(i).resp.bits.s2.entry.level.getOrElse(0.U.asUInt) 57787d0ba30Speixiaokun difftest.s2ppn := io.tlb(i).resp.bits.s2.entry.ppn 57887d0ba30Speixiaokun difftest.gpf := io.tlb(i).resp.bits.s2.gpf 57987d0ba30Speixiaokun difftest.s2xlate := io.tlb(i).resp.bits.s2xlate 5805ab1b84dSHaoyuan Feng } 5815ab1b84dSHaoyuan Feng } 5825ab1b84dSHaoyuan Feng 58392e3bfefSLemover // pmp 58492e3bfefSLemover pmp_check(0).req <> ptw.io.pmp.req 58592e3bfefSLemover ptw.io.pmp.resp <> pmp_check(0).resp 58692e3bfefSLemover pmp_check(1).req <> llptw.io.pmp.req 58792e3bfefSLemover llptw.io.pmp.resp <> pmp_check(1).resp 588c3d5cfb3Speixiaokun pmp_check(2).req <> hptw.io.pmp.req 589c3d5cfb3Speixiaokun hptw.io.pmp.resp <> pmp_check(2).resp 5908882eb68SXin Tian if (HasBitmapCheck) { 5918882eb68SXin Tian pmp_check(3).req <> bitmap.get.io.pmp.req 5928882eb68SXin Tian bitmap.get.io.pmp.resp <> pmp_check(3).resp 5938882eb68SXin Tian } 59492e3bfefSLemover 59592e3bfefSLemover llptw_out.ready := outReady(llptw_out.bits.req_info.source, outArbMqPort) 59663632028SHaoyuan Feng 597d0de7e4aSpeixiaokun // hptw and page cache -> ptw and llptw 598d0de7e4aSpeixiaokun val HptwRespArbCachePort = 0 599eb4bf3f2Speixiaokun val HptwRespArbHptw = 1 600325f0a4eSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).valid := cache.io.resp.valid && cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq 601d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).bits.id := cache.io.resp.bits.toHptw.id 602d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).bits.resp := cache.io.resp.bits.toHptw.resp 603d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).valid := hptw.io.resp.valid 604d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).bits.id := hptw.io.resp.bits.id 605d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).bits.resp := hptw.io.resp.bits.resp 606c2b430edSpeixiaokun hptw.io.resp.ready := hptw_resp_arb.io.in(HptwRespArbHptw).ready 607d0de7e4aSpeixiaokun 608d0de7e4aSpeixiaokun ptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id === FsmReqID.U 609d0de7e4aSpeixiaokun ptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp 610d0de7e4aSpeixiaokun llptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id =/= FsmReqID.U 611c3d5cfb3Speixiaokun llptw.io.hptw.resp.bits.id := hptw_resp_arb.io.out.bits.id 612d0de7e4aSpeixiaokun llptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp 613c3d5cfb3Speixiaokun hptw_resp_arb.io.out.ready := true.B 614d0de7e4aSpeixiaokun 6158882eb68SXin Tian val cfsValue = Option.when(HasBitmapCheck)(llptw_out.bits.bitmapCheck.get.cfs) 6168882eb68SXin Tian 61763632028SHaoyuan Feng // Timing: Maybe need to do some optimization or even add one more cycle 61892e3bfefSLemover for (i <- 0 until PtwWidth) { 619325f0a4eSpeixiaokun mergeArb(i).in(outArbCachePort).valid := cache.io.resp.valid && cache.io.resp.bits.hit && cache.io.resp.bits.req_info.source===i.U && !cache.io.resp.bits.isHptwReq 620d0de7e4aSpeixiaokun mergeArb(i).in(outArbCachePort).bits.s2xlate := cache.io.resp.bits.req_info.s2xlate 6216979864eSXiaokun-Pei mergeArb(i).in(outArbCachePort).bits.s1 := cache.io.resp.bits.stage1 622eb4bf3f2Speixiaokun mergeArb(i).in(outArbCachePort).bits.s2 := cache.io.resp.bits.toHptw.resp 62363632028SHaoyuan Feng mergeArb(i).in(outArbFsmPort).valid := ptw.io.resp.valid && ptw.io.resp.bits.source===i.U 624d0de7e4aSpeixiaokun mergeArb(i).in(outArbFsmPort).bits.s2xlate := ptw.io.resp.bits.s2xlate 625eb4bf3f2Speixiaokun mergeArb(i).in(outArbFsmPort).bits.s1 := ptw.io.resp.bits.resp 626eb4bf3f2Speixiaokun mergeArb(i).in(outArbFsmPort).bits.s2 := ptw.io.resp.bits.h_resp 62763632028SHaoyuan Feng mergeArb(i).in(outArbMqPort).valid := llptw_out.valid && llptw_out.bits.req_info.source===i.U 628d0de7e4aSpeixiaokun mergeArb(i).in(outArbMqPort).bits.s2xlate := llptw_out.bits.req_info.s2xlate 629dd286b6aSYanqin Li mergeArb(i).in(outArbMqPort).bits.s1 := Mux( 630dd286b6aSYanqin Li llptw_out.bits.first_s2xlate_fault, llptw_stage1(llptw_out.bits.id), 631e5429136SHaoyuan Feng // When G-stage triggers gpf | gaf, `first_s2xlate_fault` will be true 632e5429136SHaoyuan Feng // So Here only including pf | af in Stage1, or Stage1Gpf (gpf = llptw_out.bits.h_resp.gpf) 633dd286b6aSYanqin Li contiguous_pte_to_merge_ptwResp( 6348882eb68SXin Tian if (HasBitmapCheck) Mux(llptw_out.bits.bitmapCheck.get.jmp_bitmap_check, llptw_out.bits.bitmapCheck.get.ptes.asUInt, resp_pte_sector(llptw_out.bits.id).asUInt) else resp_pte_sector(llptw_out.bits.id).asUInt, llptw_out.bits.req_info.vpn, llptw_out.bits.af, 6358882eb68SXin Tian true, s2xlate = llptw_out.bits.req_info.s2xlate, mPBMTE = mPBMTE, hPBMTE = hPBMTE, gpf = llptw_out.bits.h_resp.gpf, 6368882eb68SXin Tian cfs = cfsValue.getOrElse(VecInit(Seq.fill(tlbcontiguous)(false.B))) 637dd286b6aSYanqin Li ) 638dd286b6aSYanqin Li ) 639eb4bf3f2Speixiaokun mergeArb(i).in(outArbMqPort).bits.s2 := llptw_out.bits.h_resp 64063632028SHaoyuan Feng mergeArb(i).out.ready := outArb(i).in(0).ready 64163632028SHaoyuan Feng } 64263632028SHaoyuan Feng 64363632028SHaoyuan Feng for (i <- 0 until PtwWidth) { 64463632028SHaoyuan Feng outArb(i).in(0).valid := mergeArb(i).out.valid 645eb4bf3f2Speixiaokun outArb(i).in(0).bits.s2xlate := mergeArb(i).out.bits.s2xlate 646eb4bf3f2Speixiaokun outArb(i).in(0).bits.s1 := merge_ptwResp_to_sector_ptwResp(mergeArb(i).out.bits.s1) 647eb4bf3f2Speixiaokun outArb(i).in(0).bits.s2 := mergeArb(i).out.bits.s2 64892e3bfefSLemover } 64992e3bfefSLemover 65092e3bfefSLemover // io.tlb.map(_.resp) <> outArb.map(_.out) 65192e3bfefSLemover io.tlb.map(_.resp).zip(outArb.map(_.out)).map{ 65292e3bfefSLemover case (resp, out) => resp <> out 65392e3bfefSLemover } 65492e3bfefSLemover 65592e3bfefSLemover // sfence 65692e3bfefSLemover when (flush) { 65792e3bfefSLemover for (i <- 0 until MemReqWidth) { 65892e3bfefSLemover when (waiting_resp(i)) { 65992e3bfefSLemover flush_latch(i) := true.B 66092e3bfefSLemover } 66192e3bfefSLemover } 66292e3bfefSLemover } 66392e3bfefSLemover // mem -> control signal 66492e3bfefSLemover // waiting_resp and sfence_latch will be reset when mem_resp_done 66592e3bfefSLemover when (mem_resp_done) { 66692e3bfefSLemover waiting_resp(mem.d.bits.source) := false.B 66792e3bfefSLemover flush_latch(mem.d.bits.source) := false.B 66892e3bfefSLemover } 66992e3bfefSLemover 67092e3bfefSLemover def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = { 67192e3bfefSLemover sink.valid := source.valid && !block_signal 67292e3bfefSLemover source.ready := sink.ready && !block_signal 67392e3bfefSLemover sink.bits := source.bits 67492e3bfefSLemover } 67592e3bfefSLemover 67692e3bfefSLemover def get_part(data: Vec[UInt], index: UInt): UInt = { 67792e3bfefSLemover val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W))) 67892e3bfefSLemover inner_data(index) 67992e3bfefSLemover } 68092e3bfefSLemover 68163632028SHaoyuan Feng // not_super means that this is a normal page 68263632028SHaoyuan Feng // valididx(i) will be all true when super page to be convenient for l1 tlb matching 6838882eb68SXin Tian def contiguous_pte_to_merge_ptwResp(pte: UInt, vpn: UInt, af: Bool, af_first: Boolean, s2xlate: UInt, mPBMTE: Bool, hPBMTE: Bool, not_super: Boolean = true, gpf: Bool, cfs : Vec[Bool]) : PtwMergeResp = { 68463632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 68563632028SHaoyuan Feng val ptw_merge_resp = Wire(new PtwMergeResp()) 686eb4bf3f2Speixiaokun val hasS2xlate = s2xlate =/= noS2xlate 687dd286b6aSYanqin Li val pbmte = Mux(s2xlate === onlyStage1 || s2xlate === allStage, hPBMTE, mPBMTE) 68863632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 68963632028SHaoyuan Feng val pte_in = pte(64 * i + 63, 64 * i).asTypeOf(new PteBundle()) 690718a93f5SHaoyuan Feng val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true, hasNapot = true)) 69197929664SXiaokun-Pei ptw_resp.ppn := pte_in.getPPN()(ptePPNLen - 1, sectortlbwidth) 69297929664SXiaokun-Pei ptw_resp.ppn_low := pte_in.getPPN()(sectortlbwidth - 1, 0) 6933ea4388cSHaoyuan Feng ptw_resp.level.map(_ := 0.U) 694002c10a4SYanqin Li ptw_resp.pbmt := pte_in.pbmt 695718a93f5SHaoyuan Feng ptw_resp.n.map(_ := pte_in.n) 69663632028SHaoyuan Feng ptw_resp.perm.map(_ := pte_in.getPerm()) 69763632028SHaoyuan Feng ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 698e5429136SHaoyuan Feng // LLPTW will not handle onlyS2 situations 699e5429136SHaoyuan Feng // noS2xlate. pf: allStagePf; af: af(pmp_af) || pte_in.isAf(ppn_af); gpf: never 700e5429136SHaoyuan Feng // onlyStage1. pf: allStagePf; af: af(pmp_af) || pte_in.isAf(ppn_af); gpf: never 701e5429136SHaoyuan Feng // allStage. pf: allStagePf; af: af(pmp_af) && !gpf; gpf: incoming parameter `gpf` 702e5429136SHaoyuan Feng // priority: af (pmp check) > pf (pte check) > af (pte check) 703e5429136SHaoyuan Feng val isPf = pte_in.isPf(0.U, pbmte) || !pte_in.isLeaf() 704e5429136SHaoyuan Feng val isAf = pte_in.isAf() && (s2xlate === noS2xlate || s2xlate === onlyStage1) && !isPf 705e5429136SHaoyuan Feng ptw_resp.pf := (if (af_first) !af else true.B) && isPf 706e5429136SHaoyuan Feng ptw_resp.af := (if (af_first) true.B else !isPf) && (af || isAf) 7078882eb68SXin Tian ptw_resp.cf := cfs(ptw_resp.ppn(sectortlbwidth - 1, 0)) 70863632028SHaoyuan Feng ptw_resp.v := !ptw_resp.pf 70963632028SHaoyuan Feng ptw_resp.prefetch := DontCare 710eb4bf3f2Speixiaokun ptw_resp.asid := Mux(hasS2xlate, vsatp.asid, satp.asid) 71197929664SXiaokun-Pei ptw_resp.vmid.map(_ := hgatp.vmid) 71263632028SHaoyuan Feng ptw_merge_resp.entry(i) := ptw_resp 71363632028SHaoyuan Feng } 71463632028SHaoyuan Feng ptw_merge_resp.pteidx := UIntToOH(vpn(sectortlbwidth - 1, 0)).asBools 715*39e2cc5bSHaoyuan Feng val napot = ptw_merge_resp.entry(vpn(sectortlbwidth - 1, 0)).n.getOrElse(0.U) 716*39e2cc5bSHaoyuan Feng ptw_merge_resp.not_super := not_super.B && !napot 7176962b4ffSHaoyuan Feng ptw_merge_resp.not_merge := hasS2xlate 71863632028SHaoyuan Feng ptw_merge_resp 71963632028SHaoyuan Feng } 72063632028SHaoyuan Feng 72163632028SHaoyuan Feng def merge_ptwResp_to_sector_ptwResp(pte: PtwMergeResp) : PtwSectorResp = { 72263632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 72363632028SHaoyuan Feng val ptw_sector_resp = Wire(new PtwSectorResp) 72463632028SHaoyuan Feng ptw_sector_resp.entry.tag := pte.entry(OHToUInt(pte.pteidx)).tag 72563632028SHaoyuan Feng ptw_sector_resp.entry.asid := pte.entry(OHToUInt(pte.pteidx)).asid 726c3d5cfb3Speixiaokun ptw_sector_resp.entry.vmid.map(_ := pte.entry(OHToUInt(pte.pteidx)).vmid.getOrElse(0.U)) 72763632028SHaoyuan Feng ptw_sector_resp.entry.ppn := pte.entry(OHToUInt(pte.pteidx)).ppn 728002c10a4SYanqin Li ptw_sector_resp.entry.pbmt := pte.entry(OHToUInt(pte.pteidx)).pbmt 729718a93f5SHaoyuan Feng ptw_sector_resp.entry.n.map(_ := pte.entry(OHToUInt(pte.pteidx)).n.getOrElse(0.U)) 73063632028SHaoyuan Feng ptw_sector_resp.entry.perm.map(_ := pte.entry(OHToUInt(pte.pteidx)).perm.getOrElse(0.U.asTypeOf(new PtePermBundle))) 7313ea4388cSHaoyuan Feng ptw_sector_resp.entry.level.map(_ := pte.entry(OHToUInt(pte.pteidx)).level.getOrElse(0.U(log2Up(Level + 1).W))) 73263632028SHaoyuan Feng ptw_sector_resp.entry.prefetch := pte.entry(OHToUInt(pte.pteidx)).prefetch 73363632028SHaoyuan Feng ptw_sector_resp.entry.v := pte.entry(OHToUInt(pte.pteidx)).v 73463632028SHaoyuan Feng ptw_sector_resp.af := pte.entry(OHToUInt(pte.pteidx)).af 73563632028SHaoyuan Feng ptw_sector_resp.pf := pte.entry(OHToUInt(pte.pteidx)).pf 73663632028SHaoyuan Feng ptw_sector_resp.addr_low := OHToUInt(pte.pteidx) 737b0fa7106SHaoyuan Feng ptw_sector_resp.pteidx := pte.pteidx 73863632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 73963632028SHaoyuan Feng val ppn_equal = pte.entry(i).ppn === pte.entry(OHToUInt(pte.pteidx)).ppn 740002c10a4SYanqin Li val pbmt_equal = pte.entry(i).pbmt === pte.entry(OHToUInt(pte.pteidx)).pbmt 74163632028SHaoyuan Feng val perm_equal = pte.entry(i).perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt === pte.entry(OHToUInt(pte.pteidx)).perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt 74263632028SHaoyuan Feng val v_equal = pte.entry(i).v === pte.entry(OHToUInt(pte.pteidx)).v 74363632028SHaoyuan Feng val af_equal = pte.entry(i).af === pte.entry(OHToUInt(pte.pteidx)).af 74463632028SHaoyuan Feng val pf_equal = pte.entry(i).pf === pte.entry(OHToUInt(pte.pteidx)).pf 7458882eb68SXin Tian val cf_equal = if (HasBitmapCheck) pte.entry(i).cf === pte.entry(OHToUInt(pte.pteidx)).cf else true.B 746*39e2cc5bSHaoyuan Feng ptw_sector_resp.valididx(i) := ((ppn_equal && pbmt_equal && perm_equal && v_equal && af_equal && pf_equal && cf_equal) || !pte.not_super) && !pte.not_merge 74763632028SHaoyuan Feng ptw_sector_resp.ppn_low(i) := pte.entry(i).ppn_low 74863632028SHaoyuan Feng } 74963632028SHaoyuan Feng ptw_sector_resp.valididx(OHToUInt(pte.pteidx)) := true.B 75063632028SHaoyuan Feng ptw_sector_resp 75163632028SHaoyuan Feng } 75263632028SHaoyuan Feng 75392e3bfefSLemover def outReady(source: UInt, port: Int): Bool = { 75445f43e6eSTang Haojin MuxLookup(source, true.B)((0 until PtwWidth).map(i => i.U -> mergeArb(i).in(port).ready)) 75592e3bfefSLemover } 75692e3bfefSLemover 75792e3bfefSLemover // debug info 75892e3bfefSLemover for (i <- 0 until PtwWidth) { 75992e3bfefSLemover XSDebug(p"[io.tlb(${i.U})] ${io.tlb(i)}\n") 76092e3bfefSLemover } 7617797f035SbugGenerator XSDebug(p"[sfence] ${io.sfence}\n") 76292e3bfefSLemover XSDebug(p"[io.csr.tlb] ${io.csr.tlb}\n") 76392e3bfefSLemover 76492e3bfefSLemover for (i <- 0 until PtwWidth) { 765935edac4STang Haojin XSPerfAccumulate(s"req_count${i}", io.tlb(i).req(0).fire) 76692e3bfefSLemover XSPerfAccumulate(s"req_blocked_count_${i}", io.tlb(i).req(0).valid && !io.tlb(i).req(0).ready) 76792e3bfefSLemover } 76892e3bfefSLemover XSPerfAccumulate(s"req_blocked_by_mq", arb1.io.out.valid && missQueue.io.out.valid) 76992e3bfefSLemover for (i <- 0 until (MemReqWidth + 1)) { 77092e3bfefSLemover XSPerfAccumulate(s"mem_req_util${i}", PopCount(waiting_resp) === i.U) 77192e3bfefSLemover } 77292e3bfefSLemover XSPerfAccumulate("mem_cycle", PopCount(waiting_resp) =/= 0.U) 773935edac4STang Haojin XSPerfAccumulate("mem_count", mem.a.fire) 774dd7fe201SHaoyuan Feng for (i <- 0 until PtwWidth) { 775eb4bf3f2Speixiaokun XSPerfAccumulate(s"llptw_ppn_af${i}", mergeArb(i).in(outArbMqPort).valid && mergeArb(i).in(outArbMqPort).bits.s1.entry(OHToUInt(mergeArb(i).in(outArbMqPort).bits.s1.pteidx)).af && !llptw_out.bits.af) 776d61cd5eeSpeixiaokun XSPerfAccumulate(s"access_fault${i}", io.tlb(i).resp.fire && io.tlb(i).resp.bits.s1.af) 777dd7fe201SHaoyuan Feng } 77892e3bfefSLemover 77992e3bfefSLemover // print configs 7803ea4388cSHaoyuan Feng println(s"${l2tlbParams.name}: a ptw, a llptw with size ${l2tlbParams.llptwsize}, miss queue size ${MissQueueSize} l2:${l2tlbParams.l2Size} fa l1: nSets ${l2tlbParams.l1nSets} nWays ${l2tlbParams.l1nWays} l0: ${l2tlbParams.l0nSets} nWays ${l2tlbParams.l0nWays} blockBytes:${l2tlbParams.blockBytes}") 78192e3bfefSLemover 78292e3bfefSLemover val perfEvents = Seq(llptw, cache, ptw).flatMap(_.getPerfEvents) 78392e3bfefSLemover generatePerfEvent() 7845afdf73cSHaoyuan Feng 785c686adcdSYinan Xu val isWriteL1TlbTable = Constantin.createRecord(s"isWriteL1TlbTable$hartId") 786c686adcdSYinan Xu val L1TlbTable = ChiselDB.createTable(s"L1Tlb_hart$hartId", new L1TlbDB) 7875afdf73cSHaoyuan Feng val ITlbReqDB, DTlbReqDB, ITlbRespDB, DTlbRespDB = Wire(new L1TlbDB) 7885afdf73cSHaoyuan Feng ITlbReqDB.vpn := io.tlb(0).req(0).bits.vpn 7895afdf73cSHaoyuan Feng DTlbReqDB.vpn := io.tlb(1).req(0).bits.vpn 790416c2536SHaoyuan Feng ITlbRespDB.vpn := Cat(io.tlb(0).resp.bits.s1.entry.tag, OHToUInt(io.tlb(0).resp.bits.s1.pteidx)) 791416c2536SHaoyuan Feng DTlbRespDB.vpn := Cat(io.tlb(1).resp.bits.s1.entry.tag, OHToUInt(io.tlb(1).resp.bits.s1.pteidx)) 792da3bf434SMaxpicca-Li L1TlbTable.log(ITlbReqDB, isWriteL1TlbTable.orR && io.tlb(0).req(0).fire, "ITlbReq", clock, reset) 793da3bf434SMaxpicca-Li L1TlbTable.log(DTlbReqDB, isWriteL1TlbTable.orR && io.tlb(1).req(0).fire, "DTlbReq", clock, reset) 794da3bf434SMaxpicca-Li L1TlbTable.log(ITlbRespDB, isWriteL1TlbTable.orR && io.tlb(0).resp.fire, "ITlbResp", clock, reset) 795da3bf434SMaxpicca-Li L1TlbTable.log(DTlbRespDB, isWriteL1TlbTable.orR && io.tlb(1).resp.fire, "DTlbResp", clock, reset) 7965afdf73cSHaoyuan Feng 797c686adcdSYinan Xu val isWritePageCacheTable = Constantin.createRecord(s"isWritePageCacheTable$hartId") 798c686adcdSYinan Xu val PageCacheTable = ChiselDB.createTable(s"PageCache_hart$hartId", new PageCacheDB) 7995afdf73cSHaoyuan Feng val PageCacheDB = Wire(new PageCacheDB) 8006979864eSXiaokun-Pei PageCacheDB.vpn := Cat(cache.io.resp.bits.stage1.entry(0).tag, OHToUInt(cache.io.resp.bits.stage1.pteidx)) 8015afdf73cSHaoyuan Feng PageCacheDB.source := cache.io.resp.bits.req_info.source 8025afdf73cSHaoyuan Feng PageCacheDB.bypassed := cache.io.resp.bits.bypassed 8035afdf73cSHaoyuan Feng PageCacheDB.is_first := cache.io.resp.bits.isFirst 8046979864eSXiaokun-Pei PageCacheDB.prefetched := cache.io.resp.bits.stage1.entry(0).prefetch 8055afdf73cSHaoyuan Feng PageCacheDB.prefetch := cache.io.resp.bits.prefetch 8065afdf73cSHaoyuan Feng PageCacheDB.l2Hit := cache.io.resp.bits.toFsm.l2Hit 8075afdf73cSHaoyuan Feng PageCacheDB.l1Hit := cache.io.resp.bits.toFsm.l1Hit 8085afdf73cSHaoyuan Feng PageCacheDB.hit := cache.io.resp.bits.hit 809da3bf434SMaxpicca-Li PageCacheTable.log(PageCacheDB, isWritePageCacheTable.orR && cache.io.resp.fire, "PageCache", clock, reset) 8105afdf73cSHaoyuan Feng 811c686adcdSYinan Xu val isWritePTWTable = Constantin.createRecord(s"isWritePTWTable$hartId") 812c686adcdSYinan Xu val PTWTable = ChiselDB.createTable(s"PTW_hart$hartId", new PTWDB) 8135afdf73cSHaoyuan Feng val PTWReqDB, PTWRespDB, LLPTWReqDB, LLPTWRespDB = Wire(new PTWDB) 8145afdf73cSHaoyuan Feng PTWReqDB.vpn := ptw.io.req.bits.req_info.vpn 8155afdf73cSHaoyuan Feng PTWReqDB.source := ptw.io.req.bits.req_info.source 8165afdf73cSHaoyuan Feng PTWRespDB.vpn := ptw.io.refill.req_info.vpn 8175afdf73cSHaoyuan Feng PTWRespDB.source := ptw.io.refill.req_info.source 8185afdf73cSHaoyuan Feng LLPTWReqDB.vpn := llptw.io.in.bits.req_info.vpn 8195afdf73cSHaoyuan Feng LLPTWReqDB.source := llptw.io.in.bits.req_info.source 8205afdf73cSHaoyuan Feng LLPTWRespDB.vpn := llptw.io.mem.refill.vpn 8215afdf73cSHaoyuan Feng LLPTWRespDB.source := llptw.io.mem.refill.source 822da3bf434SMaxpicca-Li PTWTable.log(PTWReqDB, isWritePTWTable.orR && ptw.io.req.fire, "PTWReq", clock, reset) 823da3bf434SMaxpicca-Li PTWTable.log(PTWRespDB, isWritePTWTable.orR && ptw.io.mem.resp.fire, "PTWResp", clock, reset) 824da3bf434SMaxpicca-Li PTWTable.log(LLPTWReqDB, isWritePTWTable.orR && llptw.io.in.fire, "LLPTWReq", clock, reset) 825da3bf434SMaxpicca-Li PTWTable.log(LLPTWRespDB, isWritePTWTable.orR && llptw.io.mem.resp.fire, "LLPTWResp", clock, reset) 8265afdf73cSHaoyuan Feng 827c686adcdSYinan Xu val isWriteL2TlbMissQueueTable = Constantin.createRecord(s"isWriteL2TlbMissQueueTable$hartId") 828c686adcdSYinan Xu val L2TlbMissQueueTable = ChiselDB.createTable(s"L2TlbMissQueue_hart$hartId", new L2TlbMissQueueDB) 8295afdf73cSHaoyuan Feng val L2TlbMissQueueInDB, L2TlbMissQueueOutDB = Wire(new L2TlbMissQueueDB) 8306967f5d5Speixiaokun L2TlbMissQueueInDB.vpn := missQueue.io.in.bits.req_info.vpn 8316967f5d5Speixiaokun L2TlbMissQueueOutDB.vpn := missQueue.io.out.bits.req_info.vpn 832da3bf434SMaxpicca-Li L2TlbMissQueueTable.log(L2TlbMissQueueInDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.in.fire, "L2TlbMissQueueIn", clock, reset) 833da3bf434SMaxpicca-Li L2TlbMissQueueTable.log(L2TlbMissQueueOutDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.out.fire, "L2TlbMissQueueOut", clock, reset) 83492e3bfefSLemover} 83592e3bfefSLemover 8367797f035SbugGenerator/** BlockHelper, block missqueue, not to send too many req to cache 8377797f035SbugGenerator * Parameter: 8387797f035SbugGenerator * enable: enable BlockHelper, mq should not send too many reqs 8397797f035SbugGenerator * start: when miss queue out fire and need, block miss queue's out 8407797f035SbugGenerator * block: block miss queue's out 8417797f035SbugGenerator * latency: last missqueue out's cache access latency 8427797f035SbugGenerator */ 8437797f035SbugGeneratorclass BlockHelper(latency: Int)(implicit p: Parameters) extends XSModule { 8447797f035SbugGenerator val io = IO(new Bundle { 8457797f035SbugGenerator val enable = Input(Bool()) 8467797f035SbugGenerator val start = Input(Bool()) 8477797f035SbugGenerator val block = Output(Bool()) 8487797f035SbugGenerator }) 8497797f035SbugGenerator 8507797f035SbugGenerator val count = RegInit(0.U(log2Ceil(latency).W)) 8517797f035SbugGenerator val valid = RegInit(false.B) 8527797f035SbugGenerator val work = RegInit(true.B) 8537797f035SbugGenerator 8547797f035SbugGenerator io.block := valid 8557797f035SbugGenerator 8567797f035SbugGenerator when (io.start && work) { valid := true.B } 8577797f035SbugGenerator when (valid) { count := count + 1.U } 8587797f035SbugGenerator when (count === (latency.U) || io.enable) { 8597797f035SbugGenerator valid := false.B 8607797f035SbugGenerator work := io.enable 8617797f035SbugGenerator count := 0.U 8627797f035SbugGenerator } 8637797f035SbugGenerator} 8647797f035SbugGenerator 86592e3bfefSLemoverclass PTEHelper() extends ExtModule { 86692e3bfefSLemover val clock = IO(Input(Clock())) 86792e3bfefSLemover val enable = IO(Input(Bool())) 86892e3bfefSLemover val satp = IO(Input(UInt(64.W))) 86992e3bfefSLemover val vpn = IO(Input(UInt(64.W))) 87092e3bfefSLemover val pte = IO(Output(UInt(64.W))) 87192e3bfefSLemover val level = IO(Output(UInt(8.W))) 87292e3bfefSLemover val pf = IO(Output(UInt(8.W))) 87392e3bfefSLemover} 87492e3bfefSLemover 8755afdf73cSHaoyuan Fengclass PTWDelayN[T <: Data](gen: T, n: Int, flush: Bool) extends Module { 8765afdf73cSHaoyuan Feng val io = IO(new Bundle() { 8775afdf73cSHaoyuan Feng val in = Input(gen) 8785afdf73cSHaoyuan Feng val out = Output(gen) 8795afdf73cSHaoyuan Feng val ptwflush = Input(flush.cloneType) 8805afdf73cSHaoyuan Feng }) 8815afdf73cSHaoyuan Feng val out = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen)))) 8825afdf73cSHaoyuan Feng val t = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen)))) 8835afdf73cSHaoyuan Feng out(0) := io.in 8845afdf73cSHaoyuan Feng if (n == 1) { 8855afdf73cSHaoyuan Feng io.out := out(0) 8865afdf73cSHaoyuan Feng } else { 8875afdf73cSHaoyuan Feng when (io.ptwflush) { 8885afdf73cSHaoyuan Feng for (i <- 0 until n) { 8895afdf73cSHaoyuan Feng t(i) := 0.U.asTypeOf(gen) 8905afdf73cSHaoyuan Feng out(i) := 0.U.asTypeOf(gen) 8915afdf73cSHaoyuan Feng } 8925afdf73cSHaoyuan Feng io.out := 0.U.asTypeOf(gen) 8935afdf73cSHaoyuan Feng } .otherwise { 8945afdf73cSHaoyuan Feng for (i <- 1 until n) { 8955afdf73cSHaoyuan Feng t(i-1) := out(i-1) 8965afdf73cSHaoyuan Feng out(i) := t(i-1) 8975afdf73cSHaoyuan Feng } 8985afdf73cSHaoyuan Feng io.out := out(n-1) 8995afdf73cSHaoyuan Feng } 9005afdf73cSHaoyuan Feng } 9015afdf73cSHaoyuan Feng} 9025afdf73cSHaoyuan Feng 9035afdf73cSHaoyuan Fengobject PTWDelayN { 9045afdf73cSHaoyuan Feng def apply[T <: Data](in: T, n: Int, flush: Bool): T = { 9055afdf73cSHaoyuan Feng val delay = Module(new PTWDelayN(in.cloneType, n, flush)) 9065afdf73cSHaoyuan Feng delay.io.in := in 9075afdf73cSHaoyuan Feng delay.io.ptwflush := flush 9085afdf73cSHaoyuan Feng delay.io.out 9095afdf73cSHaoyuan Feng } 9105afdf73cSHaoyuan Feng} 9115afdf73cSHaoyuan Feng 91292e3bfefSLemoverclass FakePTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 91392e3bfefSLemover val io = IO(new L2TLBIO) 9145afdf73cSHaoyuan Feng val flush = VecInit(Seq.fill(PtwWidth)(false.B)) 9155afdf73cSHaoyuan Feng flush(0) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, itlbParams.fenceDelay) 9165afdf73cSHaoyuan Feng flush(1) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, ldtlbParams.fenceDelay) 91792e3bfefSLemover for (i <- 0 until PtwWidth) { 91892e3bfefSLemover val helper = Module(new PTEHelper()) 91992e3bfefSLemover helper.clock := clock 92092e3bfefSLemover helper.satp := io.csr.tlb.satp.ppn 9215afdf73cSHaoyuan Feng 9225afdf73cSHaoyuan Feng if (coreParams.softPTWDelay == 1) { 9235afdf73cSHaoyuan Feng helper.enable := io.tlb(i).req(0).fire 92492e3bfefSLemover helper.vpn := io.tlb(i).req(0).bits.vpn 9255afdf73cSHaoyuan Feng } else { 9265afdf73cSHaoyuan Feng helper.enable := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay - 1, flush(i)) 9275afdf73cSHaoyuan Feng helper.vpn := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay - 1, flush(i)) 9285afdf73cSHaoyuan Feng } 9295afdf73cSHaoyuan Feng 93092e3bfefSLemover val pte = helper.pte.asTypeOf(new PteBundle) 93192e3bfefSLemover val level = helper.level 93292e3bfefSLemover val pf = helper.pf 9335afdf73cSHaoyuan Feng val empty = RegInit(true.B) 9345afdf73cSHaoyuan Feng when (io.tlb(i).req(0).fire) { 9355afdf73cSHaoyuan Feng empty := false.B 9365afdf73cSHaoyuan Feng } .elsewhen (io.tlb(i).resp.fire || flush(i)) { 9375afdf73cSHaoyuan Feng empty := true.B 9385afdf73cSHaoyuan Feng } 93992e3bfefSLemover 9405afdf73cSHaoyuan Feng io.tlb(i).req(0).ready := empty || io.tlb(i).resp.fire 9415afdf73cSHaoyuan Feng io.tlb(i).resp.valid := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay, flush(i)) 94292e3bfefSLemover assert(!io.tlb(i).resp.valid || io.tlb(i).resp.ready) 943d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.tag := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay, flush(i)) 944002c10a4SYanqin Li io.tlb(i).resp.bits.s1.entry.pbmt := pte.pbmt 945d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.ppn := pte.ppn 946d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.perm.map(_ := pte.getPerm()) 947d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.level.map(_ := level) 948d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.pf := pf 949d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.af := DontCare // TODO: implement it 950d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.v := !pf 951d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.prefetch := DontCare 952d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.asid := io.csr.tlb.satp.asid 95392e3bfefSLemover } 95492e3bfefSLemover} 95592e3bfefSLemover 95692e3bfefSLemoverclass L2TLBWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter { 95795e60e55STang Haojin override def shouldBeInlined: Boolean = false 95892e3bfefSLemover val useSoftPTW = coreParams.softPTW 95992e3bfefSLemover val node = if (!useSoftPTW) TLIdentityNode() else null 96092e3bfefSLemover val ptw = if (!useSoftPTW) LazyModule(new L2TLB()) else null 96192e3bfefSLemover if (!useSoftPTW) { 96292e3bfefSLemover node := ptw.node 96392e3bfefSLemover } 96492e3bfefSLemover 965935edac4STang Haojin class L2TLBWrapperImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) with HasPerfEvents { 96692e3bfefSLemover val io = IO(new L2TLBIO) 96792e3bfefSLemover val perfEvents = if (useSoftPTW) { 96892e3bfefSLemover val fake_ptw = Module(new FakePTW()) 96992e3bfefSLemover io <> fake_ptw.io 97092e3bfefSLemover Seq() 97192e3bfefSLemover } 97292e3bfefSLemover else { 97392e3bfefSLemover io <> ptw.module.io 97492e3bfefSLemover ptw.module.getPerfEvents 97592e3bfefSLemover } 97692e3bfefSLemover generatePerfEvent() 97792e3bfefSLemover } 978935edac4STang Haojin 979935edac4STang Haojin lazy val module = new L2TLBWrapperImp(this) 98092e3bfefSLemover} 981