xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLB.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
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
19*8891a219SYinan 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)
195cb8f2f2aSLemover    io.ptw.req(idx).valid :=  RegNext(req_out_v(idx) && missVec(idx) && !ptw_just_back, false.B) // TODO: remove the regnext, timing
196c3b763d0SYinan Xu    when (RegEnable(io.requestor(idx).req_kill, RegNext(io.requestor(idx).req.fire))) {
197c3b763d0SYinan Xu      io.ptw.req(idx).valid := false.B
198c3b763d0SYinan Xu    }
199f1fe8698SLemover    io.ptw.req(idx).bits.vpn := RegNext(get_pn(req_out(idx).vaddr))
2008744445eSMaxpicca-Li    io.ptw.req(idx).bits.memidx := RegNext(req_out(idx).memidx)
201149086eaSLemover  }
202a0301c0dSLemover
203f1fe8698SLemover  def handle_block(idx: Int): Unit = {
204f1fe8698SLemover    // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid
205935edac4STang Haojin    io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire
206f1fe8698SLemover    // req_out_v for if there is a request, may long latency, fixme
207f1fe8698SLemover
208f1fe8698SLemover    // miss request entries
209f1fe8698SLemover    val miss_req_vpn = get_pn(req_out(idx).vaddr)
2108744445eSMaxpicca-Li    val miss_req_memidx = req_out(idx).memidx
21163632028SHaoyuan Feng    val hit = io.ptw.resp.bits.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid
212f1fe8698SLemover
213f1fe8698SLemover    val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B)
214f1fe8698SLemover    val miss_wire = new_coming && missVec(idx)
215935edac4STang Haojin    val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire, flush_pipe(idx))
216f1fe8698SLemover    val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe),
217935edac4STang Haojin      io.ptw.req(idx).fire || resp(idx).fire, flush_pipe(idx))
218f1fe8698SLemover
219f1fe8698SLemover    // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu
220292bea3fSWilliam Wang    resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx))
221935edac4STang Haojin    when (io.ptw.resp.fire && hit && req_out_v(idx) && portTranslateEnable(idx)) {
222f1fe8698SLemover      val pte = io.ptw.resp.bits
223f1fe8698SLemover      resp(idx).valid := true.B
224f1fe8698SLemover      resp(idx).bits.miss := false.B // for blocked tlb, this is useless
22503efd994Shappy-lx      for (d <- 0 until nRespDups) {
22663632028SHaoyuan Feng        resp(idx).bits.paddr(d) := Cat(pte.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr))
227f9ac118cSHaoyuan Feng        perm_check(pte, req_out(idx).cmd, idx, d)
22803efd994Shappy-lx      }
22903efd994Shappy-lx      pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, idx)
230f1fe8698SLemover
231f1fe8698SLemover      // NOTE: the unfiltered req would be handled by Repeater
232f1fe8698SLemover    }
233f1fe8698SLemover    assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must")
234f1fe8698SLemover    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")
235f1fe8698SLemover
236f1fe8698SLemover    val ptw_req = io.ptw.req(idx)
237f1fe8698SLemover    ptw_req.valid := miss_req_v
238f1fe8698SLemover    ptw_req.bits.vpn := miss_req_vpn
2398744445eSMaxpicca-Li    ptw_req.bits.memidx := miss_req_memidx
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) {
245292bea3fSWilliam Wang      when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) {
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) = {
25963632028SHaoyuan Feng    val p_hit = RegNext(ptw.resp.bits.hit(vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.fire)
26063632028SHaoyuan Feng    val p_ppn = RegEnable(ptw.resp.bits.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
271935edac4STang Haojin  val result_ok = req_in.map(a => RegNext(a.fire))
272f1fe8698SLemover  val perfEvents =
273f1fe8698SLemover    Seq(
274935edac4STang Haojin      ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire else portTranslateEnable(i) && result_ok(i) })),
275935edac4STang Haojin      ("miss  ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && 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)) {
282292bea3fSWilliam Wang      XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i))
283f1fe8698SLemover      XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i))
2846d5ddbceSLemover    } else {
285292bea3fSWilliam Wang      XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegNext(req(i).bits.debug.isFirstIssue))
286292bea3fSWilliam Wang      XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i))
287292bea3fSWilliam Wang      XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue))
288292bea3fSWilliam Wang      XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i))
289a0301c0dSLemover    }
2906d5ddbceSLemover  }
291935edac4STang Haojin  XSPerfAccumulate("ptw_resp_count", ptw.resp.fire)
292935edac4STang Haojin  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) {
303935edac4STang Haojin    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
307f9ac118cSHaoyuan Feng  println(s"${q.name}: page: ${q.NWays} ${q.Associative} ${q.Replacer.get}")
308a0301c0dSLemover
3095ab1b84dSHaoyuan Feng  if (env.EnableDifftest) {
3105ab1b84dSHaoyuan Feng    for (i <- 0 until Width) {
3115ab1b84dSHaoyuan 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
3125ab1b84dSHaoyuan 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
3137d45a146SYinan Xu      val difftest = DifftestModule(new DiffL1TLBEvent)
314254e4960SHaoyuan Feng      difftest.coreid := io.hartId
3157d45a146SYinan Xu      difftest.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 && portTranslateEnable(i)
3167d45a146SYinan Xu      if (!Seq("itlb", "ldtlb", "sttlb").contains(q.name)) {
3177d45a146SYinan Xu        difftest.valid := false.B
3187d45a146SYinan Xu      }
3197d45a146SYinan Xu      difftest.index := TLBDiffId(p(XSCoreParamsKey).HartId).U
3207d45a146SYinan Xu      difftest.satp := io.csr.satp.ppn
3217d45a146SYinan Xu      difftest.vpn := RegNext(get_pn(req_in(i).bits.vaddr))
3227d45a146SYinan Xu      difftest.ppn := get_pn(io.requestor(i).resp.bits.paddr(0))
3237d45a146SYinan Xu    }
3245ab1b84dSHaoyuan Feng  }
3255ab1b84dSHaoyuan Feng}
3265ab1b84dSHaoyuan Feng
3277d45a146SYinan Xuobject TLBDiffId {
3287d45a146SYinan Xu  var i: Int = 0
3297d45a146SYinan Xu  var lastHartId: Int = -1
3307d45a146SYinan Xu  def apply(hartId: Int): Int = {
3317d45a146SYinan Xu    if (lastHartId != hartId) {
3327d45a146SYinan Xu      i = 0
3337d45a146SYinan Xu      lastHartId = hartId
3347d45a146SYinan Xu    }
3357d45a146SYinan Xu    i += 1
3367d45a146SYinan Xu    i - 1
3377d45a146SYinan Xu  }
338f1fe8698SLemover}
3391ca0e4f3SYinan Xu
34003efd994Shappy-lxclass TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q)
34103efd994Shappy-lxclass TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q)
3426d5ddbceSLemover
343a0301c0dSLemoverclass TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule {
344a0301c0dSLemover  val io = IO(new TlbReplaceIO(Width, q))
345a0301c0dSLemover
346f9ac118cSHaoyuan Feng  if (q.Associative == "fa") {
347f9ac118cSHaoyuan Feng    val re = ReplacementPolicy.fromString(q.Replacer, q.NWays)
348f9ac118cSHaoyuan Feng    re.access(io.page.access.map(_.touch_ways))
349f9ac118cSHaoyuan Feng    io.page.refillIdx := re.way
350a0301c0dSLemover  } else { // set-acco && plru
351f9ac118cSHaoyuan Feng    val re = ReplacementPolicy.fromString(q.Replacer, q.NSets, q.NWays)
352f9ac118cSHaoyuan Feng    re.access(io.page.access.map(_.sets), io.page.access.map(_.touch_ways))
353f9ac118cSHaoyuan Feng    io.page.refillIdx := { if (q.NWays == 1) 0.U else re.way(io.page.chosen_set) }
354a0301c0dSLemover  }
355a0301c0dSLemover}
356