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._ 235ab1b84dSHaoyuan 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 548744445eSMaxpicca-Li val refill_to_mem = io.refill_to_mem 556d5ddbceSLemover 56f1fe8698SLemover /** Sfence.vma & Svinval 57f1fe8698SLemover * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe 58f1fe8698SLemover * Svinval will 1. flush old entries 2. flush inflight 59f1fe8698SLemover * So, Svinval will not flush pipe, which means 60f1fe8698SLemover * it should not drop reqs from pipe and should return right resp 61f1fe8698SLemover */ 62f1fe8698SLemover val sfence = DelayN(io.sfence, q.fenceDelay) 636d5ddbceSLemover val csr = io.csr 64f1fe8698SLemover val satp = DelayN(io.csr.satp, q.fenceDelay) 65f1fe8698SLemover val flush_mmu = DelayN(sfence.valid || csr.satp.changed, q.fenceDelay) 66f1fe8698SLemover val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe 67f1fe8698SLemover val flush_pipe = io.flushPipe 68f1fe8698SLemover 69f1fe8698SLemover // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush. 70f1fe8698SLemover // because, csr will influence tlb behavior. 71a0301c0dSLemover val ifecth = if (q.fetchi) true.B else false.B 72f1fe8698SLemover val mode = if (q.useDmode) csr.priv.dmode else csr.priv.imode 736d5ddbceSLemover // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux... 74292bea3fSWilliam Wang val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U) 75292bea3fSWilliam Wang else (satp.mode === 8.U && (mode < ModeM)) 76292bea3fSWilliam Wang val portTranslateEnable = (0 until Width).map(i => vmEnable && !req(i).bits.no_translate) 776d5ddbceSLemover 78f1fe8698SLemover val req_in = req 79f1fe8698SLemover val req_out = req.map(a => RegEnable(a.bits, a.fire())) 80f1fe8698SLemover 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))) 816d5ddbceSLemover 82f1fe8698SLemover val refill = ptw.resp.fire() && !flush_mmu && vmEnable 838744445eSMaxpicca-Li refill_to_mem.valid := refill 848744445eSMaxpicca-Li refill_to_mem.memidx := ptw.resp.bits.memidx 858744445eSMaxpicca-Li 8603efd994Shappy-lx val entries = Module(new TlbStorageWrapper(Width, q, nRespDups)) 87f1fe8698SLemover entries.io.base_connect(sfence, csr, satp) 88f1fe8698SLemover if (q.outReplace) { io.replace <> entries.io.replace } 896d5ddbceSLemover for (i <- 0 until Width) { 90f1fe8698SLemover entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i) 91f1fe8698SLemover entries.io.w_apply(refill, ptw.resp.bits, io.ptw_replenish) 928744445eSMaxpicca-Li resp(i).bits.debug.isFirstIssue := RegNext(req(i).bits.debug.isFirstIssue) 938744445eSMaxpicca-Li resp(i).bits.debug.robIdx := RegNext(req(i).bits.debug.robIdx) 94a0301c0dSLemover } 956d5ddbceSLemover 96f1fe8698SLemover // read TLB, get hit/miss, paddr, perm bits 97f1fe8698SLemover val readResult = (0 until Width).map(TLBRead(_)) 98f1fe8698SLemover val hitVec = readResult.map(_._1) 99f1fe8698SLemover val missVec = readResult.map(_._2) 100f1fe8698SLemover val pmp_addr = readResult.map(_._3) 101f1fe8698SLemover val static_pm = readResult.map(_._4) 102f1fe8698SLemover val static_pm_v = readResult.map(_._5) 103f1fe8698SLemover val perm = readResult.map(_._6) 104149086eaSLemover 105f1fe8698SLemover // check pmp use paddr (for timing optization, use pmp_addr here) 106f1fe8698SLemover // check permisson 107f1fe8698SLemover (0 until Width).foreach{i => 108f1fe8698SLemover pmp_check(pmp_addr(i), req_out(i).size, req_out(i).cmd, i) 10903efd994Shappy-lx for (d <- 0 until nRespDups) { 11003efd994Shappy-lx perm_check(perm(i)(d), req_out(i).cmd, static_pm(i), static_pm_v(i), i, d) 11103efd994Shappy-lx } 112f1fe8698SLemover } 1136d5ddbceSLemover 114f1fe8698SLemover // handle block or non-block io 115f1fe8698SLemover // for non-block io, just return the above result, send miss to ptw 116f1fe8698SLemover // for block io, hold the request, send miss to ptw, 117f1fe8698SLemover // when ptw back, return the result 118f1fe8698SLemover (0 until Width) foreach {i => 119f1fe8698SLemover if (Block(i)) handle_block(i) 120f1fe8698SLemover else handle_nonblock(i) 121f1fe8698SLemover } 122f1fe8698SLemover io.ptw.resp.ready := true.B 123a0301c0dSLemover 124f1fe8698SLemover /************************ main body above | method/log/perf below ****************************/ 125f1fe8698SLemover def TLBRead(i: Int) = { 126cb8f2f2aSLemover val (e_hit, e_ppn, e_perm, e_super_hit, e_super_ppn, static_pm) = entries.io.r_resp_apply(i) 127cb8f2f2aSLemover val (p_hit, p_ppn, p_perm) = ptw_resp_bypass(get_pn(req_in(i).bits.vaddr)) 128292bea3fSWilliam Wang val enable = portTranslateEnable(i) 129cb8f2f2aSLemover 130cb8f2f2aSLemover val hit = e_hit || p_hit 131292bea3fSWilliam Wang val miss = !hit && enable 132292bea3fSWilliam Wang val fast_miss = !(e_super_hit || p_hit) && enable 133f1fe8698SLemover hit.suggestName(s"hit_read_${i}") 134f1fe8698SLemover miss.suggestName(s"miss_read_${i}") 1356d5ddbceSLemover 136f1fe8698SLemover val vaddr = SignExt(req_out(i).vaddr, PAddrBits) 137f1fe8698SLemover resp(i).bits.miss := miss 138e05a24abSLemover resp(i).bits.fast_miss := fast_miss 139e05a24abSLemover resp(i).bits.ptwBack := ptw.resp.fire() 1408744445eSMaxpicca-Li resp(i).bits.memidx := RegNext(req_in(i).bits.memidx) 1416d5ddbceSLemover 14203efd994Shappy-lx val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W)))) 14303efd994Shappy-lx val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 14403efd994Shappy-lx 14503efd994Shappy-lx for (d <- 0 until nRespDups) { 14603efd994Shappy-lx ppn(d) := Mux(p_hit, p_ppn, e_ppn(d)) 14703efd994Shappy-lx perm(d) := Mux(p_hit, p_perm, e_perm(d)) 14803efd994Shappy-lx 14903efd994Shappy-lx val paddr = Cat(ppn(d), get_off(req_out(i).vaddr)) 150292bea3fSWilliam Wang resp(i).bits.paddr(d) := Mux(enable, paddr, vaddr) 15103efd994Shappy-lx } 15203efd994Shappy-lx 15303efd994Shappy-lx XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n") 15403efd994Shappy-lx 155292bea3fSWilliam Wang val pmp_paddr = Mux(enable, Cat(Mux(p_hit, p_ppn, e_super_ppn), get_off(req_out(i).vaddr)), vaddr) 156f1fe8698SLemover // pmp_paddr seems same to paddr functionally. It abandons normal_ppn for timing optimization. 157292bea3fSWilliam Wang // val pmp_paddr = Mux(enable, paddr, vaddr) 158292bea3fSWilliam Wang val static_pm_valid = !(e_super_hit || p_hit) && enable && q.partialStaticPMP.B 159f1fe8698SLemover 160f1fe8698SLemover (hit, miss, pmp_paddr, static_pm, static_pm_valid, perm) 161f1fe8698SLemover } 162f1fe8698SLemover 163f1fe8698SLemover def pmp_check(addr: UInt, size: UInt, cmd: UInt, idx: Int): Unit = { 164f1fe8698SLemover pmp(idx).valid := resp(idx).valid 165f1fe8698SLemover pmp(idx).bits.addr := addr 166f1fe8698SLemover pmp(idx).bits.size := size 167f1fe8698SLemover pmp(idx).bits.cmd := cmd 168f1fe8698SLemover } 169f1fe8698SLemover 17003efd994Shappy-lx def perm_check(perm: TlbPermBundle, cmd: UInt, spm: TlbPMBundle, spm_v: Bool, idx: Int, nDups: Int) = { 1715b7ef044SLemover // for timing optimization, pmp check is divided into dynamic and static 1725b7ef044SLemover // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done 1735b7ef044SLemover // static: 4K pages (or sram entries) -> check pmp with pre-checked results 174f1fe8698SLemover val af = perm.af 175f1fe8698SLemover val pf = perm.pf 176f1fe8698SLemover val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception 177f1fe8698SLemover val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception 178f1fe8698SLemover val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception 179f1fe8698SLemover val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!io.csr.priv.sum || ifecth)) 180f1fe8698SLemover val ldPermFail = !(modeCheck && (perm.r || io.csr.priv.mxr && perm.x)) 181a79fef67Swakafa val stPermFail = !(modeCheck && perm.w) 182a79fef67Swakafa val instrPermFail = !(modeCheck && perm.x) 183f1fe8698SLemover val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd)) 184f1fe8698SLemover val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 185f1fe8698SLemover val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd) 186292bea3fSWilliam Wang val fault_valid = portTranslateEnable(idx) 18703efd994Shappy-lx resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && fault_valid && !af 18803efd994Shappy-lx resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && fault_valid && !af 18903efd994Shappy-lx resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && fault_valid && !af 190b6982e83SLemover // NOTE: pf need && with !af, page fault has higher priority than access fault 191b6982e83SLemover // but ptw may also have access fault, then af happens, the translation is wrong. 192b6982e83SLemover // In this case, pf has lower priority than af 1936d5ddbceSLemover 19403efd994Shappy-lx resp(idx).bits.excp(nDups).af.ld := (af || (spm_v && !spm.r)) && TlbCmd.isRead(cmd) && fault_valid 19503efd994Shappy-lx resp(idx).bits.excp(nDups).af.st := (af || (spm_v && !spm.w)) && TlbCmd.isWrite(cmd) && fault_valid 19603efd994Shappy-lx resp(idx).bits.excp(nDups).af.instr := (af || (spm_v && !spm.x)) && TlbCmd.isExec(cmd) && fault_valid 197f1fe8698SLemover resp(idx).bits.static_pm.valid := spm_v && fault_valid // ls/st unit should use this mmio, not the result from pmp 198f1fe8698SLemover resp(idx).bits.static_pm.bits := !spm.c 1996d5ddbceSLemover } 2006d5ddbceSLemover 201f1fe8698SLemover def handle_nonblock(idx: Int): Unit = { 202f1fe8698SLemover io.requestor(idx).resp.valid := req_out_v(idx) 203f1fe8698SLemover io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true 2049930e66fSLemover XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B") 205cb8f2f2aSLemover 206*63632028SHaoyuan 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) 207cb8f2f2aSLemover io.ptw.req(idx).valid := RegNext(req_out_v(idx) && missVec(idx) && !ptw_just_back, false.B) // TODO: remove the regnext, timing 208c3b763d0SYinan Xu when (RegEnable(io.requestor(idx).req_kill, RegNext(io.requestor(idx).req.fire))) { 209c3b763d0SYinan Xu io.ptw.req(idx).valid := false.B 210c3b763d0SYinan Xu } 211f1fe8698SLemover io.ptw.req(idx).bits.vpn := RegNext(get_pn(req_out(idx).vaddr)) 2128744445eSMaxpicca-Li io.ptw.req(idx).bits.memidx := RegNext(req_out(idx).memidx) 213149086eaSLemover } 214a0301c0dSLemover 215f1fe8698SLemover def handle_block(idx: Int): Unit = { 216f1fe8698SLemover // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid 217f1fe8698SLemover io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire() 218f1fe8698SLemover // req_out_v for if there is a request, may long latency, fixme 219f1fe8698SLemover 220f1fe8698SLemover // miss request entries 221f1fe8698SLemover val miss_req_vpn = get_pn(req_out(idx).vaddr) 2228744445eSMaxpicca-Li val miss_req_memidx = req_out(idx).memidx 223*63632028SHaoyuan Feng val hit = io.ptw.resp.bits.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid 224f1fe8698SLemover 225f1fe8698SLemover val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B) 226f1fe8698SLemover val miss_wire = new_coming && missVec(idx) 227f1fe8698SLemover val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire(), flush_pipe(idx)) 228f1fe8698SLemover val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe), 229f1fe8698SLemover io.ptw.req(idx).fire() || resp(idx).fire(), flush_pipe(idx)) 230f1fe8698SLemover 231f1fe8698SLemover // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu 232292bea3fSWilliam Wang resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx)) 233292bea3fSWilliam Wang when (io.ptw.resp.fire() && hit && req_out_v(idx) && portTranslateEnable(idx)) { 234f1fe8698SLemover val pte = io.ptw.resp.bits 235f1fe8698SLemover resp(idx).valid := true.B 236f1fe8698SLemover resp(idx).bits.miss := false.B // for blocked tlb, this is useless 23703efd994Shappy-lx for (d <- 0 until nRespDups) { 238*63632028SHaoyuan Feng resp(idx).bits.paddr(d) := Cat(pte.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 23903efd994Shappy-lx perm_check(pte, req_out(idx).cmd, 0.U.asTypeOf(new TlbPMBundle), false.B, idx, d) 24003efd994Shappy-lx } 24103efd994Shappy-lx pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, idx) 242f1fe8698SLemover 243f1fe8698SLemover // NOTE: the unfiltered req would be handled by Repeater 244f1fe8698SLemover } 245f1fe8698SLemover assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must") 246f1fe8698SLemover 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") 247f1fe8698SLemover 248f1fe8698SLemover val ptw_req = io.ptw.req(idx) 249f1fe8698SLemover ptw_req.valid := miss_req_v 250f1fe8698SLemover ptw_req.bits.vpn := miss_req_vpn 2518744445eSMaxpicca-Li ptw_req.bits.memidx := miss_req_memidx 252f1fe8698SLemover 253f1fe8698SLemover // NOTE: when flush pipe, tlb should abandon last req 254f1fe8698SLemover // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp 255f1fe8698SLemover // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it. 256f1fe8698SLemover if (!q.outsideRecvFlush) { 257292bea3fSWilliam Wang when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) { 258f1fe8698SLemover resp(idx).valid := true.B 25903efd994Shappy-lx for (d <- 0 until nRespDups) { 26003efd994Shappy-lx resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr 26103efd994Shappy-lx resp(idx).bits.excp(d).pf.st := true.B 26203efd994Shappy-lx resp(idx).bits.excp(d).pf.instr := true.B 26303efd994Shappy-lx } 264f1fe8698SLemover } 265f1fe8698SLemover } 266f1fe8698SLemover } 267cb8f2f2aSLemover 268cb8f2f2aSLemover // when ptw resp, tlb at refill_idx maybe set to miss by force. 269cb8f2f2aSLemover // Bypass ptw resp to check. 270cb8f2f2aSLemover def ptw_resp_bypass(vpn: UInt) = { 271*63632028SHaoyuan Feng val p_hit = RegNext(ptw.resp.bits.hit(vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.fire) 272*63632028SHaoyuan Feng val p_ppn = RegEnable(ptw.resp.bits.genPPN(vpn), io.ptw.resp.fire) 273cb8f2f2aSLemover val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits), io.ptw.resp.fire) 274cb8f2f2aSLemover (p_hit, p_ppn, p_perm) 275cb8f2f2aSLemover } 276cb8f2f2aSLemover 277f1fe8698SLemover // assert 278f1fe8698SLemover for(i <- 0 until Width) { 279f1fe8698SLemover TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.") 280149086eaSLemover } 281a0301c0dSLemover 282f1fe8698SLemover // perf event 283f1fe8698SLemover val result_ok = req_in.map(a => RegNext(a.fire())) 284f1fe8698SLemover val perfEvents = 285f1fe8698SLemover Seq( 286292bea3fSWilliam Wang ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire() else portTranslateEnable(i) && result_ok(i) })), 287292bea3fSWilliam Wang ("miss ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && result_ok(i) && missVec(i) else ptw.req(i).fire() })), 288a0301c0dSLemover ) 289f1fe8698SLemover generatePerfEvent() 290a0301c0dSLemover 291f1fe8698SLemover // perf log 2926d5ddbceSLemover for (i <- 0 until Width) { 293f1fe8698SLemover if (Block(i)) { 294292bea3fSWilliam Wang XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i)) 295f1fe8698SLemover XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i)) 2966d5ddbceSLemover } else { 297292bea3fSWilliam Wang XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegNext(req(i).bits.debug.isFirstIssue)) 298292bea3fSWilliam Wang XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i)) 299292bea3fSWilliam Wang XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue)) 300292bea3fSWilliam Wang XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i)) 301a0301c0dSLemover } 3026d5ddbceSLemover } 3036d5ddbceSLemover XSPerfAccumulate("ptw_resp_count", ptw.resp.fire()) 3046d5ddbceSLemover XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire() && ptw.resp.bits.pf) 3056d5ddbceSLemover 3066d5ddbceSLemover // Log 3076d5ddbceSLemover for(i <- 0 until Width) { 3086d5ddbceSLemover XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n") 3096d5ddbceSLemover XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n") 3106d5ddbceSLemover } 3116d5ddbceSLemover 312f1fe8698SLemover XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n") 313f1fe8698SLemover XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n") 3146d5ddbceSLemover for (i <- ptw.req.indices) { 31592e3bfefSLemover XSDebug(ptw.req(i).fire(), p"L2TLB req:${ptw.req(i).bits}\n") 3166d5ddbceSLemover } 31792e3bfefSLemover XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n") 3186d5ddbceSLemover 319a0301c0dSLemover println(s"${q.name}: normal page: ${q.normalNWays} ${q.normalAssociative} ${q.normalReplacer.get} super page: ${q.superNWays} ${q.superAssociative} ${q.superReplacer.get}") 320a0301c0dSLemover 3215ab1b84dSHaoyuan Feng if (env.EnableDifftest) { 3225ab1b84dSHaoyuan Feng val l1tlbid = Wire(UInt(2.W)) 3235ab1b84dSHaoyuan Feng if (q.name == "itlb") { 3245ab1b84dSHaoyuan Feng l1tlbid := 0.U 3255ab1b84dSHaoyuan Feng } else if (q.name == "ldtlb") { 3265ab1b84dSHaoyuan Feng l1tlbid := 1.U 327c8309e8aSHaoyuan Feng } else if (q.name == "sttlb") { 3285ab1b84dSHaoyuan Feng l1tlbid := 2.U 329c8309e8aSHaoyuan Feng } else { 330c8309e8aSHaoyuan Feng l1tlbid := 3.U 3315ab1b84dSHaoyuan Feng } 3325ab1b84dSHaoyuan Feng 3335ab1b84dSHaoyuan Feng for (i <- 0 until Width) { 3345ab1b84dSHaoyuan 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 3355ab1b84dSHaoyuan 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 3365ab1b84dSHaoyuan Feng val difftest = Module(new DifftestL1TLBEvent) 3375ab1b84dSHaoyuan Feng difftest.io.clock := clock 3385ab1b84dSHaoyuan Feng difftest.io.coreid := p(XSCoreParamsKey).HartId.asUInt 339c8309e8aSHaoyuan Feng difftest.io.valid := l1tlbid =/= 3.U && 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) 3405ab1b84dSHaoyuan Feng difftest.io.index := i.U 3415ab1b84dSHaoyuan Feng difftest.io.l1tlbid := l1tlbid 3425ab1b84dSHaoyuan Feng difftest.io.satp := io.csr.satp.ppn 3435ab1b84dSHaoyuan Feng difftest.io.vpn := RegNext(get_pn(req_in(i).bits.vaddr)) 3445ab1b84dSHaoyuan Feng difftest.io.ppn := get_pn(io.requestor(i).resp.bits.paddr(0)) 3455ab1b84dSHaoyuan Feng } 3465ab1b84dSHaoyuan Feng } 3475ab1b84dSHaoyuan Feng 348f1fe8698SLemover} 3491ca0e4f3SYinan Xu 35003efd994Shappy-lxclass TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q) 35103efd994Shappy-lxclass TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q) 3526d5ddbceSLemover 353a0301c0dSLemoverclass TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule { 354a0301c0dSLemover val io = IO(new TlbReplaceIO(Width, q)) 355a0301c0dSLemover 356a0301c0dSLemover if (q.normalAssociative == "fa") { 357a0301c0dSLemover val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays) 3583889e11eSLemover re.access(io.normalPage.access.map(_.touch_ways)) 359a0301c0dSLemover io.normalPage.refillIdx := re.way 360a0301c0dSLemover } else { // set-acco && plru 361a0301c0dSLemover val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays) 3623889e11eSLemover re.access(io.normalPage.access.map(_.sets), io.normalPage.access.map(_.touch_ways)) 363a0301c0dSLemover io.normalPage.refillIdx := { if (q.normalNWays == 1) 0.U else re.way(io.normalPage.chosen_set) } 364a0301c0dSLemover } 365a0301c0dSLemover 366a0301c0dSLemover if (q.superAssociative == "fa") { 367a0301c0dSLemover val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays) 3683889e11eSLemover re.access(io.superPage.access.map(_.touch_ways)) 369a0301c0dSLemover io.superPage.refillIdx := re.way 370a0301c0dSLemover } else { // set-acco && plru 371a0301c0dSLemover val re = ReplacementPolicy.fromString(q.superReplacer, q.superNSets, q.superNWays) 3723889e11eSLemover re.access(io.superPage.access.map(_.sets), io.superPage.access.map(_.touch_ways)) 373a0301c0dSLemover io.superPage.refillIdx := { if (q.superNWays == 1) 0.U else re.way(io.superPage.chosen_set) } 374a0301c0dSLemover } 375a0301c0dSLemover} 376