1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.cache.mmu 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.util.SRAMAnnotation 24import xiangshan._ 25import utils._ 26import utility._ 27import xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig} 28import xiangshan.backend.rob.RobPtr 29import xiangshan.backend.fu.util.HasCSRConst 30import freechips.rocketchip.rocket.PMPConfig 31 32/** TLB module 33 * support block request and non-block request io at the same time 34 * return paddr at next cycle, then go for pmp/pma check 35 * @param Width: The number of requestors 36 * @param Block: Blocked or not for each requestor ports 37 * @param q: TLB Parameters, like entry number, each TLB has its own parameters 38 * @param p: XiangShan Paramemters, like XLEN 39 */ 40 41class TLB(Width: Int, nRespDups: Int = 1, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule 42 with HasCSRConst 43 with HasPerfEvents 44{ 45 val io = IO(new TlbIO(Width, nRespDups, q)) 46 47 val req = io.requestor.map(_.req) 48 val resp = io.requestor.map(_.resp) 49 val ptw = io.ptw 50 val pmp = io.pmp 51 val refill_to_mem = io.refill_to_mem 52 53 /** Sfence.vma & Svinval 54 * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe 55 * Svinval will 1. flush old entries 2. flush inflight 56 * So, Svinval will not flush pipe, which means 57 * it should not drop reqs from pipe and should return right resp 58 */ 59 val sfence = DelayN(io.sfence, q.fenceDelay) 60 val csr = io.csr 61 val satp = DelayN(io.csr.satp, q.fenceDelay) 62 val vsatp = DelayN(io.csr.vsatp, q.fenceDelay) 63 val hgatp = DelayN(io.csr.hgatp, q.fenceDelay) 64 val mPBMTE = DelayN(io.csr.mPBMTE, q.fenceDelay) 65 val hPBMTE = DelayN(io.csr.hPBMTE, q.fenceDelay) 66 67 val flush_mmu = DelayN(sfence.valid || csr.satp.changed || csr.vsatp.changed || csr.hgatp.changed, q.fenceDelay) 68 val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe 69 val flush_pipe = io.flushPipe 70 val redirect = io.redirect 71 val req_in = req 72 val req_out = req.map(a => RegEnable(a.bits, a.fire)) 73 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))) 74 75 val isHyperInst = (0 until Width).map(i => req_out_v(i) && req_out(i).hyperinst) 76 77 // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush. 78 // because, csr will influence tlb behavior. 79 val ifecth = if (q.fetchi) true.B else false.B 80 val mode_tmp = if (q.useDmode) csr.priv.dmode else csr.priv.imode 81 val mode = (0 until Width).map(i => Mux(isHyperInst(i), csr.priv.spvp, mode_tmp)) 82 val virt_in = csr.priv.virt 83 val virt_out = req.map(a => RegEnable(csr.priv.virt, a.fire)) 84 val sum = (0 until Width).map(i => Mux(virt_out(i) || isHyperInst(i), io.csr.priv.vsum, io.csr.priv.sum)) 85 val mxr = (0 until Width).map(i => Mux(virt_out(i) || isHyperInst(i), io.csr.priv.vmxr || io.csr.priv.mxr, io.csr.priv.mxr)) 86 val req_in_s2xlate = (0 until Width).map(i => MuxCase(noS2xlate, Seq( 87 (!(virt_in || req_in(i).bits.hyperinst)) -> noS2xlate, 88 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 89 (csr.vsatp.mode === 0.U) -> onlyStage2, 90 (csr.hgatp.mode === 0.U) -> onlyStage1 91 ))) 92 val req_out_s2xlate = (0 until Width).map(i => MuxCase(noS2xlate, Seq( 93 (!(virt_out(i) || isHyperInst(i))) -> noS2xlate, 94 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 95 (csr.vsatp.mode === 0.U) -> onlyStage2, 96 (csr.hgatp.mode === 0.U) -> onlyStage1 97 ))) 98 val need_gpa = RegInit(false.B) 99 val need_gpa_robidx = Reg(new RobPtr) 100 val need_gpa_vpn = Reg(UInt(vpnLen.W)) 101 val resp_gpa_gvpn = Reg(UInt(ptePPNLen.W)) 102 val resp_gpa_refill = RegInit(false.B) 103 val resp_s1_level = RegInit(0.U(log2Up(Level + 1).W)) 104 val resp_s1_isLeaf = RegInit(false.B) 105 val resp_s1_isFakePte = RegInit(false.B) 106 val hasGpf = Wire(Vec(Width, Bool())) 107 108 val Sv39Enable = satp.mode === 8.U 109 val Sv48Enable = satp.mode === 9.U 110 val Sv39x4Enable = vsatp.mode === 8.U || hgatp.mode === 8.U 111 val Sv48x4Enable = vsatp.mode === 9.U || hgatp.mode === 9.U 112 val vmEnable = (0 until Width).map(i => !(isHyperInst(i) || virt_out(i)) && ( 113 if (EnbaleTlbDebug) (Sv39Enable || Sv48Enable) 114 else (Sv39Enable || Sv48Enable) && (mode(i) < ModeM)) 115 ) 116 val s2xlateEnable = (0 until Width).map(i => (isHyperInst(i) || virt_out(i)) && (Sv39x4Enable || Sv48x4Enable) && (mode(i) < ModeM)) 117 val portTranslateEnable = (0 until Width).map(i => (vmEnable(i) || s2xlateEnable(i)) && RegEnable(!req(i).bits.no_translate, req(i).valid)) 118 119 120 val refill = ptw.resp.fire && !(ptw.resp.bits.getGpa) && !flush_mmu 121 refill_to_mem := DontCare 122 val entries = Module(new TlbStorageWrapper(Width, q, nRespDups)) 123 entries.io.base_connect(sfence, csr, satp) 124 if (q.outReplace) { io.replace <> entries.io.replace } 125 for (i <- 0 until Width) { 126 entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i, req_in_s2xlate(i)) 127 entries.io.w_apply(refill, ptw.resp.bits) 128 // TODO: RegNext enable:req.valid 129 resp(i).bits.debug.isFirstIssue := RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid) 130 resp(i).bits.debug.robIdx := RegEnable(req(i).bits.debug.robIdx, req(i).valid) 131 } 132 133 // read TLB, get hit/miss, paddr, perm bits 134 val readResult = (0 until Width).map(TLBRead(_)) 135 val hitVec = readResult.map(_._1) 136 val missVec = readResult.map(_._2) 137 val pmp_addr = readResult.map(_._3) 138 val perm = readResult.map(_._4) 139 val g_perm = readResult.map(_._5) 140 val pbmt = readResult.map(_._6) 141 val g_pbmt = readResult.map(_._7) 142 // check pmp use paddr (for timing optization, use pmp_addr here) 143 // check permisson 144 (0 until Width).foreach{i => 145 val noTranslateReg = RegNext(req(i).bits.no_translate) 146 val addr = Mux(noTranslateReg, req(i).bits.pmp_addr, pmp_addr(i)) 147 pmp_check(addr, req_out(i).size, req_out(i).cmd, noTranslateReg, i) 148 for (d <- 0 until nRespDups) { 149 pbmt_check(i, d, pbmt(i)(d), g_pbmt(i)(d), req_out_s2xlate(i)) 150 perm_check(perm(i)(d), req_out(i).cmd, i, d, g_perm(i)(d), req_out(i).hlvx, req_out_s2xlate(i)) 151 } 152 hasGpf(i) := resp(i).bits.excp(0).gpf.ld || resp(i).bits.excp(0).gpf.st || resp(i).bits.excp(0).gpf.instr 153 } 154 155 // handle block or non-block io 156 // for non-block io, just return the above result, send miss to ptw 157 // for block io, hold the request, send miss to ptw, 158 // when ptw back, return the result 159 (0 until Width) foreach {i => 160 if (Block(i)) handle_block(i) 161 else handle_nonblock(i) 162 } 163 io.ptw.resp.ready := true.B 164 165 /************************ main body above | method/log/perf below ****************************/ 166 def TLBRead(i: Int) = { 167 val (e_hit, e_ppn, e_perm, e_g_perm, e_s2xlate, e_pbmt, e_g_pbmt) = entries.io.r_resp_apply(i) 168 val (p_hit, p_ppn, p_pbmt, p_perm, p_gvpn, p_g_pbmt, p_g_perm, p_s2xlate, p_s1_level, p_s1_isLeaf, p_s1_isFakePte) = ptw_resp_bypass(get_pn(req_in(i).bits.vaddr), req_in_s2xlate(i)) 169 val enable = portTranslateEnable(i) 170 val isOnlys2xlate = req_out_s2xlate(i) === onlyStage2 171 val need_gpa_vpn_hit = need_gpa_vpn === get_pn(req_out(i).vaddr) 172 val isitlb = TlbCmd.isExec(req_out(i).cmd) 173 174 when (!isitlb && need_gpa_robidx.needFlush(redirect) || isitlb && flush_pipe(i)){ 175 need_gpa := false.B 176 resp_gpa_refill := false.B 177 need_gpa_vpn := 0.U 178 }.elsewhen (req_out_v(i) && !p_hit && !(resp_gpa_refill && need_gpa_vpn_hit) && !isOnlys2xlate && hasGpf(i) && need_gpa === false.B && !io.requestor(i).req_kill) { 179 need_gpa := true.B 180 need_gpa_vpn := get_pn(req_out(i).vaddr) 181 resp_gpa_refill := false.B 182 need_gpa_robidx := req_out(i).debug.robIdx 183 }.elsewhen (ptw.resp.fire && need_gpa && need_gpa_vpn === ptw.resp.bits.getVpn(need_gpa_vpn)) { 184 resp_gpa_gvpn := Mux(ptw.resp.bits.s2xlate === onlyStage2, ptw.resp.bits.s2.entry.tag, ptw.resp.bits.s1.genPPN(need_gpa_vpn)) 185 resp_s1_level := ptw.resp.bits.s1.entry.level.get 186 resp_s1_isLeaf := ptw.resp.bits.s1.isLeaf() 187 resp_s1_isFakePte := ptw.resp.bits.s1.isFakePte() 188 resp_gpa_refill := true.B 189 } 190 191 when (req_out_v(i) && hasGpf(i) && resp_gpa_refill && need_gpa_vpn_hit ){ 192 need_gpa := false.B 193 } 194 195 TimeOutAssert(need_gpa && !resp_gpa_refill, timeOutThreshold, s"port${i} need gpa long time not refill.") 196 197 val hit = e_hit || p_hit 198 val miss = (!hit && enable) || hasGpf(i) && !p_hit && !(resp_gpa_refill && need_gpa_vpn_hit) && !isOnlys2xlate 199 hit.suggestName(s"hit_read_${i}") 200 miss.suggestName(s"miss_read_${i}") 201 202 val vaddr = SignExt(req_out(i).vaddr, PAddrBits) 203 resp(i).bits.miss := miss 204 resp(i).bits.ptwBack := ptw.resp.fire 205 resp(i).bits.memidx := RegEnable(req_in(i).bits.memidx, req_in(i).valid) 206 resp(i).bits.fastMiss := !hit && enable 207 208 val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W)))) 209 val pbmt = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ptePbmtLen.W)))) 210 val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 211 val gvpn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(vpnLen.W)))) 212 val level = WireInit(VecInit(Seq.fill(nRespDups)(0.U(log2Up(Level + 1).W)))) 213 val isLeaf = WireInit(VecInit(Seq.fill(nRespDups)(false.B))) 214 val isFakePte = WireInit(VecInit(Seq.fill(nRespDups)(false.B))) 215 val g_pbmt = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ptePbmtLen.W)))) 216 val g_perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 217 val r_s2xlate = WireInit(VecInit(Seq.fill(nRespDups)(0.U(2.W)))) 218 for (d <- 0 until nRespDups) { 219 ppn(d) := Mux(p_hit, p_ppn, e_ppn(d)) 220 pbmt(d) := Mux(p_hit, p_pbmt, e_pbmt(d)) 221 perm(d) := Mux(p_hit, p_perm, e_perm(d)) 222 gvpn(d) := Mux(p_hit, p_gvpn, resp_gpa_gvpn) 223 level(d) := Mux(p_hit, p_s1_level, resp_s1_level) 224 isLeaf(d) := Mux(p_hit, p_s1_isLeaf, resp_s1_isLeaf) 225 isFakePte(d) := Mux(p_hit, p_s1_isFakePte, resp_s1_isFakePte) 226 g_pbmt(d) := Mux(p_hit, p_g_pbmt, e_g_pbmt(d)) 227 g_perm(d) := Mux(p_hit, p_g_perm, e_g_perm(d)) 228 r_s2xlate(d) := Mux(p_hit, p_s2xlate, e_s2xlate(d)) 229 val paddr = Cat(ppn(d), get_off(req_out(i).vaddr)) 230 val vpn_idx = Mux1H(Seq( 231 (isFakePte(d) && vsatp.mode === Sv39) -> 2.U, 232 (isFakePte(d) && vsatp.mode === Sv48) -> 3.U, 233 (!isFakePte(d)) -> (level(d) - 1.U), 234 )) 235 val gpaddr_offset = Mux(isLeaf(d), get_off(req_out(i).vaddr), Cat(getVpnn(get_pn(req_out(i).vaddr), vpn_idx), 0.U(log2Up(XLEN/8).W))) 236 val gpaddr = Cat(gvpn(d), gpaddr_offset) 237 resp(i).bits.paddr(d) := Mux(enable, paddr, vaddr) 238 resp(i).bits.gpaddr(d) := Mux(r_s2xlate(d) === onlyStage2, vaddr, gpaddr) 239 } 240 241 XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n") 242 243 val pmp_paddr = resp(i).bits.paddr(0) 244 245 (hit, miss, pmp_paddr, perm, g_perm, pbmt, g_pbmt) 246 } 247 248 def getVpnn(vpn: UInt, idx: UInt): UInt = { 249 MuxLookup(idx, 0.U)(Seq( 250 0.U -> vpn(vpnnLen - 1, 0), 251 1.U -> vpn(vpnnLen * 2 - 1, vpnnLen), 252 2.U -> vpn(vpnnLen * 3 - 1, vpnnLen * 2), 253 3.U -> vpn(vpnnLen * 4 - 1, vpnnLen * 3)) 254 ) 255 } 256 257 def pmp_check(addr: UInt, size: UInt, cmd: UInt, noTranslate: Bool, idx: Int): Unit = { 258 pmp(idx).valid := resp(idx).valid || noTranslate 259 pmp(idx).bits.addr := addr 260 pmp(idx).bits.size := size 261 pmp(idx).bits.cmd := cmd 262 } 263 264 def pbmt_check(idx: Int, d: Int, pbmt: UInt, g_pbmt: UInt, s2xlate: UInt):Unit = { 265 val onlyS1 = s2xlate === onlyStage1 || s2xlate === noS2xlate 266 val pbmtRes = Mux(hPBMTE, pbmt, 0.U) 267 val gpbmtRes = Mux(mPBMTE, g_pbmt, 0.U) 268 val res = MuxLookup(s2xlate, 0.U)(Seq( 269 onlyStage1 -> pbmtRes, 270 onlyStage2 -> gpbmtRes, 271 allStage -> Mux(pbmtRes =/= 0.U, pbmtRes, gpbmtRes), 272 noS2xlate -> pbmtRes 273 )) 274 resp(idx).bits.pbmt(d) := Mux(portTranslateEnable(idx), res, 0.U) 275 } 276 277 // for timing optimization, pmp check is divided into dynamic and static 278 def perm_check(perm: TlbPermBundle, cmd: UInt, idx: Int, nDups: Int, g_perm: TlbPermBundle, hlvx: Bool, s2xlate: UInt) = { 279 // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done 280 // static: 4K pages (or sram entries) -> check pmp with pre-checked results 281 val hasS2xlate = s2xlate =/= noS2xlate 282 val onlyS1 = s2xlate === onlyStage1 283 val onlyS2 = s2xlate === onlyStage2 284 val af = perm.af || (hasS2xlate && g_perm.af) 285 286 // Stage 1 perm check 287 val pf = perm.pf 288 val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception 289 val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception 290 val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception 291 val modeCheck = !(mode(idx) === ModeU && !perm.u || mode(idx) === ModeS && perm.u && (!sum(idx) || ifecth)) 292 val ldPermFail = !(modeCheck && Mux(hlvx, perm.x, perm.r || mxr(idx) && perm.x)) 293 val stPermFail = !(modeCheck && perm.w) 294 val instrPermFail = !(modeCheck && perm.x) 295 val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd)) 296 val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 297 val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd) 298 val s1_valid = portTranslateEnable(idx) && !onlyS2 299 300 // Stage 2 perm check 301 val gpf = g_perm.pf 302 val g_ldUpdate = !g_perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) 303 val g_stUpdate = (!g_perm.a || !g_perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 304 val g_instrUpdate = !g_perm.a && TlbCmd.isExec(cmd) 305 val g_ldPermFail = !Mux(hlvx, g_perm.x, (g_perm.r || io.csr.priv.mxr && g_perm.x)) 306 val g_stPermFail = !g_perm.w 307 val g_instrPermFail = !g_perm.x 308 val ldGpf = (g_ldPermFail || gpf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd)) 309 val stGpf = (g_stPermFail || gpf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 310 val instrGpf = (g_instrPermFail || gpf) && TlbCmd.isExec(cmd) 311 val s2_valid = hasS2xlate && !onlyS1 && portTranslateEnable(idx) 312 313 val fault_valid = s1_valid || s2_valid 314 315 // when pf and gpf can't happens simultaneously 316 val hasPf = (ldPf || ldUpdate || stPf || stUpdate || instrPf || instrUpdate) && s1_valid && !af 317 resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && s1_valid && !af 318 resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && s1_valid && !af 319 resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && s1_valid && !af 320 // NOTE: pf need && with !af, page fault has higher priority than access fault 321 // but ptw may also have access fault, then af happens, the translation is wrong. 322 // In this case, pf has lower priority than af 323 324 resp(idx).bits.excp(nDups).gpf.ld := (ldGpf || g_ldUpdate) && s2_valid && !af && !hasPf 325 resp(idx).bits.excp(nDups).gpf.st := (stGpf || g_stUpdate) && s2_valid && !af && !hasPf 326 resp(idx).bits.excp(nDups).gpf.instr := (instrGpf || g_instrUpdate) && s2_valid && !af && !hasPf 327 328 resp(idx).bits.excp(nDups).af.ld := af && TlbCmd.isRead(cmd) && fault_valid 329 resp(idx).bits.excp(nDups).af.st := af && TlbCmd.isWrite(cmd) && fault_valid 330 resp(idx).bits.excp(nDups).af.instr := af && TlbCmd.isExec(cmd) && fault_valid 331 332 333 } 334 335 def handle_nonblock(idx: Int): Unit = { 336 io.requestor(idx).resp.valid := req_out_v(idx) 337 io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true 338 XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B") 339 340 val req_need_gpa = hasGpf(idx) 341 val req_s2xlate = Wire(UInt(2.W)) 342 req_s2xlate := MuxCase(noS2xlate, Seq( 343 (!(virt_out(idx) || req_out(idx).hyperinst)) -> noS2xlate, 344 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 345 (csr.vsatp.mode === 0.U) -> onlyStage2, 346 (csr.hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 347 )) 348 349 val ptw_just_back = ptw.resp.fire && req_s2xlate === ptw.resp.bits.s2xlate && ptw.resp.bits.hit(get_pn(req_out(idx).vaddr), io.csr.satp.asid, io.csr.vsatp.asid, io.csr.hgatp.vmid, true, false) 350 // TODO: RegNext enable: ptw.resp.valid ? req.valid 351 val ptw_resp_bits_reg = RegEnable(ptw.resp.bits, ptw.resp.valid) 352 val ptw_already_back = GatedValidRegNext(ptw.resp.fire) && req_s2xlate === ptw_resp_bits_reg.s2xlate && ptw_resp_bits_reg.hit(get_pn(req_out(idx).vaddr), io.csr.satp.asid, io.csr.vsatp.asid, io.csr.hgatp.vmid, allType = true) 353 val ptw_getGpa = req_need_gpa && hitVec(idx) 354 val need_gpa_vpn_hit = need_gpa_vpn === get_pn(req_out(idx).vaddr) 355 io.ptw.req(idx).valid := req_out_v(idx) && missVec(idx) && !(ptw_just_back || ptw_already_back || (!need_gpa_vpn_hit && req_out_v(idx) && need_gpa && !resp_gpa_refill && ptw_getGpa)) // TODO: remove the regnext, timing 356 io.tlbreplay(idx) := req_out_v(idx) && missVec(idx) && (ptw_just_back || ptw_already_back || (!need_gpa_vpn_hit && req_out_v(idx) && need_gpa && !resp_gpa_refill && ptw_getGpa)) 357 when (io.requestor(idx).req_kill && GatedValidRegNext(io.requestor(idx).req.fire)) { 358 io.ptw.req(idx).valid := false.B 359 io.tlbreplay(idx) := true.B 360 } 361 io.ptw.req(idx).bits.vpn := get_pn(req_out(idx).vaddr) 362 io.ptw.req(idx).bits.s2xlate := req_s2xlate 363 io.ptw.req(idx).bits.getGpa := ptw_getGpa 364 io.ptw.req(idx).bits.memidx := req_out(idx).memidx 365 } 366 367 def handle_block(idx: Int): Unit = { 368 // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid 369 io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire 370 // req_out_v for if there is a request, may long latency, fixme 371 372 // miss request entries 373 val req_need_gpa = hasGpf(idx) 374 val miss_req_vpn = get_pn(req_out(idx).vaddr) 375 val miss_req_memidx = req_out(idx).memidx 376 val miss_req_s2xlate = Wire(UInt(2.W)) 377 miss_req_s2xlate := MuxCase(noS2xlate, Seq( 378 (!(virt_out(idx) || req_out(idx).hyperinst)) -> noS2xlate, 379 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 380 (csr.vsatp.mode === 0.U) -> onlyStage2, 381 (csr.hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 382 )) 383 val miss_req_s2xlate_reg = RegEnable(miss_req_s2xlate, io.ptw.req(idx).fire) 384 val hasS2xlate = miss_req_s2xlate_reg =/= noS2xlate 385 val onlyS2 = miss_req_s2xlate_reg === onlyStage2 386 val hit_s1 = io.ptw.resp.bits.s1.hit(miss_req_vpn, Mux(hasS2xlate, io.csr.vsatp.asid, io.csr.satp.asid), io.csr.hgatp.vmid, allType = true, false, hasS2xlate) 387 val hit_s2 = io.ptw.resp.bits.s2.hit(miss_req_vpn, io.csr.hgatp.vmid) 388 val hit = Mux(onlyS2, hit_s2, hit_s1) && io.ptw.resp.valid && miss_req_s2xlate_reg === io.ptw.resp.bits.s2xlate 389 390 val new_coming_valid = WireInit(false.B) 391 new_coming_valid := req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx) 392 val new_coming = GatedValidRegNext(new_coming_valid) 393 val miss_wire = new_coming && missVec(idx) 394 val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire, flush_pipe(idx)) 395 val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe), 396 io.ptw.req(idx).fire || resp(idx).fire, flush_pipe(idx)) 397 398 // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu 399 resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx)) 400 when (io.ptw.resp.fire && hit && req_out_v(idx) && portTranslateEnable(idx)) { 401 val stage1 = io.ptw.resp.bits.s1 402 val stage2 = io.ptw.resp.bits.s2 403 val s2xlate = io.ptw.resp.bits.s2xlate 404 resp(idx).valid := true.B 405 resp(idx).bits.miss := false.B 406 val s1_paddr = Cat(stage1.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 407 val s2_paddr = Cat(stage2.genPPNS2(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 408 for (d <- 0 until nRespDups) { 409 resp(idx).bits.paddr(d) := Mux(s2xlate =/= noS2xlate, s2_paddr, s1_paddr) 410 resp(idx).bits.gpaddr(d) := s1_paddr 411 pbmt_check(idx, d, io.ptw.resp.bits.s1.entry.pbmt, io.ptw.resp.bits.s2.entry.pbmt, s2xlate) 412 perm_check(stage1, req_out(idx).cmd, idx, d, stage2, req_out(idx).hlvx, s2xlate) 413 } 414 pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, false.B, idx) 415 416 // NOTE: the unfiltered req would be handled by Repeater 417 } 418 assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must") 419 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") 420 421 val ptw_req = io.ptw.req(idx) 422 ptw_req.valid := miss_req_v 423 ptw_req.bits.vpn := miss_req_vpn 424 ptw_req.bits.s2xlate := miss_req_s2xlate 425 ptw_req.bits.getGpa := req_need_gpa && hitVec(idx) 426 ptw_req.bits.memidx := miss_req_memidx 427 428 io.tlbreplay(idx) := false.B 429 430 // NOTE: when flush pipe, tlb should abandon last req 431 // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp 432 // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it. 433 if (!q.outsideRecvFlush) { 434 when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) { 435 resp(idx).valid := true.B 436 for (d <- 0 until nRespDups) { 437 resp(idx).bits.pbmt(d) := 0.U 438 resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr 439 resp(idx).bits.excp(d).pf.st := true.B 440 resp(idx).bits.excp(d).pf.instr := true.B 441 } 442 } 443 } 444 } 445 446 // when ptw resp, tlb at refill_idx maybe set to miss by force. 447 // Bypass ptw resp to check. 448 def ptw_resp_bypass(vpn: UInt, s2xlate: UInt) = { 449 // TODO: RegNext enable: ptw.resp.valid 450 val hasS2xlate = s2xlate =/= noS2xlate 451 val onlyS2 = s2xlate === onlyStage2 452 val onlyS1 = s2xlate === onlyStage1 453 val s2xlate_hit = s2xlate === ptw.resp.bits.s2xlate 454 val resp_hit = ptw.resp.bits.hit(vpn, io.csr.satp.asid, io.csr.vsatp.asid, io.csr.hgatp.vmid, true, false) 455 val p_hit = GatedValidRegNext(resp_hit && io.ptw.resp.fire && s2xlate_hit) 456 val ppn_s1 = ptw.resp.bits.s1.genPPN(vpn) 457 val gvpn = Mux(onlyS2, vpn, ppn_s1) 458 val ppn_s2 = ptw.resp.bits.s2.genPPNS2(gvpn) 459 val p_ppn = RegEnable(Mux(s2xlate === onlyStage2 || s2xlate === allStage, ppn_s2, ppn_s1), io.ptw.resp.fire) 460 val p_pbmt = RegEnable(ptw.resp.bits.s1.entry.pbmt,io.ptw.resp.fire) 461 val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits.s1), io.ptw.resp.fire) 462 val p_gvpn = RegEnable(Mux(onlyS2, ptw.resp.bits.s2.entry.tag, ppn_s1), io.ptw.resp.fire) 463 val p_g_pbmt = RegEnable(ptw.resp.bits.s2.entry.pbmt,io.ptw.resp.fire) 464 val p_g_perm = RegEnable(hptwresp_to_tlbperm(ptw.resp.bits.s2), io.ptw.resp.fire) 465 val p_s2xlate = RegEnable(ptw.resp.bits.s2xlate, io.ptw.resp.fire) 466 val p_s1_level = RegEnable(ptw.resp.bits.s1.entry.level.get, io.ptw.resp.fire) 467 val p_s1_isLeaf = RegEnable(ptw.resp.bits.s1.isLeaf(), io.ptw.resp.fire) 468 val p_s1_isFakePte = RegEnable(ptw.resp.bits.s1.isFakePte(), io.ptw.resp.fire) 469 (p_hit, p_ppn, p_pbmt, p_perm, p_gvpn, p_g_pbmt, p_g_perm, p_s2xlate, p_s1_level, p_s1_isLeaf, p_s1_isFakePte) 470 } 471 472 // assert 473 for(i <- 0 until Width) { 474 TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.") 475 } 476 477 // perf event 478 val result_ok = req_in.map(a => GatedValidRegNext(a.fire)) 479 val perfEvents = 480 Seq( 481 ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire else portTranslateEnable(i) && result_ok(i) })), 482 ("miss ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && result_ok(i) && missVec(i) else ptw.req(i).fire })), 483 ) 484 generatePerfEvent() 485 486 // perf log 487 for (i <- 0 until Width) { 488 if (Block(i)) { 489 XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i)) 490 XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i)) 491 } else { 492 XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid)) 493 XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i)) 494 XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid)) 495 XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i)) 496 } 497 } 498 XSPerfAccumulate("ptw_resp_count", ptw.resp.fire) 499 XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire && ptw.resp.bits.s1.pf) 500 501 // Log 502 for(i <- 0 until Width) { 503 XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n") 504 XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n") 505 } 506 507 XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n") 508 XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n") 509 for (i <- ptw.req.indices) { 510 XSDebug(ptw.req(i).fire, p"L2TLB req:${ptw.req(i).bits}\n") 511 } 512 XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n") 513 514 println(s"${q.name}: page: ${q.NWays} ${q.Associative} ${q.Replacer.get}") 515 516 if (env.EnableDifftest) { 517 for (i <- 0 until Width) { 518 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 519 val gpf = io.requestor(i).resp.bits.excp(0).gpf.instr || io.requestor(i).resp.bits.excp(0).gpf.st || io.requestor(i).resp.bits.excp(0).gpf.ld 520 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 521 val difftest = DifftestModule(new DiffL1TLBEvent) 522 difftest.coreid := io.hartId 523 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 && !gpf && portTranslateEnable(i) 524 if (!Seq("itlb", "ldtlb", "sttlb").contains(q.name)) { 525 difftest.valid := false.B 526 } 527 difftest.index := TLBDiffId(p(XSCoreParamsKey).HartId).U 528 difftest.vpn := RegEnable(get_pn(req_in(i).bits.vaddr), req_in(i).valid) 529 difftest.ppn := get_pn(io.requestor(i).resp.bits.paddr(0)) 530 difftest.satp := Cat(io.csr.satp.mode, io.csr.satp.asid, io.csr.satp.ppn) 531 difftest.vsatp := Cat(io.csr.vsatp.mode, io.csr.vsatp.asid, io.csr.vsatp.ppn) 532 difftest.hgatp := Cat(io.csr.hgatp.mode, io.csr.hgatp.vmid, io.csr.hgatp.ppn) 533 val req_need_gpa = gpf 534 val req_s2xlate = Wire(UInt(2.W)) 535 req_s2xlate := MuxCase(noS2xlate, Seq( 536 (!RegNext(virt_in || req_in(i).bits.hyperinst)) -> noS2xlate, 537 (vsatp.mode =/= 0.U && hgatp.mode =/= 0.U) -> allStage, 538 (vsatp.mode === 0.U) -> onlyStage2, 539 (hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 540 )) 541 difftest.s2xlate := req_s2xlate 542 } 543 } 544} 545 546object TLBDiffId { 547 var i: Int = 0 548 var lastHartId: Int = -1 549 def apply(hartId: Int): Int = { 550 if (lastHartId != hartId) { 551 i = 0 552 lastHartId = hartId 553 } 554 i += 1 555 i - 1 556 } 557} 558 559class TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q) 560class TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q) 561 562class TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule { 563 val io = IO(new TlbReplaceIO(Width, q)) 564 565 if (q.Associative == "fa") { 566 val re = ReplacementPolicy.fromString(q.Replacer, q.NWays) 567 re.access(io.page.access.map(_.touch_ways)) 568 io.page.refillIdx := re.way 569 } else { // set-acco && plru 570 val re = ReplacementPolicy.fromString(q.Replacer, q.NSets, q.NWays) 571 re.access(io.page.access.map(_.sets), io.page.access.map(_.touch_ways)) 572 io.page.refillIdx := { if (q.NWays == 1) 0.U else re.way(io.page.chosen_set) } 573 } 574} 575