16d5ddbceSLemover/*************************************************************************************** 26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 46d5ddbceSLemover* 56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 86d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 96d5ddbceSLemover* 106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 136d5ddbceSLemover* 146d5ddbceSLemover* See the Mulan PSL v2 for more details. 156d5ddbceSLemover***************************************************************************************/ 166d5ddbceSLemover 176d5ddbceSLemoverpackage xiangshan.cache.mmu 186d5ddbceSLemover 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 206d5ddbceSLemoverimport chisel3._ 216d5ddbceSLemoverimport chisel3.util._ 226d5ddbceSLemoverimport xiangshan._ 236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 246d5ddbceSLemoverimport utils._ 253c02ee8fSwakafaimport utility._ 266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 276d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 286d5ddbceSLemover 2945f497a4Shappy-lxclass PTWReapterIO(Width: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 306d5ddbceSLemover val tlb = Flipped(new TlbPtwIO(Width)) 316d5ddbceSLemover val ptw = new TlbPtwIO 3245f497a4Shappy-lx 3335d6335eSZhangZifei def apply(tlb: TlbPtwIO, ptw: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 3435d6335eSZhangZifei this.tlb <> tlb 3535d6335eSZhangZifei this.ptw <> ptw 3635d6335eSZhangZifei this.sfence <> sfence 3735d6335eSZhangZifei this.csr <> csr 3835d6335eSZhangZifei } 3935d6335eSZhangZifei 4035d6335eSZhangZifei def apply(tlb: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 4135d6335eSZhangZifei this.tlb <> tlb 4235d6335eSZhangZifei this.sfence <> sfence 4335d6335eSZhangZifei this.csr <> csr 4435d6335eSZhangZifei } 4535d6335eSZhangZifei 4645f497a4Shappy-lx} 4745f497a4Shappy-lx 48f1fe8698SLemoverclass PTWRepeater(Width: Int = 1, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 4945f497a4Shappy-lx val io = IO(new PTWReapterIO(Width)) 5045f497a4Shappy-lx 516d5ddbceSLemover val req_in = if (Width == 1) { 526d5ddbceSLemover io.tlb.req(0) 536d5ddbceSLemover } else { 546d5ddbceSLemover val arb = Module(new RRArbiter(io.tlb.req(0).bits.cloneType, Width)) 556d5ddbceSLemover arb.io.in <> io.tlb.req 566d5ddbceSLemover arb.io.out 576d5ddbceSLemover } 58d0de7e4aSpeixiaokun val (tlb, ptw, flush) = (io.tlb, io.ptw, DelayN(io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed, FenceDelay)) 59935edac4STang Haojin val req = RegEnable(req_in.bits, req_in.fire) 60935edac4STang Haojin val resp = RegEnable(ptw.resp.bits, ptw.resp.fire) 61935edac4STang Haojin val haveOne = BoolStopWatch(req_in.fire, tlb.resp.fire || flush) 62935edac4STang Haojin val sent = BoolStopWatch(ptw.req(0).fire, req_in.fire || flush) 63935edac4STang Haojin val recv = BoolStopWatch(ptw.resp.fire && haveOne, req_in.fire || flush) 646d5ddbceSLemover 656d5ddbceSLemover req_in.ready := !haveOne 666d5ddbceSLemover ptw.req(0).valid := haveOne && !sent 676d5ddbceSLemover ptw.req(0).bits := req 686d5ddbceSLemover 696d5ddbceSLemover tlb.resp.bits := resp 706d5ddbceSLemover tlb.resp.valid := haveOne && recv 716d5ddbceSLemover ptw.resp.ready := !recv 726d5ddbceSLemover 73935edac4STang Haojin XSPerfAccumulate("req_count", ptw.req(0).fire) 74935edac4STang Haojin XSPerfAccumulate("tlb_req_cycle", BoolStopWatch(req_in.fire, tlb.resp.fire || flush)) 75935edac4STang Haojin XSPerfAccumulate("ptw_req_cycle", BoolStopWatch(ptw.req(0).fire, ptw.resp.fire || flush)) 766d5ddbceSLemover 7745f497a4Shappy-lx XSDebug(haveOne, p"haveOne:${haveOne} sent:${sent} recv:${recv} sfence:${flush} req:${req} resp:${resp}") 786d5ddbceSLemover XSDebug(req_in.valid || io.tlb.resp.valid, p"tlb: ${tlb}\n") 796d5ddbceSLemover XSDebug(io.ptw.req(0).valid || io.ptw.resp.valid, p"ptw: ${ptw}\n") 806d5ddbceSLemover assert(!RegNext(recv && io.ptw.resp.valid, init = false.B), "re-receive ptw.resp") 81a8bd30cdSLemover XSError(io.ptw.req(0).valid && io.ptw.resp.valid && !flush, "ptw repeater recv resp when sending") 82cca17e78Speixiaokun XSError(io.ptw.resp.valid && (req.vpn =/= io.ptw.resp.bits.s1.entry.tag), "ptw repeater recv resp with wrong tag") 83a8bd30cdSLemover XSError(io.ptw.resp.valid && !io.ptw.resp.ready, "ptw repeater's ptw resp back, but not ready") 849bd9cdfaSLemover TimeOutAssert(sent && !recv, timeOutThreshold, "Repeater doesn't recv resp in time") 856d5ddbceSLemover} 866d5ddbceSLemover 876d5ddbceSLemover/* dtlb 886d5ddbceSLemover * 896d5ddbceSLemover */ 9035d6335eSZhangZifei 91f1fe8698SLemoverclass PTWRepeaterNB(Width: Int = 1, passReady: Boolean = false, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 9235d6335eSZhangZifei val io = IO(new PTWReapterIO(Width)) 9335d6335eSZhangZifei 9435d6335eSZhangZifei val req_in = if (Width == 1) { 9535d6335eSZhangZifei io.tlb.req(0) 9635d6335eSZhangZifei } else { 9735d6335eSZhangZifei val arb = Module(new RRArbiter(io.tlb.req(0).bits.cloneType, Width)) 9835d6335eSZhangZifei arb.io.in <> io.tlb.req 9935d6335eSZhangZifei arb.io.out 10035d6335eSZhangZifei } 101f1fe8698SLemover val (tlb, ptw, flush) = (io.tlb, io.ptw, DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)) 10235d6335eSZhangZifei /* sent: tlb -> repeater -> ptw 10335d6335eSZhangZifei * recv: ptw -> repeater -> tlb 10435d6335eSZhangZifei * different from PTWRepeater 10535d6335eSZhangZifei */ 10635d6335eSZhangZifei 10735d6335eSZhangZifei // tlb -> repeater -> ptw 108935edac4STang Haojin val req = RegEnable(req_in.bits, req_in.fire) 109935edac4STang Haojin val sent = BoolStopWatch(req_in.fire, ptw.req(0).fire || flush) 11035d6335eSZhangZifei req_in.ready := !sent || { if (passReady) ptw.req(0).ready else false.B } 11135d6335eSZhangZifei ptw.req(0).valid := sent 11235d6335eSZhangZifei ptw.req(0).bits := req 11335d6335eSZhangZifei 11435d6335eSZhangZifei // ptw -> repeater -> tlb 115935edac4STang Haojin val resp = RegEnable(ptw.resp.bits, ptw.resp.fire) 116935edac4STang Haojin val recv = BoolStopWatch(ptw.resp.fire, tlb.resp.fire || flush) 11735d6335eSZhangZifei ptw.resp.ready := !recv || { if (passReady) tlb.resp.ready else false.B } 11835d6335eSZhangZifei tlb.resp.valid := recv 11935d6335eSZhangZifei tlb.resp.bits := resp 12035d6335eSZhangZifei 121935edac4STang Haojin XSPerfAccumulate("req", req_in.fire) 122935edac4STang Haojin XSPerfAccumulate("resp", tlb.resp.fire) 12335d6335eSZhangZifei if (!passReady) { 12435d6335eSZhangZifei XSPerfAccumulate("req_blank", req_in.valid && sent && ptw.req(0).ready) 12535d6335eSZhangZifei XSPerfAccumulate("resp_blank", ptw.resp.valid && recv && tlb.resp.ready) 12635d6335eSZhangZifei XSPerfAccumulate("req_blank_ignore_ready", req_in.valid && sent) 12735d6335eSZhangZifei XSPerfAccumulate("resp_blank_ignore_ready", ptw.resp.valid && recv) 12835d6335eSZhangZifei } 12935d6335eSZhangZifei XSDebug(req_in.valid || io.tlb.resp.valid, p"tlb: ${tlb}\n") 13035d6335eSZhangZifei XSDebug(io.ptw.req(0).valid || io.ptw.resp.valid, p"ptw: ${ptw}\n") 13135d6335eSZhangZifei} 13235d6335eSZhangZifei 133185e6164SHaoyuan Fengclass PTWFilterIO(Width: Int, hasHint: Boolean = false)(implicit p: Parameters) extends MMUIOBaseBundle { 134f1fe8698SLemover val tlb = Flipped(new VectorTlbPtwIO(Width)) 135a0301c0dSLemover val ptw = new TlbPtwIO() 136185e6164SHaoyuan Feng val hint = if (hasHint) Some(new TlbHintIO) else None 137d2b20d1aSTang Haojin val rob_head_miss_in_tlb = Output(Bool()) 13860ebee38STang Haojin val debugTopDown = new Bundle { 13960ebee38STang Haojin val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W))) 14060ebee38STang Haojin } 1416d5ddbceSLemover 142f1fe8698SLemover def apply(tlb: VectorTlbPtwIO, ptw: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 14335d6335eSZhangZifei this.tlb <> tlb 14435d6335eSZhangZifei this.ptw <> ptw 14535d6335eSZhangZifei this.sfence <> sfence 14635d6335eSZhangZifei this.csr <> csr 14735d6335eSZhangZifei } 14835d6335eSZhangZifei 149f1fe8698SLemover def apply(tlb: VectorTlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 15035d6335eSZhangZifei this.tlb <> tlb 15135d6335eSZhangZifei this.sfence <> sfence 15235d6335eSZhangZifei this.csr <> csr 15335d6335eSZhangZifei } 15435d6335eSZhangZifei 15545f497a4Shappy-lx} 15645f497a4Shappy-lx 157185e6164SHaoyuan Fengclass PTWFilterEntryIO(Width: Int, hasHint: Boolean = false)(implicit p: Parameters) extends PTWFilterIO(Width, hasHint){ 158185e6164SHaoyuan Feng val flush = Input(Bool()) 159185e6164SHaoyuan Feng val refill = Output(Bool()) 160185e6164SHaoyuan Feng val memidx = Output(new MemBlockidxBundle) 161185e6164SHaoyuan Feng} 162185e6164SHaoyuan Feng 163185e6164SHaoyuan Fengclass PTWFilterEntry(Width: Int, Size: Int, hasHint: Boolean = false)(implicit p: Parameters) extends XSModule with HasPtwConst { 164185e6164SHaoyuan Feng 165185e6164SHaoyuan Feng val io = IO(new PTWFilterEntryIO(Width, hasHint)) 166185e6164SHaoyuan Feng require(isPow2(Size), s"Filter Size ($Size) must be a power of 2") 167185e6164SHaoyuan Feng 168185e6164SHaoyuan Feng def firstValidIndex(v: Seq[Bool], valid: Bool): UInt = { 169185e6164SHaoyuan Feng val index = WireInit(0.U(log2Up(Size).W)) 170185e6164SHaoyuan Feng for (i <- 0 until v.size) { 171185e6164SHaoyuan Feng when (v(i) === valid) { 172185e6164SHaoyuan Feng index := i.U 173185e6164SHaoyuan Feng } 174185e6164SHaoyuan Feng } 175185e6164SHaoyuan Feng index 176185e6164SHaoyuan Feng } 177185e6164SHaoyuan Feng 178185e6164SHaoyuan Feng val v = RegInit(VecInit(Seq.fill(Size)(false.B))) 179185e6164SHaoyuan Feng val sent = RegInit(VecInit(Seq.fill(Size)(false.B))) 180185e6164SHaoyuan Feng val vpn = Reg(Vec(Size, UInt(vpnLen.W))) 181*4c4af37cSpeixiaokun val s2xlate = Reg(Vec(Size, UInt(2.W))) 182185e6164SHaoyuan Feng val memidx = Reg(Vec(Size, new MemBlockidxBundle)) 183185e6164SHaoyuan Feng 184185e6164SHaoyuan Feng val enqvalid = WireInit(VecInit(Seq.fill(Width)(false.B))) 185185e6164SHaoyuan Feng val canenq = WireInit(VecInit(Seq.fill(Width)(false.B))) 186185e6164SHaoyuan Feng val enqidx = WireInit(VecInit(Seq.fill(Width)(0.U(log2Up(Size).W)))) 187185e6164SHaoyuan Feng 188185e6164SHaoyuan Feng //val selectCount = RegInit(0.U(log2Up(Width).W)) 189185e6164SHaoyuan Feng 190185e6164SHaoyuan Feng val entryIsMatchVec = WireInit(VecInit(Seq.fill(Width)(false.B))) 191185e6164SHaoyuan Feng val entryMatchIndexVec = WireInit(VecInit(Seq.fill(Width)(0.U(log2Up(Size).W)))) 192*4c4af37cSpeixiaokun val ptwResp_EntryMatchVec = vpn.zip(v).zip(s2xlate).map{ case ((pi, vi), s2xlatei) => vi && s2xlatei === io.ptw.resp.bits.s2xlate && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, io.csr.vsatp.asid, io.csr.hgatp.asid, true, true)} 193185e6164SHaoyuan Feng val ptwResp_EntryMatchFirst = firstValidIndex(ptwResp_EntryMatchVec, true.B) 194*4c4af37cSpeixiaokun val ptwResp_ReqMatchVec = io.tlb.req.map(a => io.ptw.resp.valid && a.bits.s2xlate === io.ptw.resp.bits.s2xlate && io.ptw.resp.bits.hit(a.bits.vpn, 0.U, 0.U, io.csr.hgatp.asid, allType = true, true)) 195185e6164SHaoyuan Feng 196185e6164SHaoyuan Feng io.refill := Cat(ptwResp_EntryMatchVec).orR && io.ptw.resp.fire 197185e6164SHaoyuan Feng io.ptw.resp.ready := true.B 198185e6164SHaoyuan Feng // DontCare 199185e6164SHaoyuan Feng io.tlb.req.map(_.ready := true.B) 200185e6164SHaoyuan Feng io.tlb.resp.valid := false.B 201*4c4af37cSpeixiaokun io.tlb.resp.bits.data := 0.U.asTypeOf(new PtwRespS2withMemIdx) 202185e6164SHaoyuan Feng io.tlb.resp.bits.vector := 0.U.asTypeOf(Vec(Width, Bool())) 203185e6164SHaoyuan Feng io.memidx := 0.U.asTypeOf(new MemBlockidxBundle) 204185e6164SHaoyuan Feng 205185e6164SHaoyuan Feng // ugly code, should be optimized later 206185e6164SHaoyuan Feng require(Width <= 3, s"DTLB Filter Width ($Width) must equal or less than 3") 207185e6164SHaoyuan Feng if (Width == 1) { 208185e6164SHaoyuan Feng require(Size == 8, s"prefetch filter Size ($Size) should be 8") 209185e6164SHaoyuan Feng canenq(0) := !(Cat(v).andR) 210185e6164SHaoyuan Feng enqidx(0) := firstValidIndex(v, false.B) 211185e6164SHaoyuan Feng } else if (Width == 2) { 212185e6164SHaoyuan Feng require(Size == 8, s"store filter Size ($Size) should be 8") 213185e6164SHaoyuan Feng canenq(0) := !(Cat(v.take(Size/2)).andR) 214185e6164SHaoyuan Feng enqidx(0) := firstValidIndex(v.take(Size/2), false.B) 215185e6164SHaoyuan Feng canenq(1) := !(Cat(v.drop(Size/2)).andR) 216185e6164SHaoyuan Feng enqidx(1) := firstValidIndex(v.drop(Size/2), false.B) + (Size/2).U 217185e6164SHaoyuan Feng } else if (Width == 3) { 218185e6164SHaoyuan Feng require(Size == 16, s"load filter Size ($Size) should be 16") 219185e6164SHaoyuan Feng canenq(0) := !(Cat(v.take(8)).andR) 220185e6164SHaoyuan Feng enqidx(0) := firstValidIndex(v.take(8), false.B) 221185e6164SHaoyuan Feng canenq(1) := !(Cat(v.drop(8).take(4)).andR) 222185e6164SHaoyuan Feng enqidx(1) := firstValidIndex(v.drop(8).take(4), false.B) + 8.U 223185e6164SHaoyuan Feng // four entries for prefetch 224185e6164SHaoyuan Feng canenq(2) := !(Cat(v.drop(12)).andR) 225185e6164SHaoyuan Feng enqidx(2) := firstValidIndex(v.drop(12), false.B) + 12.U 226185e6164SHaoyuan Feng } 227185e6164SHaoyuan Feng 228185e6164SHaoyuan Feng for (i <- 0 until Width) { 229185e6164SHaoyuan Feng enqvalid(i) := io.tlb.req(i).valid && !ptwResp_ReqMatchVec(i) && !entryIsMatchVec(i) && canenq(i) 230185e6164SHaoyuan Feng when (!enqvalid(i)) { 231185e6164SHaoyuan Feng enqidx(i) := entryMatchIndexVec(i) 232185e6164SHaoyuan Feng } 233185e6164SHaoyuan Feng 234*4c4af37cSpeixiaokun val entryIsMatch = vpn.zip(v).zip(s2xlate).map{ case ((pi, vi), s2xlatei) => vi && s2xlatei === io.tlb.req(i).bits.s2xlate && pi === io.tlb.req(i).bits.vpn} 235185e6164SHaoyuan Feng entryIsMatchVec(i) := Cat(entryIsMatch).orR 236185e6164SHaoyuan Feng entryMatchIndexVec(i) := firstValidIndex(entryIsMatch, true.B) 237185e6164SHaoyuan Feng 238185e6164SHaoyuan Feng if (i > 0) { 239185e6164SHaoyuan Feng for (j <- 0 until i) { 240*4c4af37cSpeixiaokun val newIsMatch = io.tlb.req(i).bits.vpn === io.tlb.req(j).bits.vpn && io.tlb.req(i).bits.s2xlate === io.tlb.req(j).bits.s2xlate 241185e6164SHaoyuan Feng when (newIsMatch && io.tlb.req(j).valid) { 242185e6164SHaoyuan Feng enqidx(i) := enqidx(j) 243185e6164SHaoyuan Feng canenq(i) := canenq(j) 244185e6164SHaoyuan Feng enqvalid(i) := false.B 245185e6164SHaoyuan Feng } 246185e6164SHaoyuan Feng } 247185e6164SHaoyuan Feng } 248185e6164SHaoyuan Feng 249185e6164SHaoyuan Feng when (enqvalid(i)) { 250185e6164SHaoyuan Feng v(enqidx(i)) := true.B 251185e6164SHaoyuan Feng sent(enqidx(i)) := false.B 252185e6164SHaoyuan Feng vpn(enqidx(i)) := io.tlb.req(i).bits.vpn 253*4c4af37cSpeixiaokun s2xlate(enqidx(i)) := io.tlb.req(i).bits.s2xlate 254185e6164SHaoyuan Feng memidx(enqidx(i)) := io.tlb.req(i).bits.memidx 255185e6164SHaoyuan Feng } 256185e6164SHaoyuan Feng } 257185e6164SHaoyuan Feng 258185e6164SHaoyuan Feng val issuevec = v.zip(sent).map{ case (v, s) => v && !s} 259185e6164SHaoyuan Feng val issueindex = firstValidIndex(issuevec, true.B) 260185e6164SHaoyuan Feng val canissue = Cat(issuevec).orR 261185e6164SHaoyuan Feng for (i <- 0 until Size) { 262185e6164SHaoyuan Feng io.ptw.req(0).valid := canissue 263185e6164SHaoyuan Feng io.ptw.req(0).bits.vpn := vpn(issueindex) 264*4c4af37cSpeixiaokun io.ptw.req(0).bits.s2xlate := s2xlate(issueindex) 265185e6164SHaoyuan Feng } 266185e6164SHaoyuan Feng when (io.ptw.req(0).fire) { 267185e6164SHaoyuan Feng sent(issueindex) := true.B 268185e6164SHaoyuan Feng } 269185e6164SHaoyuan Feng 270185e6164SHaoyuan Feng when (io.ptw.resp.fire) { 271185e6164SHaoyuan Feng v.zip(ptwResp_EntryMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }} 272185e6164SHaoyuan Feng io.memidx := memidx(ptwResp_EntryMatchFirst) 273185e6164SHaoyuan Feng } 274185e6164SHaoyuan Feng 275185e6164SHaoyuan Feng when (io.flush) { 276185e6164SHaoyuan Feng v.map(_ := false.B) 277185e6164SHaoyuan Feng } 278185e6164SHaoyuan Feng 279185e6164SHaoyuan Feng if (hasHint) { 280185e6164SHaoyuan Feng val hintIO = io.hint.getOrElse(new TlbHintIO) 281185e6164SHaoyuan Feng for (i <- 0 until exuParameters.LduCnt) { 282185e6164SHaoyuan Feng hintIO.req(i).id := enqidx(i) 283185e6164SHaoyuan Feng hintIO.req(i).full := !canenq(i) || ptwResp_ReqMatchVec(i) 284185e6164SHaoyuan Feng } 285185e6164SHaoyuan Feng hintIO.resp.valid := io.refill 286185e6164SHaoyuan Feng hintIO.resp.bits.id := ptwResp_EntryMatchFirst 287185e6164SHaoyuan Feng hintIO.resp.bits.replay_all := PopCount(ptwResp_EntryMatchVec) > 1.U 288185e6164SHaoyuan Feng } 289185e6164SHaoyuan Feng 290185e6164SHaoyuan Feng io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => { 291185e6164SHaoyuan Feng vi && io.debugTopDown.robHeadVaddr.valid && vpni === get_pn(io.debugTopDown.robHeadVaddr.bits) 292185e6164SHaoyuan Feng }}).asUInt.orR 293185e6164SHaoyuan Feng 294185e6164SHaoyuan Feng 295185e6164SHaoyuan Feng // Perf Counter 296185e6164SHaoyuan Feng val counter = PopCount(v) 297185e6164SHaoyuan Feng val inflight_counter = RegInit(0.U(log2Up(Size).W)) 298185e6164SHaoyuan Feng val inflight_full = inflight_counter === Size.U 299185e6164SHaoyuan Feng when (io.ptw.req(0).fire =/= io.ptw.resp.fire) { 300185e6164SHaoyuan Feng inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U) 301185e6164SHaoyuan Feng } 302185e6164SHaoyuan Feng 303185e6164SHaoyuan Feng assert(inflight_counter <= Size.U, "inflight should be no more than Size") 304185e6164SHaoyuan Feng when (counter === 0.U) { 305185e6164SHaoyuan Feng assert(!io.ptw.req(0).fire, "when counter is 0, should not req") 306185e6164SHaoyuan Feng } 307185e6164SHaoyuan Feng 308185e6164SHaoyuan Feng when (io.flush) { 309185e6164SHaoyuan Feng inflight_counter := 0.U 310185e6164SHaoyuan Feng } 311185e6164SHaoyuan Feng 312185e6164SHaoyuan Feng XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid)))) 313185e6164SHaoyuan Feng XSPerfAccumulate("tlb_req_count_filtered", PopCount(enqvalid)) 314185e6164SHaoyuan Feng XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire) 315185e6164SHaoyuan Feng XSPerfAccumulate("ptw_req_cycle", inflight_counter) 316185e6164SHaoyuan Feng XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire) 317185e6164SHaoyuan Feng XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire) 318185e6164SHaoyuan Feng XSPerfAccumulate("inflight_cycle", Cat(sent).orR) 319185e6164SHaoyuan Feng 320185e6164SHaoyuan Feng for (i <- 0 until Size + 1) { 321185e6164SHaoyuan Feng XSPerfAccumulate(s"counter${i}", counter === i.U) 322185e6164SHaoyuan Feng } 323185e6164SHaoyuan Feng 324185e6164SHaoyuan Feng for (i <- 0 until Size) { 325185e6164SHaoyuan Feng TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time") 326185e6164SHaoyuan Feng } 327185e6164SHaoyuan Feng 328185e6164SHaoyuan Feng} 329185e6164SHaoyuan Feng 330185e6164SHaoyuan Fengclass PTWNewFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 331185e6164SHaoyuan Feng require(Size >= Width) 332185e6164SHaoyuan Feng 333185e6164SHaoyuan Feng val io = IO(new PTWFilterIO(Width, hasHint = true)) 334185e6164SHaoyuan Feng 335185e6164SHaoyuan Feng val load_filter = VecInit(Seq.fill(1) { 336185e6164SHaoyuan Feng val load_entry = Module(new PTWFilterEntry(Width = exuParameters.LduCnt + 1, Size = loadfiltersize, hasHint = true)) 337185e6164SHaoyuan Feng load_entry.io 338185e6164SHaoyuan Feng }) 339185e6164SHaoyuan Feng 340185e6164SHaoyuan Feng val store_filter = VecInit(Seq.fill(1) { 341185e6164SHaoyuan Feng val store_entry = Module(new PTWFilterEntry(Width = exuParameters.StuCnt, Size = storefiltersize)) 342185e6164SHaoyuan Feng store_entry.io 343185e6164SHaoyuan Feng }) 344185e6164SHaoyuan Feng 345185e6164SHaoyuan Feng val prefetch_filter = VecInit(Seq.fill(1) { 346185e6164SHaoyuan Feng val prefetch_entry = Module(new PTWFilterEntry(Width = 1, Size = prefetchfiltersize)) 347185e6164SHaoyuan Feng prefetch_entry.io 348185e6164SHaoyuan Feng }) 349185e6164SHaoyuan Feng 350185e6164SHaoyuan Feng val filter = load_filter ++ store_filter ++ prefetch_filter 351185e6164SHaoyuan Feng 352185e6164SHaoyuan Feng load_filter.map(_.tlb.req := io.tlb.req.take(exuParameters.LduCnt + 1)) 353185e6164SHaoyuan Feng store_filter.map(_.tlb.req := io.tlb.req.drop(exuParameters.LduCnt + 1).take(exuParameters.StuCnt)) 354185e6164SHaoyuan Feng prefetch_filter.map(_.tlb.req := io.tlb.req.drop(exuParameters.LduCnt + 1 + exuParameters.StuCnt)) 355185e6164SHaoyuan Feng 356185e6164SHaoyuan Feng val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay) 357185e6164SHaoyuan Feng val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire) 358185e6164SHaoyuan Feng val ptwResp_valid = Cat(filter.map(_.refill)).orR 359185e6164SHaoyuan Feng filter.map(_.tlb.resp.ready := true.B) 360185e6164SHaoyuan Feng filter.map(_.ptw.resp.valid := RegNext(io.ptw.resp.fire, init = false.B)) 361185e6164SHaoyuan Feng filter.map(_.ptw.resp.bits := ptwResp) 362185e6164SHaoyuan Feng filter.map(_.flush := flush) 363185e6164SHaoyuan Feng filter.map(_.sfence := io.sfence) 364185e6164SHaoyuan Feng filter.map(_.csr := io.csr) 365185e6164SHaoyuan Feng filter.map(_.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr) 366185e6164SHaoyuan Feng 367185e6164SHaoyuan Feng io.tlb.req.map(_.ready := true.B) 368185e6164SHaoyuan Feng io.tlb.resp.valid := ptwResp_valid 369*4c4af37cSpeixiaokun io.tlb.resp.bits.data.s2xlate := ptwResp.s2xlate 370*4c4af37cSpeixiaokun io.tlb.resp.bits.data.s1 := ptwResp.s1 371*4c4af37cSpeixiaokun io.tlb.resp.bits.data.s2 := ptwResp.s2 372185e6164SHaoyuan Feng io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle) 373185e6164SHaoyuan Feng // vector used to represent different requestors of DTLB 374185e6164SHaoyuan Feng // (e.g. the store DTLB has StuCnt requestors) 375185e6164SHaoyuan Feng // However, it is only necessary to distinguish between different DTLB now 376185e6164SHaoyuan Feng for (i <- 0 until Width) { 377185e6164SHaoyuan Feng io.tlb.resp.bits.vector(i) := false.B 378185e6164SHaoyuan Feng } 379185e6164SHaoyuan Feng io.tlb.resp.bits.vector(0) := load_filter(0).refill 380185e6164SHaoyuan Feng io.tlb.resp.bits.vector(exuParameters.LduCnt + 1) := store_filter(0).refill 381185e6164SHaoyuan Feng io.tlb.resp.bits.vector(exuParameters.LduCnt + 1 + exuParameters.StuCnt) := prefetch_filter(0).refill 382185e6164SHaoyuan Feng 383185e6164SHaoyuan Feng val hintIO = io.hint.getOrElse(new TlbHintIO) 384185e6164SHaoyuan Feng val load_hintIO = load_filter(0).hint.getOrElse(new TlbHintIO) 385185e6164SHaoyuan Feng for (i <- 0 until exuParameters.LduCnt) { 386185e6164SHaoyuan Feng hintIO.req(i) := RegNext(load_hintIO.req(i)) 387185e6164SHaoyuan Feng } 388185e6164SHaoyuan Feng hintIO.resp := RegNext(load_hintIO.resp) 389185e6164SHaoyuan Feng 390185e6164SHaoyuan Feng when (load_filter(0).refill) { 391185e6164SHaoyuan Feng io.tlb.resp.bits.vector(0) := true.B 392185e6164SHaoyuan Feng io.tlb.resp.bits.data.memidx := load_filter(0).memidx 393185e6164SHaoyuan Feng } 394185e6164SHaoyuan Feng when (store_filter(0).refill) { 395185e6164SHaoyuan Feng io.tlb.resp.bits.vector(exuParameters.LduCnt + 1) := true.B 396185e6164SHaoyuan Feng io.tlb.resp.bits.data.memidx := store_filter(0).memidx 397185e6164SHaoyuan Feng } 398185e6164SHaoyuan Feng when (prefetch_filter(0).refill) { 399185e6164SHaoyuan Feng io.tlb.resp.bits.vector(exuParameters.LduCnt + 1 + exuParameters.StuCnt) := true.B 400185e6164SHaoyuan Feng io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle) 401185e6164SHaoyuan Feng } 402185e6164SHaoyuan Feng 403185e6164SHaoyuan Feng val ptw_arb = Module(new RRArbiterInit(new PtwReq, 3)) 404185e6164SHaoyuan Feng for (i <- 0 until 3) { 405185e6164SHaoyuan Feng ptw_arb.io.in(i).valid := filter(i).ptw.req(0).valid 406185e6164SHaoyuan Feng ptw_arb.io.in(i).bits.vpn := filter(i).ptw.req(0).bits.vpn 407*4c4af37cSpeixiaokun ptw_arb.io.in(i).bits.s2xlate := filter(i).ptw.req(0).bits.s2xlate 408185e6164SHaoyuan Feng filter(i).ptw.req(0).ready := ptw_arb.io.in(i).ready 409185e6164SHaoyuan Feng } 410185e6164SHaoyuan Feng ptw_arb.io.out.ready := io.ptw.req(0).ready 411185e6164SHaoyuan Feng io.ptw.req(0).valid := ptw_arb.io.out.valid 412185e6164SHaoyuan Feng io.ptw.req(0).bits.vpn := ptw_arb.io.out.bits.vpn 413*4c4af37cSpeixiaokun io.ptw.req(0).bits.s2xlate := ptw_arb.io.out.bits.s2xlate 414185e6164SHaoyuan Feng io.ptw.resp.ready := true.B 415185e6164SHaoyuan Feng 416185e6164SHaoyuan Feng io.rob_head_miss_in_tlb := Cat(filter.map(_.rob_head_miss_in_tlb)).orR 417185e6164SHaoyuan Feng} 418185e6164SHaoyuan Feng 419f1fe8698SLemoverclass PTWFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 4206d5ddbceSLemover require(Size >= Width) 4216d5ddbceSLemover 42245f497a4Shappy-lx val io = IO(new PTWFilterIO(Width)) 42345f497a4Shappy-lx 4246d5ddbceSLemover val v = RegInit(VecInit(Seq.fill(Size)(false.B))) 425a0301c0dSLemover val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports 4266d5ddbceSLemover val vpn = Reg(Vec(Size, UInt(vpnLen.W))) 427d0de7e4aSpeixiaokun val s2xlate = Reg(Vec(Size, UInt(2.W))) 4288744445eSMaxpicca-Li val memidx = Reg(Vec(Size, new MemBlockidxBundle)) 4296d5ddbceSLemover val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq 4306d5ddbceSLemover val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw 4316d5ddbceSLemover val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq 4326d5ddbceSLemover val mayFullDeq = RegInit(false.B) 4336d5ddbceSLemover val mayFullIss = RegInit(false.B) 4346d5ddbceSLemover val counter = RegInit(0.U(log2Up(Size+1).W)) 435cca17e78Speixiaokun val flush = DelayN(io.sfence.valid || io.csr.satp.changed || (io.csr.priv.virt && io.csr.vsatp.changed), FenceDelay) 436f1fe8698SLemover val tlb_req = WireInit(io.tlb.req) // NOTE: tlb_req is not io.tlb.req, see below codes, just use cloneType 437cccfc98dSLemover tlb_req.suggestName("tlb_req") 438cccfc98dSLemover 439fa9f9690SLemover val inflight_counter = RegInit(0.U(log2Up(Size + 1).W)) 440fa9f9690SLemover val inflight_full = inflight_counter === Size.U 441d0de7e4aSpeixiaokun 442d0de7e4aSpeixiaokun def ptwResp_hit(vpn: UInt, s2xlate: UInt, resp: PtwRespS2): Bool = { 443cca17e78Speixiaokun val enableS2xlate = resp.s2xlate =/= noS2xlate 444ad0d9d89Speixiaokun val onlyS2 = resp.s2xlate === onlyStage2 445cca17e78Speixiaokun val s1hit = resp.s1.hit(vpn, 0.U, io.csr.hgatp.asid, true, true, enableS2xlate) 446d0de7e4aSpeixiaokun val s2hit = resp.s2.hit(vpn, io.csr.hgatp.asid) 447496c751cSpeixiaokun s2xlate === resp.s2xlate && Mux(enableS2xlate && onlyS2, s2hit, s1hit) 448d0de7e4aSpeixiaokun } 449d0de7e4aSpeixiaokun 450935edac4STang Haojin when (io.ptw.req(0).fire =/= io.ptw.resp.fire) { 451935edac4STang Haojin inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U) 452fa9f9690SLemover } 453fa9f9690SLemover 45487f41827SLemover val canEnqueue = Wire(Bool()) // NOTE: actually enqueue 455935edac4STang Haojin val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire) 456d0de7e4aSpeixiaokun val ptwResp_OldMatchVec = vpn.zip(v).zip(s2xlate).map { case (((vpn, v), s2xlate)) =>{ 4576f487a5dSpeixiaokun v && ptwResp_hit(vpn, s2xlate, io.ptw.resp.bits) 458d0de7e4aSpeixiaokun } 459d0de7e4aSpeixiaokun } 460935edac4STang Haojin val ptwResp_valid = RegNext(io.ptw.resp.fire && Cat(ptwResp_OldMatchVec).orR, init = false.B) 46163632028SHaoyuan Feng // May send repeated requests to L2 tlb with same vpn(26, 3) when sector tlb 462d0de7e4aSpeixiaokun val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).zip(s2xlate).map{ case ((pi, vi), s2xlate) => vi && pi === a.bits.vpn && s2xlate === a.bits.s2xlate }) 463d0de7e4aSpeixiaokun val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn && canEnqueue && b.bits.s2xlate === a.bits.s2xlate}) 464d0de7e4aSpeixiaokun val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn && a.bits.s2xlate === b.bits.s2xlate)) 465cccfc98dSLemover 466cccfc98dSLemover (0 until Width) foreach { i => 467cccfc98dSLemover tlb_req(i).valid := RegNext(io.tlb.req(i).valid && 468d0de7e4aSpeixiaokun !(ptwResp_valid && ptwResp_hit(io.tlb.req(i).bits.vpn, io.tlb.req(i).bits.s2xlate, ptwResp)) && 469cccfc98dSLemover !Cat(lastReqMatchVec_early(i)).orR, 470cccfc98dSLemover init = false.B) 471cccfc98dSLemover tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid) 472cccfc98dSLemover } 473cccfc98dSLemover 474cccfc98dSLemover val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR)) 475cccfc98dSLemover val newMatchVec = (0 until Width).map(i => (0 until Width).map(j => 476cccfc98dSLemover RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid 477cccfc98dSLemover )) 478cccfc98dSLemover val ptwResp_newMatchVec = tlb_req.map(a => 479d0de7e4aSpeixiaokun ptwResp_valid && ptwResp_hit(a.bits.vpn, a.bits.s2xlate, ptwResp)) 480cccfc98dSLemover 481cccfc98dSLemover val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid)) 482cccfc98dSLemover val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i))) 483a0301c0dSLemover val ports_init = (0 until Width).map(i => (1 << i).U(Width.W)) 484a0301c0dSLemover val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i))) 485935edac4STang Haojin val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire) 4866d5ddbceSLemover 487a0301c0dSLemover def canMerge(index: Int) : Bool = { 488cccfc98dSLemover ptwResp_newMatchVec(index) || oldMatchVec(index) || 489a0301c0dSLemover Cat(newMatchVec(index).take(index)).orR 490a0301c0dSLemover } 491a0301c0dSLemover 492a0301c0dSLemover def filter_req() = { 493a0301c0dSLemover val reqs = tlb_req.indices.map{ i => 4948744445eSMaxpicca-Li val req = Wire(ValidIO(new PtwReqwithMemIdx())) 495a0301c0dSLemover val merge = canMerge(i) 496a0301c0dSLemover req.bits := tlb_req(i).bits 497a0301c0dSLemover req.valid := !merge && tlb_req(i).valid 498a0301c0dSLemover req 499a0301c0dSLemover } 500a0301c0dSLemover reqs 501a0301c0dSLemover } 502a0301c0dSLemover 503a0301c0dSLemover val reqs = filter_req() 504a0301c0dSLemover val req_ports = filter_ports 5056d5ddbceSLemover val isFull = enqPtr === deqPtr && mayFullDeq 5066d5ddbceSLemover val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq 5076d5ddbceSLemover val isEmptyIss = enqPtr === issPtr && !mayFullIss 5086d5ddbceSLemover val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid))) 509cccfc98dSLemover val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U)) 510cccfc98dSLemover val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i)))) 5116d5ddbceSLemover val enqNum = PopCount(reqs.map(_.valid)) 51287f41827SLemover canEnqueue := counter +& enqNum <= Size.U 5136d5ddbceSLemover 514f1fe8698SLemover // the req may recv false ready, but actually received. Filter and TLB will handle it. 515f1fe8698SLemover val enqNum_fake = PopCount(io.tlb.req.map(_.valid)) 516f1fe8698SLemover val canEnqueue_fake = counter +& enqNum_fake <= Size.U 517f1fe8698SLemover io.tlb.req.map(_.ready := canEnqueue_fake) // NOTE: just drop un-fire reqs 518f1fe8698SLemover 5190ab9ba15SLemover // tlb req flushed by ptw resp: last ptw resp && current ptw resp 5200ab9ba15SLemover // the flushed tlb req will fakely enq, with a false valid 521d0de7e4aSpeixiaokun val tlb_req_flushed = reqs.map(a => io.ptw.resp.valid && ptwResp_hit(a.bits.vpn, a.bits.s2xlate, io.ptw.resp.bits)) 5220ab9ba15SLemover 523cccfc98dSLemover io.tlb.resp.valid := ptwResp_valid 52450c7aa78Speixiaokun io.tlb.resp.bits.data.s2xlate := ptwResp.s2xlate 52550c7aa78Speixiaokun io.tlb.resp.bits.data.s1 := ptwResp.s1 52650c7aa78Speixiaokun io.tlb.resp.bits.data.s2 := ptwResp.s2 5278744445eSMaxpicca-Li io.tlb.resp.bits.data.memidx := memidx(OHToUInt(ptwResp_OldMatchVec)) 528a0301c0dSLemover io.tlb.resp.bits.vector := resp_vector 5292c2c1588SLemover 530fa9f9690SLemover val issue_valid = v(issPtr) && !isEmptyIss && !inflight_full 531d0de7e4aSpeixiaokun val issue_filtered = ptwResp_valid && ptwResp_hit(io.ptw.req(0).bits.vpn, io.ptw.req(0).bits.s2xlate, ptwResp) 5322c2c1588SLemover val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/)) 5332c2c1588SLemover io.ptw.req(0).valid := issue_valid && !issue_filtered 5346d5ddbceSLemover io.ptw.req(0).bits.vpn := vpn(issPtr) 535d0de7e4aSpeixiaokun io.ptw.req(0).bits.s2xlate := s2xlate(issPtr) 5366d5ddbceSLemover io.ptw.resp.ready := true.B 5376d5ddbceSLemover 5386d5ddbceSLemover reqs.zipWithIndex.map{ 5396d5ddbceSLemover case (req, i) => 5406d5ddbceSLemover when (req.valid && canEnqueue) { 5410ab9ba15SLemover v(enqPtrVec(i)) := !tlb_req_flushed(i) 5426d5ddbceSLemover vpn(enqPtrVec(i)) := req.bits.vpn 543d0de7e4aSpeixiaokun s2xlate(enqPtrVec(i)) := req.bits.s2xlate 5448744445eSMaxpicca-Li memidx(enqPtrVec(i)) := req.bits.memidx 545a0301c0dSLemover ports(enqPtrVec(i)) := req_ports(i).asBools 546a0301c0dSLemover } 547a0301c0dSLemover } 548a0301c0dSLemover for (i <- ports.indices) { 549a0301c0dSLemover when (v(i)) { 550a0301c0dSLemover ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2) 5516d5ddbceSLemover } 5526d5ddbceSLemover } 5536d5ddbceSLemover 5546d5ddbceSLemover val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR 5556d5ddbceSLemover val do_deq = (!v(deqPtr) && !isEmptyDeq) 5562c2c1588SLemover val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss) 5576d5ddbceSLemover when (do_enq) { 5586d5ddbceSLemover enqPtr := enqPtr + enqNum 5596d5ddbceSLemover } 5606d5ddbceSLemover when (do_deq) { 5616d5ddbceSLemover deqPtr := deqPtr + 1.U 5626d5ddbceSLemover } 5636d5ddbceSLemover when (do_iss) { 5646d5ddbceSLemover issPtr := issPtr + 1.U 5656d5ddbceSLemover } 5662c2c1588SLemover when (issue_fire_fake && issue_filtered) { // issued but is filtered 5672c2c1588SLemover v(issPtr) := false.B 5682c2c1588SLemover } 5696d5ddbceSLemover when (do_enq =/= do_deq) { 5706d5ddbceSLemover mayFullDeq := do_enq 5716d5ddbceSLemover } 5726d5ddbceSLemover when (do_enq =/= do_iss) { 5736d5ddbceSLemover mayFullIss := do_enq 5746d5ddbceSLemover } 5756d5ddbceSLemover 576935edac4STang Haojin when (io.ptw.resp.fire) { 577cccfc98dSLemover v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }} 5786d5ddbceSLemover } 5796d5ddbceSLemover 5806d5ddbceSLemover counter := counter - do_deq + Mux(do_enq, enqNum, 0.U) 581fa9f9690SLemover assert(counter <= Size.U, "counter should be no more than Size") 582fa9f9690SLemover assert(inflight_counter <= Size.U, "inflight should be no more than Size") 5836d5ddbceSLemover when (counter === 0.U) { 584935edac4STang Haojin assert(!io.ptw.req(0).fire, "when counter is 0, should not req") 5856d5ddbceSLemover assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty") 5866d5ddbceSLemover } 5876d5ddbceSLemover when (counter === Size.U) { 5886d5ddbceSLemover assert(mayFullDeq, "when counter is Size, should be full") 5896d5ddbceSLemover } 5906d5ddbceSLemover 59145f497a4Shappy-lx when (flush) { 5926d5ddbceSLemover v.map(_ := false.B) 5936d5ddbceSLemover deqPtr := 0.U 5946d5ddbceSLemover enqPtr := 0.U 5956d5ddbceSLemover issPtr := 0.U 5966d5ddbceSLemover ptwResp_valid := false.B 5976d5ddbceSLemover mayFullDeq := false.B 5986d5ddbceSLemover mayFullIss := false.B 5996d5ddbceSLemover counter := 0.U 600fa9f9690SLemover inflight_counter := 0.U 6016d5ddbceSLemover } 6026d5ddbceSLemover 60360ebee38STang Haojin val robHeadVaddr = io.debugTopDown.robHeadVaddr 604d2b20d1aSTang Haojin io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => { 60560ebee38STang Haojin vi && robHeadVaddr.valid && vpni === get_pn(robHeadVaddr.bits) 606d2b20d1aSTang Haojin }}).asUInt.orR 607d2b20d1aSTang Haojin 6086d5ddbceSLemover // perf 6096d5ddbceSLemover XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid)))) 6106d5ddbceSLemover XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U)) 611935edac4STang Haojin XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire) 6126d5ddbceSLemover XSPerfAccumulate("ptw_req_cycle", inflight_counter) 613935edac4STang Haojin XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire) 614935edac4STang Haojin XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire) 6156d5ddbceSLemover XSPerfAccumulate("inflight_cycle", !isEmptyDeq) 6166d5ddbceSLemover for (i <- 0 until Size + 1) { 6176d5ddbceSLemover XSPerfAccumulate(s"counter${i}", counter === i.U) 6186d5ddbceSLemover } 6199bd9cdfaSLemover 6209bd9cdfaSLemover for (i <- 0 until Size) { 6219bd9cdfaSLemover TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time") 6229bd9cdfaSLemover } 6236d5ddbceSLemover} 62438ba1efdSLemover 62538ba1efdSLemoverobject PTWRepeater { 626f1fe8698SLemover def apply(fenceDelay: Int, 62738ba1efdSLemover tlb: TlbPtwIO, 62838ba1efdSLemover sfence: SfenceBundle, 62938ba1efdSLemover csr: TlbCsrBundle 63038ba1efdSLemover )(implicit p: Parameters) = { 63138ba1efdSLemover val width = tlb.req.size 632f1fe8698SLemover val repeater = Module(new PTWRepeater(width, fenceDelay)) 63335d6335eSZhangZifei repeater.io.apply(tlb, sfence, csr) 63438ba1efdSLemover repeater 63538ba1efdSLemover } 63638ba1efdSLemover 637f1fe8698SLemover def apply(fenceDelay: Int, 63838ba1efdSLemover tlb: TlbPtwIO, 63938ba1efdSLemover ptw: TlbPtwIO, 64038ba1efdSLemover sfence: SfenceBundle, 64138ba1efdSLemover csr: TlbCsrBundle 64238ba1efdSLemover )(implicit p: Parameters) = { 64338ba1efdSLemover val width = tlb.req.size 644f1fe8698SLemover val repeater = Module(new PTWRepeater(width, fenceDelay)) 64535d6335eSZhangZifei repeater.io.apply(tlb, ptw, sfence, csr) 64635d6335eSZhangZifei repeater 64735d6335eSZhangZifei } 64835d6335eSZhangZifei} 64938ba1efdSLemover 65035d6335eSZhangZifeiobject PTWRepeaterNB { 651f1fe8698SLemover def apply(passReady: Boolean, fenceDelay: Int, 65235d6335eSZhangZifei tlb: TlbPtwIO, 65335d6335eSZhangZifei sfence: SfenceBundle, 65435d6335eSZhangZifei csr: TlbCsrBundle 65535d6335eSZhangZifei )(implicit p: Parameters) = { 65635d6335eSZhangZifei val width = tlb.req.size 657f1fe8698SLemover val repeater = Module(new PTWRepeaterNB(width, passReady,fenceDelay)) 65835d6335eSZhangZifei repeater.io.apply(tlb, sfence, csr) 65935d6335eSZhangZifei repeater 66035d6335eSZhangZifei } 66135d6335eSZhangZifei 662f1fe8698SLemover def apply(passReady: Boolean, fenceDelay: Int, 66335d6335eSZhangZifei tlb: TlbPtwIO, 66435d6335eSZhangZifei ptw: TlbPtwIO, 66535d6335eSZhangZifei sfence: SfenceBundle, 66635d6335eSZhangZifei csr: TlbCsrBundle 66735d6335eSZhangZifei )(implicit p: Parameters) = { 66835d6335eSZhangZifei val width = tlb.req.size 669f1fe8698SLemover val repeater = Module(new PTWRepeaterNB(width, passReady, fenceDelay)) 67035d6335eSZhangZifei repeater.io.apply(tlb, ptw, sfence, csr) 67138ba1efdSLemover repeater 67238ba1efdSLemover } 67338ba1efdSLemover} 67438ba1efdSLemover 67538ba1efdSLemoverobject PTWFilter { 676f1fe8698SLemover def apply(fenceDelay: Int, 677f1fe8698SLemover tlb: VectorTlbPtwIO, 67838ba1efdSLemover ptw: TlbPtwIO, 67938ba1efdSLemover sfence: SfenceBundle, 68038ba1efdSLemover csr: TlbCsrBundle, 68138ba1efdSLemover size: Int 68238ba1efdSLemover )(implicit p: Parameters) = { 68338ba1efdSLemover val width = tlb.req.size 684f1fe8698SLemover val filter = Module(new PTWFilter(width, size, fenceDelay)) 68535d6335eSZhangZifei filter.io.apply(tlb, ptw, sfence, csr) 68638ba1efdSLemover filter 68738ba1efdSLemover } 68835d6335eSZhangZifei 689f1fe8698SLemover def apply(fenceDelay: Int, 690f1fe8698SLemover tlb: VectorTlbPtwIO, 69135d6335eSZhangZifei sfence: SfenceBundle, 69235d6335eSZhangZifei csr: TlbCsrBundle, 69335d6335eSZhangZifei size: Int 69435d6335eSZhangZifei )(implicit p: Parameters) = { 69535d6335eSZhangZifei val width = tlb.req.size 696f1fe8698SLemover val filter = Module(new PTWFilter(width, size, fenceDelay)) 69735d6335eSZhangZifei filter.io.apply(tlb, sfence, csr) 69835d6335eSZhangZifei filter 69935d6335eSZhangZifei } 700185e6164SHaoyuan Feng} 70135d6335eSZhangZifei 702185e6164SHaoyuan Fengobject PTWNewFilter { 703185e6164SHaoyuan Feng def apply(fenceDelay: Int, 704185e6164SHaoyuan Feng tlb: VectorTlbPtwIO, 705185e6164SHaoyuan Feng ptw: TlbPtwIO, 706185e6164SHaoyuan Feng sfence: SfenceBundle, 707185e6164SHaoyuan Feng csr: TlbCsrBundle, 708185e6164SHaoyuan Feng size: Int 709185e6164SHaoyuan Feng )(implicit p: Parameters) = { 710185e6164SHaoyuan Feng val width = tlb.req.size 711185e6164SHaoyuan Feng val filter = Module(new PTWNewFilter(width, size, fenceDelay)) 712185e6164SHaoyuan Feng filter.io.apply(tlb, ptw, sfence, csr) 713185e6164SHaoyuan Feng filter 714185e6164SHaoyuan Feng } 715185e6164SHaoyuan Feng 716185e6164SHaoyuan Feng def apply(fenceDelay: Int, 717185e6164SHaoyuan Feng tlb: VectorTlbPtwIO, 718185e6164SHaoyuan Feng sfence: SfenceBundle, 719185e6164SHaoyuan Feng csr: TlbCsrBundle, 720185e6164SHaoyuan Feng size: Int 721185e6164SHaoyuan Feng )(implicit p: Parameters) = { 722185e6164SHaoyuan Feng val width = tlb.req.size 723185e6164SHaoyuan Feng val filter = Module(new PTWNewFilter(width, size, fenceDelay)) 724185e6164SHaoyuan Feng filter.io.apply(tlb, sfence, csr) 725185e6164SHaoyuan Feng filter 726185e6164SHaoyuan Feng } 72738ba1efdSLemover} 728