xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/L2TLB.scala (revision 325f0a4e3ab9ec31d2358a1a9d6d39dab74d9eff)
192e3bfefSLemover/***************************************************************************************
292e3bfefSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
392e3bfefSLemover* Copyright (c) 2020-2021 Peng Cheng Laboratory
492e3bfefSLemover*
592e3bfefSLemover* XiangShan is licensed under Mulan PSL v2.
692e3bfefSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
792e3bfefSLemover* You may obtain a copy of Mulan PSL v2 at:
892e3bfefSLemover*          http://license.coscl.org.cn/MulanPSL2
992e3bfefSLemover*
1092e3bfefSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1192e3bfefSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1292e3bfefSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1392e3bfefSLemover*
1492e3bfefSLemover* See the Mulan PSL v2 for more details.
1592e3bfefSLemover***************************************************************************************/
1692e3bfefSLemover
1792e3bfefSLemoverpackage xiangshan.cache.mmu
1892e3bfefSLemover
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
2092e3bfefSLemoverimport chisel3._
2192e3bfefSLemoverimport chisel3.experimental.ExtModule
2292e3bfefSLemoverimport chisel3.util._
2392e3bfefSLemoverimport xiangshan._
2492e3bfefSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
2592e3bfefSLemoverimport utils._
263c02ee8fSwakafaimport utility._
2792e3bfefSLemoverimport freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp}
2892e3bfefSLemoverimport freechips.rocketchip.tilelink._
2992e3bfefSLemoverimport xiangshan.backend.fu.{PMP, PMPChecker, PMPReqBundle, PMPRespBundle}
3092e3bfefSLemoverimport xiangshan.backend.fu.util.HasCSRConst
319c26bab7SHaoyuan Fengimport difftest._
3292e3bfefSLemover
3392e3bfefSLemoverclass L2TLB()(implicit p: Parameters) extends LazyModule with HasPtwConst {
3495e60e55STang Haojin  override def shouldBeInlined: Boolean = false
3592e3bfefSLemover
3692e3bfefSLemover  val node = TLClientNode(Seq(TLMasterPortParameters.v1(
3792e3bfefSLemover    clients = Seq(TLMasterParameters.v1(
3892e3bfefSLemover      "ptw",
3992e3bfefSLemover      sourceId = IdRange(0, MemReqWidth)
40d2b20d1aSTang Haojin    )),
41d2b20d1aSTang Haojin    requestFields = Seq(ReqSourceField())
4292e3bfefSLemover  )))
4392e3bfefSLemover
4492e3bfefSLemover  lazy val module = new L2TLBImp(this)
4592e3bfefSLemover}
4692e3bfefSLemover
4792e3bfefSLemoverclass L2TLBImp(outer: L2TLB)(implicit p: Parameters) extends PtwModule(outer) with HasCSRConst with HasPerfEvents {
4892e3bfefSLemover
4992e3bfefSLemover  val (mem, edge) = outer.node.out.head
5092e3bfefSLemover
5192e3bfefSLemover  val io = IO(new L2TLBIO)
5292e3bfefSLemover  val difftestIO = IO(new Bundle() {
5392e3bfefSLemover    val ptwResp = Output(Bool())
5492e3bfefSLemover    val ptwAddr = Output(UInt(64.W))
5592e3bfefSLemover    val ptwData = Output(Vec(4, UInt(64.W)))
5692e3bfefSLemover  })
5792e3bfefSLemover
5892e3bfefSLemover  /* Ptw processes multiple requests
5992e3bfefSLemover   * Divide Ptw procedure into two stages: cache access ; mem access if cache miss
6092e3bfefSLemover   *           miss queue itlb       dtlb
6192e3bfefSLemover   *               |       |         |
6292e3bfefSLemover   *               ------arbiter------
6392e3bfefSLemover   *                            |
6492e3bfefSLemover   *                    l1 - l2 - l3 - sp
6592e3bfefSLemover   *                            |
6692e3bfefSLemover   *          -------------------------------------------
6792e3bfefSLemover   *    miss  |  queue                                  | hit
6892e3bfefSLemover   *    [][][][][][]                                    |
6992e3bfefSLemover   *          |                                         |
7092e3bfefSLemover   *    state machine accessing mem                     |
7192e3bfefSLemover   *          |                                         |
7292e3bfefSLemover   *          ---------------arbiter---------------------
7392e3bfefSLemover   *                 |                    |
7492e3bfefSLemover   *                itlb                 dtlb
7592e3bfefSLemover   */
7692e3bfefSLemover
7792e3bfefSLemover  difftestIO <> DontCare
7892e3bfefSLemover
797797f035SbugGenerator  val sfence_tmp = DelayN(io.sfence, 1)
807797f035SbugGenerator  val csr_tmp    = DelayN(io.csr.tlb, 1)
81d0de7e4aSpeixiaokun  val sfence_dup = Seq.fill(9)(RegNext(sfence_tmp))
82d0de7e4aSpeixiaokun  val csr_dup = Seq.fill(8)(RegNext(csr_tmp))
837797f035SbugGenerator  val satp   = csr_dup(0).satp
84d0de7e4aSpeixiaokun  val vsatp  = csr_dup(0).vsatp
85d0de7e4aSpeixiaokun  val hgatp  = csr_dup(0).hgatp
867797f035SbugGenerator  val priv   = csr_dup(0).priv
87d0de7e4aSpeixiaokun  val flush  = sfence_dup(0).valid || satp.changed || vsatp.changed || hgatp.changed
8892e3bfefSLemover
8992e3bfefSLemover  val pmp = Module(new PMP())
90c3d5cfb3Speixiaokun  val pmp_check = VecInit(Seq.fill(3)(Module(new PMPChecker(lgMaxSize = 3, sameCycle = true)).io))
9192e3bfefSLemover  pmp.io.distribute_csr := io.csr.distribute_csr
9292e3bfefSLemover  pmp_check.foreach(_.check_env.apply(ModeS, pmp.io.pmp, pmp.io.pma))
9392e3bfefSLemover
9492e3bfefSLemover  val missQueue = Module(new L2TlbMissQueue)
9592e3bfefSLemover  val cache = Module(new PtwCache)
9692e3bfefSLemover  val ptw = Module(new PTW)
97d0de7e4aSpeixiaokun  val hptw = Module(new HPTW)
9892e3bfefSLemover  val llptw = Module(new LLPTW)
997797f035SbugGenerator  val blockmq = Module(new BlockHelper(3))
10092e3bfefSLemover  val arb1 = Module(new Arbiter(new PtwReq, PtwWidth))
1016967f5d5Speixiaokun  val arb2 = Module(new Arbiter(new L2TlbWithHptwIdBundle, ((if (l2tlbParams.enablePrefetch) 4 else 3) + (if(HasHExtension) 1 else 0))))
102d0de7e4aSpeixiaokun  val hptw_req_arb = Module(new Arbiter(new Bundle {
103d0de7e4aSpeixiaokun    val id = UInt(log2Up(l2tlbParams.llptwsize).W)
104eb4bf3f2Speixiaokun    val source = UInt(bSourceWidth.W)
10582978df9Speixiaokun    val gvpn = UInt(vpnLen.W)
106d0de7e4aSpeixiaokun  }, 2))
107d0de7e4aSpeixiaokun  val hptw_resp_arb = Module(new Arbiter(new Bundle {
108d0de7e4aSpeixiaokun    val resp = new HptwResp()
109d0de7e4aSpeixiaokun    val id = UInt(log2Up(l2tlbParams.llptwsize).W)
110d0de7e4aSpeixiaokun  }, 2))
111d0de7e4aSpeixiaokun  val outArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle {
112d0de7e4aSpeixiaokun    val s2xlate = UInt(2.W)
113eb4bf3f2Speixiaokun    val s1 = new PtwSectorResp ()
114eb4bf3f2Speixiaokun    val s2 = new HptwResp()
115d0de7e4aSpeixiaokun  }, 1)).io)
116d0de7e4aSpeixiaokun  val mergeArb = (0 until PtwWidth).map(i => Module(new Arbiter(new Bundle {
117d0de7e4aSpeixiaokun    val s2xlate = UInt(2.W)
118eb4bf3f2Speixiaokun    val s1 = new PtwMergeResp()
119eb4bf3f2Speixiaokun    val s2 = new HptwResp()
120d0de7e4aSpeixiaokun  }, 3)).io)
12192e3bfefSLemover  val outArbCachePort = 0
12292e3bfefSLemover  val outArbFsmPort = 1
12392e3bfefSLemover  val outArbMqPort = 2
12492e3bfefSLemover
125d0de7e4aSpeixiaokun  // hptw arb input port
126d0de7e4aSpeixiaokun  val InHptwArbPTWPort = 0
127d0de7e4aSpeixiaokun  val InHptwArbLLPTWPort = 1
128d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbPTWPort).valid := ptw.io.hptw.req.valid
129d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbPTWPort).bits.gvpn := ptw.io.hptw.req.bits.gvpn
130d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbPTWPort).bits.id := ptw.io.hptw.req.bits.id
131c3d5cfb3Speixiaokun  hptw_req_arb.io.in(InHptwArbPTWPort).bits.source := ptw.io.hptw.req.bits.source
132d0de7e4aSpeixiaokun  ptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbPTWPort).ready
133d0de7e4aSpeixiaokun
134d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbLLPTWPort).valid := llptw.io.hptw.req.valid
135d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.gvpn := llptw.io.hptw.req.bits.gvpn
136d0de7e4aSpeixiaokun  hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.id := llptw.io.hptw.req.bits.id
137eb4bf3f2Speixiaokun  hptw_req_arb.io.in(InHptwArbLLPTWPort).bits.source := llptw.io.hptw.req.bits.source
138d0de7e4aSpeixiaokun  llptw.io.hptw.req.ready := hptw_req_arb.io.in(InHptwArbLLPTWPort).ready
139d0de7e4aSpeixiaokun
1409c503409SLemover  // arb2 input port
1417f6221c5Speixiaokun  val InArbHPTWPort = 0
1427f6221c5Speixiaokun  val InArbPTWPort = 1
1437f6221c5Speixiaokun  val InArbMissQueuePort = 2
1447f6221c5Speixiaokun  val InArbTlbPort = 3
1457f6221c5Speixiaokun  val InArbPrefetchPort = 4
14692e3bfefSLemover  // NOTE: when cache out but miss and ptw doesnt accept,
14792e3bfefSLemover  arb1.io.in <> VecInit(io.tlb.map(_.req(0)))
148eb4bf3f2Speixiaokun
14992e3bfefSLemover
1509c503409SLemover  arb2.io.in(InArbPTWPort).valid := ptw.io.llptw.valid
1516967f5d5Speixiaokun  arb2.io.in(InArbPTWPort).bits.req_info := ptw.io.llptw.bits.req_info
152*325f0a4eSpeixiaokun  arb2.io.in(InArbPTWPort).bits.isHptwReq := false.B
1537f6221c5Speixiaokun  arb2.io.in(InArbPTWPort).bits.isLLptw := false.B
1546967f5d5Speixiaokun  arb2.io.in(InArbPTWPort).bits.hptwId := DontCare
1559c503409SLemover  ptw.io.llptw.ready := arb2.io.in(InArbPTWPort).ready
156*325f0a4eSpeixiaokun  block_decoupled(missQueue.io.out, arb2.io.in(InArbMissQueuePort), Mux(missQueue.io.out.bits.isHptwReq, !hptw.io.req.ready, Mux(missQueue.io.out.bits.isLLptw, !llptw.io.in.ready, !ptw.io.req.ready)))
1577797f035SbugGenerator
15892e3bfefSLemover  arb2.io.in(InArbTlbPort).valid := arb1.io.out.valid
1596967f5d5Speixiaokun  arb2.io.in(InArbTlbPort).bits.req_info.vpn := arb1.io.out.bits.vpn
1606967f5d5Speixiaokun  arb2.io.in(InArbTlbPort).bits.req_info.s2xlate := arb1.io.out.bits.s2xlate
1616967f5d5Speixiaokun  arb2.io.in(InArbTlbPort).bits.req_info.source := arb1.io.chosen
162*325f0a4eSpeixiaokun  arb2.io.in(InArbTlbPort).bits.isHptwReq := false.B
1637f6221c5Speixiaokun  arb2.io.in(InArbTlbPort).bits.isLLptw := false.B
1646967f5d5Speixiaokun  arb2.io.in(InArbTlbPort).bits.hptwId := DontCare
165eb4bf3f2Speixiaokun  arb1.io.out.ready := arb2.io.in(InArbTlbPort).ready
166d0de7e4aSpeixiaokun
167d0de7e4aSpeixiaokun  arb2.io.in(InArbHPTWPort).valid := hptw_req_arb.io.out.valid
1686967f5d5Speixiaokun  arb2.io.in(InArbHPTWPort).bits.req_info.vpn := hptw_req_arb.io.out.bits.gvpn
1696967f5d5Speixiaokun  arb2.io.in(InArbHPTWPort).bits.req_info.s2xlate := onlyStage2
1706967f5d5Speixiaokun  arb2.io.in(InArbHPTWPort).bits.req_info.source := hptw_req_arb.io.out.bits.source
171*325f0a4eSpeixiaokun  arb2.io.in(InArbHPTWPort).bits.isHptwReq := true.B
1727f6221c5Speixiaokun  arb2.io.in(InArbHPTWPort).bits.isLLptw := false.B
1736967f5d5Speixiaokun  arb2.io.in(InArbHPTWPort).bits.hptwId := hptw_req_arb.io.out.bits.id
174eb4bf3f2Speixiaokun  hptw_req_arb.io.out.ready := arb2.io.in(InArbHPTWPort).ready
17592e3bfefSLemover  if (l2tlbParams.enablePrefetch) {
17692e3bfefSLemover    val prefetch = Module(new L2TlbPrefetch())
17792e3bfefSLemover    val recv = cache.io.resp
17892e3bfefSLemover    // NOTE: 1. prefetch doesn't gen prefetch 2. req from mq doesn't gen prefetch
17992e3bfefSLemover    // NOTE: 1. miss req gen prefetch 2. hit but prefetched gen prefetch
180935edac4STang Haojin    prefetch.io.in.valid := recv.fire && !from_pre(recv.bits.req_info.source) && (!recv.bits.hit  ||
18192e3bfefSLemover      recv.bits.prefetch) && recv.bits.isFirst
18292e3bfefSLemover    prefetch.io.in.bits.vpn := recv.bits.req_info.vpn
1837797f035SbugGenerator    prefetch.io.sfence := sfence_dup(0)
1847797f035SbugGenerator    prefetch.io.csr := csr_dup(0)
18592e3bfefSLemover    arb2.io.in(InArbPrefetchPort) <> prefetch.io.out
1865afdf73cSHaoyuan Feng
187da3bf434SMaxpicca-Li    val isWriteL2TlbPrefetchTable = WireInit(Constantin.createRecord("isWriteL2TlbPrefetchTable" + p(XSCoreParamsKey).HartId.toString))
1885afdf73cSHaoyuan Feng    val L2TlbPrefetchTable = ChiselDB.createTable("L2TlbPrefetch_hart" + p(XSCoreParamsKey).HartId.toString, new L2TlbPrefetchDB)
1895afdf73cSHaoyuan Feng    val L2TlbPrefetchDB = Wire(new L2TlbPrefetchDB)
1906967f5d5Speixiaokun    L2TlbPrefetchDB.vpn := prefetch.io.out.bits.req_info.vpn
191da3bf434SMaxpicca-Li    L2TlbPrefetchTable.log(L2TlbPrefetchDB, isWriteL2TlbPrefetchTable.orR && prefetch.io.out.fire, "L2TlbPrefetch", clock, reset)
19292e3bfefSLemover  }
19392e3bfefSLemover  arb2.io.out.ready := cache.io.req.ready
19492e3bfefSLemover
1957797f035SbugGenerator
1966967f5d5Speixiaokun  val mq_arb = Module(new Arbiter(new L2TlbWithHptwIdBundle, 2))
1977797f035SbugGenerator  mq_arb.io.in(0).valid := cache.io.resp.valid && !cache.io.resp.bits.hit &&
1986967f5d5Speixiaokun    !from_pre(cache.io.resp.bits.req_info.source) &&
199*325f0a4eSpeixiaokun    (cache.io.resp.bits.bypassed || (
200*325f0a4eSpeixiaokun      ((!cache.io.resp.bits.toFsm.l2Hit || cache.io.resp.bits.toFsm.stage1Hit) && !cache.io.resp.bits.isHptwReq && (cache.io.resp.bits.isFirst || !ptw.io.req.ready)) // send to ptw, is first or ptw is busy;
201*325f0a4eSpeixiaokun      || (cache.io.resp.bits.toFsm.l2Hit && cache.io.resp.bits.isFirst && !llptw.io.in.ready) // send to llptw, llptw is full
202*325f0a4eSpeixiaokun    ))
203*325f0a4eSpeixiaokun
2046967f5d5Speixiaokun  mq_arb.io.in(0).bits.req_info :=  cache.io.resp.bits.req_info
205*325f0a4eSpeixiaokun  mq_arb.io.in(0).bits.isHptwReq :=  cache.io.resp.bits.isHptwReq
2066967f5d5Speixiaokun  mq_arb.io.in(0).bits.hptwId :=  cache.io.resp.bits.toHptw.id
207*325f0a4eSpeixiaokun  mq_arb.io.in(0).bits.isLLptw := cache.io.resp.bits.toFsm.l2Hit
2086967f5d5Speixiaokun  mq_arb.io.in(1).bits.req_info := llptw.io.cache.bits
209*325f0a4eSpeixiaokun  mq_arb.io.in(1).bits.isHptwReq := false.B
2106967f5d5Speixiaokun  mq_arb.io.in(1).bits.hptwId := DontCare
2117f6221c5Speixiaokun  mq_arb.io.in(1).bits.isLLptw := false.B
2126967f5d5Speixiaokun  mq_arb.io.in(1).valid := llptw.io.cache.valid
2136967f5d5Speixiaokun  llptw.io.cache.ready := mq_arb.io.in(1).ready
2147797f035SbugGenerator  missQueue.io.in <> mq_arb.io.out
2157797f035SbugGenerator  missQueue.io.sfence  := sfence_dup(6)
2167797f035SbugGenerator  missQueue.io.csr := csr_dup(5)
2177797f035SbugGenerator
2187797f035SbugGenerator  blockmq.io.start := missQueue.io.out.fire
219935edac4STang Haojin  blockmq.io.enable := ptw.io.req.fire
2207797f035SbugGenerator
221d0de7e4aSpeixiaokun  llptw.io.in.valid := cache.io.resp.valid &&
222d0de7e4aSpeixiaokun    !cache.io.resp.bits.hit &&
223d0de7e4aSpeixiaokun    cache.io.resp.bits.toFsm.l2Hit &&
224d0de7e4aSpeixiaokun    !cache.io.resp.bits.bypassed &&
225*325f0a4eSpeixiaokun    !cache.io.resp.bits.isHptwReq
2269c503409SLemover  llptw.io.in.bits.req_info := cache.io.resp.bits.req_info
2279c503409SLemover  llptw.io.in.bits.ppn := cache.io.resp.bits.toFsm.ppn
2287797f035SbugGenerator  llptw.io.sfence := sfence_dup(1)
2297797f035SbugGenerator  llptw.io.csr := csr_dup(1)
23092e3bfefSLemover
23192e3bfefSLemover  cache.io.req.valid := arb2.io.out.valid
2326967f5d5Speixiaokun  cache.io.req.bits.req_info := arb2.io.out.bits.req_info
233*325f0a4eSpeixiaokun  cache.io.req.bits.isFirst := (arb2.io.chosen =/= InArbMissQueuePort.U && !arb2.io.out.bits.isHptwReq)
234*325f0a4eSpeixiaokun  cache.io.req.bits.isHptwReq := arb2.io.out.bits.isHptwReq
2356967f5d5Speixiaokun  cache.io.req.bits.hptwId := arb2.io.out.bits.hptwId
2361f4a7c0cSLemover  cache.io.req.bits.bypassed.map(_ := false.B)
2377797f035SbugGenerator  cache.io.sfence := sfence_dup(2)
2387797f035SbugGenerator  cache.io.csr := csr_dup(2)
2397797f035SbugGenerator  cache.io.sfence_dup.zip(sfence_dup.drop(2).take(4)).map(s => s._1 := s._2)
2407797f035SbugGenerator  cache.io.csr_dup.zip(csr_dup.drop(2).take(3)).map(c => c._1 := c._2)
2414c4af37cSpeixiaokun  cache.io.resp.ready := MuxCase(mq_arb.io.in(0).ready || ptw.io.req.ready, Seq(
2424c4af37cSpeixiaokun    cache.io.resp.bits.hit -> outReady(cache.io.resp.bits.req_info.source, outArbCachePort),
2437f6221c5Speixiaokun    (cache.io.resp.bits.toFsm.l2Hit && !cache.io.resp.bits.bypassed && llptw.io.in.ready) -> llptw.io.in.ready,
2446967f5d5Speixiaokun    (cache.io.resp.bits.bypassed || cache.io.resp.bits.isFirst) -> mq_arb.io.in(0).ready,
245*325f0a4eSpeixiaokun    (!cache.io.resp.bits.hit && !cache.io.resp.bits.bypassed && cache.io.resp.bits.isHptwReq) -> hptw.io.req.ready,
246*325f0a4eSpeixiaokun    (cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq) -> hptw_resp_arb.io.in(HptwRespArbCachePort).ready
2474c4af37cSpeixiaokun  ))
24892e3bfefSLemover
24992e3bfefSLemover  // NOTE: missQueue req has higher priority
2507797f035SbugGenerator  ptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && !cache.io.resp.bits.toFsm.l2Hit &&
2517797f035SbugGenerator    !cache.io.resp.bits.bypassed &&
252d0de7e4aSpeixiaokun    !cache.io.resp.bits.isFirst &&
253*325f0a4eSpeixiaokun    !cache.io.resp.bits.isHptwReq
25492e3bfefSLemover  ptw.io.req.bits.req_info := cache.io.resp.bits.req_info
25592e3bfefSLemover  ptw.io.req.bits.l1Hit := cache.io.resp.bits.toFsm.l1Hit
25692e3bfefSLemover  ptw.io.req.bits.ppn := cache.io.resp.bits.toFsm.ppn
25730104977Speixiaokun  ptw.io.req.bits.stage1Hit := cache.io.resp.bits.toFsm.stage1Hit
25830104977Speixiaokun  ptw.io.req.bits.stage1 := cache.io.resp.bits.toTlb
2597797f035SbugGenerator  ptw.io.sfence := sfence_dup(7)
2607797f035SbugGenerator  ptw.io.csr := csr_dup(6)
26192e3bfefSLemover  ptw.io.resp.ready := outReady(ptw.io.resp.bits.source, outArbFsmPort)
26292e3bfefSLemover
263*325f0a4eSpeixiaokun  hptw.io.req.valid := cache.io.resp.valid && !cache.io.resp.bits.hit && !cache.io.resp.bits.bypassed & cache.io.resp.bits.isHptwReq
26482978df9Speixiaokun  hptw.io.req.bits.gvpn := cache.io.resp.bits.req_info.vpn
265d0de7e4aSpeixiaokun  hptw.io.req.bits.id := cache.io.resp.bits.toHptw.id
266c3d5cfb3Speixiaokun  hptw.io.req.bits.source := cache.io.resp.bits.req_info.source
267d0de7e4aSpeixiaokun  hptw.io.req.bits.l1Hit := cache.io.resp.bits.toHptw.l1Hit
268d0de7e4aSpeixiaokun  hptw.io.req.bits.l2Hit := cache.io.resp.bits.toHptw.l2Hit
269979f601eSpeixiaokun  hptw.io.req.bits.ppn := cache.io.resp.bits.toHptw.ppn
270d0de7e4aSpeixiaokun  hptw.io.sfence := sfence_dup(8)
271d0de7e4aSpeixiaokun  hptw.io.csr := csr_dup(7)
27292e3bfefSLemover  // mem req
27392e3bfefSLemover  def blockBytes_align(addr: UInt) = {
27492e3bfefSLemover    Cat(addr(PAddrBits - 1, log2Up(l2tlbParams.blockBytes)), 0.U(log2Up(l2tlbParams.blockBytes).W))
27592e3bfefSLemover  }
27692e3bfefSLemover  def addr_low_from_vpn(vpn: UInt) = {
27792e3bfefSLemover    vpn(log2Ceil(l2tlbParams.blockBytes)-log2Ceil(XLEN/8)-1, 0)
27892e3bfefSLemover  }
27992e3bfefSLemover  def addr_low_from_paddr(paddr: UInt) = {
28092e3bfefSLemover    paddr(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8))
28192e3bfefSLemover  }
282d0de7e4aSpeixiaokun  def from_llptw(id: UInt) = {
283d0de7e4aSpeixiaokun    id < l2tlbParams.llptwsize.U
284d0de7e4aSpeixiaokun  }
285d0de7e4aSpeixiaokun  def from_ptw(id: UInt) = {
286d0de7e4aSpeixiaokun    id === l2tlbParams.llptwsize.U
287d0de7e4aSpeixiaokun  }
288d0de7e4aSpeixiaokun  def from_hptw(id: UInt) = {
289d0de7e4aSpeixiaokun    id === l2tlbParams.llptwsize.U + 1.U
29092e3bfefSLemover  }
29192e3bfefSLemover  val waiting_resp = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B)))
29292e3bfefSLemover  val flush_latch = RegInit(VecInit(Seq.fill(MemReqWidth)(false.B)))
29392e3bfefSLemover  for (i <- waiting_resp.indices) {
29492e3bfefSLemover    assert(!flush_latch(i) || waiting_resp(i)) // when sfence_latch wait for mem resp, waiting_resp should be true
29592e3bfefSLemover  }
29692e3bfefSLemover
29792e3bfefSLemover  val llptw_out = llptw.io.out
29892e3bfefSLemover  val llptw_mem = llptw.io.mem
29992e3bfefSLemover  llptw_mem.req_mask := waiting_resp.take(l2tlbParams.llptwsize)
300d61cd5eeSpeixiaokun  ptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize)
301d61cd5eeSpeixiaokun  hptw.io.mem.mask := waiting_resp.apply(l2tlbParams.llptwsize + 1)
30292e3bfefSLemover
303d0de7e4aSpeixiaokun  val mem_arb = Module(new Arbiter(new L2TlbMemReqBundle(), 3))
30492e3bfefSLemover  mem_arb.io.in(0) <> ptw.io.mem.req
30592e3bfefSLemover  mem_arb.io.in(1) <> llptw_mem.req
306d0de7e4aSpeixiaokun  mem_arb.io.in(2) <> hptw.io.mem.req
30792e3bfefSLemover  mem_arb.io.out.ready := mem.a.ready && !flush
30892e3bfefSLemover
3091f4a7c0cSLemover  // assert, should not send mem access at same addr for twice.
3107797f035SbugGenerator  val last_resp_vpn = RegEnable(cache.io.refill.bits.req_info_dup(0).vpn, cache.io.refill.valid)
311980ddf4cSpeixiaokun  val last_resp_s2xlate = RegEnable(cache.io.refill.bits.req_info_dup(0).s2xlate, cache.io.refill.valid)
3127797f035SbugGenerator  val last_resp_level = RegEnable(cache.io.refill.bits.level_dup(0), cache.io.refill.valid)
3131f4a7c0cSLemover  val last_resp_v = RegInit(false.B)
314dd7fe201SHaoyuan Feng  val last_has_invalid = !Cat(cache.io.refill.bits.ptes.asTypeOf(Vec(blockBits/XLEN, UInt(XLEN.W))).map(a => a(0))).andR || cache.io.refill.bits.sel_pte_dup(0).asTypeOf(new PteBundle).isAf()
3151f4a7c0cSLemover  when (cache.io.refill.valid) { last_resp_v := !last_has_invalid}
3161f4a7c0cSLemover  when (flush) { last_resp_v := false.B }
3171f4a7c0cSLemover  XSError(last_resp_v && cache.io.refill.valid &&
3187797f035SbugGenerator    (cache.io.refill.bits.req_info_dup(0).vpn === last_resp_vpn) &&
319980ddf4cSpeixiaokun    (cache.io.refill.bits.level_dup(0) === last_resp_level) &&
320980ddf4cSpeixiaokun    (cache.io.refill.bits.req_info_dup(0).s2xlate === last_resp_s2xlate),
3211f4a7c0cSLemover    "l2tlb should not access mem at same addr for twice")
322d0de7e4aSpeixiaokun  // ATTENTION: this may wrongly assert when: a ptes is l2, last part is valid,
3231f4a7c0cSLemover  // but the current part is invalid, so one more mem access happened
3241f4a7c0cSLemover  // If this happened, remove the assert.
3251f4a7c0cSLemover
32692e3bfefSLemover  val req_addr_low = Reg(Vec(MemReqWidth, UInt((log2Up(l2tlbParams.blockBytes)-log2Up(XLEN/8)).W)))
32792e3bfefSLemover
328935edac4STang Haojin  when (llptw.io.in.fire) {
32992e3bfefSLemover    // when enq miss queue, set the req_addr_low to receive the mem resp data part
33092e3bfefSLemover    req_addr_low(llptw_mem.enq_ptr) := addr_low_from_vpn(llptw.io.in.bits.req_info.vpn)
33192e3bfefSLemover  }
332935edac4STang Haojin  when (mem_arb.io.out.fire) {
33392e3bfefSLemover    req_addr_low(mem_arb.io.out.bits.id) := addr_low_from_paddr(mem_arb.io.out.bits.addr)
33492e3bfefSLemover    waiting_resp(mem_arb.io.out.bits.id) := true.B
33592e3bfefSLemover  }
33692e3bfefSLemover  // mem read
33792e3bfefSLemover  val memRead =  edge.Get(
33892e3bfefSLemover    fromSource = mem_arb.io.out.bits.id,
33992e3bfefSLemover    // toAddress  = memAddr(log2Up(CacheLineSize / 2 / 8) - 1, 0),
34092e3bfefSLemover    toAddress  = blockBytes_align(mem_arb.io.out.bits.addr),
34192e3bfefSLemover    lgSize     = log2Up(l2tlbParams.blockBytes).U
34292e3bfefSLemover  )._2
34392e3bfefSLemover  mem.a.bits := memRead
34492e3bfefSLemover  mem.a.valid := mem_arb.io.out.valid && !flush
345d2b20d1aSTang Haojin  mem.a.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.PTW.id.U)
34692e3bfefSLemover  mem.d.ready := true.B
34792e3bfefSLemover  // mem -> data buffer
34892e3bfefSLemover  val refill_data = Reg(Vec(blockBits / l1BusDataWidth, UInt(l1BusDataWidth.W)))
349935edac4STang Haojin  val refill_helper = edge.firstlastHelper(mem.d.bits, mem.d.fire)
35092e3bfefSLemover  val mem_resp_done = refill_helper._3
351d0de7e4aSpeixiaokun  val mem_resp_from_llptw = from_llptw(mem.d.bits.source)
352d0de7e4aSpeixiaokun  val mem_resp_from_ptw = from_ptw(mem.d.bits.source)
353d0de7e4aSpeixiaokun  val mem_resp_from_hptw = from_hptw(mem.d.bits.source)
35492e3bfefSLemover  when (mem.d.valid) {
355d0de7e4aSpeixiaokun    assert(mem.d.bits.source < MemReqWidth.U)
35692e3bfefSLemover    refill_data(refill_helper._4) := mem.d.bits.data
35792e3bfefSLemover  }
3587797f035SbugGenerator  // refill_data_tmp is the wire fork of refill_data, but one cycle earlier
3597797f035SbugGenerator  val refill_data_tmp = WireInit(refill_data)
3607797f035SbugGenerator  refill_data_tmp(refill_helper._4) := mem.d.bits.data
3617797f035SbugGenerator
36292e3bfefSLemover  // save only one pte for each id
36392e3bfefSLemover  // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache)
36492e3bfefSLemover  val resp_pte = VecInit((0 until MemReqWidth).map(i =>
365d0de7e4aSpeixiaokun    if (i == l2tlbParams.llptwsize + 1) {RegEnable(get_part(refill_data_tmp, req_addr_low(i)), mem_resp_done && mem_resp_from_hptw) }
366d0de7e4aSpeixiaokun    else if (i == l2tlbParams.llptwsize) {RegEnable(get_part(refill_data_tmp, req_addr_low(i)), mem_resp_done && mem_resp_from_ptw) }
36792e3bfefSLemover    else { DataHoldBypass(get_part(refill_data, req_addr_low(i)), llptw_mem.buffer_it(i)) }
3687797f035SbugGenerator    // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle
36992e3bfefSLemover  ))
37092e3bfefSLemover
37163632028SHaoyuan Feng  // save eight ptes for each id when sector tlb
37263632028SHaoyuan Feng  // (miss queue may can't resp to tlb with low latency, it should have highest priority, but diffcult to design cache)
37363632028SHaoyuan Feng  val resp_pte_sector = VecInit((0 until MemReqWidth).map(i =>
374d0de7e4aSpeixiaokun    if (i == l2tlbParams.llptwsize + 1) {RegEnable(refill_data_tmp, mem_resp_done && mem_resp_from_hptw) }
375d0de7e4aSpeixiaokun    else if (i == l2tlbParams.llptwsize) {RegEnable(refill_data_tmp, mem_resp_done && mem_resp_from_ptw) }
37663632028SHaoyuan Feng    else { DataHoldBypass(refill_data, llptw_mem.buffer_it(i)) }
37763632028SHaoyuan Feng    // llptw could not use refill_data_tmp, because enq bypass's result works at next cycle
37863632028SHaoyuan Feng  ))
37963632028SHaoyuan Feng
380d0de7e4aSpeixiaokun  // mem -> llptw
381d0de7e4aSpeixiaokun  llptw_mem.resp.valid := mem_resp_done && mem_resp_from_llptw
3827797f035SbugGenerator  llptw_mem.resp.bits.id := DataHoldBypass(mem.d.bits.source, mem.d.valid)
383ce5f4200SGuanghui Hu  llptw_mem.resp.bits.value := DataHoldBypass(refill_data_tmp.asUInt, mem.d.valid)
38492e3bfefSLemover  // mem -> ptw
3854c4af37cSpeixiaokun  // ptw.io.mem.req.ready := mem.a.ready
386d0de7e4aSpeixiaokun  ptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_ptw
387d61cd5eeSpeixiaokun  ptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize)
388d0de7e4aSpeixiaokun  // mem -> hptw
3894c4af37cSpeixiaokun  // hptw.io.mem.req.ready := mem.a.ready
390d0de7e4aSpeixiaokun  hptw.io.mem.resp.valid := mem_resp_done && mem_resp_from_hptw
391d61cd5eeSpeixiaokun  hptw.io.mem.resp.bits := resp_pte.apply(l2tlbParams.llptwsize + 1)
39292e3bfefSLemover  // mem -> cache
393d0de7e4aSpeixiaokun  val refill_from_llptw = mem_resp_from_llptw
394d0de7e4aSpeixiaokun  val refill_from_ptw = mem_resp_from_ptw
395d0de7e4aSpeixiaokun  val refill_from_hptw = mem_resp_from_hptw
3960214776eSpeixiaokun  val refill_level = Mux(refill_from_llptw, 2.U, Mux(refill_from_ptw, RegEnable(ptw.io.refill.level, 0.U, ptw.io.mem.req.fire), RegEnable(hptw.io.refill.level, 0.U, hptw.io.mem.req.fire)))
3977797f035SbugGenerator  val refill_valid = mem_resp_done && !flush && !flush_latch(mem.d.bits.source)
3987797f035SbugGenerator
3997797f035SbugGenerator  cache.io.refill.valid := RegNext(refill_valid, false.B)
40092e3bfefSLemover  cache.io.refill.bits.ptes := refill_data.asUInt
401d0de7e4aSpeixiaokun  cache.io.refill.bits.req_info_dup.map(_ := RegEnable(Mux(refill_from_llptw, llptw_mem.refill, Mux(refill_from_ptw, ptw.io.refill.req_info, hptw.io.refill.req_info)), refill_valid))
4027797f035SbugGenerator  cache.io.refill.bits.level_dup.map(_ := RegEnable(refill_level, refill_valid))
4037797f035SbugGenerator  cache.io.refill.bits.levelOH(refill_level, refill_valid)
4047797f035SbugGenerator  cache.io.refill.bits.sel_pte_dup.map(_ := RegNext(sel_data(refill_data_tmp.asUInt, req_addr_low(mem.d.bits.source))))
40592e3bfefSLemover
4069c26bab7SHaoyuan Feng  if (env.EnableDifftest) {
4079c26bab7SHaoyuan Feng    val difftest_ptw_addr = RegInit(VecInit(Seq.fill(MemReqWidth)(0.U(PAddrBits.W))))
4089c26bab7SHaoyuan Feng    when (mem.a.valid) {
4099c26bab7SHaoyuan Feng      difftest_ptw_addr(mem.a.bits.source) := mem.a.bits.address
4109c26bab7SHaoyuan Feng    }
4119c26bab7SHaoyuan Feng
412a0c65233SYinan Xu    val difftest = DifftestModule(new DiffRefillEvent, dontCare = true)
413254e4960SHaoyuan Feng    difftest.coreid := io.hartId
4147d45a146SYinan Xu    difftest.index := 2.U
4157d45a146SYinan Xu    difftest.valid := cache.io.refill.valid
4167d45a146SYinan Xu    difftest.addr := difftest_ptw_addr(RegNext(mem.d.bits.source))
4177d45a146SYinan Xu    difftest.data := refill_data.asTypeOf(difftest.data)
418935edac4STang Haojin    difftest.idtfr := DontCare
4199c26bab7SHaoyuan Feng  }
4209c26bab7SHaoyuan Feng
4215ab1b84dSHaoyuan Feng  if (env.EnableDifftest) {
4225ab1b84dSHaoyuan Feng    for (i <- 0 until PtwWidth) {
4237d45a146SYinan Xu      val difftest = DifftestModule(new DiffL2TLBEvent)
42487d0ba30Speixiaokun      difftest.clock := clock
42587d0ba30Speixiaokun      difftest.coreid := p(XSCoreParamsKey).HartId.asUInt
426d61cd5eeSpeixiaokun      difftest.valid := io.tlb(i).resp.fire && !io.tlb(i).resp.bits.s1.af && !io.tlb(i).resp.bits.s2.gaf
4277d45a146SYinan Xu      difftest.index := i.U
42887d0ba30Speixiaokun      difftest.vpn := Cat(io.tlb(i).resp.bits.s1.entry.tag, 0.U(sectortlbwidth.W))
42963632028SHaoyuan Feng      for (j <- 0 until tlbcontiguous) {
4307d45a146SYinan Xu        difftest.ppn(j) := Cat(io.tlb(i).resp.bits.entry.ppn, io.tlb(i).resp.bits.ppn_low(j))
4317d45a146SYinan Xu        difftest.valididx(j) := io.tlb(i).resp.bits.valididx(j)
43287d0ba30Speixiaokun        difftest.pteidx(j) := io.tlb(i).resp.bits.s1.pteidx(j)
43363632028SHaoyuan Feng      }
43487d0ba30Speixiaokun      difftest.perm := io.tlb(i).resp.bits.s1.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt
43587d0ba30Speixiaokun      difftest.level := io.tlb(i).resp.bits.s1.entry.level.getOrElse(0.U.asUInt)
43687d0ba30Speixiaokun      difftest.pf := io.tlb(i).resp.bits.s1.pf
43787d0ba30Speixiaokun      difftest.satp := Cat(io.csr.tlb.satp.mode, io.csr.tlb.satp.asid, io.csr.tlb.satp.ppn)
43887d0ba30Speixiaokun      difftest.vsatp := Cat(io.csr.tlb.vsatp.mode, io.csr.tlb.vsatp.asid, io.csr.tlb.vsatp.ppn)
43987d0ba30Speixiaokun      difftest.hgatp := Cat(io.csr.tlb.hgatp.mode, io.csr.tlb.hgatp.asid, io.csr.tlb.hgatp.ppn)
44087d0ba30Speixiaokun      difftest.gvpn := io.tlb(i).resp.bits.s2.entry.tag
44187d0ba30Speixiaokun      difftest.g_perm := io.tlb(i).resp.bits.s2.entry.perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt
44287d0ba30Speixiaokun      difftest.g_level := io.tlb(i).resp.bits.s2.entry.level.getOrElse(0.U.asUInt)
44387d0ba30Speixiaokun      difftest.s2ppn := io.tlb(i).resp.bits.s2.entry.ppn
44487d0ba30Speixiaokun      difftest.gpf := io.tlb(i).resp.bits.s2.gpf
44587d0ba30Speixiaokun      difftest.s2xlate := io.tlb(i).resp.bits.s2xlate
4465ab1b84dSHaoyuan Feng    }
4475ab1b84dSHaoyuan Feng  }
4485ab1b84dSHaoyuan Feng
44992e3bfefSLemover  // pmp
45092e3bfefSLemover  pmp_check(0).req <> ptw.io.pmp.req
45192e3bfefSLemover  ptw.io.pmp.resp <> pmp_check(0).resp
45292e3bfefSLemover  pmp_check(1).req <> llptw.io.pmp.req
45392e3bfefSLemover  llptw.io.pmp.resp <> pmp_check(1).resp
454c3d5cfb3Speixiaokun  pmp_check(2).req <> hptw.io.pmp.req
455c3d5cfb3Speixiaokun  hptw.io.pmp.resp <> pmp_check(2).resp
45692e3bfefSLemover
45792e3bfefSLemover  llptw_out.ready := outReady(llptw_out.bits.req_info.source, outArbMqPort)
45863632028SHaoyuan Feng
459d0de7e4aSpeixiaokun  // hptw and page cache -> ptw and llptw
460d0de7e4aSpeixiaokun  val HptwRespArbCachePort = 0
461eb4bf3f2Speixiaokun  val HptwRespArbHptw = 1
462*325f0a4eSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbCachePort).valid := cache.io.resp.valid && cache.io.resp.bits.hit && cache.io.resp.bits.isHptwReq
463d0de7e4aSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbCachePort).bits.id := cache.io.resp.bits.toHptw.id
464d0de7e4aSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbCachePort).bits.resp := cache.io.resp.bits.toHptw.resp
465d0de7e4aSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbHptw).valid := hptw.io.resp.valid
466d0de7e4aSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbHptw).bits.id := hptw.io.resp.bits.id
467d0de7e4aSpeixiaokun  hptw_resp_arb.io.in(HptwRespArbHptw).bits.resp := hptw.io.resp.bits.resp
468c2b430edSpeixiaokun  hptw.io.resp.ready := hptw_resp_arb.io.in(HptwRespArbHptw).ready
469d0de7e4aSpeixiaokun
470d0de7e4aSpeixiaokun  ptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id === FsmReqID.U
471d0de7e4aSpeixiaokun  ptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp
472d0de7e4aSpeixiaokun  llptw.io.hptw.resp.valid := hptw_resp_arb.io.out.valid && hptw_resp_arb.io.out.bits.id =/= FsmReqID.U
473c3d5cfb3Speixiaokun  llptw.io.hptw.resp.bits.id := hptw_resp_arb.io.out.bits.id
474d0de7e4aSpeixiaokun  llptw.io.hptw.resp.bits.h_resp := hptw_resp_arb.io.out.bits.resp
475c3d5cfb3Speixiaokun  hptw_resp_arb.io.out.ready := true.B
476d0de7e4aSpeixiaokun
47763632028SHaoyuan Feng  // Timing: Maybe need to do some optimization or even add one more cycle
47892e3bfefSLemover  for (i <- 0 until PtwWidth) {
479*325f0a4eSpeixiaokun    mergeArb(i).in(outArbCachePort).valid := cache.io.resp.valid && cache.io.resp.bits.hit && cache.io.resp.bits.req_info.source===i.U && !cache.io.resp.bits.isHptwReq
480d0de7e4aSpeixiaokun    mergeArb(i).in(outArbCachePort).bits.s2xlate := cache.io.resp.bits.req_info.s2xlate
481eb4bf3f2Speixiaokun    mergeArb(i).in(outArbCachePort).bits.s1 := cache.io.resp.bits.toTlb
482eb4bf3f2Speixiaokun    mergeArb(i).in(outArbCachePort).bits.s2 := cache.io.resp.bits.toHptw.resp
48363632028SHaoyuan Feng    mergeArb(i).in(outArbFsmPort).valid := ptw.io.resp.valid && ptw.io.resp.bits.source===i.U
484d0de7e4aSpeixiaokun    mergeArb(i).in(outArbFsmPort).bits.s2xlate := ptw.io.resp.bits.s2xlate
485eb4bf3f2Speixiaokun    mergeArb(i).in(outArbFsmPort).bits.s1 := ptw.io.resp.bits.resp
486eb4bf3f2Speixiaokun    mergeArb(i).in(outArbFsmPort).bits.s2 := ptw.io.resp.bits.h_resp
48763632028SHaoyuan Feng    mergeArb(i).in(outArbMqPort).valid := llptw_out.valid && llptw_out.bits.req_info.source===i.U
488d0de7e4aSpeixiaokun    mergeArb(i).in(outArbMqPort).bits.s2xlate := llptw_out.bits.req_info.s2xlate
489eb4bf3f2Speixiaokun    mergeArb(i).in(outArbMqPort).bits.s1 := contiguous_pte_to_merge_ptwResp(resp_pte_sector(llptw_out.bits.id).asUInt, llptw_out.bits.req_info.vpn, llptw_out.bits.af, true, s2xlate = llptw_out.bits.req_info.s2xlate)
490eb4bf3f2Speixiaokun    mergeArb(i).in(outArbMqPort).bits.s2 := llptw_out.bits.h_resp
49163632028SHaoyuan Feng    mergeArb(i).out.ready := outArb(i).in(0).ready
49263632028SHaoyuan Feng  }
49363632028SHaoyuan Feng
49463632028SHaoyuan Feng  for (i <- 0 until PtwWidth) {
49563632028SHaoyuan Feng    outArb(i).in(0).valid := mergeArb(i).out.valid
496eb4bf3f2Speixiaokun    outArb(i).in(0).bits.s2xlate := mergeArb(i).out.bits.s2xlate
497eb4bf3f2Speixiaokun    outArb(i).in(0).bits.s1 := merge_ptwResp_to_sector_ptwResp(mergeArb(i).out.bits.s1)
498eb4bf3f2Speixiaokun    outArb(i).in(0).bits.s2 := mergeArb(i).out.bits.s2
49992e3bfefSLemover  }
50092e3bfefSLemover
50192e3bfefSLemover  // io.tlb.map(_.resp) <> outArb.map(_.out)
50292e3bfefSLemover  io.tlb.map(_.resp).zip(outArb.map(_.out)).map{
50392e3bfefSLemover    case (resp, out) => resp <> out
50492e3bfefSLemover  }
50592e3bfefSLemover
50692e3bfefSLemover  // sfence
50792e3bfefSLemover  when (flush) {
50892e3bfefSLemover    for (i <- 0 until MemReqWidth) {
50992e3bfefSLemover      when (waiting_resp(i)) {
51092e3bfefSLemover        flush_latch(i) := true.B
51192e3bfefSLemover      }
51292e3bfefSLemover    }
51392e3bfefSLemover  }
51492e3bfefSLemover  // mem -> control signal
51592e3bfefSLemover  // waiting_resp and sfence_latch will be reset when mem_resp_done
51692e3bfefSLemover  when (mem_resp_done) {
51792e3bfefSLemover    waiting_resp(mem.d.bits.source) := false.B
51892e3bfefSLemover    flush_latch(mem.d.bits.source) := false.B
51992e3bfefSLemover  }
52092e3bfefSLemover
52192e3bfefSLemover  def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = {
52292e3bfefSLemover    sink.valid   := source.valid && !block_signal
52392e3bfefSLemover    source.ready := sink.ready   && !block_signal
52492e3bfefSLemover    sink.bits    := source.bits
52592e3bfefSLemover  }
52692e3bfefSLemover
52792e3bfefSLemover  def get_part(data: Vec[UInt], index: UInt): UInt = {
52892e3bfefSLemover    val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W)))
52992e3bfefSLemover    inner_data(index)
53092e3bfefSLemover  }
53192e3bfefSLemover
53263632028SHaoyuan Feng  // not_super means that this is a normal page
53363632028SHaoyuan Feng  // valididx(i) will be all true when super page to be convenient for l1 tlb matching
534eb4bf3f2Speixiaokun  def contiguous_pte_to_merge_ptwResp(pte: UInt, vpn: UInt, af: Bool, af_first: Boolean, not_super: Boolean = true, s2xlate: UInt) : PtwMergeResp = {
53563632028SHaoyuan Feng    assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
53663632028SHaoyuan Feng    val ptw_merge_resp = Wire(new PtwMergeResp())
537eb4bf3f2Speixiaokun    val hasS2xlate = s2xlate =/= noS2xlate
53863632028SHaoyuan Feng    for (i <- 0 until tlbcontiguous) {
53963632028SHaoyuan Feng      val pte_in = pte(64 * i + 63, 64 * i).asTypeOf(new PteBundle())
54063632028SHaoyuan Feng      val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true))
54163632028SHaoyuan Feng      ptw_resp.ppn := pte_in.ppn(ppnLen - 1, sectortlbwidth)
54263632028SHaoyuan Feng      ptw_resp.ppn_low := pte_in.ppn(sectortlbwidth - 1, 0)
54363632028SHaoyuan Feng      ptw_resp.level.map(_ := 2.U)
54463632028SHaoyuan Feng      ptw_resp.perm.map(_ := pte_in.getPerm())
54563632028SHaoyuan Feng      ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth)
54663632028SHaoyuan Feng      ptw_resp.pf := (if (af_first) !af else true.B) && pte_in.isPf(2.U)
54763632028SHaoyuan Feng      ptw_resp.af := (if (!af_first) pte_in.isPf(2.U) else true.B) && (af || pte_in.isAf())
54863632028SHaoyuan Feng      ptw_resp.v := !ptw_resp.pf
54963632028SHaoyuan Feng      ptw_resp.prefetch := DontCare
550eb4bf3f2Speixiaokun      ptw_resp.asid := Mux(hasS2xlate, vsatp.asid, satp.asid)
551eb4bf3f2Speixiaokun      ptw_resp.vmid.map(_ := hgatp.asid)
55263632028SHaoyuan Feng      ptw_merge_resp.entry(i) := ptw_resp
55363632028SHaoyuan Feng    }
55463632028SHaoyuan Feng    ptw_merge_resp.pteidx := UIntToOH(vpn(sectortlbwidth - 1, 0)).asBools
55563632028SHaoyuan Feng    ptw_merge_resp.not_super := not_super.B
55663632028SHaoyuan Feng    ptw_merge_resp
55763632028SHaoyuan Feng  }
55863632028SHaoyuan Feng
55963632028SHaoyuan Feng  def merge_ptwResp_to_sector_ptwResp(pte: PtwMergeResp) : PtwSectorResp = {
56063632028SHaoyuan Feng    assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
56163632028SHaoyuan Feng    val ptw_sector_resp = Wire(new PtwSectorResp)
56263632028SHaoyuan Feng    ptw_sector_resp.entry.tag := pte.entry(OHToUInt(pte.pteidx)).tag
56363632028SHaoyuan Feng    ptw_sector_resp.entry.asid := pte.entry(OHToUInt(pte.pteidx)).asid
564c3d5cfb3Speixiaokun    ptw_sector_resp.entry.vmid.map(_ := pte.entry(OHToUInt(pte.pteidx)).vmid.getOrElse(0.U))
56563632028SHaoyuan Feng    ptw_sector_resp.entry.ppn := pte.entry(OHToUInt(pte.pteidx)).ppn
56663632028SHaoyuan Feng    ptw_sector_resp.entry.perm.map(_ := pte.entry(OHToUInt(pte.pteidx)).perm.getOrElse(0.U.asTypeOf(new PtePermBundle)))
56763632028SHaoyuan Feng    ptw_sector_resp.entry.level.map(_ := pte.entry(OHToUInt(pte.pteidx)).level.getOrElse(0.U(2.W)))
56863632028SHaoyuan Feng    ptw_sector_resp.entry.prefetch := pte.entry(OHToUInt(pte.pteidx)).prefetch
56963632028SHaoyuan Feng    ptw_sector_resp.entry.v := pte.entry(OHToUInt(pte.pteidx)).v
57063632028SHaoyuan Feng    ptw_sector_resp.af := pte.entry(OHToUInt(pte.pteidx)).af
57163632028SHaoyuan Feng    ptw_sector_resp.pf := pte.entry(OHToUInt(pte.pteidx)).pf
57263632028SHaoyuan Feng    ptw_sector_resp.addr_low := OHToUInt(pte.pteidx)
573b0fa7106SHaoyuan Feng    ptw_sector_resp.pteidx := pte.pteidx
57463632028SHaoyuan Feng    for (i <- 0 until tlbcontiguous) {
57563632028SHaoyuan Feng      val ppn_equal = pte.entry(i).ppn === pte.entry(OHToUInt(pte.pteidx)).ppn
57663632028SHaoyuan Feng      val perm_equal = pte.entry(i).perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt === pte.entry(OHToUInt(pte.pteidx)).perm.getOrElse(0.U.asTypeOf(new PtePermBundle)).asUInt
57763632028SHaoyuan Feng      val v_equal = pte.entry(i).v === pte.entry(OHToUInt(pte.pteidx)).v
57863632028SHaoyuan Feng      val af_equal = pte.entry(i).af === pte.entry(OHToUInt(pte.pteidx)).af
57963632028SHaoyuan Feng      val pf_equal = pte.entry(i).pf === pte.entry(OHToUInt(pte.pteidx)).pf
58063632028SHaoyuan Feng      ptw_sector_resp.valididx(i) := (ppn_equal && perm_equal && v_equal && af_equal && pf_equal) || !pte.not_super
58163632028SHaoyuan Feng      ptw_sector_resp.ppn_low(i) := pte.entry(i).ppn_low
58263632028SHaoyuan Feng    }
58363632028SHaoyuan Feng    ptw_sector_resp.valididx(OHToUInt(pte.pteidx)) := true.B
58463632028SHaoyuan Feng    ptw_sector_resp
58563632028SHaoyuan Feng  }
58663632028SHaoyuan Feng
58792e3bfefSLemover  def outReady(source: UInt, port: Int): Bool = {
58845f43e6eSTang Haojin    MuxLookup(source, true.B)((0 until PtwWidth).map(i => i.U -> mergeArb(i).in(port).ready))
58992e3bfefSLemover  }
59092e3bfefSLemover
59192e3bfefSLemover  // debug info
59292e3bfefSLemover  for (i <- 0 until PtwWidth) {
59392e3bfefSLemover    XSDebug(p"[io.tlb(${i.U})] ${io.tlb(i)}\n")
59492e3bfefSLemover  }
5957797f035SbugGenerator  XSDebug(p"[sfence] ${io.sfence}\n")
59692e3bfefSLemover  XSDebug(p"[io.csr.tlb] ${io.csr.tlb}\n")
59792e3bfefSLemover
59892e3bfefSLemover  for (i <- 0 until PtwWidth) {
599935edac4STang Haojin    XSPerfAccumulate(s"req_count${i}", io.tlb(i).req(0).fire)
60092e3bfefSLemover    XSPerfAccumulate(s"req_blocked_count_${i}", io.tlb(i).req(0).valid && !io.tlb(i).req(0).ready)
60192e3bfefSLemover  }
60292e3bfefSLemover  XSPerfAccumulate(s"req_blocked_by_mq", arb1.io.out.valid && missQueue.io.out.valid)
60392e3bfefSLemover  for (i <- 0 until (MemReqWidth + 1)) {
60492e3bfefSLemover    XSPerfAccumulate(s"mem_req_util${i}", PopCount(waiting_resp) === i.U)
60592e3bfefSLemover  }
60692e3bfefSLemover  XSPerfAccumulate("mem_cycle", PopCount(waiting_resp) =/= 0.U)
607935edac4STang Haojin  XSPerfAccumulate("mem_count", mem.a.fire)
608dd7fe201SHaoyuan Feng  for (i <- 0 until PtwWidth) {
609eb4bf3f2Speixiaokun    XSPerfAccumulate(s"llptw_ppn_af${i}", mergeArb(i).in(outArbMqPort).valid && mergeArb(i).in(outArbMqPort).bits.s1.entry(OHToUInt(mergeArb(i).in(outArbMqPort).bits.s1.pteidx)).af && !llptw_out.bits.af)
610d61cd5eeSpeixiaokun    XSPerfAccumulate(s"access_fault${i}", io.tlb(i).resp.fire && io.tlb(i).resp.bits.s1.af)
611dd7fe201SHaoyuan Feng  }
61292e3bfefSLemover
61392e3bfefSLemover  // print configs
614f1fe8698SLemover  println(s"${l2tlbParams.name}: a ptw, a llptw with size ${l2tlbParams.llptwsize}, miss queue size ${MissQueueSize} l1:${l2tlbParams.l1Size} fa l2: nSets ${l2tlbParams.l2nSets} nWays ${l2tlbParams.l2nWays} l3: ${l2tlbParams.l3nSets} nWays ${l2tlbParams.l3nWays} blockBytes:${l2tlbParams.blockBytes}")
61592e3bfefSLemover
61692e3bfefSLemover  // time out assert
61792e3bfefSLemover  for (i <- 0 until MemReqWidth) {
61892e3bfefSLemover    TimeOutAssert(waiting_resp(i), timeOutThreshold, s"ptw mem resp time out wait_resp${i}")
61992e3bfefSLemover    TimeOutAssert(flush_latch(i), timeOutThreshold, s"ptw mem resp time out flush_latch${i}")
62092e3bfefSLemover  }
62192e3bfefSLemover
62292e3bfefSLemover
62392e3bfefSLemover  val perfEvents  = Seq(llptw, cache, ptw).flatMap(_.getPerfEvents)
62492e3bfefSLemover  generatePerfEvent()
6255afdf73cSHaoyuan Feng
626da3bf434SMaxpicca-Li  val isWriteL1TlbTable = WireInit(Constantin.createRecord("isWriteL1TlbTable" + p(XSCoreParamsKey).HartId.toString))
6275afdf73cSHaoyuan Feng  val L1TlbTable = ChiselDB.createTable("L1Tlb_hart" + p(XSCoreParamsKey).HartId.toString, new L1TlbDB)
6285afdf73cSHaoyuan Feng  val ITlbReqDB, DTlbReqDB, ITlbRespDB, DTlbRespDB = Wire(new L1TlbDB)
6295afdf73cSHaoyuan Feng  ITlbReqDB.vpn := io.tlb(0).req(0).bits.vpn
6305afdf73cSHaoyuan Feng  DTlbReqDB.vpn := io.tlb(1).req(0).bits.vpn
631d61cd5eeSpeixiaokun  ITlbRespDB.vpn := io.tlb(0).resp.bits.s1.entry.tag
632d61cd5eeSpeixiaokun  DTlbRespDB.vpn := io.tlb(1).resp.bits.s1.entry.tag
633da3bf434SMaxpicca-Li  L1TlbTable.log(ITlbReqDB, isWriteL1TlbTable.orR && io.tlb(0).req(0).fire, "ITlbReq", clock, reset)
634da3bf434SMaxpicca-Li  L1TlbTable.log(DTlbReqDB, isWriteL1TlbTable.orR && io.tlb(1).req(0).fire, "DTlbReq", clock, reset)
635da3bf434SMaxpicca-Li  L1TlbTable.log(ITlbRespDB, isWriteL1TlbTable.orR && io.tlb(0).resp.fire, "ITlbResp", clock, reset)
636da3bf434SMaxpicca-Li  L1TlbTable.log(DTlbRespDB, isWriteL1TlbTable.orR && io.tlb(1).resp.fire, "DTlbResp", clock, reset)
6375afdf73cSHaoyuan Feng
638da3bf434SMaxpicca-Li  val isWritePageCacheTable = WireInit(Constantin.createRecord("isWritePageCacheTable" + p(XSCoreParamsKey).HartId.toString))
6395afdf73cSHaoyuan Feng  val PageCacheTable = ChiselDB.createTable("PageCache_hart" + p(XSCoreParamsKey).HartId.toString, new PageCacheDB)
6405afdf73cSHaoyuan Feng  val PageCacheDB = Wire(new PageCacheDB)
64163632028SHaoyuan Feng  PageCacheDB.vpn := Cat(cache.io.resp.bits.toTlb.entry(0).tag, OHToUInt(cache.io.resp.bits.toTlb.pteidx))
6425afdf73cSHaoyuan Feng  PageCacheDB.source := cache.io.resp.bits.req_info.source
6435afdf73cSHaoyuan Feng  PageCacheDB.bypassed := cache.io.resp.bits.bypassed
6445afdf73cSHaoyuan Feng  PageCacheDB.is_first := cache.io.resp.bits.isFirst
64563632028SHaoyuan Feng  PageCacheDB.prefetched := cache.io.resp.bits.toTlb.entry(0).prefetch
6465afdf73cSHaoyuan Feng  PageCacheDB.prefetch := cache.io.resp.bits.prefetch
6475afdf73cSHaoyuan Feng  PageCacheDB.l2Hit := cache.io.resp.bits.toFsm.l2Hit
6485afdf73cSHaoyuan Feng  PageCacheDB.l1Hit := cache.io.resp.bits.toFsm.l1Hit
6495afdf73cSHaoyuan Feng  PageCacheDB.hit := cache.io.resp.bits.hit
650da3bf434SMaxpicca-Li  PageCacheTable.log(PageCacheDB, isWritePageCacheTable.orR && cache.io.resp.fire, "PageCache", clock, reset)
6515afdf73cSHaoyuan Feng
652da3bf434SMaxpicca-Li  val isWritePTWTable = WireInit(Constantin.createRecord("isWritePTWTable" + p(XSCoreParamsKey).HartId.toString))
6535afdf73cSHaoyuan Feng  val PTWTable = ChiselDB.createTable("PTW_hart" + p(XSCoreParamsKey).HartId.toString, new PTWDB)
6545afdf73cSHaoyuan Feng  val PTWReqDB, PTWRespDB, LLPTWReqDB, LLPTWRespDB = Wire(new PTWDB)
6555afdf73cSHaoyuan Feng  PTWReqDB.vpn := ptw.io.req.bits.req_info.vpn
6565afdf73cSHaoyuan Feng  PTWReqDB.source := ptw.io.req.bits.req_info.source
6575afdf73cSHaoyuan Feng  PTWRespDB.vpn := ptw.io.refill.req_info.vpn
6585afdf73cSHaoyuan Feng  PTWRespDB.source := ptw.io.refill.req_info.source
6595afdf73cSHaoyuan Feng  LLPTWReqDB.vpn := llptw.io.in.bits.req_info.vpn
6605afdf73cSHaoyuan Feng  LLPTWReqDB.source := llptw.io.in.bits.req_info.source
6615afdf73cSHaoyuan Feng  LLPTWRespDB.vpn := llptw.io.mem.refill.vpn
6625afdf73cSHaoyuan Feng  LLPTWRespDB.source := llptw.io.mem.refill.source
663da3bf434SMaxpicca-Li  PTWTable.log(PTWReqDB, isWritePTWTable.orR && ptw.io.req.fire, "PTWReq", clock, reset)
664da3bf434SMaxpicca-Li  PTWTable.log(PTWRespDB, isWritePTWTable.orR && ptw.io.mem.resp.fire, "PTWResp", clock, reset)
665da3bf434SMaxpicca-Li  PTWTable.log(LLPTWReqDB, isWritePTWTable.orR && llptw.io.in.fire, "LLPTWReq", clock, reset)
666da3bf434SMaxpicca-Li  PTWTable.log(LLPTWRespDB, isWritePTWTable.orR && llptw.io.mem.resp.fire, "LLPTWResp", clock, reset)
6675afdf73cSHaoyuan Feng
668da3bf434SMaxpicca-Li  val isWriteL2TlbMissQueueTable = WireInit(Constantin.createRecord("isWriteL2TlbMissQueueTable" + p(XSCoreParamsKey).HartId.toString))
6695afdf73cSHaoyuan Feng  val L2TlbMissQueueTable = ChiselDB.createTable("L2TlbMissQueue_hart" + p(XSCoreParamsKey).HartId.toString, new L2TlbMissQueueDB)
6705afdf73cSHaoyuan Feng  val L2TlbMissQueueInDB, L2TlbMissQueueOutDB = Wire(new L2TlbMissQueueDB)
6716967f5d5Speixiaokun  L2TlbMissQueueInDB.vpn := missQueue.io.in.bits.req_info.vpn
6726967f5d5Speixiaokun  L2TlbMissQueueOutDB.vpn := missQueue.io.out.bits.req_info.vpn
673da3bf434SMaxpicca-Li  L2TlbMissQueueTable.log(L2TlbMissQueueInDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.in.fire, "L2TlbMissQueueIn", clock, reset)
674da3bf434SMaxpicca-Li  L2TlbMissQueueTable.log(L2TlbMissQueueOutDB, isWriteL2TlbMissQueueTable.orR && missQueue.io.out.fire, "L2TlbMissQueueOut", clock, reset)
67592e3bfefSLemover}
67692e3bfefSLemover
6777797f035SbugGenerator/** BlockHelper, block missqueue, not to send too many req to cache
6787797f035SbugGenerator *  Parameter:
6797797f035SbugGenerator *    enable: enable BlockHelper, mq should not send too many reqs
6807797f035SbugGenerator *    start: when miss queue out fire and need, block miss queue's out
6817797f035SbugGenerator *    block: block miss queue's out
6827797f035SbugGenerator *    latency: last missqueue out's cache access latency
6837797f035SbugGenerator */
6847797f035SbugGeneratorclass BlockHelper(latency: Int)(implicit p: Parameters) extends XSModule {
6857797f035SbugGenerator  val io = IO(new Bundle {
6867797f035SbugGenerator    val enable = Input(Bool())
6877797f035SbugGenerator    val start = Input(Bool())
6887797f035SbugGenerator    val block = Output(Bool())
6897797f035SbugGenerator  })
6907797f035SbugGenerator
6917797f035SbugGenerator  val count = RegInit(0.U(log2Ceil(latency).W))
6927797f035SbugGenerator  val valid = RegInit(false.B)
6937797f035SbugGenerator  val work = RegInit(true.B)
6947797f035SbugGenerator
6957797f035SbugGenerator  io.block := valid
6967797f035SbugGenerator
6977797f035SbugGenerator  when (io.start && work) { valid := true.B }
6987797f035SbugGenerator  when (valid) { count := count + 1.U }
6997797f035SbugGenerator  when (count === (latency.U) || io.enable) {
7007797f035SbugGenerator    valid := false.B
7017797f035SbugGenerator    work := io.enable
7027797f035SbugGenerator    count := 0.U
7037797f035SbugGenerator  }
7047797f035SbugGenerator}
7057797f035SbugGenerator
70692e3bfefSLemoverclass PTEHelper() extends ExtModule {
70792e3bfefSLemover  val clock  = IO(Input(Clock()))
70892e3bfefSLemover  val enable = IO(Input(Bool()))
70992e3bfefSLemover  val satp   = IO(Input(UInt(64.W)))
71092e3bfefSLemover  val vpn    = IO(Input(UInt(64.W)))
71192e3bfefSLemover  val pte    = IO(Output(UInt(64.W)))
71292e3bfefSLemover  val level  = IO(Output(UInt(8.W)))
71392e3bfefSLemover  val pf     = IO(Output(UInt(8.W)))
71492e3bfefSLemover}
71592e3bfefSLemover
7165afdf73cSHaoyuan Fengclass PTWDelayN[T <: Data](gen: T, n: Int, flush: Bool) extends Module {
7175afdf73cSHaoyuan Feng  val io = IO(new Bundle() {
7185afdf73cSHaoyuan Feng    val in = Input(gen)
7195afdf73cSHaoyuan Feng    val out = Output(gen)
7205afdf73cSHaoyuan Feng    val ptwflush = Input(flush.cloneType)
7215afdf73cSHaoyuan Feng  })
7225afdf73cSHaoyuan Feng  val out = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen))))
7235afdf73cSHaoyuan Feng  val t = RegInit(VecInit(Seq.fill(n)(0.U.asTypeOf(gen))))
7245afdf73cSHaoyuan Feng  out(0) := io.in
7255afdf73cSHaoyuan Feng  if (n == 1) {
7265afdf73cSHaoyuan Feng    io.out := out(0)
7275afdf73cSHaoyuan Feng  } else {
7285afdf73cSHaoyuan Feng    when (io.ptwflush) {
7295afdf73cSHaoyuan Feng      for (i <- 0 until n) {
7305afdf73cSHaoyuan Feng        t(i) := 0.U.asTypeOf(gen)
7315afdf73cSHaoyuan Feng        out(i) := 0.U.asTypeOf(gen)
7325afdf73cSHaoyuan Feng      }
7335afdf73cSHaoyuan Feng      io.out := 0.U.asTypeOf(gen)
7345afdf73cSHaoyuan Feng    } .otherwise {
7355afdf73cSHaoyuan Feng      for (i <- 1 until n) {
7365afdf73cSHaoyuan Feng        t(i-1) := out(i-1)
7375afdf73cSHaoyuan Feng        out(i) := t(i-1)
7385afdf73cSHaoyuan Feng      }
7395afdf73cSHaoyuan Feng      io.out := out(n-1)
7405afdf73cSHaoyuan Feng    }
7415afdf73cSHaoyuan Feng  }
7425afdf73cSHaoyuan Feng}
7435afdf73cSHaoyuan Feng
7445afdf73cSHaoyuan Fengobject PTWDelayN {
7455afdf73cSHaoyuan Feng  def apply[T <: Data](in: T, n: Int, flush: Bool): T = {
7465afdf73cSHaoyuan Feng    val delay = Module(new PTWDelayN(in.cloneType, n, flush))
7475afdf73cSHaoyuan Feng    delay.io.in := in
7485afdf73cSHaoyuan Feng    delay.io.ptwflush := flush
7495afdf73cSHaoyuan Feng    delay.io.out
7505afdf73cSHaoyuan Feng  }
7515afdf73cSHaoyuan Feng}
7525afdf73cSHaoyuan Feng
75392e3bfefSLemoverclass FakePTW()(implicit p: Parameters) extends XSModule with HasPtwConst {
75492e3bfefSLemover  val io = IO(new L2TLBIO)
7555afdf73cSHaoyuan Feng  val flush = VecInit(Seq.fill(PtwWidth)(false.B))
7565afdf73cSHaoyuan Feng  flush(0) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, itlbParams.fenceDelay)
7575afdf73cSHaoyuan Feng  flush(1) := DelayN(io.sfence.valid || io.csr.tlb.satp.changed, ldtlbParams.fenceDelay)
75892e3bfefSLemover  for (i <- 0 until PtwWidth) {
75992e3bfefSLemover    val helper = Module(new PTEHelper())
76092e3bfefSLemover    helper.clock := clock
76192e3bfefSLemover    helper.satp := io.csr.tlb.satp.ppn
7625afdf73cSHaoyuan Feng
7635afdf73cSHaoyuan Feng    if (coreParams.softPTWDelay == 1) {
7645afdf73cSHaoyuan Feng      helper.enable := io.tlb(i).req(0).fire
76592e3bfefSLemover      helper.vpn := io.tlb(i).req(0).bits.vpn
7665afdf73cSHaoyuan Feng    } else {
7675afdf73cSHaoyuan Feng      helper.enable := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay - 1, flush(i))
7685afdf73cSHaoyuan Feng      helper.vpn := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay - 1, flush(i))
7695afdf73cSHaoyuan Feng    }
7705afdf73cSHaoyuan Feng
77192e3bfefSLemover    val pte = helper.pte.asTypeOf(new PteBundle)
77292e3bfefSLemover    val level = helper.level
77392e3bfefSLemover    val pf = helper.pf
7745afdf73cSHaoyuan Feng    val empty = RegInit(true.B)
7755afdf73cSHaoyuan Feng    when (io.tlb(i).req(0).fire) {
7765afdf73cSHaoyuan Feng      empty := false.B
7775afdf73cSHaoyuan Feng    } .elsewhen (io.tlb(i).resp.fire || flush(i)) {
7785afdf73cSHaoyuan Feng      empty := true.B
7795afdf73cSHaoyuan Feng    }
78092e3bfefSLemover
7815afdf73cSHaoyuan Feng    io.tlb(i).req(0).ready := empty || io.tlb(i).resp.fire
7825afdf73cSHaoyuan Feng    io.tlb(i).resp.valid := PTWDelayN(io.tlb(i).req(0).fire, coreParams.softPTWDelay, flush(i))
78392e3bfefSLemover    assert(!io.tlb(i).resp.valid || io.tlb(i).resp.ready)
784d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.tag := PTWDelayN(io.tlb(i).req(0).bits.vpn, coreParams.softPTWDelay, flush(i))
785d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.ppn := pte.ppn
786d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.perm.map(_ := pte.getPerm())
787d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.level.map(_ := level)
788d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.pf := pf
789d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.af := DontCare // TODO: implement it
790d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.v := !pf
791d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.prefetch := DontCare
792d61cd5eeSpeixiaokun    io.tlb(i).resp.bits.s1.entry.asid := io.csr.tlb.satp.asid
79392e3bfefSLemover  }
79492e3bfefSLemover}
79592e3bfefSLemover
79692e3bfefSLemoverclass L2TLBWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter {
79795e60e55STang Haojin  override def shouldBeInlined: Boolean = false
79892e3bfefSLemover  val useSoftPTW = coreParams.softPTW
79992e3bfefSLemover  val node = if (!useSoftPTW) TLIdentityNode() else null
80092e3bfefSLemover  val ptw = if (!useSoftPTW) LazyModule(new L2TLB()) else null
80192e3bfefSLemover  if (!useSoftPTW) {
80292e3bfefSLemover    node := ptw.node
80392e3bfefSLemover  }
80492e3bfefSLemover
805935edac4STang Haojin  class L2TLBWrapperImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) with HasPerfEvents {
80692e3bfefSLemover    val io = IO(new L2TLBIO)
80792e3bfefSLemover    val perfEvents = if (useSoftPTW) {
80892e3bfefSLemover      val fake_ptw = Module(new FakePTW())
80992e3bfefSLemover      io <> fake_ptw.io
81092e3bfefSLemover      Seq()
81192e3bfefSLemover    }
81292e3bfefSLemover    else {
81392e3bfefSLemover        io <> ptw.module.io
81492e3bfefSLemover        ptw.module.getPerfEvents
81592e3bfefSLemover    }
81692e3bfefSLemover    generatePerfEvent()
81792e3bfefSLemover  }
818935edac4STang Haojin
819935edac4STang Haojin  lazy val module = new L2TLBWrapperImp(this)
82092e3bfefSLemover}
821