xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLB.scala (revision 5ab1b84d02afebec93f68fa9ef6f37243fbdc0f9)
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
9f1fe8698SLemover
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
196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters
206d5ddbceSLemoverimport chisel3._
21a0301c0dSLemoverimport chisel3.internal.naming.chiselName
226d5ddbceSLemoverimport chisel3.util._
23*5ab1b84dSHaoyuan Fengimport difftest._
24a0301c0dSLemoverimport freechips.rocketchip.util.SRAMAnnotation
256d5ddbceSLemoverimport xiangshan._
266d5ddbceSLemoverimport utils._
273c02ee8fSwakafaimport utility._
28f1fe8698SLemoverimport xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig}
299aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr
306d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst
31f1fe8698SLemoverimport firrtl.FirrtlProtos.Firrtl.Module.ExternalModule.Parameter
32f1fe8698SLemoverimport freechips.rocketchip.rocket.PMPConfig
336d5ddbceSLemover
34f1fe8698SLemover/** TLB module
35f1fe8698SLemover  * support block request and non-block request io at the same time
36f1fe8698SLemover  * return paddr at next cycle, then go for pmp/pma check
37f1fe8698SLemover  * @param Width: The number of requestors
38f1fe8698SLemover  * @param Block: Blocked or not for each requestor ports
39f1fe8698SLemover  * @param q: TLB Parameters, like entry number, each TLB has its own parameters
40f1fe8698SLemover  * @param p: XiangShan Paramemters, like XLEN
41f1fe8698SLemover  */
42a0301c0dSLemover
43a0301c0dSLemover@chiselName
4403efd994Shappy-lxclass TLB(Width: Int, nRespDups: Int = 1, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule
45f1fe8698SLemover  with HasCSRConst
46f1fe8698SLemover  with HasPerfEvents
47f1fe8698SLemover{
4803efd994Shappy-lx  val io = IO(new TlbIO(Width, nRespDups, q))
49a0301c0dSLemover
506d5ddbceSLemover  val req = io.requestor.map(_.req)
516d5ddbceSLemover  val resp = io.requestor.map(_.resp)
526d5ddbceSLemover  val ptw = io.ptw
53b6982e83SLemover  val pmp = io.pmp
546d5ddbceSLemover
55f1fe8698SLemover  /** Sfence.vma & Svinval
56f1fe8698SLemover    * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe
57f1fe8698SLemover    * Svinval will 1. flush old entries 2. flush inflight
58f1fe8698SLemover    * So, Svinval will not flush pipe, which means
59f1fe8698SLemover    * it should not drop reqs from pipe and should return right resp
60f1fe8698SLemover    */
61f1fe8698SLemover  val sfence = DelayN(io.sfence, q.fenceDelay)
626d5ddbceSLemover  val csr = io.csr
63f1fe8698SLemover  val satp = DelayN(io.csr.satp, q.fenceDelay)
64f1fe8698SLemover  val flush_mmu = DelayN(sfence.valid || csr.satp.changed, q.fenceDelay)
65f1fe8698SLemover  val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe
66f1fe8698SLemover  val flush_pipe = io.flushPipe
67f1fe8698SLemover
68f1fe8698SLemover  // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush.
69f1fe8698SLemover  // because, csr will influence tlb behavior.
70a0301c0dSLemover  val ifecth = if (q.fetchi) true.B else false.B
71f1fe8698SLemover  val mode = if (q.useDmode) csr.priv.dmode else csr.priv.imode
726d5ddbceSLemover  // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux...
736d5ddbceSLemover  val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U)
746d5ddbceSLemover    else (satp.mode === 8.U && (mode < ModeM))
756d5ddbceSLemover
76f1fe8698SLemover  val req_in = req
77f1fe8698SLemover  val req_out = req.map(a => RegEnable(a.bits, a.fire()))
78f1fe8698SLemover  val req_out_v = (0 until Width).map(i => ValidHold(req_in(i).fire && !req_in(i).bits.kill, resp(i).fire, flush_pipe(i)))
796d5ddbceSLemover
80f1fe8698SLemover  val refill = ptw.resp.fire() && !flush_mmu && vmEnable
8103efd994Shappy-lx  val entries = Module(new TlbStorageWrapper(Width, q, nRespDups))
82f1fe8698SLemover  entries.io.base_connect(sfence, csr, satp)
83f1fe8698SLemover  if (q.outReplace) { io.replace <> entries.io.replace }
846d5ddbceSLemover  for (i <- 0 until Width) {
85f1fe8698SLemover    entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i)
86f1fe8698SLemover    entries.io.w_apply(refill, ptw.resp.bits, io.ptw_replenish)
87a0301c0dSLemover  }
886d5ddbceSLemover
89f1fe8698SLemover  // read TLB, get hit/miss, paddr, perm bits
90f1fe8698SLemover  val readResult = (0 until Width).map(TLBRead(_))
91f1fe8698SLemover  val hitVec = readResult.map(_._1)
92f1fe8698SLemover  val missVec = readResult.map(_._2)
93f1fe8698SLemover  val pmp_addr = readResult.map(_._3)
94f1fe8698SLemover  val static_pm = readResult.map(_._4)
95f1fe8698SLemover  val static_pm_v = readResult.map(_._5)
96f1fe8698SLemover  val perm = readResult.map(_._6)
97149086eaSLemover
98f1fe8698SLemover  // check pmp use paddr (for timing optization, use pmp_addr here)
99f1fe8698SLemover  // check permisson
100f1fe8698SLemover  (0 until Width).foreach{i =>
101f1fe8698SLemover    pmp_check(pmp_addr(i), req_out(i).size, req_out(i).cmd, i)
10203efd994Shappy-lx    for (d <- 0 until nRespDups) {
10303efd994Shappy-lx      perm_check(perm(i)(d), req_out(i).cmd, static_pm(i), static_pm_v(i), i, d)
10403efd994Shappy-lx    }
105f1fe8698SLemover  }
1066d5ddbceSLemover
107f1fe8698SLemover  // handle block or non-block io
108f1fe8698SLemover  // for non-block io, just return the above result, send miss to ptw
109f1fe8698SLemover  // for block io, hold the request, send miss to ptw,
110f1fe8698SLemover  //   when ptw back, return the result
111f1fe8698SLemover  (0 until Width) foreach {i =>
112f1fe8698SLemover    if (Block(i)) handle_block(i)
113f1fe8698SLemover    else handle_nonblock(i)
114f1fe8698SLemover  }
115f1fe8698SLemover  io.ptw.resp.ready := true.B
116a0301c0dSLemover
117f1fe8698SLemover  /************************  main body above | method/log/perf below ****************************/
118f1fe8698SLemover  def TLBRead(i: Int) = {
119cb8f2f2aSLemover    val (e_hit, e_ppn, e_perm, e_super_hit, e_super_ppn, static_pm) = entries.io.r_resp_apply(i)
120cb8f2f2aSLemover    val (p_hit, p_ppn, p_perm) = ptw_resp_bypass(get_pn(req_in(i).bits.vaddr))
121cb8f2f2aSLemover
122cb8f2f2aSLemover    val hit = e_hit || p_hit
123a0301c0dSLemover    val miss = !hit && vmEnable
124cb8f2f2aSLemover    val fast_miss = !(e_super_hit || p_hit) && vmEnable
125f1fe8698SLemover    hit.suggestName(s"hit_read_${i}")
126f1fe8698SLemover    miss.suggestName(s"miss_read_${i}")
1276d5ddbceSLemover
128f1fe8698SLemover    val vaddr = SignExt(req_out(i).vaddr, PAddrBits)
129f1fe8698SLemover    resp(i).bits.miss := miss
130e05a24abSLemover    resp(i).bits.fast_miss := fast_miss
131e05a24abSLemover    resp(i).bits.ptwBack := ptw.resp.fire()
1326d5ddbceSLemover
13303efd994Shappy-lx    val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W))))
13403efd994Shappy-lx    val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle))))
13503efd994Shappy-lx
13603efd994Shappy-lx    for (d <- 0 until nRespDups) {
13703efd994Shappy-lx      ppn(d) := Mux(p_hit, p_ppn, e_ppn(d))
13803efd994Shappy-lx      perm(d) := Mux(p_hit, p_perm, e_perm(d))
13903efd994Shappy-lx
14003efd994Shappy-lx      val paddr = Cat(ppn(d), get_off(req_out(i).vaddr))
14103efd994Shappy-lx      resp(i).bits.paddr(d) := Mux(vmEnable, paddr, vaddr)
14203efd994Shappy-lx    }
14303efd994Shappy-lx
14403efd994Shappy-lx    XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n")
14503efd994Shappy-lx
146cb8f2f2aSLemover    val pmp_paddr = Mux(vmEnable, Cat(Mux(p_hit, p_ppn, e_super_ppn), get_off(req_out(i).vaddr)), vaddr)
147f1fe8698SLemover    // pmp_paddr seems same to paddr functionally. It abandons normal_ppn for timing optimization.
148cb8f2f2aSLemover    // val pmp_paddr = Mux(vmEnable, paddr, vaddr)
149cb8f2f2aSLemover    val static_pm_valid = !(e_super_hit || p_hit) && vmEnable && q.partialStaticPMP.B
150f1fe8698SLemover
151f1fe8698SLemover    (hit, miss, pmp_paddr, static_pm, static_pm_valid, perm)
152f1fe8698SLemover  }
153f1fe8698SLemover
154f1fe8698SLemover  def pmp_check(addr: UInt, size: UInt, cmd: UInt, idx: Int): Unit = {
155f1fe8698SLemover    pmp(idx).valid := resp(idx).valid
156f1fe8698SLemover    pmp(idx).bits.addr := addr
157f1fe8698SLemover    pmp(idx).bits.size := size
158f1fe8698SLemover    pmp(idx).bits.cmd := cmd
159f1fe8698SLemover  }
160f1fe8698SLemover
16103efd994Shappy-lx  def perm_check(perm: TlbPermBundle, cmd: UInt, spm: TlbPMBundle, spm_v: Bool, idx: Int, nDups: Int) = {
1625b7ef044SLemover    // for timing optimization, pmp check is divided into dynamic and static
1635b7ef044SLemover    // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done
1645b7ef044SLemover    // static: 4K pages (or sram entries) -> check pmp with pre-checked results
165f1fe8698SLemover    val af = perm.af
166f1fe8698SLemover    val pf = perm.pf
167f1fe8698SLemover    val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception
168f1fe8698SLemover    val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception
169f1fe8698SLemover    val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception
170f1fe8698SLemover    val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!io.csr.priv.sum || ifecth))
171f1fe8698SLemover    val ldPermFail = !(modeCheck && (perm.r || io.csr.priv.mxr && perm.x))
172a79fef67Swakafa    val stPermFail = !(modeCheck && perm.w)
173a79fef67Swakafa    val instrPermFail = !(modeCheck && perm.x)
174f1fe8698SLemover    val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd))
175f1fe8698SLemover    val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd))
176f1fe8698SLemover    val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd)
1772c2c1588SLemover    val fault_valid = vmEnable
17803efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && fault_valid && !af
17903efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && fault_valid && !af
18003efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && fault_valid && !af
181b6982e83SLemover    // NOTE: pf need && with !af, page fault has higher priority than access fault
182b6982e83SLemover    // but ptw may also have access fault, then af happens, the translation is wrong.
183b6982e83SLemover    // In this case, pf has lower priority than af
1846d5ddbceSLemover
18503efd994Shappy-lx    resp(idx).bits.excp(nDups).af.ld    := (af || (spm_v && !spm.r)) && TlbCmd.isRead(cmd) && fault_valid
18603efd994Shappy-lx    resp(idx).bits.excp(nDups).af.st    := (af || (spm_v && !spm.w)) && TlbCmd.isWrite(cmd) && fault_valid
18703efd994Shappy-lx    resp(idx).bits.excp(nDups).af.instr := (af || (spm_v && !spm.x)) && TlbCmd.isExec(cmd) && fault_valid
188f1fe8698SLemover    resp(idx).bits.static_pm.valid := spm_v && fault_valid // ls/st unit should use this mmio, not the result from pmp
189f1fe8698SLemover    resp(idx).bits.static_pm.bits := !spm.c
1906d5ddbceSLemover  }
1916d5ddbceSLemover
192f1fe8698SLemover  def handle_nonblock(idx: Int): Unit = {
193f1fe8698SLemover    io.requestor(idx).resp.valid := req_out_v(idx)
194f1fe8698SLemover    io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true
1959930e66fSLemover    XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B")
196cb8f2f2aSLemover
197cb8f2f2aSLemover    val ptw_just_back = ptw.resp.fire && ptw.resp.bits.entry.hit(get_pn(req_out(idx).vaddr), asid = io.csr.satp.asid, allType = true)
198cb8f2f2aSLemover    io.ptw.req(idx).valid :=  RegNext(req_out_v(idx) && missVec(idx) && !ptw_just_back, false.B) // TODO: remove the regnext, timing
199c3b763d0SYinan Xu    when (RegEnable(io.requestor(idx).req_kill, RegNext(io.requestor(idx).req.fire))) {
200c3b763d0SYinan Xu      io.ptw.req(idx).valid := false.B
201c3b763d0SYinan Xu    }
202f1fe8698SLemover    io.ptw.req(idx).bits.vpn := RegNext(get_pn(req_out(idx).vaddr))
203149086eaSLemover  }
204a0301c0dSLemover
205f1fe8698SLemover  def handle_block(idx: Int): Unit = {
206f1fe8698SLemover    // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid
207f1fe8698SLemover    io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire()
208f1fe8698SLemover    // req_out_v for if there is a request, may long latency, fixme
209f1fe8698SLemover
210f1fe8698SLemover    // miss request entries
211f1fe8698SLemover    val miss_req_vpn = get_pn(req_out(idx).vaddr)
212f1fe8698SLemover    val hit = io.ptw.resp.bits.entry.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid
213f1fe8698SLemover
214f1fe8698SLemover    val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B)
215f1fe8698SLemover    val miss_wire = new_coming && missVec(idx)
216f1fe8698SLemover    val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire(), flush_pipe(idx))
217f1fe8698SLemover    val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe),
218f1fe8698SLemover      io.ptw.req(idx).fire() || resp(idx).fire(), flush_pipe(idx))
219f1fe8698SLemover
220f1fe8698SLemover    // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu
221f1fe8698SLemover    resp(idx).valid := req_out_v(idx) && !(miss_v && vmEnable)
222f1fe8698SLemover    when (io.ptw.resp.fire() && hit && req_out_v(idx) && vmEnable) {
223f1fe8698SLemover      val pte = io.ptw.resp.bits
224f1fe8698SLemover      resp(idx).valid := true.B
225f1fe8698SLemover      resp(idx).bits.miss := false.B // for blocked tlb, this is useless
22603efd994Shappy-lx      for (d <- 0 until nRespDups) {
22703efd994Shappy-lx        resp(idx).bits.paddr(d) := Cat(pte.entry.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr))
22803efd994Shappy-lx        perm_check(pte, req_out(idx).cmd, 0.U.asTypeOf(new TlbPMBundle), false.B, idx, d)
22903efd994Shappy-lx      }
23003efd994Shappy-lx      pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, idx)
231f1fe8698SLemover
232f1fe8698SLemover      // NOTE: the unfiltered req would be handled by Repeater
233f1fe8698SLemover    }
234f1fe8698SLemover    assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must")
235f1fe8698SLemover    assert(RegNext(req_out_v(idx) || !(miss_v || miss_req_v), true.B), "when not req_out_v, should not set miss_v/miss_req_v")
236f1fe8698SLemover
237f1fe8698SLemover    val ptw_req = io.ptw.req(idx)
238f1fe8698SLemover    ptw_req.valid := miss_req_v
239f1fe8698SLemover    ptw_req.bits.vpn := miss_req_vpn
240f1fe8698SLemover
241f1fe8698SLemover    // NOTE: when flush pipe, tlb should abandon last req
242f1fe8698SLemover    // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp
243f1fe8698SLemover    // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it.
244f1fe8698SLemover    if (!q.outsideRecvFlush) {
245f1fe8698SLemover      when (req_out_v(idx) && flush_pipe(idx) && vmEnable) {
246f1fe8698SLemover        resp(idx).valid := true.B
24703efd994Shappy-lx        for (d <- 0 until nRespDups) {
24803efd994Shappy-lx          resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr
24903efd994Shappy-lx          resp(idx).bits.excp(d).pf.st := true.B
25003efd994Shappy-lx          resp(idx).bits.excp(d).pf.instr := true.B
25103efd994Shappy-lx        }
252f1fe8698SLemover      }
253f1fe8698SLemover    }
254f1fe8698SLemover  }
255cb8f2f2aSLemover
256cb8f2f2aSLemover  // when ptw resp, tlb at refill_idx maybe set to miss by force.
257cb8f2f2aSLemover  // Bypass ptw resp to check.
258cb8f2f2aSLemover  def ptw_resp_bypass(vpn: UInt) = {
259cb8f2f2aSLemover    val p_hit = RegNext(ptw.resp.bits.entry.hit(vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.fire)
260cb8f2f2aSLemover    val p_ppn = RegEnable(ptw.resp.bits.entry.genPPN(vpn), io.ptw.resp.fire)
261cb8f2f2aSLemover    val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits), io.ptw.resp.fire)
262cb8f2f2aSLemover    (p_hit, p_ppn, p_perm)
263cb8f2f2aSLemover  }
264cb8f2f2aSLemover
265f1fe8698SLemover  // assert
266f1fe8698SLemover  for(i <- 0 until Width) {
267f1fe8698SLemover    TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.")
268149086eaSLemover  }
269a0301c0dSLemover
270f1fe8698SLemover  // perf event
271f1fe8698SLemover  val result_ok = req_in.map(a => RegNext(a.fire()))
272f1fe8698SLemover  val perfEvents =
273f1fe8698SLemover    Seq(
274f1fe8698SLemover      ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire() else vmEnable && result_ok(i) })),
275f1fe8698SLemover      ("miss  ", PopCount((0 until Width).map{i => if (Block(i)) vmEnable && result_ok(i) && missVec(i) else ptw.req(i).fire() })),
276a0301c0dSLemover    )
277f1fe8698SLemover  generatePerfEvent()
278a0301c0dSLemover
279f1fe8698SLemover  // perf log
2806d5ddbceSLemover  for (i <- 0 until Width) {
281f1fe8698SLemover    if (Block(i)) {
282f1fe8698SLemover      XSPerfAccumulate(s"access${i}",result_ok(i)  && vmEnable)
283f1fe8698SLemover      XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i))
2846d5ddbceSLemover    } else {
285f1fe8698SLemover      XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && vmEnable && RegNext(req(i).bits.debug.isFirstIssue))
286f1fe8698SLemover      XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && vmEnable)
287f1fe8698SLemover      XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && vmEnable && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue))
288f1fe8698SLemover      XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && vmEnable && missVec(i))
289a0301c0dSLemover    }
2906d5ddbceSLemover  }
2916d5ddbceSLemover  XSPerfAccumulate("ptw_resp_count", ptw.resp.fire())
2926d5ddbceSLemover  XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire() && ptw.resp.bits.pf)
2936d5ddbceSLemover
2946d5ddbceSLemover  // Log
2956d5ddbceSLemover  for(i <- 0 until Width) {
2966d5ddbceSLemover    XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n")
2976d5ddbceSLemover    XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n")
2986d5ddbceSLemover  }
2996d5ddbceSLemover
300f1fe8698SLemover  XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n")
301f1fe8698SLemover  XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n")
3026d5ddbceSLemover  for (i <- ptw.req.indices) {
30392e3bfefSLemover    XSDebug(ptw.req(i).fire(), p"L2TLB req:${ptw.req(i).bits}\n")
3046d5ddbceSLemover  }
30592e3bfefSLemover  XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n")
3066d5ddbceSLemover
307a0301c0dSLemover  println(s"${q.name}: normal page: ${q.normalNWays} ${q.normalAssociative} ${q.normalReplacer.get} super page: ${q.superNWays} ${q.superAssociative} ${q.superReplacer.get}")
308a0301c0dSLemover
309*5ab1b84dSHaoyuan Feng  if (env.EnableDifftest) {
310*5ab1b84dSHaoyuan Feng    val l1tlbid = Wire(UInt(2.W))
311*5ab1b84dSHaoyuan Feng    if (q.name == "itlb") {
312*5ab1b84dSHaoyuan Feng      l1tlbid := 0.U
313*5ab1b84dSHaoyuan Feng    } else if (q.name == "ldtlb") {
314*5ab1b84dSHaoyuan Feng      l1tlbid := 1.U
315*5ab1b84dSHaoyuan Feng    } else {
316*5ab1b84dSHaoyuan Feng      l1tlbid := 2.U
317*5ab1b84dSHaoyuan Feng    }
318*5ab1b84dSHaoyuan Feng
319*5ab1b84dSHaoyuan Feng    for (i <- 0 until Width) {
320*5ab1b84dSHaoyuan Feng      val pf = io.requestor(i).resp.bits.excp(0).pf.instr || io.requestor(i).resp.bits.excp(0).pf.st || io.requestor(i).resp.bits.excp(0).pf.ld
321*5ab1b84dSHaoyuan Feng      val af = io.requestor(i).resp.bits.excp(0).af.instr || io.requestor(i).resp.bits.excp(0).af.st || io.requestor(i).resp.bits.excp(0).af.ld
322*5ab1b84dSHaoyuan Feng      val difftest = Module(new DifftestL1TLBEvent)
323*5ab1b84dSHaoyuan Feng      difftest.io.clock := clock
324*5ab1b84dSHaoyuan Feng      difftest.io.coreid := p(XSCoreParamsKey).HartId.asUInt
325*5ab1b84dSHaoyuan Feng      difftest.io.valid := RegNext(io.requestor(i).req.fire) && !RegNext(io.requestor(i).req_kill) && io.requestor(i).resp.fire && !io.requestor(i).resp.bits.miss && !pf && !af && vmEnable
326*5ab1b84dSHaoyuan Feng      difftest.io.index := i.U
327*5ab1b84dSHaoyuan Feng      difftest.io.l1tlbid := l1tlbid
328*5ab1b84dSHaoyuan Feng      difftest.io.satp := io.csr.satp.ppn
329*5ab1b84dSHaoyuan Feng      difftest.io.vpn := RegNext(get_pn(req_in(i).bits.vaddr))
330*5ab1b84dSHaoyuan Feng      difftest.io.ppn := get_pn(io.requestor(i).resp.bits.paddr(0))
331*5ab1b84dSHaoyuan Feng    }
332*5ab1b84dSHaoyuan Feng  }
333*5ab1b84dSHaoyuan Feng
334f1fe8698SLemover}
3351ca0e4f3SYinan Xu
33603efd994Shappy-lxclass TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q)
33703efd994Shappy-lxclass TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q)
3386d5ddbceSLemover
339a0301c0dSLemoverclass TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule {
340a0301c0dSLemover  val io = IO(new TlbReplaceIO(Width, q))
341a0301c0dSLemover
342a0301c0dSLemover  if (q.normalAssociative == "fa") {
343a0301c0dSLemover    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays)
3443889e11eSLemover    re.access(io.normalPage.access.map(_.touch_ways))
345a0301c0dSLemover    io.normalPage.refillIdx := re.way
346a0301c0dSLemover  } else { // set-acco && plru
347a0301c0dSLemover    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays)
3483889e11eSLemover    re.access(io.normalPage.access.map(_.sets), io.normalPage.access.map(_.touch_ways))
349a0301c0dSLemover    io.normalPage.refillIdx := { if (q.normalNWays == 1) 0.U else re.way(io.normalPage.chosen_set) }
350a0301c0dSLemover  }
351a0301c0dSLemover
352a0301c0dSLemover  if (q.superAssociative == "fa") {
353a0301c0dSLemover    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays)
3543889e11eSLemover    re.access(io.superPage.access.map(_.touch_ways))
355a0301c0dSLemover    io.superPage.refillIdx := re.way
356a0301c0dSLemover  } else { // set-acco && plru
357a0301c0dSLemover    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNSets, q.superNWays)
3583889e11eSLemover    re.access(io.superPage.access.map(_.sets), io.superPage.access.map(_.touch_ways))
359a0301c0dSLemover    io.superPage.refillIdx := { if (q.superNWays == 1) 0.U else re.way(io.superPage.chosen_set) }
360a0301c0dSLemover  }
361a0301c0dSLemover}
362