192e3bfefSLemover/*************************************************************************************** 292e3bfefSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 392e3bfefSLemover* Copyright (c) 2020-2021 Peng Cheng Laboratory 492e3bfefSLemover* 592e3bfefSLemover* XiangShan is licensed under Mulan PSL v2. 692e3bfefSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 792e3bfefSLemover* You may obtain a copy of Mulan PSL v2 at: 892e3bfefSLemover* http://license.coscl.org.cn/MulanPSL2 992e3bfefSLemover* 1092e3bfefSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1192e3bfefSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1292e3bfefSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1392e3bfefSLemover* 1492e3bfefSLemover* See the Mulan PSL v2 for more details. 1592e3bfefSLemover***************************************************************************************/ 1692e3bfefSLemover 1792e3bfefSLemoverpackage xiangshan.cache.mmu 1892e3bfefSLemover 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2092e3bfefSLemoverimport chisel3._ 2192e3bfefSLemoverimport chisel3.experimental.ExtModule 2292e3bfefSLemoverimport chisel3.util._ 2392e3bfefSLemoverimport xiangshan._ 2492e3bfefSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 2592e3bfefSLemoverimport utils._ 263c02ee8fSwakafaimport utility._ 2792e3bfefSLemoverimport freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp} 2892e3bfefSLemoverimport freechips.rocketchip.tilelink._ 2992e3bfefSLemoverimport xiangshan.backend.fu.{PMP, PMPChecker, PMPReqBundle, PMPRespBundle} 3092e3bfefSLemoverimport xiangshan.backend.fu.util.HasCSRConst 319c26bab7SHaoyuan Fengimport difftest._ 3292e3bfefSLemover 3392e3bfefSLemoverclass L2TLB()(implicit p: Parameters) extends LazyModule with HasPtwConst { 3495e60e55STang Haojin override def shouldBeInlined: Boolean = false 3592e3bfefSLemover 3692e3bfefSLemover val node = TLClientNode(Seq(TLMasterPortParameters.v1( 3792e3bfefSLemover clients = Seq(TLMasterParameters.v1( 3892e3bfefSLemover "ptw", 3992e3bfefSLemover sourceId = IdRange(0, MemReqWidth) 40d2b20d1aSTang Haojin )), 41d2b20d1aSTang Haojin requestFields = Seq(ReqSourceField()) 4292e3bfefSLemover ))) 4392e3bfefSLemover 4492e3bfefSLemover lazy val module = new L2TLBImp(this) 4592e3bfefSLemover} 4692e3bfefSLemover 4792e3bfefSLemoverclass L2TLBImp(outer: L2TLB)(implicit p: Parameters) extends PtwModule(outer) with HasCSRConst with HasPerfEvents { 4892e3bfefSLemover 4992e3bfefSLemover val (mem, edge) = outer.node.out.head 5092e3bfefSLemover 5192e3bfefSLemover val io = IO(new L2TLBIO) 5292e3bfefSLemover val difftestIO = IO(new Bundle() { 5392e3bfefSLemover val ptwResp = Output(Bool()) 5492e3bfefSLemover val ptwAddr = Output(UInt(64.W)) 5592e3bfefSLemover val ptwData = Output(Vec(4, UInt(64.W))) 5692e3bfefSLemover }) 5792e3bfefSLemover 5892e3bfefSLemover /* Ptw processes multiple requests 5992e3bfefSLemover * Divide Ptw procedure into two stages: cache access ; mem access if cache miss 6092e3bfefSLemover * miss queue itlb dtlb 6192e3bfefSLemover * | | | 6292e3bfefSLemover * ------arbiter------ 6392e3bfefSLemover * | 6492e3bfefSLemover * l1 - l2 - l3 - sp 6592e3bfefSLemover * | 6692e3bfefSLemover * ------------------------------------------- 6792e3bfefSLemover * miss | queue | hit 6892e3bfefSLemover * [][][][][][] | 6992e3bfefSLemover * | | 7092e3bfefSLemover * state machine accessing mem | 7192e3bfefSLemover * | | 7292e3bfefSLemover * ---------------arbiter--------------------- 7392e3bfefSLemover * | | 7492e3bfefSLemover * itlb dtlb 7592e3bfefSLemover */ 7692e3bfefSLemover 7792e3bfefSLemover difftestIO <> DontCare 7892e3bfefSLemover 797797f035SbugGenerator val sfence_tmp = DelayN(io.sfence, 1) 807797f035SbugGenerator val csr_tmp = DelayN(io.csr.tlb, 1) 81d0de7e4aSpeixiaokun val sfence_dup = Seq.fill(9)(RegNext(sfence_tmp)) 825adc4829SYanqin Li val csr_dup = Seq.fill(8)(RegNext(csr_tmp)) // TODO: add csr_modified? 837797f035SbugGenerator val satp = csr_dup(0).satp 84d0de7e4aSpeixiaokun val vsatp = csr_dup(0).vsatp 85d0de7e4aSpeixiaokun val hgatp = csr_dup(0).hgatp 867797f035SbugGenerator val priv = csr_dup(0).priv 87dd286b6aSYanqin Li val mPBMTE = csr_dup(0).mPBMTE 88dd286b6aSYanqin Li val hPBMTE = csr_dup(0).hPBMTE 89d0de7e4aSpeixiaokun val flush = sfence_dup(0).valid || satp.changed || vsatp.changed || hgatp.changed 9092e3bfefSLemover 9192e3bfefSLemover val pmp = Module(new PMP()) 92c3d5cfb3Speixiaokun val pmp_check = VecInit(Seq.fill(3)(Module(new PMPChecker(lgMaxSize = 3, sameCycle = true)).io)) 9392e3bfefSLemover pmp.io.distribute_csr := io.csr.distribute_csr 9492e3bfefSLemover pmp_check.foreach(_.check_env.apply(ModeS, pmp.io.pmp, pmp.io.pma)) 9592e3bfefSLemover 9692e3bfefSLemover val missQueue = Module(new L2TlbMissQueue) 9792e3bfefSLemover val cache = Module(new PtwCache) 9892e3bfefSLemover val ptw = Module(new PTW) 99d0de7e4aSpeixiaokun val hptw = Module(new HPTW) 10092e3bfefSLemover val llptw = Module(new LLPTW) 1017797f035SbugGenerator val blockmq = Module(new BlockHelper(3)) 10292e3bfefSLemover val arb1 = Module(new Arbiter(new PtwReq, PtwWidth)) 1036967f5d5Speixiaokun val arb2 = Module(new Arbiter(new L2TlbWithHptwIdBundle, ((if (l2tlbParams.enablePrefetch) 4 else 3) + (if(HasHExtension) 1 else 0)))) 104d0de7e4aSpeixiaokun val hptw_req_arb = Module(new Arbiter(new Bundle { 105d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 106eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 10797929664SXiaokun-Pei val gvpn = UInt(gvpnLen.W) 108d0de7e4aSpeixiaokun }, 2)) 109d0de7e4aSpeixiaokun val hptw_resp_arb = Module(new Arbiter(new Bundle { 110d0de7e4aSpeixiaokun val resp = new HptwResp() 111d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 112d0de7e4aSpeixiaokun }, 2)) 113d0de7e4aSpeixiaokun val outArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle { 114d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 115eb4bf3f2Speixiaokun val s1 = new PtwSectorResp () 116eb4bf3f2Speixiaokun val s2 = new HptwResp() 117d0de7e4aSpeixiaokun }, 1)).io) 118d0de7e4aSpeixiaokun val mergeArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle { 119d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 120eb4bf3f2Speixiaokun val s1 = new PtwMergeResp() 121eb4bf3f2Speixiaokun val s2 = new HptwResp() 122d0de7e4aSpeixiaokun }, 3)).io) 12392e3bfefSLemover val outArbCachePort = 0 12492e3bfefSLemover val outArbFsmPort = 1 12592e3bfefSLemover val outArbMqPort = 2 12692e3bfefSLemover 127d0de7e4aSpeixiaokun // hptw arb input port 128d0de7e4aSpeixiaokun val InHptwArbPTWPort = 0 129d0de7e4aSpeixiaokun val InHptwArbLLPTWPort = 1 130d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).valid := ptw.io.hptw.req.valid 131d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.gvpn := ptw.io.hptw.req.bits.gvpn 132d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.id := ptw.io.hptw.req.bits.id 133c3d5cfb3Speixiaokun hptw_req_arb.io.in(InHptwArbPTWPort).bits.source := ptw.io.hptw.req.bits.source 134d0de7e4aSpeixiaokun ptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbPTWPort).ready 135d0de7e4aSpeixiaokun 136d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).valid := llptw.io.hptw.req.valid 137d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.gvpn := llptw.io.hptw.req.bits.gvpn 138d0de7e4aSpeixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.id := llptw.io.hptw.req.bits.id 139eb4bf3f2Speixiaokun hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.source := llptw.io.hptw.req.bits.source 140d0de7e4aSpeixiaokun llptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbLLPTWPort).ready 141d0de7e4aSpeixiaokun 1429c503409SLemover // arb2 input port 1437f6221c5Speixiaokun val InArbHPTWPort = 0 1447f6221c5Speixiaokun val InArbPTWPort = 1 1457f6221c5Speixiaokun val InArbMissQueuePort = 2 1467f6221c5Speixiaokun val InArbTlbPort = 3 1477f6221c5Speixiaokun val InArbPrefetchPort = 4 14892e3bfefSLemover // NOTE: when cache out but miss and ptw doesnt accept, 14992e3bfefSLemover arb1.io.in <> VecInit(io.tlb.map(_.req(0))) 150eb4bf3f2Speixiaokun 151416c2536SHaoyuan Feng val tlbCounter = RegInit(0.U(log2Ceil(MissQueueSize + 1).W)) 152416c2536SHaoyuan Feng val reqVec = WireInit(VecInit(Seq.fill(PtwWidth)(false.B))) 153416c2536SHaoyuan Feng val respVec = WireInit(VecInit(Seq.fill(PtwWidth)(false.B))) 154416c2536SHaoyuan Feng 155416c2536SHaoyuan Feng for (i <- 0 until PtwWidth) { 156416c2536SHaoyuan Feng when (io.tlb(i).req(0).fire) { 157416c2536SHaoyuan Feng reqVec(i) := true.B 158416c2536SHaoyuan Feng } 159416c2536SHaoyuan Feng when (io.tlb(i).resp.fire) { 160416c2536SHaoyuan Feng respVec(i) := true.B 161416c2536SHaoyuan Feng } 162416c2536SHaoyuan Feng } 163416c2536SHaoyuan Feng 164*b575c4e8SHaoyuan Feng when (flush) { 165*b575c4e8SHaoyuan Feng tlbCounter := 0.U 166*b575c4e8SHaoyuan Feng } .otherwise { 167416c2536SHaoyuan Feng tlbCounter := tlbCounter + PopCount(reqVec) - PopCount(respVec) 168*b575c4e8SHaoyuan Feng } 169416c2536SHaoyuan Feng XSError(!(tlbCounter >= 0.U && tlbCounter <= MissQueueSize.U), s"l2tlb full!") 17092e3bfefSLemover 1719c503409SLemover arb2.io.in(InArbPTWPort).valid := ptw.io.llptw.valid 1726967f5d5Speixiaokun arb2.io.in(InArbPTWPort).bits.req_info := ptw.io.llptw.bits.req_info 173325f0a4eSpeixiaokun arb2.io.in(InArbPTWPort).bits.isHptwReq := false.B 1747f6221c5Speixiaokun arb2.io.in(InArbPTWPort).bits.isLLptw := false.B 1756967f5d5Speixiaokun arb2.io.in(InArbPTWPort).bits.hptwId := DontCare 1769c503409SLemover ptw.io.llptw.ready := arb2.io.in(InArbPTWPort).ready 17783d93d53Speixiaokun block_decoupled(missQueue.io.out, arb2.io.in(InArbMissQueuePort), Mux(missQueue.io.out.bits.isLLptw, !llptw.io.in.ready, !ptw.io.req.ready)) 1787797f035SbugGenerator 179416c2536SHaoyuan Feng arb2.io.in(InArbTlbPort).valid := arb1.io.out.fire 1806967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.vpn := arb1.io.out.bits.vpn 1816967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.s2xlate := arb1.io.out.bits.s2xlate 1826967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.req_info.source := arb1.io.chosen 183325f0a4eSpeixiaokun arb2.io.in(InArbTlbPort).bits.isHptwReq := false.B 1847f6221c5Speixiaokun arb2.io.in(InArbTlbPort).bits.isLLptw := false.B 1856967f5d5Speixiaokun arb2.io.in(InArbTlbPort).bits.hptwId := DontCare 186416c2536SHaoyuan Feng // 1. arb1 and arb2 are both comb logic, so ready can work just the same cycle 187416c2536SHaoyuan Feng // 2. arb1 can send one req at most in a cycle, so do not need to write 188416c2536SHaoyuan Feng // "tlbCounter <= (MissQueueSize - 2).U" 189416c2536SHaoyuan Feng arb1.io.out.ready := arb2.io.in(InArbTlbPort).ready && tlbCounter < MissQueueSize.U 190d0de7e4aSpeixiaokun 191d0de7e4aSpeixiaokun arb2.io.in(InArbHPTWPort).valid := hptw_req_arb.io.out.valid 1926967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.vpn := hptw_req_arb.io.out.bits.gvpn 1936967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.s2xlate := onlyStage2 1946967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.req_info.source := hptw_req_arb.io.out.bits.source 195325f0a4eSpeixiaokun arb2.io.in(InArbHPTWPort).bits.isHptwReq := true.B 1967f6221c5Speixiaokun arb2.io.in(InArbHPTWPort).bits.isLLptw := false.B 1976967f5d5Speixiaokun arb2.io.in(InArbHPTWPort).bits.hptwId := hptw_req_arb.io.out.bits.id 198eb4bf3f2Speixiaokun hptw_req_arb.io.out.ready := arb2.io.in(InArbHPTWPort).ready 199c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 20092e3bfefSLemover if (l2tlbParams.enablePrefetch) { 20192e3bfefSLemover val prefetch = Module(new L2TlbPrefetch()) 20292e3bfefSLemover val recv = cache.io.resp 20392e3bfefSLemover // NOTE: 1. prefetch doesn't gen prefetch 2. req from mq doesn't gen prefetch 20492e3bfefSLemover // NOTE: 1. miss req gen prefetch 2. hit but prefetched gen prefetch 205935edac4STang Haojin prefetch.io.in.valid := recv.fire && !from_pre(recv.bits.req_info.source) && (!recv.bits.hit || 20692e3bfefSLemover recv.bits.prefetch) && recv.bits.isFirst 20792e3bfefSLemover prefetch.io.in.bits.vpn := recv.bits.req_info.vpn 2087797f035SbugGenerator prefetch.io.sfence := sfence_dup(0) 2097797f035SbugGenerator prefetch.io.csr := csr_dup(0) 21092e3bfefSLemover arb2.io.in(InArbPrefetchPort) <> prefetch.io.out 2115afdf73cSHaoyuan Feng 212c686adcdSYinan Xu val isWriteL2TlbPrefetchTable = Constantin.createRecord(s"isWriteL2TlbPrefetchTable$hartId") 213c686adcdSYinan Xu val L2TlbPrefetchTable = ChiselDB.createTable(s"L2TlbPrefetch_hart$hartId", new L2TlbPrefetchDB) 2145afdf73cSHaoyuan Feng val L2TlbPrefetchDB = Wire(new L2TlbPrefetchDB) 2156967f5d5Speixiaokun L2TlbPrefetchDB.vpn := prefetch.io.out.bits.req_info.vpn 216da3bf434SMaxpicca-Li L2TlbPrefetchTable.log(L2TlbPrefetchDB, isWriteL2TlbPrefetchTable.orR && prefetch.io.out.fire, "L2TlbPrefetch", clock, reset) 21792e3bfefSLemover } 21892e3bfefSLemover arb2.io.out.ready := cache.io.req.ready 21992e3bfefSLemover 2207797f035SbugGenerator 2216967f5d5Speixiaokun val mq_arb = Module(new Arbiter(new L2TlbWithHptwIdBundle, 2)) 2227797f035SbugGenerator mq_arb.io.in(0).valid := cache.io.resp.valid && !cache.io.resp.bits.hit && 22383d93d53Speixiaokun !from_pre(cache.io.resp.bits.req_info.source) && !cache.io.resp.bits.isHptwReq && // hptw reqs are not sent to missqueue 224325f0a4eSpeixiaokun (cache.io.resp.bits.bypassed || ( 2253ea4388cSHaoyuan Feng ((!cache.io.resp.bits.toFsm.l1Hit || 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; 2263ea4388cSHaoyuan Feng || (cache.io.resp.bits.toFsm.l1Hit && !llptw.io.in.ready) // send to llptw, llptw is full 227325f0a4eSpeixiaokun )) 228325f0a4eSpeixiaokun 2296967f5d5Speixiaokun mq_arb.io.in(0).bits.req_info := cache.io.resp.bits.req_info 23083d93d53Speixiaokun mq_arb.io.in(0).bits.isHptwReq := false.B 23183d93d53Speixiaokun mq_arb.io.in(0).bits.hptwId := DontCare 2323ea4388cSHaoyuan Feng mq_arb.io.in(0).bits.isLLptw := cache.io.resp.bits.toFsm.l1Hit 2336967f5d5Speixiaokun mq_arb.io.in(1).bits.req_info := llptw.io.cache.bits 234325f0a4eSpeixiaokun mq_arb.io.in(1).bits.isHptwReq := false.B 2356967f5d5Speixiaokun mq_arb.io.in(1).bits.hptwId := DontCare 2367f6221c5Speixiaokun mq_arb.io.in(1).bits.isLLptw := false.B 2376967f5d5Speixiaokun mq_arb.io.in(1).valid := llptw.io.cache.valid 2386967f5d5Speixiaokun llptw.io.cache.ready := mq_arb.io.in(1).ready 2397797f035SbugGenerator missQueue.io.in <> mq_arb.io.out 2407797f035SbugGenerator missQueue.io.sfence := sfence_dup(6) 2417797f035SbugGenerator missQueue.io.csr := csr_dup(5) 2427797f035SbugGenerator 2437797f035SbugGenerator blockmq.io.start := missQueue.io.out.fire 244935edac4STang Haojin blockmq.io.enable := ptw.io.req.fire 2457797f035SbugGenerator 246d0de7e4aSpeixiaokun llptw.io.in.valid := cache.io.resp.valid && 247d0de7e4aSpeixiaokun !cache.io.resp.bits.hit && 2483ea4388cSHaoyuan Feng cache.io.resp.bits.toFsm.l1Hit && 249d0de7e4aSpeixiaokun !cache.io.resp.bits.bypassed && 250325f0a4eSpeixiaokun !cache.io.resp.bits.isHptwReq 2519c503409SLemover llptw.io.in.bits.req_info := cache.io.resp.bits.req_info 2529c503409SLemover llptw.io.in.bits.ppn := cache.io.resp.bits.toFsm.ppn 2537797f035SbugGenerator llptw.io.sfence := sfence_dup(1) 2547797f035SbugGenerator llptw.io.csr := csr_dup(1) 2550ede9a33SXiaokun-Pei val llptw_stage1 = Reg(Vec(l2tlbParams.llptwsize, new PtwMergeResp())) 2560ede9a33SXiaokun-Pei when(llptw.io.in.fire){ 2570ede9a33SXiaokun-Pei llptw_stage1(llptw.io.mem.enq_ptr) := cache.io.resp.bits.stage1 2580ede9a33SXiaokun-Pei } 25992e3bfefSLemover 260416c2536SHaoyuan Feng cache.io.req.valid := arb2.io.out.fire 2616967f5d5Speixiaokun cache.io.req.bits.req_info := arb2.io.out.bits.req_info 262325f0a4eSpeixiaokun cache.io.req.bits.isFirst := (arb2.io.chosen =/= InArbMissQueuePort.U && !arb2.io.out.bits.isHptwReq) 263325f0a4eSpeixiaokun cache.io.req.bits.isHptwReq := arb2.io.out.bits.isHptwReq 2646967f5d5Speixiaokun cache.io.req.bits.hptwId := arb2.io.out.bits.hptwId 2651f4a7c0cSLemover cache.io.req.bits.bypassed.map(_ := false.B) 2667797f035SbugGenerator cache.io.sfence := sfence_dup(2) 2677797f035SbugGenerator cache.io.csr := csr_dup(2) 2687797f035SbugGenerator cache.io.sfence_dup.zip(sfence_dup.drop(2).take(4)).map(s => s._1 := s._2) 2697797f035SbugGenerator cache.io.csr_dup.zip(csr_dup.drop(2).take(3)).map(c => c._1 := c._2) 2704c4af37cSpeixiaokun cache.io.resp.ready := MuxCase(mq_arb.io.in(0).ready || ptw.io.req.ready, Seq( 27183d93d53Speixiaokun (!cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq) -> hptw.io.req.ready, 27283d93d53Speixiaokun (cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq) -> hptw_resp_arb.io.in(HptwRespArbCachePort).ready, 2734c4af37cSpeixiaokun cache.io.resp.bits.hit -> outReady(cache.io.resp.bits.req_info.source, outArbCachePort), 2743ea4388cSHaoyuan Feng (cache.io.resp.bits.toFsm.l1Hit && !cache.io.resp.bits.bypassed && llptw.io.in.ready) -> llptw.io.in.ready, 27583d93d53Speixiaokun (cache.io.resp.bits.bypassed || cache.io.resp.bits.isFirst) -> mq_arb.io.in(0).ready 2764c4af37cSpeixiaokun )) 27792e3bfefSLemover 27892e3bfefSLemover // NOTE: missQueue req has higher priority 2793ea4388cSHaoyuan Feng ptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && !cache.io.resp.bits.toFsm.l1Hit && 2807797f035SbugGenerator !cache.io.resp.bits.bypassed && 281d0de7e4aSpeixiaokun !cache.io.resp.bits.isFirst && 282325f0a4eSpeixiaokun !cache.io.resp.bits.isHptwReq 28392e3bfefSLemover ptw.io.req.bits.req_info := cache.io.resp.bits.req_info 2843ea4388cSHaoyuan Feng if (EnableSv48) { 2853ea4388cSHaoyuan Feng ptw.io.req.bits.l3Hit.get := cache.io.resp.bits.toFsm.l3Hit.get 2863ea4388cSHaoyuan Feng } 2873ea4388cSHaoyuan Feng ptw.io.req.bits.l2Hit := cache.io.resp.bits.toFsm.l2Hit 28892e3bfefSLemover ptw.io.req.bits.ppn := cache.io.resp.bits.toFsm.ppn 28930104977Speixiaokun ptw.io.req.bits.stage1Hit := cache.io.resp.bits.toFsm.stage1Hit 2906979864eSXiaokun-Pei ptw.io.req.bits.stage1 := cache.io.resp.bits.stage1 2917797f035SbugGenerator ptw.io.sfence := sfence_dup(7) 2927797f035SbugGenerator ptw.io.csr := csr_dup(6) 29392e3bfefSLemover ptw.io.resp.ready := outReady(ptw.io.resp.bits.source, outArbFsmPort) 29492e3bfefSLemover 29583d93d53Speixiaokun hptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq 29682978df9Speixiaokun hptw.io.req.bits.gvpn := cache.io.resp.bits.req_info.vpn 297d0de7e4aSpeixiaokun hptw.io.req.bits.id := cache.io.resp.bits.toHptw.id 298c3d5cfb3Speixiaokun hptw.io.req.bits.source := cache.io.resp.bits.req_info.source 2993ea4388cSHaoyuan Feng if (EnableSv48) { 3003ea4388cSHaoyuan Feng hptw.io.req.bits.l3Hit.get := cache.io.resp.bits.toHptw.l3Hit.get 3013ea4388cSHaoyuan Feng } 302d0de7e4aSpeixiaokun hptw.io.req.bits.l2Hit := cache.io.resp.bits.toHptw.l2Hit 3033ea4388cSHaoyuan Feng hptw.io.req.bits.l1Hit := cache.io.resp.bits.toHptw.l1Hit 304979f601eSpeixiaokun hptw.io.req.bits.ppn := cache.io.resp.bits.toHptw.ppn 30583d93d53Speixiaokun hptw.io.req.bits.bypassed := cache.io.resp.bits.toHptw.bypassed 306d0de7e4aSpeixiaokun hptw.io.sfence := sfence_dup(8) 307d0de7e4aSpeixiaokun hptw.io.csr := csr_dup(7) 30892e3bfefSLemover // mem req 30992e3bfefSLemover def blockBytes_align(addr: UInt) = { 31092e3bfefSLemover Cat(addr(PAddrBits - 1, log2Up(l2tlbParams.blockBytes)), 0.U(log2Up(l2tlbParams.blockBytes).W)) 31192e3bfefSLemover } 31292e3bfefSLemover def addr_low_from_vpn(vpn: UInt) = { 31392e3bfefSLemover vpn(log2Ceil(l2tlbParams.blockBytes)-log2Ceil(XLEN/8)-1, 0) 31492e3bfefSLemover } 31592e3bfefSLemover def addr_low_from_paddr(paddr: UInt) = { 31692e3bfefSLemover paddr(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 31792e3bfefSLemover } 318d0de7e4aSpeixiaokun def from_llptw(id: UInt) = { 319d0de7e4aSpeixiaokun id < l2tlbParams.llptwsize.U 320d0de7e4aSpeixiaokun } 321d0de7e4aSpeixiaokun def from_ptw(id: UInt) = { 322d0de7e4aSpeixiaokun id === l2tlbParams.llptwsize.U 323d0de7e4aSpeixiaokun } 324d0de7e4aSpeixiaokun def from_hptw(id: UInt) = { 325d0de7e4aSpeixiaokun id === l2tlbParams.llptwsize.U + 1.U 32692e3bfefSLemover } 32792e3bfefSLemover val waiting_resp = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B))) 32892e3bfefSLemover val flush_latch = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B))) 32983d93d53Speixiaokun val hptw_bypassed = RegInit(false.B) 33092e3bfefSLemover for (i <- waiting_resp.indices) { 33192e3bfefSLemover assert(!flush_latch(i) || waiting_resp(i)) // when sfence_latch wait for mem resp, waiting_resp should be true 33292e3bfefSLemover } 33392e3bfefSLemover 33492e3bfefSLemover val llptw_out = llptw.io.out 33592e3bfefSLemover val llptw_mem = llptw.io.mem 33697929664SXiaokun-Pei llptw_mem.flush_latch := flush_latch.take(l2tlbParams.llptwsize) 33792e3bfefSLemover llptw_mem.req_mask := waiting_resp.take(l2tlbParams.llptwsize) 338d61cd5eeSpeixiaokun ptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize) 339d61cd5eeSpeixiaokun hptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize + 1) 34092e3bfefSLemover 341d0de7e4aSpeixiaokun val mem_arb = Module(new Arbiter(new L2TlbMemReqBundle(), 3)) 34292e3bfefSLemover mem_arb.io.in(0) <> ptw.io.mem.req 34392e3bfefSLemover mem_arb.io.in(1) <> llptw_mem.req 344d0de7e4aSpeixiaokun mem_arb.io.in(2) <> hptw.io.mem.req 34592e3bfefSLemover mem_arb.io.out.ready := mem.a.ready && !flush 34692e3bfefSLemover 34727ba10c1SXiaokun-Pei // // assert, should not send mem access at same addr for twice. 34827ba10c1SXiaokun-Pei // val last_resp_vpn = RegEnable(cache.io.refill.bits.req_info_dup(0).vpn, cache.io.refill.valid) 34927ba10c1SXiaokun-Pei // val last_resp_s2xlate = RegEnable(cache.io.refill.bits.req_info_dup(0).s2xlate, cache.io.refill.valid) 35027ba10c1SXiaokun-Pei // val last_resp_level = RegEnable(cache.io.refill.bits.level_dup(0), cache.io.refill.valid) 35127ba10c1SXiaokun-Pei // val last_resp_v = RegInit(false.B) 35227ba10c1SXiaokun-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() 35327ba10c1SXiaokun-Pei // when (cache.io.refill.valid) { last_resp_v := !last_has_invalid} 35427ba10c1SXiaokun-Pei // when (flush) { last_resp_v := false.B } 35527ba10c1SXiaokun-Pei // XSError(last_resp_v && cache.io.refill.valid && 35627ba10c1SXiaokun-Pei // (cache.io.refill.bits.req_info_dup(0).vpn === last_resp_vpn) && 35727ba10c1SXiaokun-Pei // (cache.io.refill.bits.level_dup(0) === last_resp_level) && 35827ba10c1SXiaokun-Pei // (cache.io.refill.bits.req_info_dup(0).s2xlate === last_resp_s2xlate), 35927ba10c1SXiaokun-Pei // "l2tlb should not access mem at same addr for twice") 36027ba10c1SXiaokun-Pei // // ATTENTION: this may wrongly assert when: a ptes is l2, last part is valid, 36127ba10c1SXiaokun-Pei // // but the current part is invalid, so one more mem access happened 36227ba10c1SXiaokun-Pei // // If this happened, remove the assert. 3631f4a7c0cSLemover 36492e3bfefSLemover val req_addr_low = Reg(Vec(MemReqWidth, UInt((log2Up(l2tlbParams.blockBytes)-log2Up(XLEN/8)).W))) 36592e3bfefSLemover 366935edac4STang Haojin when (llptw.io.in.fire) { 36792e3bfefSLemover // when enq miss queue, set the req_addr_low to receive the mem resp data part 36892e3bfefSLemover req_addr_low(llptw_mem.enq_ptr) := addr_low_from_vpn(llptw.io.in.bits.req_info.vpn) 36992e3bfefSLemover } 370935edac4STang Haojin when (mem_arb.io.out.fire) { 37192e3bfefSLemover req_addr_low(mem_arb.io.out.bits.id) := addr_low_from_paddr(mem_arb.io.out.bits.addr) 37292e3bfefSLemover waiting_resp(mem_arb.io.out.bits.id) := true.B 37383d93d53Speixiaokun hptw_bypassed := from_hptw(mem_arb.io.out.bits.id) && mem_arb.io.out.bits.hptw_bypassed 37492e3bfefSLemover } 37592e3bfefSLemover // mem read 37692e3bfefSLemover val memRead = edge.Get( 37792e3bfefSLemover fromSource = mem_arb.io.out.bits.id, 37892e3bfefSLemover // toAddress = memAddr(log2Up(CacheLineSize / 2 / 8) - 1, 0), 37992e3bfefSLemover toAddress = blockBytes_align(mem_arb.io.out.bits.addr), 38092e3bfefSLemover lgSize = log2Up(l2tlbParams.blockBytes).U 38192e3bfefSLemover )._2 38292e3bfefSLemover mem.a.bits := memRead 38392e3bfefSLemover mem.a.valid := mem_arb.io.out.valid && !flush 384d2b20d1aSTang Haojin mem.a.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.PTW.id.U) 38592e3bfefSLemover mem.d.ready := true.B 38692e3bfefSLemover // mem -> data buffer 38797929664SXiaokun-Pei val refill_data = RegInit(VecInit.fill(blockBits / l1BusDataWidth)(0.U(l1BusDataWidth.W))) 388935edac4STang Haojin val refill_helper = edge.firstlastHelper(mem.d.bits, mem.d.fire) 38992e3bfefSLemover val mem_resp_done = refill_helper._3 390d0de7e4aSpeixiaokun val mem_resp_from_llptw = from_llptw(mem.d.bits.source) 391d0de7e4aSpeixiaokun val mem_resp_from_ptw = from_ptw(mem.d.bits.source) 392d0de7e4aSpeixiaokun val mem_resp_from_hptw = from_hptw(mem.d.bits.source) 39392e3bfefSLemover when (mem.d.valid) { 394d0de7e4aSpeixiaokun assert(mem.d.bits.source < MemReqWidth.U) 39592e3bfefSLemover refill_data(refill_helper._4) := mem.d.bits.data 39692e3bfefSLemover } 3977797f035SbugGenerator // refill_data_tmp is the wire fork of refill_data, but one cycle earlier 3987797f035SbugGenerator val refill_data_tmp = WireInit(refill_data) 3997797f035SbugGenerator refill_data_tmp(refill_helper._4) := mem.d.bits.data 4007797f035SbugGenerator 40192e3bfefSLemover // save only one pte for each id 40292e3bfefSLemover // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache) 40392e3bfefSLemover val resp_pte = VecInit((0 until MemReqWidth).map(i => 40497929664SXiaokun-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) } 40597929664SXiaokun-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) } 40697929664SXiaokun-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))) } 4077797f035SbugGenerator // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle 40892e3bfefSLemover )) 40992e3bfefSLemover 41063632028SHaoyuan Feng // save eight ptes for each id when sector tlb 41163632028SHaoyuan Feng // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache) 41263632028SHaoyuan Feng val resp_pte_sector = VecInit((0 until MemReqWidth).map(i => 41397929664SXiaokun-Pei if (i == l2tlbParams.llptwsize + 1) {RegEnable(refill_data_tmp, 0.U.asTypeOf(refill_data_tmp), mem_resp_done && mem_resp_from_hptw) } 41497929664SXiaokun-Pei else if (i == l2tlbParams.llptwsize) {RegEnable(refill_data_tmp, 0.U.asTypeOf(refill_data_tmp), mem_resp_done && mem_resp_from_ptw) } 41597929664SXiaokun-Pei else { Mux(llptw_mem.buffer_it(i), refill_data, RegEnable(refill_data, 0.U.asTypeOf(refill_data), llptw_mem.buffer_it(i))) } 41663632028SHaoyuan Feng // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle 41763632028SHaoyuan Feng )) 41863632028SHaoyuan Feng 419d0de7e4aSpeixiaokun // mem -> llptw 420d0de7e4aSpeixiaokun llptw_mem.resp.valid := mem_resp_done && mem_resp_from_llptw 4217797f035SbugGenerator llptw_mem.resp.bits.id := DataHoldBypass(mem.d.bits.source, mem.d.valid) 422ce5f4200SGuanghui Hu llptw_mem.resp.bits.value := DataHoldBypass(refill_data_tmp.asUInt, mem.d.valid) 42392e3bfefSLemover // mem -> ptw 424d0de7e4aSpeixiaokun ptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_ptw 425d61cd5eeSpeixiaokun ptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize) 426d0de7e4aSpeixiaokun // mem -> hptw 427d0de7e4aSpeixiaokun hptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_hptw 428d61cd5eeSpeixiaokun hptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize + 1) 42992e3bfefSLemover // mem -> cache 430d0de7e4aSpeixiaokun val refill_from_llptw = mem_resp_from_llptw 431d0de7e4aSpeixiaokun val refill_from_ptw = mem_resp_from_ptw 432d0de7e4aSpeixiaokun val refill_from_hptw = mem_resp_from_hptw 4333ea4388cSHaoyuan 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))) 43483d93d53Speixiaokun val refill_valid = mem_resp_done && !flush && !flush_latch(mem.d.bits.source) && !hptw_bypassed 4357797f035SbugGenerator 4365adc4829SYanqin Li cache.io.refill.valid := GatedValidRegNext(refill_valid, false.B) 43792e3bfefSLemover cache.io.refill.bits.ptes := refill_data.asUInt 438d0de7e4aSpeixiaokun 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)) 4397797f035SbugGenerator cache.io.refill.bits.level_dup.map(_ := RegEnable(refill_level, refill_valid)) 4407797f035SbugGenerator cache.io.refill.bits.levelOH(refill_level, refill_valid) 4415adc4829SYanqin 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)) 44292e3bfefSLemover 4439c26bab7SHaoyuan Feng if (env.EnableDifftest) { 4449c26bab7SHaoyuan Feng val difftest_ptw_addr = RegInit(VecInit(Seq.fill(MemReqWidth)(0.U(PAddrBits.W)))) 4459c26bab7SHaoyuan Feng when (mem.a.valid) { 4469c26bab7SHaoyuan Feng difftest_ptw_addr(mem.a.bits.source) := mem.a.bits.address 4479c26bab7SHaoyuan Feng } 4489c26bab7SHaoyuan Feng 449a0c65233SYinan Xu val difftest = DifftestModule(new DiffRefillEvent, dontCare = true) 450254e4960SHaoyuan Feng difftest.coreid := io.hartId 4517d45a146SYinan Xu difftest.index := 2.U 4527d45a146SYinan Xu difftest.valid := cache.io.refill.valid 4535adc4829SYanqin Li difftest.addr := difftest_ptw_addr(RegEnable(mem.d.bits.source, mem.d.valid)) 4547d45a146SYinan Xu difftest.data := refill_data.asTypeOf(difftest.data) 455935edac4STang Haojin difftest.idtfr := DontCare 4569c26bab7SHaoyuan Feng } 4579c26bab7SHaoyuan Feng 4585ab1b84dSHaoyuan Feng if (env.EnableDifftest) { 4595ab1b84dSHaoyuan Feng for (i <- 0 until PtwWidth) { 4607d45a146SYinan Xu val difftest = DifftestModule(new DiffL2TLBEvent) 461b436d3b6Speixiaokun difftest.coreid := io.hartId 462d61cd5eeSpeixiaokun difftest.valid := io.tlb(i).resp.fire && !io.tlb(i).resp.bits.s1.af && !io.tlb(i).resp.bits.s2.gaf 4637d45a146SYinan Xu difftest.index := i.U 46487d0ba30Speixiaokun difftest.vpn := Cat(io.tlb(i).resp.bits.s1.entry.tag, 0.U(sectortlbwidth.W)) 465002c10a4SYanqin Li difftest.pbmt := io.tlb(i).resp.bits.s1.entry.pbmt 466002c10a4SYanqin Li difftest.g_pbmt := io.tlb(i).resp.bits.s2.entry.pbmt 46763632028SHaoyuan Feng for (j <- 0 until tlbcontiguous) { 468b436d3b6Speixiaokun difftest.ppn(j) := Cat(io.tlb(i).resp.bits.s1.entry.ppn, io.tlb(i).resp.bits.s1.ppn_low(j)) 469b436d3b6Speixiaokun difftest.valididx(j) := io.tlb(i).resp.bits.s1.valididx(j) 47087d0ba30Speixiaokun difftest.pteidx(j) := io.tlb(i).resp.bits.s1.pteidx(j) 47163632028SHaoyuan Feng } 47287d0ba30Speixiaokun difftest.perm := io.tlb(i).resp.bits.s1.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt 47387d0ba30Speixiaokun difftest.level := io.tlb(i).resp.bits.s1.entry.level.getOrElse(0.U.asUInt) 47487d0ba30Speixiaokun difftest.pf := io.tlb(i).resp.bits.s1.pf 47587d0ba30Speixiaokun difftest.satp := Cat(io.csr.tlb.satp.mode, io.csr.tlb.satp.asid, io.csr.tlb.satp.ppn) 47687d0ba30Speixiaokun difftest.vsatp := Cat(io.csr.tlb.vsatp.mode, io.csr.tlb.vsatp.asid, io.csr.tlb.vsatp.ppn) 47797929664SXiaokun-Pei difftest.hgatp := Cat(io.csr.tlb.hgatp.mode, io.csr.tlb.hgatp.vmid, io.csr.tlb.hgatp.ppn) 47887d0ba30Speixiaokun difftest.gvpn := io.tlb(i).resp.bits.s2.entry.tag 47987d0ba30Speixiaokun difftest.g_perm := io.tlb(i).resp.bits.s2.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt 48087d0ba30Speixiaokun difftest.g_level := io.tlb(i).resp.bits.s2.entry.level.getOrElse(0.U.asUInt) 48187d0ba30Speixiaokun difftest.s2ppn := io.tlb(i).resp.bits.s2.entry.ppn 48287d0ba30Speixiaokun difftest.gpf := io.tlb(i).resp.bits.s2.gpf 48387d0ba30Speixiaokun difftest.s2xlate := io.tlb(i).resp.bits.s2xlate 4845ab1b84dSHaoyuan Feng } 4855ab1b84dSHaoyuan Feng } 4865ab1b84dSHaoyuan Feng 48792e3bfefSLemover // pmp 48892e3bfefSLemover pmp_check(0).req <> ptw.io.pmp.req 48992e3bfefSLemover ptw.io.pmp.resp <> pmp_check(0).resp 49092e3bfefSLemover pmp_check(1).req <> llptw.io.pmp.req 49192e3bfefSLemover llptw.io.pmp.resp <> pmp_check(1).resp 492c3d5cfb3Speixiaokun pmp_check(2).req <> hptw.io.pmp.req 493c3d5cfb3Speixiaokun hptw.io.pmp.resp <> pmp_check(2).resp 49492e3bfefSLemover 49592e3bfefSLemover llptw_out.ready := outReady(llptw_out.bits.req_info.source, outArbMqPort) 49663632028SHaoyuan Feng 497d0de7e4aSpeixiaokun // hptw and page cache -> ptw and llptw 498d0de7e4aSpeixiaokun val HptwRespArbCachePort = 0 499eb4bf3f2Speixiaokun val HptwRespArbHptw = 1 500325f0a4eSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).valid := cache.io.resp.valid && cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq 501d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).bits.id := cache.io.resp.bits.toHptw.id 502d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbCachePort).bits.resp := cache.io.resp.bits.toHptw.resp 503d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).valid := hptw.io.resp.valid 504d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).bits.id := hptw.io.resp.bits.id 505d0de7e4aSpeixiaokun hptw_resp_arb.io.in(HptwRespArbHptw).bits.resp := hptw.io.resp.bits.resp 506c2b430edSpeixiaokun hptw.io.resp.ready := hptw_resp_arb.io.in(HptwRespArbHptw).ready 507d0de7e4aSpeixiaokun 508d0de7e4aSpeixiaokun ptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id === FsmReqID.U 509d0de7e4aSpeixiaokun ptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp 510d0de7e4aSpeixiaokun llptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id =/= FsmReqID.U 511c3d5cfb3Speixiaokun llptw.io.hptw.resp.bits.id := hptw_resp_arb.io.out.bits.id 512d0de7e4aSpeixiaokun llptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp 513c3d5cfb3Speixiaokun hptw_resp_arb.io.out.ready := true.B 514d0de7e4aSpeixiaokun 51563632028SHaoyuan Feng // Timing: Maybe need to do some optimization or even add one more cycle 51692e3bfefSLemover for (i <- 0 until PtwWidth) { 517325f0a4eSpeixiaokun 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 518d0de7e4aSpeixiaokun mergeArb(i).in(outArbCachePort).bits.s2xlate := cache.io.resp.bits.req_info.s2xlate 5196979864eSXiaokun-Pei mergeArb(i).in(outArbCachePort).bits.s1 := cache.io.resp.bits.stage1 520eb4bf3f2Speixiaokun mergeArb(i).in(outArbCachePort).bits.s2 := cache.io.resp.bits.toHptw.resp 52163632028SHaoyuan Feng mergeArb(i).in(outArbFsmPort).valid := ptw.io.resp.valid && ptw.io.resp.bits.source===i.U 522d0de7e4aSpeixiaokun mergeArb(i).in(outArbFsmPort).bits.s2xlate := ptw.io.resp.bits.s2xlate 523eb4bf3f2Speixiaokun mergeArb(i).in(outArbFsmPort).bits.s1 := ptw.io.resp.bits.resp 524eb4bf3f2Speixiaokun mergeArb(i).in(outArbFsmPort).bits.s2 := ptw.io.resp.bits.h_resp 52563632028SHaoyuan Feng mergeArb(i).in(outArbMqPort).valid := llptw_out.valid && llptw_out.bits.req_info.source===i.U 526d0de7e4aSpeixiaokun mergeArb(i).in(outArbMqPort).bits.s2xlate := llptw_out.bits.req_info.s2xlate 527dd286b6aSYanqin Li mergeArb(i).in(outArbMqPort).bits.s1 := Mux( 528dd286b6aSYanqin Li llptw_out.bits.first_s2xlate_fault, llptw_stage1(llptw_out.bits.id), 529dd286b6aSYanqin Li contiguous_pte_to_merge_ptwResp( 530dd286b6aSYanqin Li resp_pte_sector(llptw_out.bits.id).asUInt, llptw_out.bits.req_info.vpn, llptw_out.bits.af, 5316962b4ffSHaoyuan Feng true, s2xlate = llptw_out.bits.req_info.s2xlate, mPBMTE = mPBMTE, hPBMTE = hPBMTE, gpf = llptw_out.bits.h_resp.gpf 532dd286b6aSYanqin Li ) 533dd286b6aSYanqin Li ) 534eb4bf3f2Speixiaokun mergeArb(i).in(outArbMqPort).bits.s2 := llptw_out.bits.h_resp 53563632028SHaoyuan Feng mergeArb(i).out.ready := outArb(i).in(0).ready 53663632028SHaoyuan Feng } 53763632028SHaoyuan Feng 53863632028SHaoyuan Feng for (i <- 0 until PtwWidth) { 53963632028SHaoyuan Feng outArb(i).in(0).valid := mergeArb(i).out.valid 540eb4bf3f2Speixiaokun outArb(i).in(0).bits.s2xlate := mergeArb(i).out.bits.s2xlate 541eb4bf3f2Speixiaokun outArb(i).in(0).bits.s1 := merge_ptwResp_to_sector_ptwResp(mergeArb(i).out.bits.s1) 542eb4bf3f2Speixiaokun outArb(i).in(0).bits.s2 := mergeArb(i).out.bits.s2 54392e3bfefSLemover } 54492e3bfefSLemover 54592e3bfefSLemover // io.tlb.map(_.resp) <> outArb.map(_.out) 54692e3bfefSLemover io.tlb.map(_.resp).zip(outArb.map(_.out)).map{ 54792e3bfefSLemover case (resp, out) => resp <> out 54892e3bfefSLemover } 54992e3bfefSLemover 55092e3bfefSLemover // sfence 55192e3bfefSLemover when (flush) { 55292e3bfefSLemover for (i <- 0 until MemReqWidth) { 55392e3bfefSLemover when (waiting_resp(i)) { 55492e3bfefSLemover flush_latch(i) := true.B 55592e3bfefSLemover } 55692e3bfefSLemover } 55792e3bfefSLemover } 55892e3bfefSLemover // mem -> control signal 55992e3bfefSLemover // waiting_resp and sfence_latch will be reset when mem_resp_done 56092e3bfefSLemover when (mem_resp_done) { 56192e3bfefSLemover waiting_resp(mem.d.bits.source) := false.B 56292e3bfefSLemover flush_latch(mem.d.bits.source) := false.B 56392e3bfefSLemover } 56492e3bfefSLemover 56592e3bfefSLemover def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = { 56692e3bfefSLemover sink.valid := source.valid && !block_signal 56792e3bfefSLemover source.ready := sink.ready && !block_signal 56892e3bfefSLemover sink.bits := source.bits 56992e3bfefSLemover } 57092e3bfefSLemover 57192e3bfefSLemover def get_part(data: Vec[UInt], index: UInt): UInt = { 57292e3bfefSLemover val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W))) 57392e3bfefSLemover inner_data(index) 57492e3bfefSLemover } 57592e3bfefSLemover 57663632028SHaoyuan Feng // not_super means that this is a normal page 57763632028SHaoyuan Feng // valididx(i) will be all true when super page to be convenient for l1 tlb matching 5786962b4ffSHaoyuan Feng 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) : PtwMergeResp = { 57963632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 58063632028SHaoyuan Feng val ptw_merge_resp = Wire(new PtwMergeResp()) 581eb4bf3f2Speixiaokun val hasS2xlate = s2xlate =/= noS2xlate 582dd286b6aSYanqin Li val pbmte = Mux(s2xlate === onlyStage1 || s2xlate === allStage, hPBMTE, mPBMTE) 58363632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 58463632028SHaoyuan Feng val pte_in = pte(64 * i + 63, 64 * i).asTypeOf(new PteBundle()) 585718a93f5SHaoyuan Feng val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true, hasNapot = true)) 58697929664SXiaokun-Pei ptw_resp.ppn := pte_in.getPPN()(ptePPNLen - 1, sectortlbwidth) 58797929664SXiaokun-Pei ptw_resp.ppn_low := pte_in.getPPN()(sectortlbwidth - 1, 0) 5883ea4388cSHaoyuan Feng ptw_resp.level.map(_ := 0.U) 589002c10a4SYanqin Li ptw_resp.pbmt := pte_in.pbmt 590718a93f5SHaoyuan Feng ptw_resp.n.map(_ := pte_in.n) 59163632028SHaoyuan Feng ptw_resp.perm.map(_ := pte_in.getPerm()) 59263632028SHaoyuan Feng ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 593dd286b6aSYanqin Li ptw_resp.pf := (if (af_first) !af else true.B) && (pte_in.isPf(0.U, pbmte) || !pte_in.isLeaf()) 5946962b4ffSHaoyuan Feng ptw_resp.af := (if (!af_first) pte_in.isPf(0.U, pbmte) else true.B) && (af || (Mux(s2xlate === allStage, false.B, pte_in.isAf()) && !(hasS2xlate && gpf))) 59563632028SHaoyuan Feng ptw_resp.v := !ptw_resp.pf 59663632028SHaoyuan Feng ptw_resp.prefetch := DontCare 597eb4bf3f2Speixiaokun ptw_resp.asid := Mux(hasS2xlate, vsatp.asid, satp.asid) 59897929664SXiaokun-Pei ptw_resp.vmid.map(_ := hgatp.vmid) 59963632028SHaoyuan Feng ptw_merge_resp.entry(i) := ptw_resp 60063632028SHaoyuan Feng } 60163632028SHaoyuan Feng ptw_merge_resp.pteidx := UIntToOH(vpn(sectortlbwidth - 1, 0)).asBools 60263632028SHaoyuan Feng ptw_merge_resp.not_super := not_super.B 6036962b4ffSHaoyuan Feng ptw_merge_resp.not_merge := hasS2xlate 60463632028SHaoyuan Feng ptw_merge_resp 60563632028SHaoyuan Feng } 60663632028SHaoyuan Feng 60763632028SHaoyuan Feng def merge_ptwResp_to_sector_ptwResp(pte: PtwMergeResp) : PtwSectorResp = { 60863632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 60963632028SHaoyuan Feng val ptw_sector_resp = Wire(new PtwSectorResp) 61063632028SHaoyuan Feng ptw_sector_resp.entry.tag := pte.entry(OHToUInt(pte.pteidx)).tag 61163632028SHaoyuan Feng ptw_sector_resp.entry.asid := pte.entry(OHToUInt(pte.pteidx)).asid 612c3d5cfb3Speixiaokun ptw_sector_resp.entry.vmid.map(_ := pte.entry(OHToUInt(pte.pteidx)).vmid.getOrElse(0.U)) 61363632028SHaoyuan Feng ptw_sector_resp.entry.ppn := pte.entry(OHToUInt(pte.pteidx)).ppn 614002c10a4SYanqin Li ptw_sector_resp.entry.pbmt := pte.entry(OHToUInt(pte.pteidx)).pbmt 615718a93f5SHaoyuan Feng ptw_sector_resp.entry.n.map(_ := pte.entry(OHToUInt(pte.pteidx)).n.getOrElse(0.U)) 61663632028SHaoyuan Feng ptw_sector_resp.entry.perm.map(_ := pte.entry(OHToUInt(pte.pteidx)).perm.getOrElse(0.U.asTypeOf(new PtePermBundle))) 6173ea4388cSHaoyuan Feng ptw_sector_resp.entry.level.map(_ := pte.entry(OHToUInt(pte.pteidx)).level.getOrElse(0.U(log2Up(Level + 1).W))) 61863632028SHaoyuan Feng ptw_sector_resp.entry.prefetch := pte.entry(OHToUInt(pte.pteidx)).prefetch 61963632028SHaoyuan Feng ptw_sector_resp.entry.v := pte.entry(OHToUInt(pte.pteidx)).v 62063632028SHaoyuan Feng ptw_sector_resp.af := pte.entry(OHToUInt(pte.pteidx)).af 62163632028SHaoyuan Feng ptw_sector_resp.pf := pte.entry(OHToUInt(pte.pteidx)).pf 62263632028SHaoyuan Feng ptw_sector_resp.addr_low := OHToUInt(pte.pteidx) 623b0fa7106SHaoyuan Feng ptw_sector_resp.pteidx := pte.pteidx 62463632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 62563632028SHaoyuan Feng val ppn_equal = pte.entry(i).ppn === pte.entry(OHToUInt(pte.pteidx)).ppn 626002c10a4SYanqin Li val pbmt_equal = pte.entry(i).pbmt === pte.entry(OHToUInt(pte.pteidx)).pbmt 627718a93f5SHaoyuan Feng val n_equal = pte.entry(i).n.getOrElse(0.U) === pte.entry(OHToUInt(pte.pteidx)).n.getOrElse(0.U) 62863632028SHaoyuan 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 62963632028SHaoyuan Feng val v_equal = pte.entry(i).v === pte.entry(OHToUInt(pte.pteidx)).v 63063632028SHaoyuan Feng val af_equal = pte.entry(i).af === pte.entry(OHToUInt(pte.pteidx)).af 63163632028SHaoyuan Feng val pf_equal = pte.entry(i).pf === pte.entry(OHToUInt(pte.pteidx)).pf 632718a93f5SHaoyuan Feng ptw_sector_resp.valididx(i) := ((ppn_equal && pbmt_equal && n_equal && perm_equal && v_equal && af_equal && pf_equal) || !pte.not_super) && !pte.not_merge 63363632028SHaoyuan Feng ptw_sector_resp.ppn_low(i) := pte.entry(i).ppn_low 63463632028SHaoyuan Feng } 63563632028SHaoyuan Feng ptw_sector_resp.valididx(OHToUInt(pte.pteidx)) := true.B 63663632028SHaoyuan Feng ptw_sector_resp 63763632028SHaoyuan Feng } 63863632028SHaoyuan Feng 63992e3bfefSLemover def outReady(source: UInt, port: Int): Bool = { 64045f43e6eSTang Haojin MuxLookup(source, true.B)((0 until PtwWidth).map(i => i.U -> mergeArb(i).in(port).ready)) 64192e3bfefSLemover } 64292e3bfefSLemover 64392e3bfefSLemover // debug info 64492e3bfefSLemover for (i <- 0 until PtwWidth) { 64592e3bfefSLemover XSDebug(p"[io.tlb(${i.U})] ${io.tlb(i)}\n") 64692e3bfefSLemover } 6477797f035SbugGenerator XSDebug(p"[sfence] ${io.sfence}\n") 64892e3bfefSLemover XSDebug(p"[io.csr.tlb] ${io.csr.tlb}\n") 64992e3bfefSLemover 65092e3bfefSLemover for (i <- 0 until PtwWidth) { 651935edac4STang Haojin XSPerfAccumulate(s"req_count${i}", io.tlb(i).req(0).fire) 65292e3bfefSLemover XSPerfAccumulate(s"req_blocked_count_${i}", io.tlb(i).req(0).valid && !io.tlb(i).req(0).ready) 65392e3bfefSLemover } 65492e3bfefSLemover XSPerfAccumulate(s"req_blocked_by_mq", arb1.io.out.valid && missQueue.io.out.valid) 65592e3bfefSLemover for (i <- 0 until (MemReqWidth + 1)) { 65692e3bfefSLemover XSPerfAccumulate(s"mem_req_util${i}", PopCount(waiting_resp) === i.U) 65792e3bfefSLemover } 65892e3bfefSLemover XSPerfAccumulate("mem_cycle", PopCount(waiting_resp) =/= 0.U) 659935edac4STang Haojin XSPerfAccumulate("mem_count", mem.a.fire) 660dd7fe201SHaoyuan Feng for (i <- 0 until PtwWidth) { 661eb4bf3f2Speixiaokun 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) 662d61cd5eeSpeixiaokun XSPerfAccumulate(s"access_fault${i}", io.tlb(i).resp.fire && io.tlb(i).resp.bits.s1.af) 663dd7fe201SHaoyuan Feng } 66492e3bfefSLemover 66592e3bfefSLemover // print configs 6663ea4388cSHaoyuan 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}") 66792e3bfefSLemover 66892e3bfefSLemover val perfEvents = Seq(llptw, cache, ptw).flatMap(_.getPerfEvents) 66992e3bfefSLemover generatePerfEvent() 6705afdf73cSHaoyuan Feng 671c686adcdSYinan Xu val isWriteL1TlbTable = Constantin.createRecord(s"isWriteL1TlbTable$hartId") 672c686adcdSYinan Xu val L1TlbTable = ChiselDB.createTable(s"L1Tlb_hart$hartId", new L1TlbDB) 6735afdf73cSHaoyuan Feng val ITlbReqDB, DTlbReqDB, ITlbRespDB, DTlbRespDB = Wire(new L1TlbDB) 6745afdf73cSHaoyuan Feng ITlbReqDB.vpn := io.tlb(0).req(0).bits.vpn 6755afdf73cSHaoyuan Feng DTlbReqDB.vpn := io.tlb(1).req(0).bits.vpn 676416c2536SHaoyuan Feng ITlbRespDB.vpn := Cat(io.tlb(0).resp.bits.s1.entry.tag, OHToUInt(io.tlb(0).resp.bits.s1.pteidx)) 677416c2536SHaoyuan Feng DTlbRespDB.vpn := Cat(io.tlb(1).resp.bits.s1.entry.tag, OHToUInt(io.tlb(1).resp.bits.s1.pteidx)) 678da3bf434SMaxpicca-Li L1TlbTable.log(ITlbReqDB, isWriteL1TlbTable.orR && io.tlb(0).req(0).fire, "ITlbReq", clock, reset) 679da3bf434SMaxpicca-Li L1TlbTable.log(DTlbReqDB, isWriteL1TlbTable.orR && io.tlb(1).req(0).fire, "DTlbReq", clock, reset) 680da3bf434SMaxpicca-Li L1TlbTable.log(ITlbRespDB, isWriteL1TlbTable.orR && io.tlb(0).resp.fire, "ITlbResp", clock, reset) 681da3bf434SMaxpicca-Li L1TlbTable.log(DTlbRespDB, isWriteL1TlbTable.orR && io.tlb(1).resp.fire, "DTlbResp", clock, reset) 6825afdf73cSHaoyuan Feng 683c686adcdSYinan Xu val isWritePageCacheTable = Constantin.createRecord(s"isWritePageCacheTable$hartId") 684c686adcdSYinan Xu val PageCacheTable = ChiselDB.createTable(s"PageCache_hart$hartId", new PageCacheDB) 6855afdf73cSHaoyuan Feng val PageCacheDB = Wire(new PageCacheDB) 6866979864eSXiaokun-Pei PageCacheDB.vpn := Cat(cache.io.resp.bits.stage1.entry(0).tag, OHToUInt(cache.io.resp.bits.stage1.pteidx)) 6875afdf73cSHaoyuan Feng PageCacheDB.source := cache.io.resp.bits.req_info.source 6885afdf73cSHaoyuan Feng PageCacheDB.bypassed := cache.io.resp.bits.bypassed 6895afdf73cSHaoyuan Feng PageCacheDB.is_first := cache.io.resp.bits.isFirst 6906979864eSXiaokun-Pei PageCacheDB.prefetched := cache.io.resp.bits.stage1.entry(0).prefetch 6915afdf73cSHaoyuan Feng PageCacheDB.prefetch := cache.io.resp.bits.prefetch 6925afdf73cSHaoyuan Feng PageCacheDB.l2Hit := cache.io.resp.bits.toFsm.l2Hit 6935afdf73cSHaoyuan Feng PageCacheDB.l1Hit := cache.io.resp.bits.toFsm.l1Hit 6945afdf73cSHaoyuan Feng PageCacheDB.hit := cache.io.resp.bits.hit 695da3bf434SMaxpicca-Li PageCacheTable.log(PageCacheDB, isWritePageCacheTable.orR && cache.io.resp.fire, "PageCache", clock, reset) 6965afdf73cSHaoyuan Feng 697c686adcdSYinan Xu val isWritePTWTable = Constantin.createRecord(s"isWritePTWTable$hartId") 698c686adcdSYinan Xu val PTWTable = ChiselDB.createTable(s"PTW_hart$hartId", new PTWDB) 6995afdf73cSHaoyuan Feng val PTWReqDB, PTWRespDB, LLPTWReqDB, LLPTWRespDB = Wire(new PTWDB) 7005afdf73cSHaoyuan Feng PTWReqDB.vpn := ptw.io.req.bits.req_info.vpn 7015afdf73cSHaoyuan Feng PTWReqDB.source := ptw.io.req.bits.req_info.source 7025afdf73cSHaoyuan Feng PTWRespDB.vpn := ptw.io.refill.req_info.vpn 7035afdf73cSHaoyuan Feng PTWRespDB.source := ptw.io.refill.req_info.source 7045afdf73cSHaoyuan Feng LLPTWReqDB.vpn := llptw.io.in.bits.req_info.vpn 7055afdf73cSHaoyuan Feng LLPTWReqDB.source := llptw.io.in.bits.req_info.source 7065afdf73cSHaoyuan Feng LLPTWRespDB.vpn := llptw.io.mem.refill.vpn 7075afdf73cSHaoyuan Feng LLPTWRespDB.source := llptw.io.mem.refill.source 708da3bf434SMaxpicca-Li PTWTable.log(PTWReqDB, isWritePTWTable.orR && ptw.io.req.fire, "PTWReq", clock, reset) 709da3bf434SMaxpicca-Li PTWTable.log(PTWRespDB, isWritePTWTable.orR && ptw.io.mem.resp.fire, "PTWResp", clock, reset) 710da3bf434SMaxpicca-Li PTWTable.log(LLPTWReqDB, isWritePTWTable.orR && llptw.io.in.fire, "LLPTWReq", clock, reset) 711da3bf434SMaxpicca-Li PTWTable.log(LLPTWRespDB, isWritePTWTable.orR && llptw.io.mem.resp.fire, "LLPTWResp", clock, reset) 7125afdf73cSHaoyuan Feng 713c686adcdSYinan Xu val isWriteL2TlbMissQueueTable = Constantin.createRecord(s"isWriteL2TlbMissQueueTable$hartId") 714c686adcdSYinan Xu val L2TlbMissQueueTable = ChiselDB.createTable(s"L2TlbMissQueue_hart$hartId", new L2TlbMissQueueDB) 7155afdf73cSHaoyuan Feng val L2TlbMissQueueInDB, L2TlbMissQueueOutDB = Wire(new L2TlbMissQueueDB) 7166967f5d5Speixiaokun L2TlbMissQueueInDB.vpn := missQueue.io.in.bits.req_info.vpn 7176967f5d5Speixiaokun L2TlbMissQueueOutDB.vpn := missQueue.io.out.bits.req_info.vpn 718da3bf434SMaxpicca-Li L2TlbMissQueueTable.log(L2TlbMissQueueInDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.in.fire, "L2TlbMissQueueIn", clock, reset) 719da3bf434SMaxpicca-Li L2TlbMissQueueTable.log(L2TlbMissQueueOutDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.out.fire, "L2TlbMissQueueOut", clock, reset) 72092e3bfefSLemover} 72192e3bfefSLemover 7227797f035SbugGenerator/** BlockHelper, block missqueue, not to send too many req to cache 7237797f035SbugGenerator * Parameter: 7247797f035SbugGenerator * enable: enable BlockHelper, mq should not send too many reqs 7257797f035SbugGenerator * start: when miss queue out fire and need, block miss queue's out 7267797f035SbugGenerator * block: block miss queue's out 7277797f035SbugGenerator * latency: last missqueue out's cache access latency 7287797f035SbugGenerator */ 7297797f035SbugGeneratorclass BlockHelper(latency: Int)(implicit p: Parameters) extends XSModule { 7307797f035SbugGenerator val io = IO(new Bundle { 7317797f035SbugGenerator val enable = Input(Bool()) 7327797f035SbugGenerator val start = Input(Bool()) 7337797f035SbugGenerator val block = Output(Bool()) 7347797f035SbugGenerator }) 7357797f035SbugGenerator 7367797f035SbugGenerator val count = RegInit(0.U(log2Ceil(latency).W)) 7377797f035SbugGenerator val valid = RegInit(false.B) 7387797f035SbugGenerator val work = RegInit(true.B) 7397797f035SbugGenerator 7407797f035SbugGenerator io.block := valid 7417797f035SbugGenerator 7427797f035SbugGenerator when (io.start && work) { valid := true.B } 7437797f035SbugGenerator when (valid) { count := count + 1.U } 7447797f035SbugGenerator when (count === (latency.U) || io.enable) { 7457797f035SbugGenerator valid := false.B 7467797f035SbugGenerator work := io.enable 7477797f035SbugGenerator count := 0.U 7487797f035SbugGenerator } 7497797f035SbugGenerator} 7507797f035SbugGenerator 75192e3bfefSLemoverclass PTEHelper() extends ExtModule { 75292e3bfefSLemover val clock = IO(Input(Clock())) 75392e3bfefSLemover val enable = IO(Input(Bool())) 75492e3bfefSLemover val satp = IO(Input(UInt(64.W))) 75592e3bfefSLemover val vpn = IO(Input(UInt(64.W))) 75692e3bfefSLemover val pte = IO(Output(UInt(64.W))) 75792e3bfefSLemover val level = IO(Output(UInt(8.W))) 75892e3bfefSLemover val pf = IO(Output(UInt(8.W))) 75992e3bfefSLemover} 76092e3bfefSLemover 7615afdf73cSHaoyuan Fengclass PTWDelayN[T <: Data](gen: T, n: Int, flush: Bool) extends Module { 7625afdf73cSHaoyuan Feng val io = IO(new Bundle() { 7635afdf73cSHaoyuan Feng val in = Input(gen) 7645afdf73cSHaoyuan Feng val out = Output(gen) 7655afdf73cSHaoyuan Feng val ptwflush = Input(flush.cloneType) 7665afdf73cSHaoyuan Feng }) 7675afdf73cSHaoyuan Feng val out = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen)))) 7685afdf73cSHaoyuan Feng val t = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen)))) 7695afdf73cSHaoyuan Feng out(0) := io.in 7705afdf73cSHaoyuan Feng if (n == 1) { 7715afdf73cSHaoyuan Feng io.out := out(0) 7725afdf73cSHaoyuan Feng } else { 7735afdf73cSHaoyuan Feng when (io.ptwflush) { 7745afdf73cSHaoyuan Feng for (i <- 0 until n) { 7755afdf73cSHaoyuan Feng t(i) := 0.U.asTypeOf(gen) 7765afdf73cSHaoyuan Feng out(i) := 0.U.asTypeOf(gen) 7775afdf73cSHaoyuan Feng } 7785afdf73cSHaoyuan Feng io.out := 0.U.asTypeOf(gen) 7795afdf73cSHaoyuan Feng } .otherwise { 7805afdf73cSHaoyuan Feng for (i <- 1 until n) { 7815afdf73cSHaoyuan Feng t(i-1) := out(i-1) 7825afdf73cSHaoyuan Feng out(i) := t(i-1) 7835afdf73cSHaoyuan Feng } 7845afdf73cSHaoyuan Feng io.out := out(n-1) 7855afdf73cSHaoyuan Feng } 7865afdf73cSHaoyuan Feng } 7875afdf73cSHaoyuan Feng} 7885afdf73cSHaoyuan Feng 7895afdf73cSHaoyuan Fengobject PTWDelayN { 7905afdf73cSHaoyuan Feng def apply[T <: Data](in: T, n: Int, flush: Bool): T = { 7915afdf73cSHaoyuan Feng val delay = Module(new PTWDelayN(in.cloneType, n, flush)) 7925afdf73cSHaoyuan Feng delay.io.in := in 7935afdf73cSHaoyuan Feng delay.io.ptwflush := flush 7945afdf73cSHaoyuan Feng delay.io.out 7955afdf73cSHaoyuan Feng } 7965afdf73cSHaoyuan Feng} 7975afdf73cSHaoyuan Feng 79892e3bfefSLemoverclass FakePTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 79992e3bfefSLemover val io = IO(new L2TLBIO) 8005afdf73cSHaoyuan Feng val flush = VecInit(Seq.fill(PtwWidth)(false.B)) 8015afdf73cSHaoyuan Feng flush(0) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, itlbParams.fenceDelay) 8025afdf73cSHaoyuan Feng flush(1) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, ldtlbParams.fenceDelay) 80392e3bfefSLemover for (i <- 0 until PtwWidth) { 80492e3bfefSLemover val helper = Module(new PTEHelper()) 80592e3bfefSLemover helper.clock := clock 80692e3bfefSLemover helper.satp := io.csr.tlb.satp.ppn 8075afdf73cSHaoyuan Feng 8085afdf73cSHaoyuan Feng if (coreParams.softPTWDelay == 1) { 8095afdf73cSHaoyuan Feng helper.enable := io.tlb(i).req(0).fire 81092e3bfefSLemover helper.vpn := io.tlb(i).req(0).bits.vpn 8115afdf73cSHaoyuan Feng } else { 8125afdf73cSHaoyuan Feng helper.enable := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay - 1, flush(i)) 8135afdf73cSHaoyuan Feng helper.vpn := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay - 1, flush(i)) 8145afdf73cSHaoyuan Feng } 8155afdf73cSHaoyuan Feng 81692e3bfefSLemover val pte = helper.pte.asTypeOf(new PteBundle) 81792e3bfefSLemover val level = helper.level 81892e3bfefSLemover val pf = helper.pf 8195afdf73cSHaoyuan Feng val empty = RegInit(true.B) 8205afdf73cSHaoyuan Feng when (io.tlb(i).req(0).fire) { 8215afdf73cSHaoyuan Feng empty := false.B 8225afdf73cSHaoyuan Feng } .elsewhen (io.tlb(i).resp.fire || flush(i)) { 8235afdf73cSHaoyuan Feng empty := true.B 8245afdf73cSHaoyuan Feng } 82592e3bfefSLemover 8265afdf73cSHaoyuan Feng io.tlb(i).req(0).ready := empty || io.tlb(i).resp.fire 8275afdf73cSHaoyuan Feng io.tlb(i).resp.valid := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay, flush(i)) 82892e3bfefSLemover assert(!io.tlb(i).resp.valid || io.tlb(i).resp.ready) 829d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.tag := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay, flush(i)) 830002c10a4SYanqin Li io.tlb(i).resp.bits.s1.entry.pbmt := pte.pbmt 831d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.ppn := pte.ppn 832d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.perm.map(_ := pte.getPerm()) 833d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.level.map(_ := level) 834d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.pf := pf 835d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.af := DontCare // TODO: implement it 836d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.v := !pf 837d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.prefetch := DontCare 838d61cd5eeSpeixiaokun io.tlb(i).resp.bits.s1.entry.asid := io.csr.tlb.satp.asid 83992e3bfefSLemover } 84092e3bfefSLemover} 84192e3bfefSLemover 84292e3bfefSLemoverclass L2TLBWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter { 84395e60e55STang Haojin override def shouldBeInlined: Boolean = false 84492e3bfefSLemover val useSoftPTW = coreParams.softPTW 84592e3bfefSLemover val node = if (!useSoftPTW) TLIdentityNode() else null 84692e3bfefSLemover val ptw = if (!useSoftPTW) LazyModule(new L2TLB()) else null 84792e3bfefSLemover if (!useSoftPTW) { 84892e3bfefSLemover node := ptw.node 84992e3bfefSLemover } 85092e3bfefSLemover 851935edac4STang Haojin class L2TLBWrapperImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) with HasPerfEvents { 85292e3bfefSLemover val io = IO(new L2TLBIO) 85392e3bfefSLemover val perfEvents = if (useSoftPTW) { 85492e3bfefSLemover val fake_ptw = Module(new FakePTW()) 85592e3bfefSLemover io <> fake_ptw.io 85692e3bfefSLemover Seq() 85792e3bfefSLemover } 85892e3bfefSLemover else { 85992e3bfefSLemover io <> ptw.module.io 86092e3bfefSLemover ptw.module.getPerfEvents 86192e3bfefSLemover } 86292e3bfefSLemover generatePerfEvent() 86392e3bfefSLemover } 864935edac4STang Haojin 865935edac4STang Haojin lazy val module = new L2TLBWrapperImp(this) 86692e3bfefSLemover} 867