xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/Repeater.scala (revision 185e616497509644b544b0098b6235fbe03f878f)
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
133*185e6164SHaoyuan Fengclass PTWFilterIO(Width: Int, hasHint: Boolean = false)(implicit p: Parameters) extends MMUIOBaseBundle {
134f1fe8698SLemover  val tlb = Flipped(new VectorTlbPtwIO(Width))
135a0301c0dSLemover  val ptw = new TlbPtwIO()
136*185e6164SHaoyuan 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
157*185e6164SHaoyuan Fengclass PTWFilterEntryIO(Width: Int, hasHint: Boolean = false)(implicit p: Parameters) extends PTWFilterIO(Width, hasHint){
158*185e6164SHaoyuan Feng  val flush = Input(Bool())
159*185e6164SHaoyuan Feng  val refill = Output(Bool())
160*185e6164SHaoyuan Feng  val memidx = Output(new MemBlockidxBundle)
161*185e6164SHaoyuan Feng}
162*185e6164SHaoyuan Feng
163*185e6164SHaoyuan Fengclass PTWFilterEntry(Width: Int, Size: Int, hasHint: Boolean = false)(implicit p: Parameters) extends XSModule with HasPtwConst {
164*185e6164SHaoyuan Feng
165*185e6164SHaoyuan Feng  val io = IO(new PTWFilterEntryIO(Width, hasHint))
166*185e6164SHaoyuan Feng  require(isPow2(Size), s"Filter Size ($Size) must be a power of 2")
167*185e6164SHaoyuan Feng
168*185e6164SHaoyuan Feng  def firstValidIndex(v: Seq[Bool], valid: Bool): UInt = {
169*185e6164SHaoyuan Feng    val index = WireInit(0.U(log2Up(Size).W))
170*185e6164SHaoyuan Feng    for (i <- 0 until v.size) {
171*185e6164SHaoyuan Feng      when (v(i) === valid) {
172*185e6164SHaoyuan Feng        index := i.U
173*185e6164SHaoyuan Feng      }
174*185e6164SHaoyuan Feng    }
175*185e6164SHaoyuan Feng    index
176*185e6164SHaoyuan Feng  }
177*185e6164SHaoyuan Feng
178*185e6164SHaoyuan Feng  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
179*185e6164SHaoyuan Feng  val sent = RegInit(VecInit(Seq.fill(Size)(false.B)))
180*185e6164SHaoyuan Feng  val vpn = Reg(Vec(Size, UInt(vpnLen.W)))
181*185e6164SHaoyuan Feng  val memidx = Reg(Vec(Size, new MemBlockidxBundle))
182*185e6164SHaoyuan Feng
183*185e6164SHaoyuan Feng  val enqvalid = WireInit(VecInit(Seq.fill(Width)(false.B)))
184*185e6164SHaoyuan Feng  val canenq = WireInit(VecInit(Seq.fill(Width)(false.B)))
185*185e6164SHaoyuan Feng  val enqidx = WireInit(VecInit(Seq.fill(Width)(0.U(log2Up(Size).W))))
186*185e6164SHaoyuan Feng
187*185e6164SHaoyuan Feng  //val selectCount = RegInit(0.U(log2Up(Width).W))
188*185e6164SHaoyuan Feng
189*185e6164SHaoyuan Feng  val entryIsMatchVec = WireInit(VecInit(Seq.fill(Width)(false.B)))
190*185e6164SHaoyuan Feng  val entryMatchIndexVec = WireInit(VecInit(Seq.fill(Width)(0.U(log2Up(Size).W))))
191*185e6164SHaoyuan Feng  val ptwResp_EntryMatchVec = vpn.zip(v).map{ case (pi, vi) => vi && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, true, true)}
192*185e6164SHaoyuan Feng  val ptwResp_EntryMatchFirst = firstValidIndex(ptwResp_EntryMatchVec, true.B)
193*185e6164SHaoyuan 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))
194*185e6164SHaoyuan Feng
195*185e6164SHaoyuan Feng  io.refill := Cat(ptwResp_EntryMatchVec).orR && io.ptw.resp.fire
196*185e6164SHaoyuan Feng  io.ptw.resp.ready := true.B
197*185e6164SHaoyuan Feng  // DontCare
198*185e6164SHaoyuan Feng  io.tlb.req.map(_.ready := true.B)
199*185e6164SHaoyuan Feng  io.tlb.resp.valid := false.B
200*185e6164SHaoyuan Feng  io.tlb.resp.bits.data := 0.U.asTypeOf(new PtwSectorRespwithMemIdx)
201*185e6164SHaoyuan Feng  io.tlb.resp.bits.vector := 0.U.asTypeOf(Vec(Width, Bool()))
202*185e6164SHaoyuan Feng  io.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
203*185e6164SHaoyuan Feng
204*185e6164SHaoyuan Feng  // ugly code, should be optimized later
205*185e6164SHaoyuan Feng  require(Width <= 3, s"DTLB Filter Width ($Width) must equal or less than 3")
206*185e6164SHaoyuan Feng  if (Width == 1) {
207*185e6164SHaoyuan Feng    require(Size == 8, s"prefetch filter Size ($Size) should be 8")
208*185e6164SHaoyuan Feng    canenq(0) := !(Cat(v).andR)
209*185e6164SHaoyuan Feng    enqidx(0) := firstValidIndex(v, false.B)
210*185e6164SHaoyuan Feng  } else if (Width == 2) {
211*185e6164SHaoyuan Feng    require(Size == 8, s"store filter Size ($Size) should be 8")
212*185e6164SHaoyuan Feng    canenq(0) := !(Cat(v.take(Size/2)).andR)
213*185e6164SHaoyuan Feng    enqidx(0) := firstValidIndex(v.take(Size/2), false.B)
214*185e6164SHaoyuan Feng    canenq(1) := !(Cat(v.drop(Size/2)).andR)
215*185e6164SHaoyuan Feng    enqidx(1) := firstValidIndex(v.drop(Size/2), false.B) + (Size/2).U
216*185e6164SHaoyuan Feng  } else if (Width == 3) {
217*185e6164SHaoyuan Feng    require(Size == 16, s"load filter Size ($Size) should be 16")
218*185e6164SHaoyuan Feng    canenq(0) := !(Cat(v.take(8)).andR)
219*185e6164SHaoyuan Feng    enqidx(0) := firstValidIndex(v.take(8), false.B)
220*185e6164SHaoyuan Feng    canenq(1) := !(Cat(v.drop(8).take(4)).andR)
221*185e6164SHaoyuan Feng    enqidx(1) := firstValidIndex(v.drop(8).take(4), false.B) + 8.U
222*185e6164SHaoyuan Feng    // four entries for prefetch
223*185e6164SHaoyuan Feng    canenq(2) := !(Cat(v.drop(12)).andR)
224*185e6164SHaoyuan Feng    enqidx(2) := firstValidIndex(v.drop(12), false.B) + 12.U
225*185e6164SHaoyuan Feng  }
226*185e6164SHaoyuan Feng
227*185e6164SHaoyuan Feng  for (i <- 0 until Width) {
228*185e6164SHaoyuan Feng    enqvalid(i) := io.tlb.req(i).valid && !ptwResp_ReqMatchVec(i) && !entryIsMatchVec(i) && canenq(i)
229*185e6164SHaoyuan Feng    when (!enqvalid(i)) {
230*185e6164SHaoyuan Feng      enqidx(i) := entryMatchIndexVec(i)
231*185e6164SHaoyuan Feng    }
232*185e6164SHaoyuan Feng
233*185e6164SHaoyuan Feng    val entryIsMatch = vpn.zip(v).map{ case (pi, vi) => vi && pi === io.tlb.req(i).bits.vpn}
234*185e6164SHaoyuan Feng    entryIsMatchVec(i) := Cat(entryIsMatch).orR
235*185e6164SHaoyuan Feng    entryMatchIndexVec(i) := firstValidIndex(entryIsMatch, true.B)
236*185e6164SHaoyuan Feng
237*185e6164SHaoyuan Feng    if (i > 0) {
238*185e6164SHaoyuan Feng      for (j <- 0 until i) {
239*185e6164SHaoyuan Feng        val newIsMatch = io.tlb.req(i).bits.vpn === io.tlb.req(j).bits.vpn
240*185e6164SHaoyuan Feng        when (newIsMatch && io.tlb.req(j).valid) {
241*185e6164SHaoyuan Feng          enqidx(i) := enqidx(j)
242*185e6164SHaoyuan Feng          canenq(i) := canenq(j)
243*185e6164SHaoyuan Feng          enqvalid(i) := false.B
244*185e6164SHaoyuan Feng        }
245*185e6164SHaoyuan Feng      }
246*185e6164SHaoyuan Feng    }
247*185e6164SHaoyuan Feng
248*185e6164SHaoyuan Feng    when (enqvalid(i)) {
249*185e6164SHaoyuan Feng      v(enqidx(i)) := true.B
250*185e6164SHaoyuan Feng      sent(enqidx(i)) := false.B
251*185e6164SHaoyuan Feng      vpn(enqidx(i)) := io.tlb.req(i).bits.vpn
252*185e6164SHaoyuan Feng      memidx(enqidx(i)) := io.tlb.req(i).bits.memidx
253*185e6164SHaoyuan Feng    }
254*185e6164SHaoyuan Feng  }
255*185e6164SHaoyuan Feng
256*185e6164SHaoyuan Feng  val issuevec = v.zip(sent).map{ case (v, s) => v && !s}
257*185e6164SHaoyuan Feng  val issueindex = firstValidIndex(issuevec, true.B)
258*185e6164SHaoyuan Feng  val canissue = Cat(issuevec).orR
259*185e6164SHaoyuan Feng  for (i <- 0 until Size) {
260*185e6164SHaoyuan Feng    io.ptw.req(0).valid := canissue
261*185e6164SHaoyuan Feng    io.ptw.req(0).bits.vpn := vpn(issueindex)
262*185e6164SHaoyuan Feng  }
263*185e6164SHaoyuan Feng  when (io.ptw.req(0).fire) {
264*185e6164SHaoyuan Feng    sent(issueindex) := true.B
265*185e6164SHaoyuan Feng  }
266*185e6164SHaoyuan Feng
267*185e6164SHaoyuan Feng  when (io.ptw.resp.fire) {
268*185e6164SHaoyuan Feng    v.zip(ptwResp_EntryMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
269*185e6164SHaoyuan Feng    io.memidx := memidx(ptwResp_EntryMatchFirst)
270*185e6164SHaoyuan Feng  }
271*185e6164SHaoyuan Feng
272*185e6164SHaoyuan Feng  when (io.flush) {
273*185e6164SHaoyuan Feng    v.map(_ := false.B)
274*185e6164SHaoyuan Feng  }
275*185e6164SHaoyuan Feng
276*185e6164SHaoyuan Feng  if (hasHint) {
277*185e6164SHaoyuan Feng    val hintIO = io.hint.getOrElse(new TlbHintIO)
278*185e6164SHaoyuan Feng    for (i <- 0 until exuParameters.LduCnt) {
279*185e6164SHaoyuan Feng      hintIO.req(i).id := enqidx(i)
280*185e6164SHaoyuan Feng      hintIO.req(i).full := !canenq(i) || ptwResp_ReqMatchVec(i)
281*185e6164SHaoyuan Feng    }
282*185e6164SHaoyuan Feng    hintIO.resp.valid := io.refill
283*185e6164SHaoyuan Feng    hintIO.resp.bits.id := ptwResp_EntryMatchFirst
284*185e6164SHaoyuan Feng    hintIO.resp.bits.replay_all := PopCount(ptwResp_EntryMatchVec) > 1.U
285*185e6164SHaoyuan Feng  }
286*185e6164SHaoyuan Feng
287*185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
288*185e6164SHaoyuan Feng    vi && io.debugTopDown.robHeadVaddr.valid && vpni === get_pn(io.debugTopDown.robHeadVaddr.bits)
289*185e6164SHaoyuan Feng  }}).asUInt.orR
290*185e6164SHaoyuan Feng
291*185e6164SHaoyuan Feng
292*185e6164SHaoyuan Feng  // Perf Counter
293*185e6164SHaoyuan Feng  val counter = PopCount(v)
294*185e6164SHaoyuan Feng  val inflight_counter = RegInit(0.U(log2Up(Size).W))
295*185e6164SHaoyuan Feng  val inflight_full = inflight_counter === Size.U
296*185e6164SHaoyuan Feng  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
297*185e6164SHaoyuan Feng    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
298*185e6164SHaoyuan Feng  }
299*185e6164SHaoyuan Feng
300*185e6164SHaoyuan Feng  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
301*185e6164SHaoyuan Feng  when (counter === 0.U) {
302*185e6164SHaoyuan Feng    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
303*185e6164SHaoyuan Feng  }
304*185e6164SHaoyuan Feng
305*185e6164SHaoyuan Feng  when (io.flush) {
306*185e6164SHaoyuan Feng    inflight_counter := 0.U
307*185e6164SHaoyuan Feng  }
308*185e6164SHaoyuan Feng
309*185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
310*185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_req_count_filtered", PopCount(enqvalid))
311*185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
312*185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
313*185e6164SHaoyuan Feng  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
314*185e6164SHaoyuan Feng  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
315*185e6164SHaoyuan Feng  XSPerfAccumulate("inflight_cycle", Cat(sent).orR)
316*185e6164SHaoyuan Feng
317*185e6164SHaoyuan Feng  for (i <- 0 until Size + 1) {
318*185e6164SHaoyuan Feng    XSPerfAccumulate(s"counter${i}", counter === i.U)
319*185e6164SHaoyuan Feng  }
320*185e6164SHaoyuan Feng
321*185e6164SHaoyuan Feng  for (i <- 0 until Size) {
322*185e6164SHaoyuan Feng    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
323*185e6164SHaoyuan Feng  }
324*185e6164SHaoyuan Feng
325*185e6164SHaoyuan Feng}
326*185e6164SHaoyuan Feng
327*185e6164SHaoyuan Fengclass PTWNewFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
328*185e6164SHaoyuan Feng  require(Size >= Width)
329*185e6164SHaoyuan Feng
330*185e6164SHaoyuan Feng  val io = IO(new PTWFilterIO(Width, hasHint = true))
331*185e6164SHaoyuan Feng
332*185e6164SHaoyuan Feng  val load_filter = VecInit(Seq.fill(1) {
333*185e6164SHaoyuan Feng    val load_entry = Module(new PTWFilterEntry(Width = exuParameters.LduCnt + 1, Size = loadfiltersize, hasHint = true))
334*185e6164SHaoyuan Feng    load_entry.io
335*185e6164SHaoyuan Feng  })
336*185e6164SHaoyuan Feng
337*185e6164SHaoyuan Feng  val store_filter = VecInit(Seq.fill(1) {
338*185e6164SHaoyuan Feng    val store_entry = Module(new PTWFilterEntry(Width = exuParameters.StuCnt, Size = storefiltersize))
339*185e6164SHaoyuan Feng    store_entry.io
340*185e6164SHaoyuan Feng  })
341*185e6164SHaoyuan Feng
342*185e6164SHaoyuan Feng  val prefetch_filter = VecInit(Seq.fill(1) {
343*185e6164SHaoyuan Feng    val prefetch_entry = Module(new PTWFilterEntry(Width = 1, Size = prefetchfiltersize))
344*185e6164SHaoyuan Feng    prefetch_entry.io
345*185e6164SHaoyuan Feng  })
346*185e6164SHaoyuan Feng
347*185e6164SHaoyuan Feng  val filter = load_filter ++ store_filter ++ prefetch_filter
348*185e6164SHaoyuan Feng
349*185e6164SHaoyuan Feng  load_filter.map(_.tlb.req := io.tlb.req.take(exuParameters.LduCnt + 1))
350*185e6164SHaoyuan Feng  store_filter.map(_.tlb.req := io.tlb.req.drop(exuParameters.LduCnt + 1).take(exuParameters.StuCnt))
351*185e6164SHaoyuan Feng  prefetch_filter.map(_.tlb.req := io.tlb.req.drop(exuParameters.LduCnt + 1 + exuParameters.StuCnt))
352*185e6164SHaoyuan Feng
353*185e6164SHaoyuan Feng  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
354*185e6164SHaoyuan Feng  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
355*185e6164SHaoyuan Feng  val ptwResp_valid = Cat(filter.map(_.refill)).orR
356*185e6164SHaoyuan Feng  filter.map(_.tlb.resp.ready := true.B)
357*185e6164SHaoyuan Feng  filter.map(_.ptw.resp.valid := RegNext(io.ptw.resp.fire, init = false.B))
358*185e6164SHaoyuan Feng  filter.map(_.ptw.resp.bits := ptwResp)
359*185e6164SHaoyuan Feng  filter.map(_.flush := flush)
360*185e6164SHaoyuan Feng  filter.map(_.sfence := io.sfence)
361*185e6164SHaoyuan Feng  filter.map(_.csr := io.csr)
362*185e6164SHaoyuan Feng  filter.map(_.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr)
363*185e6164SHaoyuan Feng
364*185e6164SHaoyuan Feng  io.tlb.req.map(_.ready := true.B)
365*185e6164SHaoyuan Feng  io.tlb.resp.valid := ptwResp_valid
366*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.entry := ptwResp.entry
367*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
368*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
369*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
370*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
371*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.pf := ptwResp.pf
372*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.af := ptwResp.af
373*185e6164SHaoyuan Feng  io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
374*185e6164SHaoyuan Feng  // vector used to represent different requestors of DTLB
375*185e6164SHaoyuan Feng  // (e.g. the store DTLB has StuCnt requestors)
376*185e6164SHaoyuan Feng  // However, it is only necessary to distinguish between different DTLB now
377*185e6164SHaoyuan Feng  for (i <- 0 until Width) {
378*185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(i) := false.B
379*185e6164SHaoyuan Feng  }
380*185e6164SHaoyuan Feng  io.tlb.resp.bits.vector(0) := load_filter(0).refill
381*185e6164SHaoyuan Feng  io.tlb.resp.bits.vector(exuParameters.LduCnt + 1) := store_filter(0).refill
382*185e6164SHaoyuan Feng  io.tlb.resp.bits.vector(exuParameters.LduCnt + 1 + exuParameters.StuCnt) := prefetch_filter(0).refill
383*185e6164SHaoyuan Feng
384*185e6164SHaoyuan Feng  val hintIO = io.hint.getOrElse(new TlbHintIO)
385*185e6164SHaoyuan Feng  val load_hintIO = load_filter(0).hint.getOrElse(new TlbHintIO)
386*185e6164SHaoyuan Feng  for (i <- 0 until exuParameters.LduCnt) {
387*185e6164SHaoyuan Feng    hintIO.req(i) := RegNext(load_hintIO.req(i))
388*185e6164SHaoyuan Feng  }
389*185e6164SHaoyuan Feng  hintIO.resp := RegNext(load_hintIO.resp)
390*185e6164SHaoyuan Feng
391*185e6164SHaoyuan Feng  when (load_filter(0).refill) {
392*185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(0) := true.B
393*185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := load_filter(0).memidx
394*185e6164SHaoyuan Feng  }
395*185e6164SHaoyuan Feng  when (store_filter(0).refill) {
396*185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(exuParameters.LduCnt + 1) := true.B
397*185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := store_filter(0).memidx
398*185e6164SHaoyuan Feng  }
399*185e6164SHaoyuan Feng  when (prefetch_filter(0).refill) {
400*185e6164SHaoyuan Feng    io.tlb.resp.bits.vector(exuParameters.LduCnt + 1 + exuParameters.StuCnt) := true.B
401*185e6164SHaoyuan Feng    io.tlb.resp.bits.data.memidx := 0.U.asTypeOf(new MemBlockidxBundle)
402*185e6164SHaoyuan Feng  }
403*185e6164SHaoyuan Feng
404*185e6164SHaoyuan Feng  val ptw_arb = Module(new RRArbiterInit(new PtwReq, 3))
405*185e6164SHaoyuan Feng  for (i <- 0 until 3) {
406*185e6164SHaoyuan Feng    ptw_arb.io.in(i).valid := filter(i).ptw.req(0).valid
407*185e6164SHaoyuan Feng    ptw_arb.io.in(i).bits.vpn := filter(i).ptw.req(0).bits.vpn
408*185e6164SHaoyuan Feng    filter(i).ptw.req(0).ready := ptw_arb.io.in(i).ready
409*185e6164SHaoyuan Feng  }
410*185e6164SHaoyuan Feng  ptw_arb.io.out.ready := io.ptw.req(0).ready
411*185e6164SHaoyuan Feng  io.ptw.req(0).valid := ptw_arb.io.out.valid
412*185e6164SHaoyuan Feng  io.ptw.req(0).bits.vpn := ptw_arb.io.out.bits.vpn
413*185e6164SHaoyuan Feng  io.ptw.resp.ready := true.B
414*185e6164SHaoyuan Feng
415*185e6164SHaoyuan Feng  io.rob_head_miss_in_tlb := Cat(filter.map(_.rob_head_miss_in_tlb)).orR
416*185e6164SHaoyuan Feng}
417*185e6164SHaoyuan Feng
418f1fe8698SLemoverclass PTWFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
4196d5ddbceSLemover  require(Size >= Width)
4206d5ddbceSLemover
42145f497a4Shappy-lx  val io = IO(new PTWFilterIO(Width))
42245f497a4Shappy-lx
4236d5ddbceSLemover  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
424a0301c0dSLemover  val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports
4256d5ddbceSLemover  val vpn = Reg(Vec(Size, UInt(vpnLen.W)))
4268744445eSMaxpicca-Li  val memidx = Reg(Vec(Size, new MemBlockidxBundle))
4276d5ddbceSLemover  val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq
4286d5ddbceSLemover  val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw
4296d5ddbceSLemover  val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq
4306d5ddbceSLemover  val mayFullDeq = RegInit(false.B)
4316d5ddbceSLemover  val mayFullIss = RegInit(false.B)
4326d5ddbceSLemover  val counter = RegInit(0.U(log2Up(Size+1).W))
4336d5ddbceSLemover
434f1fe8698SLemover  val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)
435f1fe8698SLemover  val tlb_req = WireInit(io.tlb.req) // NOTE: tlb_req is not io.tlb.req, see below codes, just use cloneType
436cccfc98dSLemover  tlb_req.suggestName("tlb_req")
437cccfc98dSLemover
438fa9f9690SLemover  val inflight_counter = RegInit(0.U(log2Up(Size + 1).W))
439fa9f9690SLemover  val inflight_full = inflight_counter === Size.U
440935edac4STang Haojin  when (io.ptw.req(0).fire =/= io.ptw.resp.fire) {
441935edac4STang Haojin    inflight_counter := Mux(io.ptw.req(0).fire, inflight_counter + 1.U, inflight_counter - 1.U)
442fa9f9690SLemover  }
443fa9f9690SLemover
44487f41827SLemover  val canEnqueue = Wire(Bool()) // NOTE: actually enqueue
445935edac4STang Haojin  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire)
446cccfc98dSLemover  val ptwResp_OldMatchVec = vpn.zip(v).map{ case (pi, vi) =>
44763632028SHaoyuan Feng    vi && io.ptw.resp.bits.hit(pi, io.csr.satp.asid, true, true)}
448935edac4STang Haojin  val ptwResp_valid = RegNext(io.ptw.resp.fire && Cat(ptwResp_OldMatchVec).orR, init = false.B)
44963632028SHaoyuan Feng  // May send repeated requests to L2 tlb with same vpn(26, 3) when sector tlb
450cccfc98dSLemover  val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).map{ case (pi, vi) => vi && pi === a.bits.vpn})
45187f41827SLemover  val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn && canEnqueue})
452cccfc98dSLemover  val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn))
453cccfc98dSLemover
454cccfc98dSLemover  (0 until Width) foreach { i =>
455cccfc98dSLemover    tlb_req(i).valid := RegNext(io.tlb.req(i).valid &&
45663632028SHaoyuan Feng      !(ptwResp_valid && ptwResp.hit(io.tlb.req(i).bits.vpn, 0.U, true, true)) &&
457cccfc98dSLemover      !Cat(lastReqMatchVec_early(i)).orR,
458cccfc98dSLemover      init = false.B)
459cccfc98dSLemover    tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid)
460cccfc98dSLemover  }
461cccfc98dSLemover
462cccfc98dSLemover  val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR))
463cccfc98dSLemover  val newMatchVec = (0 until Width).map(i => (0 until Width).map(j =>
464cccfc98dSLemover    RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid
465cccfc98dSLemover  ))
466cccfc98dSLemover  val ptwResp_newMatchVec = tlb_req.map(a =>
46763632028SHaoyuan Feng    ptwResp_valid && ptwResp.hit(a.bits.vpn, 0.U, allType = true, true))
468cccfc98dSLemover
469cccfc98dSLemover  val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid))
470cccfc98dSLemover  val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i)))
471a0301c0dSLemover  val ports_init = (0 until Width).map(i => (1 << i).U(Width.W))
472a0301c0dSLemover  val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i)))
473935edac4STang Haojin  val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire)
4746d5ddbceSLemover
475a0301c0dSLemover  def canMerge(index: Int) : Bool = {
476cccfc98dSLemover    ptwResp_newMatchVec(index) || oldMatchVec(index) ||
477a0301c0dSLemover    Cat(newMatchVec(index).take(index)).orR
478a0301c0dSLemover  }
479a0301c0dSLemover
480a0301c0dSLemover  def filter_req() = {
481a0301c0dSLemover    val reqs =  tlb_req.indices.map{ i =>
4828744445eSMaxpicca-Li      val req = Wire(ValidIO(new PtwReqwithMemIdx()))
483a0301c0dSLemover      val merge = canMerge(i)
484a0301c0dSLemover      req.bits := tlb_req(i).bits
485a0301c0dSLemover      req.valid := !merge && tlb_req(i).valid
486a0301c0dSLemover      req
487a0301c0dSLemover    }
488a0301c0dSLemover    reqs
489a0301c0dSLemover  }
490a0301c0dSLemover
491a0301c0dSLemover  val reqs = filter_req()
492a0301c0dSLemover  val req_ports = filter_ports
4936d5ddbceSLemover  val isFull = enqPtr === deqPtr && mayFullDeq
4946d5ddbceSLemover  val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq
4956d5ddbceSLemover  val isEmptyIss = enqPtr === issPtr && !mayFullIss
4966d5ddbceSLemover  val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid)))
497cccfc98dSLemover  val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U))
498cccfc98dSLemover  val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i))))
4996d5ddbceSLemover  val enqNum = PopCount(reqs.map(_.valid))
50087f41827SLemover  canEnqueue := counter +& enqNum <= Size.U
5016d5ddbceSLemover
502f1fe8698SLemover  // the req may recv false ready, but actually received. Filter and TLB will handle it.
503f1fe8698SLemover  val enqNum_fake = PopCount(io.tlb.req.map(_.valid))
504f1fe8698SLemover  val canEnqueue_fake = counter +& enqNum_fake <= Size.U
505f1fe8698SLemover  io.tlb.req.map(_.ready := canEnqueue_fake) // NOTE: just drop un-fire reqs
506f1fe8698SLemover
5070ab9ba15SLemover  // tlb req flushed by ptw resp: last ptw resp && current ptw resp
5080ab9ba15SLemover  // the flushed tlb req will fakely enq, with a false valid
50963632028SHaoyuan Feng  val tlb_req_flushed = reqs.map(a => io.ptw.resp.valid && io.ptw.resp.bits.hit(a.bits.vpn, 0.U, true, true))
5100ab9ba15SLemover
511cccfc98dSLemover  io.tlb.resp.valid := ptwResp_valid
5128744445eSMaxpicca-Li  io.tlb.resp.bits.data.entry := ptwResp.entry
51363632028SHaoyuan Feng  io.tlb.resp.bits.data.addr_low := ptwResp.addr_low
51463632028SHaoyuan Feng  io.tlb.resp.bits.data.ppn_low := ptwResp.ppn_low
51563632028SHaoyuan Feng  io.tlb.resp.bits.data.valididx := ptwResp.valididx
516b0fa7106SHaoyuan Feng  io.tlb.resp.bits.data.pteidx := ptwResp.pteidx
5178744445eSMaxpicca-Li  io.tlb.resp.bits.data.pf := ptwResp.pf
5188744445eSMaxpicca-Li  io.tlb.resp.bits.data.af := ptwResp.af
5198744445eSMaxpicca-Li  io.tlb.resp.bits.data.memidx := memidx(OHToUInt(ptwResp_OldMatchVec))
520a0301c0dSLemover  io.tlb.resp.bits.vector := resp_vector
5212c2c1588SLemover
522fa9f9690SLemover  val issue_valid = v(issPtr) && !isEmptyIss && !inflight_full
52363632028SHaoyuan Feng  val issue_filtered = ptwResp_valid && ptwResp.hit(io.ptw.req(0).bits.vpn, io.csr.satp.asid, allType=true, ignoreAsid=true)
5242c2c1588SLemover  val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/))
5252c2c1588SLemover  io.ptw.req(0).valid := issue_valid && !issue_filtered
5266d5ddbceSLemover  io.ptw.req(0).bits.vpn := vpn(issPtr)
5276d5ddbceSLemover  io.ptw.resp.ready := true.B
5286d5ddbceSLemover
5296d5ddbceSLemover  reqs.zipWithIndex.map{
5306d5ddbceSLemover    case (req, i) =>
5316d5ddbceSLemover      when (req.valid && canEnqueue) {
5320ab9ba15SLemover        v(enqPtrVec(i)) := !tlb_req_flushed(i)
5336d5ddbceSLemover        vpn(enqPtrVec(i)) := req.bits.vpn
5348744445eSMaxpicca-Li        memidx(enqPtrVec(i)) := req.bits.memidx
535a0301c0dSLemover        ports(enqPtrVec(i)) := req_ports(i).asBools
536a0301c0dSLemover      }
537a0301c0dSLemover  }
538a0301c0dSLemover  for (i <- ports.indices) {
539a0301c0dSLemover    when (v(i)) {
540a0301c0dSLemover      ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2)
5416d5ddbceSLemover    }
5426d5ddbceSLemover  }
5436d5ddbceSLemover
5446d5ddbceSLemover  val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR
5456d5ddbceSLemover  val do_deq = (!v(deqPtr) && !isEmptyDeq)
5462c2c1588SLemover  val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss)
5476d5ddbceSLemover  when (do_enq) {
5486d5ddbceSLemover    enqPtr := enqPtr + enqNum
5496d5ddbceSLemover  }
5506d5ddbceSLemover  when (do_deq) {
5516d5ddbceSLemover    deqPtr := deqPtr + 1.U
5526d5ddbceSLemover  }
5536d5ddbceSLemover  when (do_iss) {
5546d5ddbceSLemover    issPtr := issPtr + 1.U
5556d5ddbceSLemover  }
5562c2c1588SLemover  when (issue_fire_fake && issue_filtered) { // issued but is filtered
5572c2c1588SLemover    v(issPtr) := false.B
5582c2c1588SLemover  }
5596d5ddbceSLemover  when (do_enq =/= do_deq) {
5606d5ddbceSLemover    mayFullDeq := do_enq
5616d5ddbceSLemover  }
5626d5ddbceSLemover  when (do_enq =/= do_iss) {
5636d5ddbceSLemover    mayFullIss := do_enq
5646d5ddbceSLemover  }
5656d5ddbceSLemover
566935edac4STang Haojin  when (io.ptw.resp.fire) {
567cccfc98dSLemover    v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
5686d5ddbceSLemover  }
5696d5ddbceSLemover
5706d5ddbceSLemover  counter := counter - do_deq + Mux(do_enq, enqNum, 0.U)
571fa9f9690SLemover  assert(counter <= Size.U, "counter should be no more than Size")
572fa9f9690SLemover  assert(inflight_counter <= Size.U, "inflight should be no more than Size")
5736d5ddbceSLemover  when (counter === 0.U) {
574935edac4STang Haojin    assert(!io.ptw.req(0).fire, "when counter is 0, should not req")
5756d5ddbceSLemover    assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty")
5766d5ddbceSLemover  }
5776d5ddbceSLemover  when (counter === Size.U) {
5786d5ddbceSLemover    assert(mayFullDeq, "when counter is Size, should be full")
5796d5ddbceSLemover  }
5806d5ddbceSLemover
58145f497a4Shappy-lx  when (flush) {
5826d5ddbceSLemover    v.map(_ := false.B)
5836d5ddbceSLemover    deqPtr := 0.U
5846d5ddbceSLemover    enqPtr := 0.U
5856d5ddbceSLemover    issPtr := 0.U
5866d5ddbceSLemover    ptwResp_valid := false.B
5876d5ddbceSLemover    mayFullDeq := false.B
5886d5ddbceSLemover    mayFullIss := false.B
5896d5ddbceSLemover    counter := 0.U
590fa9f9690SLemover    inflight_counter := 0.U
5916d5ddbceSLemover  }
5926d5ddbceSLemover
59360ebee38STang Haojin  val robHeadVaddr = io.debugTopDown.robHeadVaddr
594d2b20d1aSTang Haojin  io.rob_head_miss_in_tlb := VecInit(v.zip(vpn).map{case (vi, vpni) => {
59560ebee38STang Haojin    vi && robHeadVaddr.valid && vpni === get_pn(robHeadVaddr.bits)
596d2b20d1aSTang Haojin  }}).asUInt.orR
597d2b20d1aSTang Haojin
5986d5ddbceSLemover  // perf
5996d5ddbceSLemover  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
6006d5ddbceSLemover  XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U))
601935edac4STang Haojin  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire)
6026d5ddbceSLemover  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
603935edac4STang Haojin  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire)
604935edac4STang Haojin  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire)
6056d5ddbceSLemover  XSPerfAccumulate("inflight_cycle", !isEmptyDeq)
6066d5ddbceSLemover  for (i <- 0 until Size + 1) {
6076d5ddbceSLemover    XSPerfAccumulate(s"counter${i}", counter === i.U)
6086d5ddbceSLemover  }
6099bd9cdfaSLemover
6109bd9cdfaSLemover  for (i <- 0 until Size) {
6119bd9cdfaSLemover    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
6129bd9cdfaSLemover  }
6136d5ddbceSLemover}
61438ba1efdSLemover
61538ba1efdSLemoverobject PTWRepeater {
616f1fe8698SLemover  def apply(fenceDelay: Int,
61738ba1efdSLemover    tlb: TlbPtwIO,
61838ba1efdSLemover    sfence: SfenceBundle,
61938ba1efdSLemover    csr: TlbCsrBundle
62038ba1efdSLemover  )(implicit p: Parameters) = {
62138ba1efdSLemover    val width = tlb.req.size
622f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
62335d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
62438ba1efdSLemover    repeater
62538ba1efdSLemover  }
62638ba1efdSLemover
627f1fe8698SLemover  def apply(fenceDelay: Int,
62838ba1efdSLemover    tlb: TlbPtwIO,
62938ba1efdSLemover    ptw: TlbPtwIO,
63038ba1efdSLemover    sfence: SfenceBundle,
63138ba1efdSLemover    csr: TlbCsrBundle
63238ba1efdSLemover  )(implicit p: Parameters) = {
63338ba1efdSLemover    val width = tlb.req.size
634f1fe8698SLemover    val repeater = Module(new PTWRepeater(width, fenceDelay))
63535d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
63635d6335eSZhangZifei    repeater
63735d6335eSZhangZifei  }
63835d6335eSZhangZifei}
63938ba1efdSLemover
64035d6335eSZhangZifeiobject PTWRepeaterNB {
641f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
64235d6335eSZhangZifei    tlb: TlbPtwIO,
64335d6335eSZhangZifei    sfence: SfenceBundle,
64435d6335eSZhangZifei    csr: TlbCsrBundle
64535d6335eSZhangZifei  )(implicit p: Parameters) = {
64635d6335eSZhangZifei    val width = tlb.req.size
647f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady,fenceDelay))
64835d6335eSZhangZifei    repeater.io.apply(tlb, sfence, csr)
64935d6335eSZhangZifei    repeater
65035d6335eSZhangZifei  }
65135d6335eSZhangZifei
652f1fe8698SLemover  def apply(passReady: Boolean, fenceDelay: Int,
65335d6335eSZhangZifei    tlb: TlbPtwIO,
65435d6335eSZhangZifei    ptw: TlbPtwIO,
65535d6335eSZhangZifei    sfence: SfenceBundle,
65635d6335eSZhangZifei    csr: TlbCsrBundle
65735d6335eSZhangZifei  )(implicit p: Parameters) = {
65835d6335eSZhangZifei    val width = tlb.req.size
659f1fe8698SLemover    val repeater = Module(new PTWRepeaterNB(width, passReady, fenceDelay))
66035d6335eSZhangZifei    repeater.io.apply(tlb, ptw, sfence, csr)
66138ba1efdSLemover    repeater
66238ba1efdSLemover  }
66338ba1efdSLemover}
66438ba1efdSLemover
66538ba1efdSLemoverobject PTWFilter {
666f1fe8698SLemover  def apply(fenceDelay: Int,
667f1fe8698SLemover    tlb: VectorTlbPtwIO,
66838ba1efdSLemover    ptw: TlbPtwIO,
66938ba1efdSLemover    sfence: SfenceBundle,
67038ba1efdSLemover    csr: TlbCsrBundle,
67138ba1efdSLemover    size: Int
67238ba1efdSLemover  )(implicit p: Parameters) = {
67338ba1efdSLemover    val width = tlb.req.size
674f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
67535d6335eSZhangZifei    filter.io.apply(tlb, ptw, sfence, csr)
67638ba1efdSLemover    filter
67738ba1efdSLemover  }
67835d6335eSZhangZifei
679f1fe8698SLemover  def apply(fenceDelay: Int,
680f1fe8698SLemover    tlb: VectorTlbPtwIO,
68135d6335eSZhangZifei    sfence: SfenceBundle,
68235d6335eSZhangZifei    csr: TlbCsrBundle,
68335d6335eSZhangZifei    size: Int
68435d6335eSZhangZifei  )(implicit p: Parameters) = {
68535d6335eSZhangZifei    val width = tlb.req.size
686f1fe8698SLemover    val filter = Module(new PTWFilter(width, size, fenceDelay))
68735d6335eSZhangZifei    filter.io.apply(tlb, sfence, csr)
68835d6335eSZhangZifei    filter
68935d6335eSZhangZifei  }
690*185e6164SHaoyuan Feng}
69135d6335eSZhangZifei
692*185e6164SHaoyuan Fengobject PTWNewFilter {
693*185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
694*185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
695*185e6164SHaoyuan Feng            ptw: TlbPtwIO,
696*185e6164SHaoyuan Feng            sfence: SfenceBundle,
697*185e6164SHaoyuan Feng            csr: TlbCsrBundle,
698*185e6164SHaoyuan Feng            size: Int
699*185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
700*185e6164SHaoyuan Feng    val width = tlb.req.size
701*185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
702*185e6164SHaoyuan Feng    filter.io.apply(tlb, ptw, sfence, csr)
703*185e6164SHaoyuan Feng    filter
704*185e6164SHaoyuan Feng  }
705*185e6164SHaoyuan Feng
706*185e6164SHaoyuan Feng  def apply(fenceDelay: Int,
707*185e6164SHaoyuan Feng            tlb: VectorTlbPtwIO,
708*185e6164SHaoyuan Feng            sfence: SfenceBundle,
709*185e6164SHaoyuan Feng            csr: TlbCsrBundle,
710*185e6164SHaoyuan Feng            size: Int
711*185e6164SHaoyuan Feng           )(implicit p: Parameters) = {
712*185e6164SHaoyuan Feng    val width = tlb.req.size
713*185e6164SHaoyuan Feng    val filter = Module(new PTWNewFilter(width, size, fenceDelay))
714*185e6164SHaoyuan Feng    filter.io.apply(tlb, sfence, csr)
715*185e6164SHaoyuan Feng    filter
716*185e6164SHaoyuan Feng  }
71738ba1efdSLemover}
718