xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/Repeater.scala (revision 71489510d9fea663a88bc55495efdba34d5256d1)
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  }
58f1fe8698SLemover  val (tlb, ptw, flush) = (io.tlb, io.ptw, DelayN(io.sfence.valid || io.csr.satp.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")
82a8bd30cdSLemover  XSError(io.ptw.resp.valid && (req.vpn =/= io.ptw.resp.bits.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 {
164*71489510SXuan Hu  private val LdExuCnt = backendParams.LdExuCnt
165185e6164SHaoyuan Feng
166185e6164SHaoyuan Feng  val io = IO(new PTWFilterEntryIO(Width, hasHint))
167185e6164SHaoyuan Feng  require(isPow2(Size), s"Filter Size ($Size) must be a power of 2")
168185e6164SHaoyuan Feng
169185e6164SHaoyuan Feng  def firstValidIndex(v: Seq[Bool], valid: Bool): UInt = {
170185e6164SHaoyuan Feng    val index = WireInit(0.U(log2Up(Size).W))
171185e6164SHaoyuan Feng    for (i <- 0 until v.size) {
172185e6164SHaoyuan Feng      when (v(i) === valid) {
173185e6164SHaoyuan Feng        index := i.U
174185e6164SHaoyuan Feng      }
175185e6164SHaoyuan Feng    }
176185e6164SHaoyuan Feng    index
177185e6164SHaoyuan Feng  }
178185e6164SHaoyuan Feng
179185e6164SHaoyuan Feng  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
180185e6164SHaoyuan Feng  val sent = RegInit(VecInit(Seq.fill(Size)(false.B)))
181185e6164SHaoyuan Feng  val vpn = Reg(Vec(Size, UInt(vpnLen.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))))
192185e6164SHaoyuan Feng  val ptwResp_EntryMatchVec = vpn.zip(v).map{ case (pi, vi) => vi && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, true, true)}
193185e6164SHaoyuan Feng  val ptwResp_EntryMatchFirst = firstValidIndex(ptwResp_EntryMatchVec, true.B)
194185e6164SHaoyuan Feng  val ptwResp_ReqMatchVec = io.tlb.req.map(a => io.ptw.resp.valid && io.ptw.resp.bits.hit(a.bits.vpn, 0.U, 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
201185e6164SHaoyuan Feng  io.tlb.resp.bits.data := 0.U.asTypeOf(new PtwSectorRespwithMemIdx)
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
234185e6164SHaoyuan Feng    val entryIsMatch = vpn.zip(v).map{ case (pi, vi) => vi && 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) {
240185e6164SHaoyuan Feng        val newIsMatch = io.tlb.req(i).bits.vpn === io.tlb.req(j).bits.vpn
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
253185e6164SHaoyuan Feng      memidx(enqidx(i)) := io.tlb.req(i).bits.memidx
254185e6164SHaoyuan Feng    }
255185e6164SHaoyuan Feng  }
256185e6164SHaoyuan Feng
257185e6164SHaoyuan Feng  val issuevec = v.zip(sent).map{ case (v, s) => v && !s}
258185e6164SHaoyuan Feng  val issueindex = firstValidIndex(issuevec, true.B)
259185e6164SHaoyuan Feng  val canissue = Cat(issuevec).orR
260185e6164SHaoyuan Feng  for (i <- 0 until Size) {
261185e6164SHaoyuan Feng    io.ptw.req(0).valid := canissue
262185e6164SHaoyuan Feng    io.ptw.req(0).bits.vpn := vpn(issueindex)
263185e6164SHaoyuan Feng  }
264185e6164SHaoyuan Feng  when (io.ptw.req(0).fire) {
265185e6164SHaoyuan Feng    sent(issueindex) := true.B
266185e6164SHaoyuan Feng  }
267185e6164SHaoyuan Feng
268185e6164SHaoyuan Feng  when (io.ptw.resp.fire) {
269185e6164SHaoyuan Feng    v.zip(ptwResp_EntryMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
270185e6164SHaoyuan Feng    io.memidx := memidx(ptwResp_EntryMatchFirst)
271185e6164SHaoyuan Feng  }
272185e6164SHaoyuan Feng
273185e6164SHaoyuan Feng  when (io.flush) {
274185e6164SHaoyuan Feng    v.map(_ := false.B)
275185e6164SHaoyuan Feng  }
276185e6164SHaoyuan Feng
277185e6164SHaoyuan Feng  if (hasHint) {
278185e6164SHaoyuan Feng    val hintIO = io.hint.getOrElse(new TlbHintIO)
279*71489510SXuan Hu    for (i <- 0 until LdExuCnt) {
280185e6164SHaoyuan Feng      hintIO.req(i).id := enqidx(i)
281185e6164SHaoyuan Feng      hintIO.req(i).full := !canenq(i) || ptwResp_ReqMatchVec(i)
282185e6164SHaoyuan Feng    }
283185e6164SHaoyuan Feng    hintIO.resp.valid := io.refill
284185e6164SHaoyuan Feng    hintIO.resp.bits.id := ptwResp_EntryMatchFirst
285185e6164SHaoyuan Feng    hintIO.resp.bits.replay_all := PopCount(ptwResp_EntryMatchVec) > 1.U
286185e6164SHaoyuan Feng  }
287185e6164SHaoyuan Feng
288185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
289185e6164SHaoyuan Feng    vi && io.debugTopDown.robHeadVaddr.valid && vpni === get_pn(io.debugTopDown.robHeadVaddr.bits)
290185e6164SHaoyuan Feng  }}).asUInt.orR
291185e6164SHaoyuan Feng
292185e6164SHaoyuan Feng
293185e6164SHaoyuan Feng  // Perf Counter
294185e6164SHaoyuan Feng  val counter = PopCount(v)
295185e6164SHaoyuan Feng  val inflight_counter = RegInit(0.U(log2Up(Size).W))
296185e6164SHaoyuan Feng  val inflight_full = inflight_counter === Size.U
297185e6164SHaoyuan Feng  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
298185e6164SHaoyuan Feng    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
299185e6164SHaoyuan Feng  }
300185e6164SHaoyuan Feng
301185e6164SHaoyuan Feng  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
302185e6164SHaoyuan Feng  when (counter === 0.U) {
303185e6164SHaoyuan Feng    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
304185e6164SHaoyuan Feng  }
305185e6164SHaoyuan Feng
306185e6164SHaoyuan Feng  when (io.flush) {
307185e6164SHaoyuan Feng    inflight_counter := 0.U
308185e6164SHaoyuan Feng  }
309185e6164SHaoyuan Feng
310185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
311185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count_filtered", PopCount(enqvalid))
312185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
313185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
314185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
315185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
316185e6164SHaoyuan Feng  XSPerfAccumulate("inflight_cycle", Cat(sent).orR)
317185e6164SHaoyuan Feng
318185e6164SHaoyuan Feng  for (i <- 0 until Size + 1) {
319185e6164SHaoyuan Feng    XSPerfAccumulate(s"counter${i}", counter === i.U)
320185e6164SHaoyuan Feng  }
321185e6164SHaoyuan Feng
322185e6164SHaoyuan Feng  for (i <- 0 until Size) {
323185e6164SHaoyuan Feng    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
324185e6164SHaoyuan Feng  }
325185e6164SHaoyuan Feng
326185e6164SHaoyuan Feng}
327185e6164SHaoyuan Feng
328185e6164SHaoyuan Fengclass PTWNewFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
329185e6164SHaoyuan Feng  require(Size >= Width)
330185e6164SHaoyuan Feng
331*71489510SXuan Hu  // all load execute unit, including ldu and hyu
332*71489510SXuan Hu  private val LdExuCnt = backendParams.LdExuCnt
333*71489510SXuan Hu  // all store address execute unit, including sta and hyu
334*71489510SXuan Hu  private val StaExuCnt = backendParams.StaExuCnt
335*71489510SXuan Hu
336185e6164SHaoyuan Feng  val io = IO(new PTWFilterIO(Width, hasHint = true))
337185e6164SHaoyuan Feng
338185e6164SHaoyuan Feng  val load_filter = VecInit(Seq.fill(1) {
339*71489510SXuan Hu    val load_entry = Module(new PTWFilterEntry(Width = LdExuCnt + 1, Size = loadfiltersize, hasHint = true))
340185e6164SHaoyuan Feng    load_entry.io
341185e6164SHaoyuan Feng  })
342185e6164SHaoyuan Feng
343185e6164SHaoyuan Feng  val store_filter = VecInit(Seq.fill(1) {
344*71489510SXuan Hu    val store_entry = Module(new PTWFilterEntry(Width = StaExuCnt, Size = storefiltersize))
345185e6164SHaoyuan Feng    store_entry.io
346185e6164SHaoyuan Feng  })
347185e6164SHaoyuan Feng
348185e6164SHaoyuan Feng  val prefetch_filter = VecInit(Seq.fill(1) {
349185e6164SHaoyuan Feng    val prefetch_entry = Module(new PTWFilterEntry(Width = 1, Size = prefetchfiltersize))
350185e6164SHaoyuan Feng    prefetch_entry.io
351185e6164SHaoyuan Feng  })
352185e6164SHaoyuan Feng
353185e6164SHaoyuan Feng  val filter = load_filter ++ store_filter ++ prefetch_filter
354185e6164SHaoyuan Feng
355*71489510SXuan Hu  load_filter.map(_.tlb.req := io.tlb.req.take(LdExuCnt + 1))
356*71489510SXuan Hu  store_filter.map(_.tlb.req := io.tlb.req.drop(LdExuCnt + 1).take(StaExuCnt))
357*71489510SXuan Hu  prefetch_filter.map(_.tlb.req := io.tlb.req.drop(LdExuCnt + 1 + StaExuCnt))
358185e6164SHaoyuan Feng
359185e6164SHaoyuan Feng  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
360185e6164SHaoyuan Feng  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
361185e6164SHaoyuan Feng  val ptwResp_valid = Cat(filter.map(_.refill)).orR
362185e6164SHaoyuan Feng  filter.map(_.tlb.resp.ready := true.B)
363185e6164SHaoyuan Feng  filter.map(_.ptw.resp.valid := RegNext(io.ptw.resp.fire, init = false.B))
364185e6164SHaoyuan Feng  filter.map(_.ptw.resp.bits := ptwResp)
365185e6164SHaoyuan Feng  filter.map(_.flush := flush)
366185e6164SHaoyuan Feng  filter.map(_.sfence := io.sfence)
367185e6164SHaoyuan Feng  filter.map(_.csr := io.csr)
368185e6164SHaoyuan Feng  filter.map(_.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr)
369185e6164SHaoyuan Feng
370185e6164SHaoyuan Feng  io.tlb.req.map(_.ready := true.B)
371185e6164SHaoyuan Feng  io.tlb.resp.valid := ptwResp_valid
372185e6164SHaoyuan Feng  io.tlb.resp.bits.data.entry := ptwResp.entry
373185e6164SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
374185e6164SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
375185e6164SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
376185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
377185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pf := ptwResp.pf
378185e6164SHaoyuan Feng  io.tlb.resp.bits.data.af := ptwResp.af
379185e6164SHaoyuan Feng  io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
380185e6164SHaoyuan Feng  // vector used to represent different requestors of DTLB
381185e6164SHaoyuan Feng  // (e.g. the store DTLB has StuCnt requestors)
382185e6164SHaoyuan Feng  // However, it is only necessary to distinguish between different DTLB now
383185e6164SHaoyuan Feng  for (i <- 0 until Width) {
384185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(i) := false.B
385185e6164SHaoyuan Feng  }
386185e6164SHaoyuan Feng  io.tlb.resp.bits.vector(0) := load_filter(0).refill
387*71489510SXuan Hu  io.tlb.resp.bits.vector(LdExuCnt + 1) := store_filter(0).refill
388*71489510SXuan Hu  io.tlb.resp.bits.vector(LdExuCnt + 1 + StaExuCnt) := prefetch_filter(0).refill
389185e6164SHaoyuan Feng
390185e6164SHaoyuan Feng  val hintIO = io.hint.getOrElse(new TlbHintIO)
391185e6164SHaoyuan Feng  val load_hintIO = load_filter(0).hint.getOrElse(new TlbHintIO)
392*71489510SXuan Hu  for (i <- 0 until LdExuCnt) {
393185e6164SHaoyuan Feng    hintIO.req(i) := RegNext(load_hintIO.req(i))
394185e6164SHaoyuan Feng  }
395185e6164SHaoyuan Feng  hintIO.resp := RegNext(load_hintIO.resp)
396185e6164SHaoyuan Feng
397185e6164SHaoyuan Feng  when (load_filter(0).refill) {
398185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(0) := true.B
399185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := load_filter(0).memidx
400185e6164SHaoyuan Feng  }
401185e6164SHaoyuan Feng  when (store_filter(0).refill) {
402*71489510SXuan Hu    io.tlb.resp.bits.vector(LdExuCnt + 1) := true.B
403185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := store_filter(0).memidx
404185e6164SHaoyuan Feng  }
405185e6164SHaoyuan Feng  when (prefetch_filter(0).refill) {
406*71489510SXuan Hu    io.tlb.resp.bits.vector(LdExuCnt + 1 + StaExuCnt) := true.B
407185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
408185e6164SHaoyuan Feng  }
409185e6164SHaoyuan Feng
410185e6164SHaoyuan Feng  val ptw_arb = Module(new RRArbiterInit(new PtwReq, 3))
411185e6164SHaoyuan Feng  for (i <- 0 until 3) {
412185e6164SHaoyuan Feng    ptw_arb.io.in(i).valid := filter(i).ptw.req(0).valid
413185e6164SHaoyuan Feng    ptw_arb.io.in(i).bits.vpn := filter(i).ptw.req(0).bits.vpn
414185e6164SHaoyuan Feng    filter(i).ptw.req(0).ready := ptw_arb.io.in(i).ready
415185e6164SHaoyuan Feng  }
416185e6164SHaoyuan Feng  ptw_arb.io.out.ready := io.ptw.req(0).ready
417185e6164SHaoyuan Feng  io.ptw.req(0).valid := ptw_arb.io.out.valid
418185e6164SHaoyuan Feng  io.ptw.req(0).bits.vpn := ptw_arb.io.out.bits.vpn
419185e6164SHaoyuan Feng  io.ptw.resp.ready := true.B
420185e6164SHaoyuan Feng
421185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := Cat(filter.map(_.rob_head_miss_in_tlb)).orR
422185e6164SHaoyuan Feng}
423185e6164SHaoyuan Feng
424f1fe8698SLemoverclass PTWFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
4256d5ddbceSLemover  require(Size >= Width)
4266d5ddbceSLemover
42745f497a4Shappy-lx  val io = IO(new PTWFilterIO(Width))
42845f497a4Shappy-lx
4296d5ddbceSLemover  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
430a0301c0dSLemover  val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports
4316d5ddbceSLemover  val vpn = Reg(Vec(Size, UInt(vpnLen.W)))
4328744445eSMaxpicca-Li  val memidx = Reg(Vec(Size, new MemBlockidxBundle))
4336d5ddbceSLemover  val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq
4346d5ddbceSLemover  val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw
4356d5ddbceSLemover  val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq
4366d5ddbceSLemover  val mayFullDeq = RegInit(false.B)
4376d5ddbceSLemover  val mayFullIss = RegInit(false.B)
4386d5ddbceSLemover  val counter = RegInit(0.U(log2Up(Size+1).W))
4396d5ddbceSLemover
440f1fe8698SLemover  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
441f1fe8698SLemover  val tlb_req = WireInit(io.tlb.req) // NOTE: tlb_req is not io.tlb.req, see below codes, just use cloneType
442cccfc98dSLemover  tlb_req.suggestName("tlb_req")
443cccfc98dSLemover
444fa9f9690SLemover  val inflight_counter = RegInit(0.U(log2Up(Size + 1).W))
445fa9f9690SLemover  val inflight_full = inflight_counter === Size.U
446935edac4STang Haojin  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
447935edac4STang Haojin    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
448fa9f9690SLemover  }
449fa9f9690SLemover
45087f41827SLemover  val canEnqueue = Wire(Bool()) // NOTE: actually enqueue
451935edac4STang Haojin  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
452cccfc98dSLemover  val ptwResp_OldMatchVec = vpn.zip(v).map{ case (pi, vi) =>
45363632028SHaoyuan Feng    vi && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, true, true)}
454935edac4STang Haojin  val ptwResp_valid = RegNext(io.ptw.resp.fire && Cat(ptwResp_OldMatchVec).orR, init = false.B)
45563632028SHaoyuan Feng  // May send repeated requests to L2 tlb with same vpn(26, 3) when sector tlb
456cccfc98dSLemover  val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).map{ case (pi, vi) => vi && pi === a.bits.vpn})
45787f41827SLemover  val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn && canEnqueue})
458cccfc98dSLemover  val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn))
459cccfc98dSLemover
460cccfc98dSLemover  (0 until Width) foreach { i =>
461cccfc98dSLemover    tlb_req(i).valid := RegNext(io.tlb.req(i).valid &&
46263632028SHaoyuan Feng      !(ptwResp_valid && ptwResp.hit(io.tlb.req(i).bits.vpn, 0.U, true, true)) &&
463cccfc98dSLemover      !Cat(lastReqMatchVec_early(i)).orR,
464cccfc98dSLemover      init = false.B)
465cccfc98dSLemover    tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid)
466cccfc98dSLemover  }
467cccfc98dSLemover
468cccfc98dSLemover  val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR))
469cccfc98dSLemover  val newMatchVec = (0 until Width).map(i => (0 until Width).map(j =>
470cccfc98dSLemover    RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid
471cccfc98dSLemover  ))
472cccfc98dSLemover  val ptwResp_newMatchVec = tlb_req.map(a =>
47363632028SHaoyuan Feng    ptwResp_valid && ptwResp.hit(a.bits.vpn, 0.U, allType = true, true))
474cccfc98dSLemover
475cccfc98dSLemover  val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid))
476cccfc98dSLemover  val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i)))
477a0301c0dSLemover  val ports_init = (0 until Width).map(i => (1 << i).U(Width.W))
478a0301c0dSLemover  val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i)))
479935edac4STang Haojin  val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire)
4806d5ddbceSLemover
481a0301c0dSLemover  def canMerge(index: Int) : Bool = {
482cccfc98dSLemover    ptwResp_newMatchVec(index) || oldMatchVec(index) ||
483a0301c0dSLemover    Cat(newMatchVec(index).take(index)).orR
484a0301c0dSLemover  }
485a0301c0dSLemover
486a0301c0dSLemover  def filter_req() = {
487a0301c0dSLemover    val reqs =  tlb_req.indices.map{ i =>
4888744445eSMaxpicca-Li      val req = Wire(ValidIO(new PtwReqwithMemIdx()))
489a0301c0dSLemover      val merge = canMerge(i)
490a0301c0dSLemover      req.bits := tlb_req(i).bits
491a0301c0dSLemover      req.valid := !merge && tlb_req(i).valid
492a0301c0dSLemover      req
493a0301c0dSLemover    }
494a0301c0dSLemover    reqs
495a0301c0dSLemover  }
496a0301c0dSLemover
497a0301c0dSLemover  val reqs = filter_req()
498a0301c0dSLemover  val req_ports = filter_ports
4996d5ddbceSLemover  val isFull = enqPtr === deqPtr && mayFullDeq
5006d5ddbceSLemover  val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq
5016d5ddbceSLemover  val isEmptyIss = enqPtr === issPtr && !mayFullIss
5026d5ddbceSLemover  val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid)))
503cccfc98dSLemover  val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U))
504cccfc98dSLemover  val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i))))
5056d5ddbceSLemover  val enqNum = PopCount(reqs.map(_.valid))
50687f41827SLemover  canEnqueue := counter +& enqNum <= Size.U
5076d5ddbceSLemover
508f1fe8698SLemover  // the req may recv false ready, but actually received. Filter and TLB will handle it.
509f1fe8698SLemover  val enqNum_fake = PopCount(io.tlb.req.map(_.valid))
510f1fe8698SLemover  val canEnqueue_fake = counter +& enqNum_fake <= Size.U
511f1fe8698SLemover  io.tlb.req.map(_.ready := canEnqueue_fake) // NOTE: just drop un-fire reqs
512f1fe8698SLemover
5130ab9ba15SLemover  // tlb req flushed by ptw resp: last ptw resp && current ptw resp
5140ab9ba15SLemover  // the flushed tlb req will fakely enq, with a false valid
51563632028SHaoyuan Feng  val tlb_req_flushed = reqs.map(a => io.ptw.resp.valid && io.ptw.resp.bits.hit(a.bits.vpn, 0.U, true, true))
5160ab9ba15SLemover
517cccfc98dSLemover  io.tlb.resp.valid := ptwResp_valid
5188744445eSMaxpicca-Li  io.tlb.resp.bits.data.entry := ptwResp.entry
51963632028SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
52063632028SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
52163632028SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
522b0fa7106SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
5238744445eSMaxpicca-Li  io.tlb.resp.bits.data.pf := ptwResp.pf
5248744445eSMaxpicca-Li  io.tlb.resp.bits.data.af := ptwResp.af
5258744445eSMaxpicca-Li  io.tlb.resp.bits.data.memidx := memidx(OHToUInt(ptwResp_OldMatchVec))
526a0301c0dSLemover  io.tlb.resp.bits.vector := resp_vector
5272c2c1588SLemover
528fa9f9690SLemover  val issue_valid = v(issPtr) && !isEmptyIss && !inflight_full
52963632028SHaoyuan Feng  val issue_filtered = ptwResp_valid && ptwResp.hit(io.ptw.req(0).bits.vpn, io.csr.satp.asid, allType=true, ignoreAsid=true)
5302c2c1588SLemover  val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/))
5312c2c1588SLemover  io.ptw.req(0).valid := issue_valid && !issue_filtered
5326d5ddbceSLemover  io.ptw.req(0).bits.vpn := vpn(issPtr)
5336d5ddbceSLemover  io.ptw.resp.ready := true.B
5346d5ddbceSLemover
5356d5ddbceSLemover  reqs.zipWithIndex.map{
5366d5ddbceSLemover    case (req, i) =>
5376d5ddbceSLemover      when (req.valid && canEnqueue) {
5380ab9ba15SLemover        v(enqPtrVec(i)) := !tlb_req_flushed(i)
5396d5ddbceSLemover        vpn(enqPtrVec(i)) := req.bits.vpn
5408744445eSMaxpicca-Li        memidx(enqPtrVec(i)) := req.bits.memidx
541a0301c0dSLemover        ports(enqPtrVec(i)) := req_ports(i).asBools
542a0301c0dSLemover      }
543a0301c0dSLemover  }
544a0301c0dSLemover  for (i <- ports.indices) {
545a0301c0dSLemover    when (v(i)) {
546a0301c0dSLemover      ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2)
5476d5ddbceSLemover    }
5486d5ddbceSLemover  }
5496d5ddbceSLemover
5506d5ddbceSLemover  val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR
5516d5ddbceSLemover  val do_deq = (!v(deqPtr) && !isEmptyDeq)
5522c2c1588SLemover  val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss)
5536d5ddbceSLemover  when (do_enq) {
5546d5ddbceSLemover    enqPtr := enqPtr + enqNum
5556d5ddbceSLemover  }
5566d5ddbceSLemover  when (do_deq) {
5576d5ddbceSLemover    deqPtr := deqPtr + 1.U
5586d5ddbceSLemover  }
5596d5ddbceSLemover  when (do_iss) {
5606d5ddbceSLemover    issPtr := issPtr + 1.U
5616d5ddbceSLemover  }
5622c2c1588SLemover  when (issue_fire_fake && issue_filtered) { // issued but is filtered
5632c2c1588SLemover    v(issPtr) := false.B
5642c2c1588SLemover  }
5656d5ddbceSLemover  when (do_enq =/= do_deq) {
5666d5ddbceSLemover    mayFullDeq := do_enq
5676d5ddbceSLemover  }
5686d5ddbceSLemover  when (do_enq =/= do_iss) {
5696d5ddbceSLemover    mayFullIss := do_enq
5706d5ddbceSLemover  }
5716d5ddbceSLemover
572935edac4STang Haojin  when (io.ptw.resp.fire) {
573cccfc98dSLemover    v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
5746d5ddbceSLemover  }
5756d5ddbceSLemover
5766d5ddbceSLemover  counter := counter - do_deq + Mux(do_enq, enqNum, 0.U)
577fa9f9690SLemover  assert(counter <= Size.U, "counter should be no more than Size")
578fa9f9690SLemover  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
5796d5ddbceSLemover  when (counter === 0.U) {
580935edac4STang Haojin    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
5816d5ddbceSLemover    assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty")
5826d5ddbceSLemover  }
5836d5ddbceSLemover  when (counter === Size.U) {
5846d5ddbceSLemover    assert(mayFullDeq, "when counter is Size, should be full")
5856d5ddbceSLemover  }
5866d5ddbceSLemover
58745f497a4Shappy-lx  when (flush) {
5886d5ddbceSLemover    v.map(_ := false.B)
5896d5ddbceSLemover    deqPtr := 0.U
5906d5ddbceSLemover    enqPtr := 0.U
5916d5ddbceSLemover    issPtr := 0.U
5926d5ddbceSLemover    ptwResp_valid := false.B
5936d5ddbceSLemover    mayFullDeq := false.B
5946d5ddbceSLemover    mayFullIss := false.B
5956d5ddbceSLemover    counter := 0.U
596fa9f9690SLemover    inflight_counter := 0.U
5976d5ddbceSLemover  }
5986d5ddbceSLemover
59960ebee38STang Haojin  val robHeadVaddr = io.debugTopDown.robHeadVaddr
600d2b20d1aSTang Haojin  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
60160ebee38STang Haojin    vi && robHeadVaddr.valid && vpni === get_pn(robHeadVaddr.bits)
602d2b20d1aSTang Haojin  }}).asUInt.orR
603d2b20d1aSTang Haojin
6046d5ddbceSLemover  // perf
6056d5ddbceSLemover  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
6066d5ddbceSLemover  XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U))
607935edac4STang Haojin  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
6086d5ddbceSLemover  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
609935edac4STang Haojin  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
610935edac4STang Haojin  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
6116d5ddbceSLemover  XSPerfAccumulate("inflight_cycle", !isEmptyDeq)
6126d5ddbceSLemover  for (i <- 0 until Size + 1) {
6136d5ddbceSLemover    XSPerfAccumulate(s"counter${i}", counter === i.U)
6146d5ddbceSLemover  }
6159bd9cdfaSLemover
6169bd9cdfaSLemover  for (i <- 0 until Size) {
6179bd9cdfaSLemover    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
6189bd9cdfaSLemover  }
6196d5ddbceSLemover}
62038ba1efdSLemover
62138ba1efdSLemoverobject PTWRepeater {
622f1fe8698SLemover  def apply(fenceDelay: Int,
62338ba1efdSLemover    tlb: TlbPtwIO,
62438ba1efdSLemover    sfence: SfenceBundle,
62538ba1efdSLemover    csr: TlbCsrBundle
62638ba1efdSLemover  )(implicit p: Parameters) = {
62738ba1efdSLemover    val width = tlb.req.size
628f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
62935d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
63038ba1efdSLemover    repeater
63138ba1efdSLemover  }
63238ba1efdSLemover
633f1fe8698SLemover  def apply(fenceDelay: Int,
63438ba1efdSLemover    tlb: TlbPtwIO,
63538ba1efdSLemover    ptw: TlbPtwIO,
63638ba1efdSLemover    sfence: SfenceBundle,
63738ba1efdSLemover    csr: TlbCsrBundle
63838ba1efdSLemover  )(implicit p: Parameters) = {
63938ba1efdSLemover    val width = tlb.req.size
640f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
64135d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
64235d6335eSZhangZifei    repeater
64335d6335eSZhangZifei  }
64435d6335eSZhangZifei}
64538ba1efdSLemover
64635d6335eSZhangZifeiobject PTWRepeaterNB {
647f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
64835d6335eSZhangZifei    tlb: TlbPtwIO,
64935d6335eSZhangZifei    sfence: SfenceBundle,
65035d6335eSZhangZifei    csr: TlbCsrBundle
65135d6335eSZhangZifei  )(implicit p: Parameters) = {
65235d6335eSZhangZifei    val width = tlb.req.size
653f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady,fenceDelay))
65435d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
65535d6335eSZhangZifei    repeater
65635d6335eSZhangZifei  }
65735d6335eSZhangZifei
658f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
65935d6335eSZhangZifei    tlb: TlbPtwIO,
66035d6335eSZhangZifei    ptw: TlbPtwIO,
66135d6335eSZhangZifei    sfence: SfenceBundle,
66235d6335eSZhangZifei    csr: TlbCsrBundle
66335d6335eSZhangZifei  )(implicit p: Parameters) = {
66435d6335eSZhangZifei    val width = tlb.req.size
665f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady, fenceDelay))
66635d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
66738ba1efdSLemover    repeater
66838ba1efdSLemover  }
66938ba1efdSLemover}
67038ba1efdSLemover
67138ba1efdSLemoverobject PTWFilter {
672f1fe8698SLemover  def apply(fenceDelay: Int,
673f1fe8698SLemover    tlb: VectorTlbPtwIO,
67438ba1efdSLemover    ptw: TlbPtwIO,
67538ba1efdSLemover    sfence: SfenceBundle,
67638ba1efdSLemover    csr: TlbCsrBundle,
67738ba1efdSLemover    size: Int
67838ba1efdSLemover  )(implicit p: Parameters) = {
67938ba1efdSLemover    val width = tlb.req.size
680f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
68135d6335eSZhangZifei    filter.io.apply(tlb, ptw, sfence, csr)
68238ba1efdSLemover    filter
68338ba1efdSLemover  }
68435d6335eSZhangZifei
685f1fe8698SLemover  def apply(fenceDelay: Int,
686f1fe8698SLemover    tlb: VectorTlbPtwIO,
68735d6335eSZhangZifei    sfence: SfenceBundle,
68835d6335eSZhangZifei    csr: TlbCsrBundle,
68935d6335eSZhangZifei    size: Int
69035d6335eSZhangZifei  )(implicit p: Parameters) = {
69135d6335eSZhangZifei    val width = tlb.req.size
692f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
69335d6335eSZhangZifei    filter.io.apply(tlb, sfence, csr)
69435d6335eSZhangZifei    filter
69535d6335eSZhangZifei  }
696185e6164SHaoyuan Feng}
69735d6335eSZhangZifei
698185e6164SHaoyuan Fengobject PTWNewFilter {
699185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
700185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
701185e6164SHaoyuan Feng            ptw: TlbPtwIO,
702185e6164SHaoyuan Feng            sfence: SfenceBundle,
703185e6164SHaoyuan Feng            csr: TlbCsrBundle,
704185e6164SHaoyuan Feng            size: Int
705185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
706185e6164SHaoyuan Feng    val width = tlb.req.size
707185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
708185e6164SHaoyuan Feng    filter.io.apply(tlb, ptw, sfence, csr)
709185e6164SHaoyuan Feng    filter
710185e6164SHaoyuan Feng  }
711185e6164SHaoyuan Feng
712185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
713185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
714185e6164SHaoyuan Feng            sfence: SfenceBundle,
715185e6164SHaoyuan Feng            csr: TlbCsrBundle,
716185e6164SHaoyuan Feng            size: Int
717185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
718185e6164SHaoyuan Feng    val width = tlb.req.size
719185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
720185e6164SHaoyuan Feng    filter.io.apply(tlb, sfence, csr)
721185e6164SHaoyuan Feng    filter
722185e6164SHaoyuan Feng  }
72338ba1efdSLemover}
724