xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/Repeater.scala (revision 8ef35e01b49dafbb221876f76d7fcca8e21d4980)
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 {
16471489510SXuan 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
206ae970023SXuan Hu  require(Width <= 4, s"DTLB Filter Width ($Width) must equal or less than 4")
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
226ae970023SXuan Hu  } else if (Width == 4) {
227ae970023SXuan Hu    require(Size == 16, s"load filter Size ($Size) should be 16")
228ae970023SXuan Hu    for (i <- 0 until Width) {
229ae970023SXuan Hu      canenq(i) := !(Cat(VecInit(v.slice(i * 4, (i + 1) * 4))).andR)
230ae970023SXuan Hu      enqidx(i) := firstValidIndex(v.slice(i * 4, (i + 1) * 4), false.B) + (i * 4).U
231ae970023SXuan Hu    }
232185e6164SHaoyuan Feng  }
233185e6164SHaoyuan Feng
234185e6164SHaoyuan Feng  for (i <- 0 until Width) {
235185e6164SHaoyuan Feng    enqvalid(i) := io.tlb.req(i).valid && !ptwResp_ReqMatchVec(i) && !entryIsMatchVec(i) && canenq(i)
236185e6164SHaoyuan Feng    when (!enqvalid(i)) {
237185e6164SHaoyuan Feng      enqidx(i) := entryMatchIndexVec(i)
238185e6164SHaoyuan Feng    }
239185e6164SHaoyuan Feng
240185e6164SHaoyuan Feng    val entryIsMatch = vpn.zip(v).map{ case (pi, vi) => vi && pi === io.tlb.req(i).bits.vpn}
241185e6164SHaoyuan Feng    entryIsMatchVec(i) := Cat(entryIsMatch).orR
242185e6164SHaoyuan Feng    entryMatchIndexVec(i) := firstValidIndex(entryIsMatch, true.B)
243185e6164SHaoyuan Feng
244185e6164SHaoyuan Feng    if (i > 0) {
245185e6164SHaoyuan Feng      for (j <- 0 until i) {
246185e6164SHaoyuan Feng        val newIsMatch = io.tlb.req(i).bits.vpn === io.tlb.req(j).bits.vpn
247185e6164SHaoyuan Feng        when (newIsMatch && io.tlb.req(j).valid) {
248185e6164SHaoyuan Feng          enqidx(i) := enqidx(j)
249185e6164SHaoyuan Feng          canenq(i) := canenq(j)
250185e6164SHaoyuan Feng          enqvalid(i) := false.B
251185e6164SHaoyuan Feng        }
252185e6164SHaoyuan Feng      }
253185e6164SHaoyuan Feng    }
254185e6164SHaoyuan Feng
255185e6164SHaoyuan Feng    when (enqvalid(i)) {
256185e6164SHaoyuan Feng      v(enqidx(i)) := true.B
257185e6164SHaoyuan Feng      sent(enqidx(i)) := false.B
258185e6164SHaoyuan Feng      vpn(enqidx(i)) := io.tlb.req(i).bits.vpn
259185e6164SHaoyuan Feng      memidx(enqidx(i)) := io.tlb.req(i).bits.memidx
260185e6164SHaoyuan Feng    }
261185e6164SHaoyuan Feng  }
262185e6164SHaoyuan Feng
263185e6164SHaoyuan Feng  val issuevec = v.zip(sent).map{ case (v, s) => v && !s}
264185e6164SHaoyuan Feng  val issueindex = firstValidIndex(issuevec, true.B)
265185e6164SHaoyuan Feng  val canissue = Cat(issuevec).orR
266185e6164SHaoyuan Feng  for (i <- 0 until Size) {
267185e6164SHaoyuan Feng    io.ptw.req(0).valid := canissue
268185e6164SHaoyuan Feng    io.ptw.req(0).bits.vpn := vpn(issueindex)
269185e6164SHaoyuan Feng  }
270185e6164SHaoyuan Feng  when (io.ptw.req(0).fire) {
271185e6164SHaoyuan Feng    sent(issueindex) := true.B
272185e6164SHaoyuan Feng  }
273185e6164SHaoyuan Feng
274185e6164SHaoyuan Feng  when (io.ptw.resp.fire) {
275185e6164SHaoyuan Feng    v.zip(ptwResp_EntryMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
276185e6164SHaoyuan Feng    io.memidx := memidx(ptwResp_EntryMatchFirst)
277185e6164SHaoyuan Feng  }
278185e6164SHaoyuan Feng
279185e6164SHaoyuan Feng  when (io.flush) {
280185e6164SHaoyuan Feng    v.map(_ := false.B)
281185e6164SHaoyuan Feng  }
282185e6164SHaoyuan Feng
283185e6164SHaoyuan Feng  if (hasHint) {
284185e6164SHaoyuan Feng    val hintIO = io.hint.getOrElse(new TlbHintIO)
28571489510SXuan Hu    for (i <- 0 until LdExuCnt) {
286185e6164SHaoyuan Feng      hintIO.req(i).id := enqidx(i)
287185e6164SHaoyuan Feng      hintIO.req(i).full := !canenq(i) || ptwResp_ReqMatchVec(i)
288185e6164SHaoyuan Feng    }
289185e6164SHaoyuan Feng    hintIO.resp.valid := io.refill
290185e6164SHaoyuan Feng    hintIO.resp.bits.id := ptwResp_EntryMatchFirst
291185e6164SHaoyuan Feng    hintIO.resp.bits.replay_all := PopCount(ptwResp_EntryMatchVec) > 1.U
292185e6164SHaoyuan Feng  }
293185e6164SHaoyuan Feng
294185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
295185e6164SHaoyuan Feng    vi && io.debugTopDown.robHeadVaddr.valid && vpni === get_pn(io.debugTopDown.robHeadVaddr.bits)
296185e6164SHaoyuan Feng  }}).asUInt.orR
297185e6164SHaoyuan Feng
298185e6164SHaoyuan Feng
299185e6164SHaoyuan Feng  // Perf Counter
300185e6164SHaoyuan Feng  val counter = PopCount(v)
301185e6164SHaoyuan Feng  val inflight_counter = RegInit(0.U(log2Up(Size).W))
302185e6164SHaoyuan Feng  val inflight_full = inflight_counter === Size.U
303185e6164SHaoyuan Feng  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
304185e6164SHaoyuan Feng    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
305185e6164SHaoyuan Feng  }
306185e6164SHaoyuan Feng
307185e6164SHaoyuan Feng  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
308185e6164SHaoyuan Feng  when (counter === 0.U) {
309185e6164SHaoyuan Feng    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
310185e6164SHaoyuan Feng  }
311185e6164SHaoyuan Feng
312185e6164SHaoyuan Feng  when (io.flush) {
313185e6164SHaoyuan Feng    inflight_counter := 0.U
314185e6164SHaoyuan Feng  }
315185e6164SHaoyuan Feng
316185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
317185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count_filtered", PopCount(enqvalid))
318185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
319185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
320185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
321185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
322185e6164SHaoyuan Feng  XSPerfAccumulate("inflight_cycle", Cat(sent).orR)
323185e6164SHaoyuan Feng
324185e6164SHaoyuan Feng  for (i <- 0 until Size + 1) {
325185e6164SHaoyuan Feng    XSPerfAccumulate(s"counter${i}", counter === i.U)
326185e6164SHaoyuan Feng  }
327185e6164SHaoyuan Feng
328185e6164SHaoyuan Feng  for (i <- 0 until Size) {
329185e6164SHaoyuan Feng    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
330185e6164SHaoyuan Feng  }
331185e6164SHaoyuan Feng
332185e6164SHaoyuan Feng}
333185e6164SHaoyuan Feng
334185e6164SHaoyuan Fengclass PTWNewFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
335185e6164SHaoyuan Feng  require(Size >= Width)
336185e6164SHaoyuan Feng
337*8ef35e01SXuan Hu  private val LduCnt = backendParams.LduCnt
338*8ef35e01SXuan Hu  private val HyuCnt = backendParams.HyuCnt
339*8ef35e01SXuan Hu  private val StaCnt = backendParams.StaCnt
340*8ef35e01SXuan Hu  // all load execute units, including ldu and hyu
34171489510SXuan Hu  private val LdExuCnt = backendParams.LdExuCnt
342*8ef35e01SXuan Hu  // all store address execute units, including sta and hyu
34371489510SXuan Hu  private val StaExuCnt = backendParams.StaExuCnt
34471489510SXuan Hu
345185e6164SHaoyuan Feng  val io = IO(new PTWFilterIO(Width, hasHint = true))
346185e6164SHaoyuan Feng
347185e6164SHaoyuan Feng  val load_filter = VecInit(Seq.fill(1) {
34871489510SXuan Hu    val load_entry = Module(new PTWFilterEntry(Width = LdExuCnt + 1, Size = loadfiltersize, hasHint = true))
349185e6164SHaoyuan Feng    load_entry.io
350185e6164SHaoyuan Feng  })
351185e6164SHaoyuan Feng
352185e6164SHaoyuan Feng  val store_filter = VecInit(Seq.fill(1) {
353*8ef35e01SXuan Hu    val store_entry = Module(new PTWFilterEntry(Width = StaCnt, Size = storefiltersize))
354185e6164SHaoyuan Feng    store_entry.io
355185e6164SHaoyuan Feng  })
356185e6164SHaoyuan Feng
357185e6164SHaoyuan Feng  val prefetch_filter = VecInit(Seq.fill(1) {
358185e6164SHaoyuan Feng    val prefetch_entry = Module(new PTWFilterEntry(Width = 1, Size = prefetchfiltersize))
359185e6164SHaoyuan Feng    prefetch_entry.io
360185e6164SHaoyuan Feng  })
361185e6164SHaoyuan Feng
362185e6164SHaoyuan Feng  val filter = load_filter ++ store_filter ++ prefetch_filter
363185e6164SHaoyuan Feng
36471489510SXuan Hu  load_filter.map(_.tlb.req := io.tlb.req.take(LdExuCnt + 1))
365*8ef35e01SXuan Hu  store_filter.map(_.tlb.req := io.tlb.req.drop(LdExuCnt + 1).take(StaCnt))
366*8ef35e01SXuan Hu  prefetch_filter.map(_.tlb.req := io.tlb.req.drop(LdExuCnt + 1 + StaCnt))
367185e6164SHaoyuan Feng
368185e6164SHaoyuan Feng  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
369185e6164SHaoyuan Feng  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
370185e6164SHaoyuan Feng  val ptwResp_valid = Cat(filter.map(_.refill)).orR
371185e6164SHaoyuan Feng  filter.map(_.tlb.resp.ready := true.B)
372185e6164SHaoyuan Feng  filter.map(_.ptw.resp.valid := RegNext(io.ptw.resp.fire, init = false.B))
373185e6164SHaoyuan Feng  filter.map(_.ptw.resp.bits := ptwResp)
374185e6164SHaoyuan Feng  filter.map(_.flush := flush)
375185e6164SHaoyuan Feng  filter.map(_.sfence := io.sfence)
376185e6164SHaoyuan Feng  filter.map(_.csr := io.csr)
377185e6164SHaoyuan Feng  filter.map(_.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr)
378185e6164SHaoyuan Feng
379185e6164SHaoyuan Feng  io.tlb.req.map(_.ready := true.B)
380185e6164SHaoyuan Feng  io.tlb.resp.valid := ptwResp_valid
381185e6164SHaoyuan Feng  io.tlb.resp.bits.data.entry := ptwResp.entry
382185e6164SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
383185e6164SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
384185e6164SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
385185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
386185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pf := ptwResp.pf
387185e6164SHaoyuan Feng  io.tlb.resp.bits.data.af := ptwResp.af
388185e6164SHaoyuan Feng  io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
389185e6164SHaoyuan Feng  // vector used to represent different requestors of DTLB
390185e6164SHaoyuan Feng  // (e.g. the store DTLB has StuCnt requestors)
391185e6164SHaoyuan Feng  // However, it is only necessary to distinguish between different DTLB now
392185e6164SHaoyuan Feng  for (i <- 0 until Width) {
393185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(i) := false.B
394185e6164SHaoyuan Feng  }
395185e6164SHaoyuan Feng  io.tlb.resp.bits.vector(0) := load_filter(0).refill
39671489510SXuan Hu  io.tlb.resp.bits.vector(LdExuCnt + 1) := store_filter(0).refill
397*8ef35e01SXuan Hu  io.tlb.resp.bits.vector(LdExuCnt + 1 + StaCnt) := prefetch_filter(0).refill
398185e6164SHaoyuan Feng
399185e6164SHaoyuan Feng  val hintIO = io.hint.getOrElse(new TlbHintIO)
400185e6164SHaoyuan Feng  val load_hintIO = load_filter(0).hint.getOrElse(new TlbHintIO)
40171489510SXuan Hu  for (i <- 0 until LdExuCnt) {
402185e6164SHaoyuan Feng    hintIO.req(i) := RegNext(load_hintIO.req(i))
403185e6164SHaoyuan Feng  }
404185e6164SHaoyuan Feng  hintIO.resp := RegNext(load_hintIO.resp)
405185e6164SHaoyuan Feng
406185e6164SHaoyuan Feng  when (load_filter(0).refill) {
407185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(0) := true.B
408185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := load_filter(0).memidx
409185e6164SHaoyuan Feng  }
410185e6164SHaoyuan Feng  when (store_filter(0).refill) {
41171489510SXuan Hu    io.tlb.resp.bits.vector(LdExuCnt + 1) := true.B
412185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := store_filter(0).memidx
413185e6164SHaoyuan Feng  }
414185e6164SHaoyuan Feng  when (prefetch_filter(0).refill) {
415*8ef35e01SXuan Hu    io.tlb.resp.bits.vector(LdExuCnt + 1 + StaCnt) := true.B
416185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
417185e6164SHaoyuan Feng  }
418185e6164SHaoyuan Feng
419185e6164SHaoyuan Feng  val ptw_arb = Module(new RRArbiterInit(new PtwReq, 3))
420185e6164SHaoyuan Feng  for (i <- 0 until 3) {
421185e6164SHaoyuan Feng    ptw_arb.io.in(i).valid := filter(i).ptw.req(0).valid
422185e6164SHaoyuan Feng    ptw_arb.io.in(i).bits.vpn := filter(i).ptw.req(0).bits.vpn
423185e6164SHaoyuan Feng    filter(i).ptw.req(0).ready := ptw_arb.io.in(i).ready
424185e6164SHaoyuan Feng  }
425185e6164SHaoyuan Feng  ptw_arb.io.out.ready := io.ptw.req(0).ready
426185e6164SHaoyuan Feng  io.ptw.req(0).valid := ptw_arb.io.out.valid
427185e6164SHaoyuan Feng  io.ptw.req(0).bits.vpn := ptw_arb.io.out.bits.vpn
428185e6164SHaoyuan Feng  io.ptw.resp.ready := true.B
429185e6164SHaoyuan Feng
430185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := Cat(filter.map(_.rob_head_miss_in_tlb)).orR
431185e6164SHaoyuan Feng}
432185e6164SHaoyuan Feng
433f1fe8698SLemoverclass PTWFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
4346d5ddbceSLemover  require(Size >= Width)
4356d5ddbceSLemover
43645f497a4Shappy-lx  val io = IO(new PTWFilterIO(Width))
43745f497a4Shappy-lx
4386d5ddbceSLemover  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
439a0301c0dSLemover  val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports
4406d5ddbceSLemover  val vpn = Reg(Vec(Size, UInt(vpnLen.W)))
4418744445eSMaxpicca-Li  val memidx = Reg(Vec(Size, new MemBlockidxBundle))
4426d5ddbceSLemover  val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq
4436d5ddbceSLemover  val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw
4446d5ddbceSLemover  val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq
4456d5ddbceSLemover  val mayFullDeq = RegInit(false.B)
4466d5ddbceSLemover  val mayFullIss = RegInit(false.B)
4476d5ddbceSLemover  val counter = RegInit(0.U(log2Up(Size+1).W))
4486d5ddbceSLemover
449f1fe8698SLemover  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
450f1fe8698SLemover  val tlb_req = WireInit(io.tlb.req) // NOTE: tlb_req is not io.tlb.req, see below codes, just use cloneType
451cccfc98dSLemover  tlb_req.suggestName("tlb_req")
452cccfc98dSLemover
453fa9f9690SLemover  val inflight_counter = RegInit(0.U(log2Up(Size + 1).W))
454fa9f9690SLemover  val inflight_full = inflight_counter === Size.U
455935edac4STang Haojin  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
456935edac4STang Haojin    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
457fa9f9690SLemover  }
458fa9f9690SLemover
45987f41827SLemover  val canEnqueue = Wire(Bool()) // NOTE: actually enqueue
460935edac4STang Haojin  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
461cccfc98dSLemover  val ptwResp_OldMatchVec = vpn.zip(v).map{ case (pi, vi) =>
46263632028SHaoyuan Feng    vi && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, true, true)}
463935edac4STang Haojin  val ptwResp_valid = RegNext(io.ptw.resp.fire && Cat(ptwResp_OldMatchVec).orR, init = false.B)
46463632028SHaoyuan Feng  // May send repeated requests to L2 tlb with same vpn(26, 3) when sector tlb
465cccfc98dSLemover  val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).map{ case (pi, vi) => vi && pi === a.bits.vpn})
46687f41827SLemover  val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn && canEnqueue})
467cccfc98dSLemover  val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn))
468cccfc98dSLemover
469cccfc98dSLemover  (0 until Width) foreach { i =>
470cccfc98dSLemover    tlb_req(i).valid := RegNext(io.tlb.req(i).valid &&
47163632028SHaoyuan Feng      !(ptwResp_valid && ptwResp.hit(io.tlb.req(i).bits.vpn, 0.U, true, true)) &&
472cccfc98dSLemover      !Cat(lastReqMatchVec_early(i)).orR,
473cccfc98dSLemover      init = false.B)
474cccfc98dSLemover    tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid)
475cccfc98dSLemover  }
476cccfc98dSLemover
477cccfc98dSLemover  val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR))
478cccfc98dSLemover  val newMatchVec = (0 until Width).map(i => (0 until Width).map(j =>
479cccfc98dSLemover    RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid
480cccfc98dSLemover  ))
481cccfc98dSLemover  val ptwResp_newMatchVec = tlb_req.map(a =>
48263632028SHaoyuan Feng    ptwResp_valid && ptwResp.hit(a.bits.vpn, 0.U, allType = true, true))
483cccfc98dSLemover
484cccfc98dSLemover  val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid))
485cccfc98dSLemover  val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i)))
486a0301c0dSLemover  val ports_init = (0 until Width).map(i => (1 << i).U(Width.W))
487a0301c0dSLemover  val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i)))
488935edac4STang Haojin  val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire)
4896d5ddbceSLemover
490a0301c0dSLemover  def canMerge(index: Int) : Bool = {
491cccfc98dSLemover    ptwResp_newMatchVec(index) || oldMatchVec(index) ||
492a0301c0dSLemover    Cat(newMatchVec(index).take(index)).orR
493a0301c0dSLemover  }
494a0301c0dSLemover
495a0301c0dSLemover  def filter_req() = {
496a0301c0dSLemover    val reqs =  tlb_req.indices.map{ i =>
4978744445eSMaxpicca-Li      val req = Wire(ValidIO(new PtwReqwithMemIdx()))
498a0301c0dSLemover      val merge = canMerge(i)
499a0301c0dSLemover      req.bits := tlb_req(i).bits
500a0301c0dSLemover      req.valid := !merge && tlb_req(i).valid
501a0301c0dSLemover      req
502a0301c0dSLemover    }
503a0301c0dSLemover    reqs
504a0301c0dSLemover  }
505a0301c0dSLemover
506a0301c0dSLemover  val reqs = filter_req()
507a0301c0dSLemover  val req_ports = filter_ports
5086d5ddbceSLemover  val isFull = enqPtr === deqPtr && mayFullDeq
5096d5ddbceSLemover  val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq
5106d5ddbceSLemover  val isEmptyIss = enqPtr === issPtr && !mayFullIss
5116d5ddbceSLemover  val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid)))
512cccfc98dSLemover  val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U))
513cccfc98dSLemover  val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i))))
5146d5ddbceSLemover  val enqNum = PopCount(reqs.map(_.valid))
51587f41827SLemover  canEnqueue := counter +& enqNum <= Size.U
5166d5ddbceSLemover
517f1fe8698SLemover  // the req may recv false ready, but actually received. Filter and TLB will handle it.
518f1fe8698SLemover  val enqNum_fake = PopCount(io.tlb.req.map(_.valid))
519f1fe8698SLemover  val canEnqueue_fake = counter +& enqNum_fake <= Size.U
520f1fe8698SLemover  io.tlb.req.map(_.ready := canEnqueue_fake) // NOTE: just drop un-fire reqs
521f1fe8698SLemover
5220ab9ba15SLemover  // tlb req flushed by ptw resp: last ptw resp && current ptw resp
5230ab9ba15SLemover  // the flushed tlb req will fakely enq, with a false valid
52463632028SHaoyuan Feng  val tlb_req_flushed = reqs.map(a => io.ptw.resp.valid && io.ptw.resp.bits.hit(a.bits.vpn, 0.U, true, true))
5250ab9ba15SLemover
526cccfc98dSLemover  io.tlb.resp.valid := ptwResp_valid
5278744445eSMaxpicca-Li  io.tlb.resp.bits.data.entry := ptwResp.entry
52863632028SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
52963632028SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
53063632028SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
531b0fa7106SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
5328744445eSMaxpicca-Li  io.tlb.resp.bits.data.pf := ptwResp.pf
5338744445eSMaxpicca-Li  io.tlb.resp.bits.data.af := ptwResp.af
5348744445eSMaxpicca-Li  io.tlb.resp.bits.data.memidx := memidx(OHToUInt(ptwResp_OldMatchVec))
535a0301c0dSLemover  io.tlb.resp.bits.vector := resp_vector
5362c2c1588SLemover
537fa9f9690SLemover  val issue_valid = v(issPtr) && !isEmptyIss && !inflight_full
53863632028SHaoyuan Feng  val issue_filtered = ptwResp_valid && ptwResp.hit(io.ptw.req(0).bits.vpn, io.csr.satp.asid, allType=true, ignoreAsid=true)
5392c2c1588SLemover  val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/))
5402c2c1588SLemover  io.ptw.req(0).valid := issue_valid && !issue_filtered
5416d5ddbceSLemover  io.ptw.req(0).bits.vpn := vpn(issPtr)
5426d5ddbceSLemover  io.ptw.resp.ready := true.B
5436d5ddbceSLemover
5446d5ddbceSLemover  reqs.zipWithIndex.map{
5456d5ddbceSLemover    case (req, i) =>
5466d5ddbceSLemover      when (req.valid && canEnqueue) {
5470ab9ba15SLemover        v(enqPtrVec(i)) := !tlb_req_flushed(i)
5486d5ddbceSLemover        vpn(enqPtrVec(i)) := req.bits.vpn
5498744445eSMaxpicca-Li        memidx(enqPtrVec(i)) := req.bits.memidx
550a0301c0dSLemover        ports(enqPtrVec(i)) := req_ports(i).asBools
551a0301c0dSLemover      }
552a0301c0dSLemover  }
553a0301c0dSLemover  for (i <- ports.indices) {
554a0301c0dSLemover    when (v(i)) {
555a0301c0dSLemover      ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2)
5566d5ddbceSLemover    }
5576d5ddbceSLemover  }
5586d5ddbceSLemover
5596d5ddbceSLemover  val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR
5606d5ddbceSLemover  val do_deq = (!v(deqPtr) && !isEmptyDeq)
5612c2c1588SLemover  val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss)
5626d5ddbceSLemover  when (do_enq) {
5636d5ddbceSLemover    enqPtr := enqPtr + enqNum
5646d5ddbceSLemover  }
5656d5ddbceSLemover  when (do_deq) {
5666d5ddbceSLemover    deqPtr := deqPtr + 1.U
5676d5ddbceSLemover  }
5686d5ddbceSLemover  when (do_iss) {
5696d5ddbceSLemover    issPtr := issPtr + 1.U
5706d5ddbceSLemover  }
5712c2c1588SLemover  when (issue_fire_fake && issue_filtered) { // issued but is filtered
5722c2c1588SLemover    v(issPtr) := false.B
5732c2c1588SLemover  }
5746d5ddbceSLemover  when (do_enq =/= do_deq) {
5756d5ddbceSLemover    mayFullDeq := do_enq
5766d5ddbceSLemover  }
5776d5ddbceSLemover  when (do_enq =/= do_iss) {
5786d5ddbceSLemover    mayFullIss := do_enq
5796d5ddbceSLemover  }
5806d5ddbceSLemover
581935edac4STang Haojin  when (io.ptw.resp.fire) {
582cccfc98dSLemover    v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
5836d5ddbceSLemover  }
5846d5ddbceSLemover
5856d5ddbceSLemover  counter := counter - do_deq + Mux(do_enq, enqNum, 0.U)
586fa9f9690SLemover  assert(counter <= Size.U, "counter should be no more than Size")
587fa9f9690SLemover  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
5886d5ddbceSLemover  when (counter === 0.U) {
589935edac4STang Haojin    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
5906d5ddbceSLemover    assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty")
5916d5ddbceSLemover  }
5926d5ddbceSLemover  when (counter === Size.U) {
5936d5ddbceSLemover    assert(mayFullDeq, "when counter is Size, should be full")
5946d5ddbceSLemover  }
5956d5ddbceSLemover
59645f497a4Shappy-lx  when (flush) {
5976d5ddbceSLemover    v.map(_ := false.B)
5986d5ddbceSLemover    deqPtr := 0.U
5996d5ddbceSLemover    enqPtr := 0.U
6006d5ddbceSLemover    issPtr := 0.U
6016d5ddbceSLemover    ptwResp_valid := false.B
6026d5ddbceSLemover    mayFullDeq := false.B
6036d5ddbceSLemover    mayFullIss := false.B
6046d5ddbceSLemover    counter := 0.U
605fa9f9690SLemover    inflight_counter := 0.U
6066d5ddbceSLemover  }
6076d5ddbceSLemover
60860ebee38STang Haojin  val robHeadVaddr = io.debugTopDown.robHeadVaddr
609d2b20d1aSTang Haojin  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
61060ebee38STang Haojin    vi && robHeadVaddr.valid && vpni === get_pn(robHeadVaddr.bits)
611d2b20d1aSTang Haojin  }}).asUInt.orR
612d2b20d1aSTang Haojin
6136d5ddbceSLemover  // perf
6146d5ddbceSLemover  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
6156d5ddbceSLemover  XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U))
616935edac4STang Haojin  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
6176d5ddbceSLemover  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
618935edac4STang Haojin  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
619935edac4STang Haojin  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
6206d5ddbceSLemover  XSPerfAccumulate("inflight_cycle", !isEmptyDeq)
6216d5ddbceSLemover  for (i <- 0 until Size + 1) {
6226d5ddbceSLemover    XSPerfAccumulate(s"counter${i}", counter === i.U)
6236d5ddbceSLemover  }
6249bd9cdfaSLemover
6259bd9cdfaSLemover  for (i <- 0 until Size) {
6269bd9cdfaSLemover    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
6279bd9cdfaSLemover  }
6286d5ddbceSLemover}
62938ba1efdSLemover
63038ba1efdSLemoverobject PTWRepeater {
631f1fe8698SLemover  def apply(fenceDelay: Int,
63238ba1efdSLemover    tlb: TlbPtwIO,
63338ba1efdSLemover    sfence: SfenceBundle,
63438ba1efdSLemover    csr: TlbCsrBundle
63538ba1efdSLemover  )(implicit p: Parameters) = {
63638ba1efdSLemover    val width = tlb.req.size
637f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
63835d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
63938ba1efdSLemover    repeater
64038ba1efdSLemover  }
64138ba1efdSLemover
642f1fe8698SLemover  def apply(fenceDelay: Int,
64338ba1efdSLemover    tlb: TlbPtwIO,
64438ba1efdSLemover    ptw: TlbPtwIO,
64538ba1efdSLemover    sfence: SfenceBundle,
64638ba1efdSLemover    csr: TlbCsrBundle
64738ba1efdSLemover  )(implicit p: Parameters) = {
64838ba1efdSLemover    val width = tlb.req.size
649f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
65035d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
65135d6335eSZhangZifei    repeater
65235d6335eSZhangZifei  }
65335d6335eSZhangZifei}
65438ba1efdSLemover
65535d6335eSZhangZifeiobject PTWRepeaterNB {
656f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
65735d6335eSZhangZifei    tlb: TlbPtwIO,
65835d6335eSZhangZifei    sfence: SfenceBundle,
65935d6335eSZhangZifei    csr: TlbCsrBundle
66035d6335eSZhangZifei  )(implicit p: Parameters) = {
66135d6335eSZhangZifei    val width = tlb.req.size
662f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady,fenceDelay))
66335d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
66435d6335eSZhangZifei    repeater
66535d6335eSZhangZifei  }
66635d6335eSZhangZifei
667f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
66835d6335eSZhangZifei    tlb: TlbPtwIO,
66935d6335eSZhangZifei    ptw: TlbPtwIO,
67035d6335eSZhangZifei    sfence: SfenceBundle,
67135d6335eSZhangZifei    csr: TlbCsrBundle
67235d6335eSZhangZifei  )(implicit p: Parameters) = {
67335d6335eSZhangZifei    val width = tlb.req.size
674f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady, fenceDelay))
67535d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
67638ba1efdSLemover    repeater
67738ba1efdSLemover  }
67838ba1efdSLemover}
67938ba1efdSLemover
68038ba1efdSLemoverobject PTWFilter {
681f1fe8698SLemover  def apply(fenceDelay: Int,
682f1fe8698SLemover    tlb: VectorTlbPtwIO,
68338ba1efdSLemover    ptw: TlbPtwIO,
68438ba1efdSLemover    sfence: SfenceBundle,
68538ba1efdSLemover    csr: TlbCsrBundle,
68638ba1efdSLemover    size: Int
68738ba1efdSLemover  )(implicit p: Parameters) = {
68838ba1efdSLemover    val width = tlb.req.size
689f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
69035d6335eSZhangZifei    filter.io.apply(tlb, ptw, sfence, csr)
69138ba1efdSLemover    filter
69238ba1efdSLemover  }
69335d6335eSZhangZifei
694f1fe8698SLemover  def apply(fenceDelay: Int,
695f1fe8698SLemover    tlb: VectorTlbPtwIO,
69635d6335eSZhangZifei    sfence: SfenceBundle,
69735d6335eSZhangZifei    csr: TlbCsrBundle,
69835d6335eSZhangZifei    size: Int
69935d6335eSZhangZifei  )(implicit p: Parameters) = {
70035d6335eSZhangZifei    val width = tlb.req.size
701f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
70235d6335eSZhangZifei    filter.io.apply(tlb, sfence, csr)
70335d6335eSZhangZifei    filter
70435d6335eSZhangZifei  }
705185e6164SHaoyuan Feng}
70635d6335eSZhangZifei
707185e6164SHaoyuan Fengobject PTWNewFilter {
708185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
709185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
710185e6164SHaoyuan Feng            ptw: TlbPtwIO,
711185e6164SHaoyuan Feng            sfence: SfenceBundle,
712185e6164SHaoyuan Feng            csr: TlbCsrBundle,
713185e6164SHaoyuan Feng            size: Int
714185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
715185e6164SHaoyuan Feng    val width = tlb.req.size
716185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
717185e6164SHaoyuan Feng    filter.io.apply(tlb, ptw, sfence, csr)
718185e6164SHaoyuan Feng    filter
719185e6164SHaoyuan Feng  }
720185e6164SHaoyuan Feng
721185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
722185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
723185e6164SHaoyuan Feng            sfence: SfenceBundle,
724185e6164SHaoyuan Feng            csr: TlbCsrBundle,
725185e6164SHaoyuan Feng            size: Int
726185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
727185e6164SHaoyuan Feng    val width = tlb.req.size
728185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
729185e6164SHaoyuan Feng    filter.io.apply(tlb, sfence, csr)
730185e6164SHaoyuan Feng    filter
731185e6164SHaoyuan Feng  }
73238ba1efdSLemover}
733