xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLB.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
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
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
225ab1b84dSHaoyuan Fengimport difftest._
23a0301c0dSLemoverimport freechips.rocketchip.util.SRAMAnnotation
246d5ddbceSLemoverimport xiangshan._
256d5ddbceSLemoverimport utils._
263c02ee8fSwakafaimport utility._
27f1fe8698SLemoverimport xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig}
289aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr
296d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst
30f1fe8698SLemoverimport freechips.rocketchip.rocket.PMPConfig
316d5ddbceSLemover
32f1fe8698SLemover/** TLB module
33f1fe8698SLemover  * support block request and non-block request io at the same time
34f1fe8698SLemover  * return paddr at next cycle, then go for pmp/pma check
35f1fe8698SLemover  * @param Width: The number of requestors
36f1fe8698SLemover  * @param Block: Blocked or not for each requestor ports
37f1fe8698SLemover  * @param q: TLB Parameters, like entry number, each TLB has its own parameters
38f1fe8698SLemover  * @param p: XiangShan Paramemters, like XLEN
39f1fe8698SLemover  */
40a0301c0dSLemover
4103efd994Shappy-lxclass TLB(Width: Int, nRespDups: Int = 1, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule
42f1fe8698SLemover  with HasCSRConst
43f1fe8698SLemover  with HasPerfEvents
44f1fe8698SLemover{
4503efd994Shappy-lx  val io = IO(new TlbIO(Width, nRespDups, q))
46a0301c0dSLemover
476d5ddbceSLemover  val req = io.requestor.map(_.req)
486d5ddbceSLemover  val resp = io.requestor.map(_.resp)
496d5ddbceSLemover  val ptw = io.ptw
50b6982e83SLemover  val pmp = io.pmp
518744445eSMaxpicca-Li  val refill_to_mem = io.refill_to_mem
526d5ddbceSLemover
53f1fe8698SLemover  /** Sfence.vma & Svinval
54f1fe8698SLemover    * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe
55f1fe8698SLemover    * Svinval will 1. flush old entries 2. flush inflight
56f1fe8698SLemover    * So, Svinval will not flush pipe, which means
57f1fe8698SLemover    * it should not drop reqs from pipe and should return right resp
58f1fe8698SLemover    */
59f1fe8698SLemover  val sfence = DelayN(io.sfence, q.fenceDelay)
606d5ddbceSLemover  val csr = io.csr
61f1fe8698SLemover  val satp = DelayN(io.csr.satp, q.fenceDelay)
62f1fe8698SLemover  val flush_mmu = DelayN(sfence.valid || csr.satp.changed, q.fenceDelay)
63f1fe8698SLemover  val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe
64f1fe8698SLemover  val flush_pipe = io.flushPipe
65f1fe8698SLemover
66f1fe8698SLemover  // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush.
67f1fe8698SLemover  // because, csr will influence tlb behavior.
68a0301c0dSLemover  val ifecth = if (q.fetchi) true.B else false.B
69f1fe8698SLemover  val mode = if (q.useDmode) csr.priv.dmode else csr.priv.imode
706d5ddbceSLemover  // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux...
71292bea3fSWilliam Wang  val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U)
72292bea3fSWilliam Wang    else (satp.mode === 8.U && (mode < ModeM))
7357fe673eSMa-YX  val portTranslateEnable = (0 until Width).map(i => vmEnable && RegNext(!req(i).bits.no_translate))
746d5ddbceSLemover
75f1fe8698SLemover  val req_in = req
76935edac4STang Haojin  val req_out = req.map(a => RegEnable(a.bits, a.fire))
77f1fe8698SLemover  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)))
786d5ddbceSLemover
79935edac4STang Haojin  val refill = ptw.resp.fire && !flush_mmu && vmEnable
808744445eSMaxpicca-Li  refill_to_mem.valid := refill
818744445eSMaxpicca-Li  refill_to_mem.memidx := ptw.resp.bits.memidx
828744445eSMaxpicca-Li
8303efd994Shappy-lx  val entries = Module(new TlbStorageWrapper(Width, q, nRespDups))
84f1fe8698SLemover  entries.io.base_connect(sfence, csr, satp)
85f1fe8698SLemover  if (q.outReplace) { io.replace <> entries.io.replace }
866d5ddbceSLemover  for (i <- 0 until Width) {
87f1fe8698SLemover    entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i)
88f9ac118cSHaoyuan Feng    entries.io.w_apply(refill, ptw.resp.bits)
898744445eSMaxpicca-Li    resp(i).bits.debug.isFirstIssue := RegNext(req(i).bits.debug.isFirstIssue)
908744445eSMaxpicca-Li    resp(i).bits.debug.robIdx := RegNext(req(i).bits.debug.robIdx)
91a0301c0dSLemover  }
926d5ddbceSLemover
93f1fe8698SLemover  // read TLB, get hit/miss, paddr, perm bits
94f1fe8698SLemover  val readResult = (0 until Width).map(TLBRead(_))
95f1fe8698SLemover  val hitVec = readResult.map(_._1)
96f1fe8698SLemover  val missVec = readResult.map(_._2)
97f1fe8698SLemover  val pmp_addr = readResult.map(_._3)
98f9ac118cSHaoyuan Feng  val perm = readResult.map(_._4)
99149086eaSLemover
100f1fe8698SLemover  // check pmp use paddr (for timing optization, use pmp_addr here)
101f1fe8698SLemover  // check permisson
102f1fe8698SLemover  (0 until Width).foreach{i =>
103f1fe8698SLemover    pmp_check(pmp_addr(i), req_out(i).size, req_out(i).cmd, i)
10403efd994Shappy-lx    for (d <- 0 until nRespDups) {
105f9ac118cSHaoyuan Feng      perm_check(perm(i)(d), req_out(i).cmd, i, d)
10603efd994Shappy-lx    }
107f1fe8698SLemover  }
1086d5ddbceSLemover
109f1fe8698SLemover  // handle block or non-block io
110f1fe8698SLemover  // for non-block io, just return the above result, send miss to ptw
111f1fe8698SLemover  // for block io, hold the request, send miss to ptw,
112f1fe8698SLemover  //   when ptw back, return the result
113f1fe8698SLemover  (0 until Width) foreach {i =>
114f1fe8698SLemover    if (Block(i)) handle_block(i)
115f1fe8698SLemover    else handle_nonblock(i)
116f1fe8698SLemover  }
117f1fe8698SLemover  io.ptw.resp.ready := true.B
118a0301c0dSLemover
119f1fe8698SLemover  /************************  main body above | method/log/perf below ****************************/
120f1fe8698SLemover  def TLBRead(i: Int) = {
121f9ac118cSHaoyuan Feng    val (e_hit, e_ppn, e_perm) = entries.io.r_resp_apply(i)
122cb8f2f2aSLemover    val (p_hit, p_ppn, p_perm) = ptw_resp_bypass(get_pn(req_in(i).bits.vaddr))
123292bea3fSWilliam Wang    val enable = portTranslateEnable(i)
124cb8f2f2aSLemover
125cb8f2f2aSLemover    val hit = e_hit || p_hit
126292bea3fSWilliam Wang    val miss = !hit && enable
127f1fe8698SLemover    hit.suggestName(s"hit_read_${i}")
128f1fe8698SLemover    miss.suggestName(s"miss_read_${i}")
1296d5ddbceSLemover
130f1fe8698SLemover    val vaddr = SignExt(req_out(i).vaddr, PAddrBits)
131f1fe8698SLemover    resp(i).bits.miss := miss
132935edac4STang Haojin    resp(i).bits.ptwBack := ptw.resp.fire
1338744445eSMaxpicca-Li    resp(i).bits.memidx := RegNext(req_in(i).bits.memidx)
1346d5ddbceSLemover
13503efd994Shappy-lx    val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W))))
13603efd994Shappy-lx    val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle))))
13703efd994Shappy-lx
13803efd994Shappy-lx    for (d <- 0 until nRespDups) {
13903efd994Shappy-lx      ppn(d) := Mux(p_hit, p_ppn, e_ppn(d))
14003efd994Shappy-lx      perm(d) := Mux(p_hit, p_perm, e_perm(d))
14103efd994Shappy-lx
14203efd994Shappy-lx      val paddr = Cat(ppn(d), get_off(req_out(i).vaddr))
143292bea3fSWilliam Wang      resp(i).bits.paddr(d) := Mux(enable, paddr, vaddr)
14403efd994Shappy-lx    }
14503efd994Shappy-lx
14603efd994Shappy-lx    XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n")
14703efd994Shappy-lx
148f9ac118cSHaoyuan Feng    val pmp_paddr = resp(i).bits.paddr(0)
149f1fe8698SLemover
150f9ac118cSHaoyuan Feng    (hit, miss, pmp_paddr, perm)
151f1fe8698SLemover  }
152f1fe8698SLemover
153f1fe8698SLemover  def pmp_check(addr: UInt, size: UInt, cmd: UInt, idx: Int): Unit = {
154f1fe8698SLemover    pmp(idx).valid := resp(idx).valid
155f1fe8698SLemover    pmp(idx).bits.addr := addr
156f1fe8698SLemover    pmp(idx).bits.size := size
157f1fe8698SLemover    pmp(idx).bits.cmd := cmd
158f1fe8698SLemover  }
159f1fe8698SLemover
160f9ac118cSHaoyuan Feng  def perm_check(perm: TlbPermBundle, cmd: UInt, idx: Int, nDups: Int) = {
1615b7ef044SLemover    // for timing optimization, pmp check is divided into dynamic and static
1625b7ef044SLemover    // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done
1635b7ef044SLemover    // static: 4K pages (or sram entries) -> check pmp with pre-checked results
164f1fe8698SLemover    val af = perm.af
165f1fe8698SLemover    val pf = perm.pf
166f1fe8698SLemover    val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception
167f1fe8698SLemover    val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception
168f1fe8698SLemover    val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception
169f1fe8698SLemover    val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!io.csr.priv.sum || ifecth))
170f1fe8698SLemover    val ldPermFail = !(modeCheck && (perm.r || io.csr.priv.mxr && perm.x))
171a79fef67Swakafa    val stPermFail = !(modeCheck && perm.w)
172a79fef67Swakafa    val instrPermFail = !(modeCheck && perm.x)
173f1fe8698SLemover    val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd))
174f1fe8698SLemover    val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd))
175f1fe8698SLemover    val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd)
176292bea3fSWilliam Wang    val fault_valid = portTranslateEnable(idx)
17703efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && fault_valid && !af
17803efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && fault_valid && !af
17903efd994Shappy-lx    resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && fault_valid && !af
180b6982e83SLemover    // NOTE: pf need && with !af, page fault has higher priority than access fault
181b6982e83SLemover    // but ptw may also have access fault, then af happens, the translation is wrong.
182b6982e83SLemover    // In this case, pf has lower priority than af
1836d5ddbceSLemover
184f9ac118cSHaoyuan Feng    resp(idx).bits.excp(nDups).af.ld    := af && TlbCmd.isRead(cmd) && fault_valid
185f9ac118cSHaoyuan Feng    resp(idx).bits.excp(nDups).af.st    := af && TlbCmd.isWrite(cmd) && fault_valid
186f9ac118cSHaoyuan Feng    resp(idx).bits.excp(nDups).af.instr := af && TlbCmd.isExec(cmd) && fault_valid
1876d5ddbceSLemover  }
1886d5ddbceSLemover
189f1fe8698SLemover  def handle_nonblock(idx: Int): Unit = {
190f1fe8698SLemover    io.requestor(idx).resp.valid := req_out_v(idx)
191f1fe8698SLemover    io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true
1929930e66fSLemover    XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B")
193cb8f2f2aSLemover
19463632028SHaoyuan Feng    val ptw_just_back = ptw.resp.fire && ptw.resp.bits.hit(get_pn(req_out(idx).vaddr), asid = io.csr.satp.asid, allType = true)
195*185e6164SHaoyuan Feng    val ptw_already_back = RegNext(ptw.resp.fire) && RegNext(ptw.resp.bits).hit(get_pn(req_out(idx).vaddr), asid = io.csr.satp.asid, allType = true)
196*185e6164SHaoyuan Feng    io.ptw.req(idx).valid := req_out_v(idx) && missVec(idx) && !(ptw_just_back || ptw_already_back) // TODO: remove the regnext, timing
197*185e6164SHaoyuan Feng    io.tlbreplay(idx) := req_out_v(idx) && missVec(idx) && (ptw_just_back || ptw_already_back)
198*185e6164SHaoyuan Feng    when (io.requestor(idx).req_kill && RegNext(io.requestor(idx).req.fire)) {
199c3b763d0SYinan Xu      io.ptw.req(idx).valid := false.B
200*185e6164SHaoyuan Feng      io.tlbreplay(idx) := true.B
201c3b763d0SYinan Xu    }
202*185e6164SHaoyuan Feng    io.ptw.req(idx).bits.vpn := get_pn(req_out(idx).vaddr)
203*185e6164SHaoyuan Feng    io.ptw.req(idx).bits.memidx := req_out(idx).memidx
204149086eaSLemover  }
205a0301c0dSLemover
206f1fe8698SLemover  def handle_block(idx: Int): Unit = {
207f1fe8698SLemover    // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid
208935edac4STang Haojin    io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire
209f1fe8698SLemover    // req_out_v for if there is a request, may long latency, fixme
210f1fe8698SLemover
211f1fe8698SLemover    // miss request entries
212f1fe8698SLemover    val miss_req_vpn = get_pn(req_out(idx).vaddr)
2138744445eSMaxpicca-Li    val miss_req_memidx = req_out(idx).memidx
21463632028SHaoyuan Feng    val hit = io.ptw.resp.bits.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid
215f1fe8698SLemover
216f1fe8698SLemover    val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B)
217f1fe8698SLemover    val miss_wire = new_coming && missVec(idx)
218935edac4STang Haojin    val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire, flush_pipe(idx))
219f1fe8698SLemover    val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe),
220935edac4STang Haojin      io.ptw.req(idx).fire || resp(idx).fire, flush_pipe(idx))
221f1fe8698SLemover
222f1fe8698SLemover    // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu
223292bea3fSWilliam Wang    resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx))
224935edac4STang Haojin    when (io.ptw.resp.fire && hit && req_out_v(idx) && portTranslateEnable(idx)) {
225f1fe8698SLemover      val pte = io.ptw.resp.bits
226f1fe8698SLemover      resp(idx).valid := true.B
227f1fe8698SLemover      resp(idx).bits.miss := false.B // for blocked tlb, this is useless
22803efd994Shappy-lx      for (d <- 0 until nRespDups) {
22963632028SHaoyuan Feng        resp(idx).bits.paddr(d) := Cat(pte.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr))
230f9ac118cSHaoyuan Feng        perm_check(pte, req_out(idx).cmd, idx, d)
23103efd994Shappy-lx      }
23203efd994Shappy-lx      pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, idx)
233f1fe8698SLemover
234f1fe8698SLemover      // NOTE: the unfiltered req would be handled by Repeater
235f1fe8698SLemover    }
236f1fe8698SLemover    assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must")
237f1fe8698SLemover    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")
238f1fe8698SLemover
239f1fe8698SLemover    val ptw_req = io.ptw.req(idx)
240f1fe8698SLemover    ptw_req.valid := miss_req_v
241f1fe8698SLemover    ptw_req.bits.vpn := miss_req_vpn
2428744445eSMaxpicca-Li    ptw_req.bits.memidx := miss_req_memidx
243f1fe8698SLemover
244*185e6164SHaoyuan Feng    io.tlbreplay(idx) := false.B
245*185e6164SHaoyuan Feng
246f1fe8698SLemover    // NOTE: when flush pipe, tlb should abandon last req
247f1fe8698SLemover    // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp
248f1fe8698SLemover    // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it.
249f1fe8698SLemover    if (!q.outsideRecvFlush) {
250292bea3fSWilliam Wang      when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) {
251f1fe8698SLemover        resp(idx).valid := true.B
25203efd994Shappy-lx        for (d <- 0 until nRespDups) {
25303efd994Shappy-lx          resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr
25403efd994Shappy-lx          resp(idx).bits.excp(d).pf.st := true.B
25503efd994Shappy-lx          resp(idx).bits.excp(d).pf.instr := true.B
25603efd994Shappy-lx        }
257f1fe8698SLemover      }
258f1fe8698SLemover    }
259f1fe8698SLemover  }
260cb8f2f2aSLemover
261cb8f2f2aSLemover  // when ptw resp, tlb at refill_idx maybe set to miss by force.
262cb8f2f2aSLemover  // Bypass ptw resp to check.
263cb8f2f2aSLemover  def ptw_resp_bypass(vpn: UInt) = {
26463632028SHaoyuan Feng    val p_hit = RegNext(ptw.resp.bits.hit(vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.fire)
26563632028SHaoyuan Feng    val p_ppn = RegEnable(ptw.resp.bits.genPPN(vpn), io.ptw.resp.fire)
266cb8f2f2aSLemover    val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits), io.ptw.resp.fire)
267cb8f2f2aSLemover    (p_hit, p_ppn, p_perm)
268cb8f2f2aSLemover  }
269cb8f2f2aSLemover
270f1fe8698SLemover  // assert
271f1fe8698SLemover  for(i <- 0 until Width) {
272f1fe8698SLemover    TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.")
273149086eaSLemover  }
274a0301c0dSLemover
275f1fe8698SLemover  // perf event
276935edac4STang Haojin  val result_ok = req_in.map(a => RegNext(a.fire))
277f1fe8698SLemover  val perfEvents =
278f1fe8698SLemover    Seq(
279935edac4STang Haojin      ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire else portTranslateEnable(i) && result_ok(i) })),
280935edac4STang Haojin      ("miss  ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && result_ok(i) && missVec(i) else ptw.req(i).fire })),
281a0301c0dSLemover    )
282f1fe8698SLemover  generatePerfEvent()
283a0301c0dSLemover
284f1fe8698SLemover  // perf log
2856d5ddbceSLemover  for (i <- 0 until Width) {
286f1fe8698SLemover    if (Block(i)) {
287292bea3fSWilliam Wang      XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i))
288f1fe8698SLemover      XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i))
2896d5ddbceSLemover    } else {
290292bea3fSWilliam Wang      XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegNext(req(i).bits.debug.isFirstIssue))
291292bea3fSWilliam Wang      XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i))
292292bea3fSWilliam Wang      XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue))
293292bea3fSWilliam Wang      XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i))
294a0301c0dSLemover    }
2956d5ddbceSLemover  }
296935edac4STang Haojin  XSPerfAccumulate("ptw_resp_count", ptw.resp.fire)
297935edac4STang Haojin  XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire && ptw.resp.bits.pf)
2986d5ddbceSLemover
2996d5ddbceSLemover  // Log
3006d5ddbceSLemover  for(i <- 0 until Width) {
3016d5ddbceSLemover    XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n")
3026d5ddbceSLemover    XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n")
3036d5ddbceSLemover  }
3046d5ddbceSLemover
305f1fe8698SLemover  XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n")
306f1fe8698SLemover  XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n")
3076d5ddbceSLemover  for (i <- ptw.req.indices) {
308935edac4STang Haojin    XSDebug(ptw.req(i).fire, p"L2TLB req:${ptw.req(i).bits}\n")
3096d5ddbceSLemover  }
31092e3bfefSLemover  XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n")
3116d5ddbceSLemover
312f9ac118cSHaoyuan Feng  println(s"${q.name}: page: ${q.NWays} ${q.Associative} ${q.Replacer.get}")
313a0301c0dSLemover
3145ab1b84dSHaoyuan Feng  if (env.EnableDifftest) {
3155ab1b84dSHaoyuan Feng    for (i <- 0 until Width) {
3165ab1b84dSHaoyuan 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
3175ab1b84dSHaoyuan 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
3187d45a146SYinan Xu      val difftest = DifftestModule(new DiffL1TLBEvent)
319254e4960SHaoyuan Feng      difftest.coreid := io.hartId
320*185e6164SHaoyuan Feng      difftest.valid := RegNext(io.requestor(i).req.fire) && !io.requestor(i).req_kill && io.requestor(i).resp.fire && !io.requestor(i).resp.bits.miss && !pf && !af && portTranslateEnable(i)
3217d45a146SYinan Xu      if (!Seq("itlb", "ldtlb", "sttlb").contains(q.name)) {
3227d45a146SYinan Xu        difftest.valid := false.B
3237d45a146SYinan Xu      }
3247d45a146SYinan Xu      difftest.index := TLBDiffId(p(XSCoreParamsKey).HartId).U
3257d45a146SYinan Xu      difftest.satp := io.csr.satp.ppn
3267d45a146SYinan Xu      difftest.vpn := RegNext(get_pn(req_in(i).bits.vaddr))
3277d45a146SYinan Xu      difftest.ppn := get_pn(io.requestor(i).resp.bits.paddr(0))
3287d45a146SYinan Xu    }
3295ab1b84dSHaoyuan Feng  }
3305ab1b84dSHaoyuan Feng}
3315ab1b84dSHaoyuan Feng
3327d45a146SYinan Xuobject TLBDiffId {
3337d45a146SYinan Xu  var i: Int = 0
3347d45a146SYinan Xu  var lastHartId: Int = -1
3357d45a146SYinan Xu  def apply(hartId: Int): Int = {
3367d45a146SYinan Xu    if (lastHartId != hartId) {
3377d45a146SYinan Xu      i = 0
3387d45a146SYinan Xu      lastHartId = hartId
3397d45a146SYinan Xu    }
3407d45a146SYinan Xu    i += 1
3417d45a146SYinan Xu    i - 1
3427d45a146SYinan Xu  }
343f1fe8698SLemover}
3441ca0e4f3SYinan Xu
34503efd994Shappy-lxclass TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q)
34603efd994Shappy-lxclass TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q)
3476d5ddbceSLemover
348a0301c0dSLemoverclass TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule {
349a0301c0dSLemover  val io = IO(new TlbReplaceIO(Width, q))
350a0301c0dSLemover
351f9ac118cSHaoyuan Feng  if (q.Associative == "fa") {
352f9ac118cSHaoyuan Feng    val re = ReplacementPolicy.fromString(q.Replacer, q.NWays)
353f9ac118cSHaoyuan Feng    re.access(io.page.access.map(_.touch_ways))
354f9ac118cSHaoyuan Feng    io.page.refillIdx := re.way
355a0301c0dSLemover  } else { // set-acco && plru
356f9ac118cSHaoyuan Feng    val re = ReplacementPolicy.fromString(q.Replacer, q.NSets, q.NWays)
357f9ac118cSHaoyuan Feng    re.access(io.page.access.map(_.sets), io.page.access.map(_.touch_ways))
358f9ac118cSHaoyuan Feng    io.page.refillIdx := { if (q.NWays == 1) 0.U else re.way(io.page.chosen_set) }
359a0301c0dSLemover  }
360a0301c0dSLemover}
361