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* 17* Acknowledgement 18* 19* This implementation is inspired by several key papers: 20* [1] Binh Pham, Viswanathan Vaidyanathan, Aamer Jaleel, and Abhishek Bhattacharjee. "[Colt: Coalesced large-reach 21* tlbs.](https://doi.org/10.1109/MICRO.2012.32)" 45th Annual IEEE/ACM International Symposium on Microarchitecture 22* (MICRO). 2012. 23***************************************************************************************/ 24 25package xiangshan.cache.mmu 26 27import org.chipsalliance.cde.config.Parameters 28import chisel3._ 29import chisel3.util._ 30import difftest._ 31import freechips.rocketchip.util.SRAMAnnotation 32import xiangshan._ 33import utils._ 34import utility._ 35import xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig} 36import xiangshan.backend.rob.RobPtr 37import xiangshan.backend.fu.util.HasCSRConst 38import freechips.rocketchip.rocket.PMPConfig 39 40/** TLB module 41 * support block request and non-block request io at the same time 42 * return paddr at next cycle, then go for pmp/pma check 43 * @param Width: The number of requestors 44 * @param Block: Blocked or not for each requestor ports 45 * @param q: TLB Parameters, like entry number, each TLB has its own parameters 46 * @param p: XiangShan Paramemters, like XLEN 47 */ 48 49class TLB(Width: Int, nRespDups: Int = 1, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule 50 with HasCSRConst 51 with HasPerfEvents 52{ 53 val io = IO(new TlbIO(Width, nRespDups, q)) 54 55 val req = io.requestor.map(_.req) 56 val resp = io.requestor.map(_.resp) 57 val ptw = io.ptw 58 val pmp = io.pmp 59 val refill_to_mem = io.refill_to_mem 60 61 /** Sfence.vma & Svinval 62 * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe 63 * Svinval will 1. flush old entries 2. flush inflight 64 * So, Svinval will not flush pipe, which means 65 * it should not drop reqs from pipe and should return right resp 66 */ 67 val sfence = DelayN(io.sfence, q.fenceDelay) 68 val csr = io.csr 69 val satp = DelayN(io.csr.satp, q.fenceDelay) 70 val vsatp = DelayN(io.csr.vsatp, q.fenceDelay) 71 val hgatp = DelayN(io.csr.hgatp, q.fenceDelay) 72 val mPBMTE = DelayN(io.csr.mPBMTE, q.fenceDelay) 73 val hPBMTE = DelayN(io.csr.hPBMTE, q.fenceDelay) 74 75 val flush_mmu = DelayN(sfence.valid || csr.satp.changed || csr.vsatp.changed || csr.hgatp.changed, q.fenceDelay) 76 val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe 77 val flush_pipe = io.flushPipe 78 val redirect = io.redirect 79 val EffectiveVa = Wire(Vec(Width, UInt(XLEN.W))) 80 val req_in = req 81 val req_out = Reg(Vec(Width, new TlbReq)) 82 for (i <- 0 until Width) { 83 when (req(i).fire) { 84 req_out(i) := req(i).bits 85 req_out(i).fullva := EffectiveVa(i) 86 } 87 } 88 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))) 89 90 val isHyperInst = (0 until Width).map(i => req_out_v(i) && req_out(i).hyperinst) 91 92 // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush. 93 // because, csr will influence tlb behavior. 94 val ifetch = if (q.fetchi) true.B else false.B 95 val mode_tmp = if (q.useDmode) csr.priv.dmode else csr.priv.imode 96 val mode = (0 until Width).map(i => Mux(isHyperInst(i), csr.priv.spvp, mode_tmp)) 97 val virt_in = csr.priv.virt 98 val virt_out = req.map(a => RegEnable(csr.priv.virt, a.fire)) 99 val sum = (0 until Width).map(i => Mux(virt_out(i) || isHyperInst(i), io.csr.priv.vsum, io.csr.priv.sum)) 100 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)) 101 val req_in_s2xlate = (0 until Width).map(i => MuxCase(noS2xlate, Seq( 102 (!(virt_in || req_in(i).bits.hyperinst)) -> noS2xlate, 103 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 104 (csr.vsatp.mode === 0.U) -> onlyStage2, 105 (csr.hgatp.mode === 0.U) -> onlyStage1 106 ))) 107 val req_out_s2xlate = (0 until Width).map(i => MuxCase(noS2xlate, Seq( 108 (!(virt_out(i) || isHyperInst(i))) -> noS2xlate, 109 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 110 (csr.vsatp.mode === 0.U) -> onlyStage2, 111 (csr.hgatp.mode === 0.U) -> onlyStage1 112 ))) 113 val need_gpa = RegInit(false.B) 114 val need_gpa_robidx = Reg(new RobPtr) 115 val need_gpa_vpn = Reg(UInt(vpnLen.W)) 116 val resp_gpa_gvpn = Reg(UInt(ptePPNLen.W)) 117 val resp_gpa_refill = RegInit(false.B) 118 val resp_s1_level = RegInit(0.U(log2Up(Level + 1).W)) 119 val resp_s1_isLeaf = RegInit(false.B) 120 val resp_s1_isFakePte = RegInit(false.B) 121 val hasGpf = Wire(Vec(Width, Bool())) 122 123 val Sv39Enable = satp.mode === 8.U 124 val Sv48Enable = satp.mode === 9.U 125 val Sv39x4Enable = vsatp.mode === 8.U || hgatp.mode === 8.U 126 val Sv48x4Enable = vsatp.mode === 9.U || hgatp.mode === 9.U 127 val vmEnable = (0 until Width).map(i => !(isHyperInst(i) || virt_out(i)) && ( 128 if (EnbaleTlbDebug) (Sv39Enable || Sv48Enable) 129 else (Sv39Enable || Sv48Enable) && (mode(i) < ModeM)) 130 ) 131 val s2xlateEnable = (0 until Width).map(i => (isHyperInst(i) || virt_out(i)) && (Sv39x4Enable || Sv48x4Enable) && (mode(i) < ModeM)) 132 val portTranslateEnable = (0 until Width).map(i => (vmEnable(i) || s2xlateEnable(i)) && RegEnable(!req(i).bits.no_translate, req(i).valid)) 133 134 // pre fault: check fault before real do translate 135 val prepf = WireInit(VecInit(Seq.fill(Width)(false.B))) 136 val pregpf = WireInit(VecInit(Seq.fill(Width)(false.B))) 137 val preaf = WireInit(VecInit(Seq.fill(Width)(false.B))) 138 val premode = (0 until Width).map(i => Mux(req_in(i).bits.hyperinst, csr.priv.spvp, mode_tmp)) 139 for (i <- 0 until Width) { 140 resp(i).bits.fullva := RegEnable(EffectiveVa(i), req(i).valid) 141 } 142 val prevmEnable = (0 until Width).map(i => !(virt_in || req_in(i).bits.hyperinst) && ( 143 if (EnbaleTlbDebug) (Sv39Enable || Sv48Enable) 144 else (Sv39Enable || Sv48Enable) && (premode(i) < ModeM)) 145 ) 146 val pres2xlateEnable = (0 until Width).map(i => (virt_in || req_in(i).bits.hyperinst) && (Sv39x4Enable || Sv48x4Enable) && (premode(i) < ModeM)) 147 148 (0 until Width).foreach{i => 149 150 val pmm = WireInit(0.U(2.W)) 151 152 when (ifetch || req(i).bits.hlvx) { 153 pmm := 0.U 154 } .elsewhen (premode(i) === ModeM) { 155 pmm := csr.pmm.mseccfg 156 } .elsewhen (!(virt_in || req_in(i).bits.hyperinst) && premode(i) === ModeS) { 157 pmm := csr.pmm.menvcfg 158 } .elsewhen ((virt_in || req_in(i).bits.hyperinst) && premode(i) === ModeS) { 159 pmm := csr.pmm.henvcfg 160 } .elsewhen (req_in(i).bits.hyperinst && csr.priv.imode === ModeU) { 161 pmm := csr.pmm.hstatus 162 } .elsewhen (premode(i) === ModeU) { 163 pmm := csr.pmm.senvcfg 164 } 165 166 when (prevmEnable(i) || (pres2xlateEnable(i) && vsatp.mode =/= 0.U)) { 167 when (pmm === PMLEN7) { 168 EffectiveVa(i) := SignExt(req_in(i).bits.fullva(56, 0), XLEN) 169 } .elsewhen (pmm === PMLEN16) { 170 EffectiveVa(i) := SignExt(req_in(i).bits.fullva(47, 0), XLEN) 171 } .otherwise { 172 EffectiveVa(i) := req_in(i).bits.fullva 173 } 174 } .otherwise { 175 when (pmm === PMLEN7) { 176 EffectiveVa(i) := ZeroExt(req_in(i).bits.fullva(56, 0), XLEN) 177 } .elsewhen (pmm === PMLEN16) { 178 EffectiveVa(i) := ZeroExt(req_in(i).bits.fullva(47, 0), XLEN) 179 } .otherwise { 180 EffectiveVa(i) := req_in(i).bits.fullva 181 } 182 } 183 184 val pf48 = SignExt(EffectiveVa(i)(47, 0), XLEN) =/= EffectiveVa(i) 185 val pf39 = SignExt(EffectiveVa(i)(38, 0), XLEN) =/= EffectiveVa(i) 186 val gpf48 = EffectiveVa(i)(XLEN - 1, 48 + 2) =/= 0.U 187 val gpf39 = EffectiveVa(i)(XLEN - 1, 39 + 2) =/= 0.U 188 val af = EffectiveVa(i)(XLEN - 1, PAddrBits) =/= 0.U 189 when (req(i).valid && req(i).bits.checkfullva) { 190 when (prevmEnable(i) || pres2xlateEnable(i)) { 191 when (req_in_s2xlate(i) === onlyStage2) { 192 when (Sv48x4Enable) { 193 pregpf(i) := gpf48 194 } .elsewhen (Sv39x4Enable) { 195 pregpf(i) := gpf39 196 } 197 } .otherwise { 198 when (Sv48Enable) { 199 prepf(i) := pf48 200 } .elsewhen (Sv39Enable) { 201 prepf(i) := pf39 202 } 203 } 204 } .otherwise { 205 preaf(i) := af 206 } 207 } 208 } 209 210 val refill = ptw.resp.fire && !(ptw.resp.bits.getGpa) && !need_gpa && !flush_mmu 211 // prevent ptw refill when: 1) it's a getGpa request; 2) l1tlb is in need_gpa state; 3) mmu is being flushed. 212 213 refill_to_mem := DontCare 214 val entries = Module(new TlbStorageWrapper(Width, q, nRespDups)) 215 entries.io.base_connect(sfence, csr, satp) 216 if (q.outReplace) { io.replace <> entries.io.replace } 217 for (i <- 0 until Width) { 218 entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i, req_in_s2xlate(i)) 219 entries.io.w_apply(refill, ptw.resp.bits) 220 // TODO: RegNext enable:req.valid 221 resp(i).bits.debug.isFirstIssue := RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid) 222 resp(i).bits.debug.robIdx := RegEnable(req(i).bits.debug.robIdx, req(i).valid) 223 } 224 225 // read TLB, get hit/miss, paddr, perm bits 226 val readResult = (0 until Width).map(TLBRead(_)) 227 val hitVec = readResult.map(_._1) 228 val missVec = readResult.map(_._2) 229 val pmp_addr = readResult.map(_._3) 230 val perm = readResult.map(_._4) 231 val g_perm = readResult.map(_._5) 232 val pbmt = readResult.map(_._6) 233 val g_pbmt = readResult.map(_._7) 234 // check pmp use paddr (for timing optization, use pmp_addr here) 235 // check permisson 236 (0 until Width).foreach{i => 237 val noTranslateReg = RegNext(req(i).bits.no_translate) 238 val addr = Mux(noTranslateReg, req(i).bits.pmp_addr, pmp_addr(i)) 239 pmp_check(addr, req_out(i).size, req_out(i).cmd, noTranslateReg, i) 240 for (d <- 0 until nRespDups) { 241 pbmt_check(i, d, pbmt(i)(d), g_pbmt(i)(d), req_out_s2xlate(i)) 242 perm_check(perm(i)(d), req_out(i).cmd, i, d, g_perm(i)(d), req_out(i).hlvx, req_out_s2xlate(i), prepf(i), pregpf(i), preaf(i)) 243 } 244 hasGpf(i) := hitVec(i) && (resp(i).bits.excp(0).gpf.ld || resp(i).bits.excp(0).gpf.st || resp(i).bits.excp(0).gpf.instr) 245 } 246 247 // handle block or non-block io 248 // for non-block io, just return the above result, send miss to ptw 249 // for block io, hold the request, send miss to ptw, 250 // when ptw back, return the result 251 (0 until Width) foreach {i => 252 if (Block(i)) handle_block(i) 253 else handle_nonblock(i) 254 } 255 io.ptw.resp.ready := true.B 256 257 /************************ main body above | method/log/perf below ****************************/ 258 def TLBRead(i: Int) = { 259 val (e_hit, e_ppn, e_perm, e_g_perm, e_s2xlate, e_pbmt, e_g_pbmt) = entries.io.r_resp_apply(i) 260 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)) 261 val enable = portTranslateEnable(i) 262 val isOnlys2xlate = req_out_s2xlate(i) === onlyStage2 263 val need_gpa_vpn_hit = need_gpa_vpn === get_pn(req_out(i).vaddr) 264 val isitlb = TlbCmd.isExec(req_out(i).cmd) 265 val isPrefetch = req_out(i).isPrefetch 266 val currentRedirect = req_out(i).debug.robIdx.needFlush(redirect) 267 val lastCycleRedirect = req_out(i).debug.robIdx.needFlush(RegNext(redirect)) 268 269 when (!isitlb && need_gpa_robidx.needFlush(redirect) || isitlb && flush_pipe(i)){ 270 need_gpa := false.B 271 resp_gpa_refill := false.B 272 need_gpa_vpn := 0.U 273 }.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 && !isPrefetch && !currentRedirect && !lastCycleRedirect) { 274 need_gpa := true.B 275 need_gpa_vpn := get_pn(req_out(i).vaddr) 276 resp_gpa_refill := false.B 277 need_gpa_robidx := req_out(i).debug.robIdx 278 }.elsewhen (ptw.resp.fire && need_gpa && need_gpa_vpn === ptw.resp.bits.getVpn(need_gpa_vpn)) { 279 resp_gpa_gvpn := Mux(ptw.resp.bits.s2xlate === onlyStage2, ptw.resp.bits.s2.entry.tag, ptw.resp.bits.s1.genGVPN(need_gpa_vpn)) 280 resp_s1_level := ptw.resp.bits.s1.entry.level.get 281 resp_s1_isLeaf := ptw.resp.bits.s1.isLeaf() 282 resp_s1_isFakePte := ptw.resp.bits.s1.isFakePte() 283 resp_gpa_refill := true.B 284 } 285 286 when (req_out_v(i) && hasGpf(i) && resp_gpa_refill && need_gpa_vpn_hit){ 287 need_gpa := false.B 288 } 289 290 val hit = e_hit || p_hit 291 val miss = (!hit && enable) || hasGpf(i) && !p_hit && !(resp_gpa_refill && need_gpa_vpn_hit) && !isOnlys2xlate && !isPrefetch && !lastCycleRedirect 292 hit.suggestName(s"hit_read_${i}") 293 miss.suggestName(s"miss_read_${i}") 294 295 val vaddr = SignExt(req_out(i).vaddr, PAddrBits) 296 resp(i).bits.miss := miss 297 resp(i).bits.ptwBack := ptw.resp.fire 298 resp(i).bits.memidx := RegEnable(req_in(i).bits.memidx, req_in(i).valid) 299 resp(i).bits.fastMiss := !hit && enable 300 301 val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W)))) 302 val pbmt = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ptePbmtLen.W)))) 303 val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 304 val gvpn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ptePPNLen.W)))) 305 val level = WireInit(VecInit(Seq.fill(nRespDups)(0.U(log2Up(Level + 1).W)))) 306 val isLeaf = WireInit(VecInit(Seq.fill(nRespDups)(false.B))) 307 val isFakePte = WireInit(VecInit(Seq.fill(nRespDups)(false.B))) 308 val g_pbmt = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ptePbmtLen.W)))) 309 val g_perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 310 val r_s2xlate = WireInit(VecInit(Seq.fill(nRespDups)(0.U(2.W)))) 311 for (d <- 0 until nRespDups) { 312 ppn(d) := Mux(p_hit, p_ppn, e_ppn(d)) 313 pbmt(d) := Mux(p_hit, p_pbmt, e_pbmt(d)) 314 perm(d) := Mux(p_hit, p_perm, e_perm(d)) 315 gvpn(d) := Mux(p_hit, p_gvpn, resp_gpa_gvpn) 316 level(d) := Mux(p_hit, p_s1_level, resp_s1_level) 317 isLeaf(d) := Mux(p_hit, p_s1_isLeaf, resp_s1_isLeaf) 318 isFakePte(d) := Mux(p_hit, p_s1_isFakePte, resp_s1_isFakePte) 319 g_pbmt(d) := Mux(p_hit, p_g_pbmt, e_g_pbmt(d)) 320 g_perm(d) := Mux(p_hit, p_g_perm, e_g_perm(d)) 321 r_s2xlate(d) := Mux(p_hit, p_s2xlate, e_s2xlate(d)) 322 val paddr = Cat(ppn(d), get_off(req_out(i).vaddr)) 323 val vpn_idx = Mux1H(Seq( 324 (isFakePte(d) && vsatp.mode === Sv39) -> 2.U, 325 (isFakePte(d) && vsatp.mode === Sv48) -> 3.U, 326 (!isFakePte(d)) -> (level(d) - 1.U), 327 )) 328 // We use `fullva` here when `isLeaf`, in order to cope with the situation of an unaligned load/store cross page 329 // for example, a `ld` instruction on address 0x81000ffb will be splited into two loads 330 // 1. ld 0x81000ff8. vaddr = 0x81000ff8, fullva = 0x80000ffb 331 // 2. ld 0x81001000. vaddr = 0x81001000, fullva = 0x80000ffb 332 // When load 1 trigger a guest page fault, we should use offset of fullva when generate gpaddr 333 // and when load 2 trigger a guest page fault, we should just use offset of vaddr(all zero). 334 // Also, when onlyS2, if crosspage, gpaddr = vaddr(start address of a new page), else gpaddr = fullva(original vaddr) 335 // By the way, frontend handles the cross page instruction fetch by itself, so TLB doesn't need to do anything extra. 336 // Also, the fullva of iTLB is not used and always zero. crossPageVaddr should never use fullva in iTLB. 337 val crossPageVaddr = Mux(isitlb || req_out(i).fullva(12) =/= vaddr(12), vaddr, req_out(i).fullva) 338 val gpaddr_offset = Mux(isLeaf(d), get_off(crossPageVaddr), Cat(getVpnn(get_pn(crossPageVaddr), vpn_idx), 0.U(log2Up(XLEN/8).W))) 339 val gpaddr = Cat(gvpn(d), gpaddr_offset) 340 resp(i).bits.paddr(d) := Mux(enable, paddr, vaddr) 341 resp(i).bits.gpaddr(d) := Mux(r_s2xlate(d) === onlyStage2, crossPageVaddr, gpaddr) 342 } 343 344 XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n") 345 346 val pmp_paddr = resp(i).bits.paddr(0) 347 348 (hit, miss, pmp_paddr, perm, g_perm, pbmt, g_pbmt) 349 } 350 351 def getVpnn(vpn: UInt, idx: UInt): UInt = { 352 MuxLookup(idx, 0.U)(Seq( 353 0.U -> vpn(vpnnLen - 1, 0), 354 1.U -> vpn(vpnnLen * 2 - 1, vpnnLen), 355 2.U -> vpn(vpnnLen * 3 - 1, vpnnLen * 2), 356 3.U -> vpn(vpnnLen * 4 - 1, vpnnLen * 3)) 357 ) 358 } 359 360 def pmp_check(addr: UInt, size: UInt, cmd: UInt, noTranslate: Bool, idx: Int): Unit = { 361 pmp(idx).valid := resp(idx).valid || noTranslate 362 pmp(idx).bits.addr := addr 363 pmp(idx).bits.size := size 364 pmp(idx).bits.cmd := cmd 365 } 366 367 def pbmt_check(idx: Int, d: Int, pbmt: UInt, g_pbmt: UInt, s2xlate: UInt):Unit = { 368 val onlyS1 = s2xlate === onlyStage1 || s2xlate === noS2xlate 369 val pbmtRes = pbmt 370 val gpbmtRes = g_pbmt 371 val res = MuxLookup(s2xlate, 0.U)(Seq( 372 onlyStage1 -> pbmtRes, 373 onlyStage2 -> gpbmtRes, 374 allStage -> Mux(pbmtRes =/= 0.U, pbmtRes, gpbmtRes), 375 noS2xlate -> pbmtRes 376 )) 377 resp(idx).bits.pbmt(d) := Mux(portTranslateEnable(idx), res, 0.U) 378 } 379 380 // for timing optimization, pmp check is divided into dynamic and static 381 def perm_check(perm: TlbPermBundle, cmd: UInt, idx: Int, nDups: Int, g_perm: TlbPermBundle, hlvx: Bool, s2xlate: UInt, prepf: Bool = false.B, pregpf: Bool = false.B, preaf: Bool = false.B) = { 382 // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done 383 // static: 4K pages (or sram entries) -> check pmp with pre-checked results 384 val hasS2xlate = s2xlate =/= noS2xlate 385 val onlyS1 = s2xlate === onlyStage1 386 val onlyS2 = s2xlate === onlyStage2 387 val af = perm.af || (hasS2xlate && g_perm.af) 388 389 // Stage 1 perm check 390 val pf = perm.pf 391 val isLd = TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) 392 val isSt = TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd) 393 val isInst = TlbCmd.isExec(cmd) 394 val ldUpdate = !perm.a && isLd // update A/D through exception 395 val stUpdate = (!perm.a || !perm.d) && isSt // update A/D through exception 396 val instrUpdate = !perm.a && isInst // update A/D through exception 397 val modeCheck = !(mode(idx) === ModeU && !perm.u || mode(idx) === ModeS && perm.u && (!sum(idx) || ifetch)) 398 val ldPermFail = !(modeCheck && Mux(hlvx, perm.x, perm.r || mxr(idx) && perm.x)) 399 val stPermFail = !(modeCheck && perm.w) 400 val instrPermFail = !(modeCheck && perm.x) 401 val ldPf = (ldPermFail || pf) && isLd 402 val stPf = (stPermFail || pf) && isSt 403 val instrPf = (instrPermFail || pf) && isInst 404 val isFakePte = !perm.v && !perm.pf && !perm.af && !onlyS2 405 val isNonLeaf = !(perm.r || perm.w || perm.x) && perm.v && !perm.pf && !perm.af 406 val s1_valid = portTranslateEnable(idx) && !onlyS2 407 408 // Stage 2 perm check 409 val gpf = g_perm.pf 410 val g_ldUpdate = !g_perm.a && isLd 411 val g_stUpdate = (!g_perm.a || !g_perm.d) && isSt 412 val g_instrUpdate = !g_perm.a && isInst 413 val g_ldPermFail = !Mux(hlvx, g_perm.x, (g_perm.r || io.csr.priv.mxr && g_perm.x)) 414 val g_stPermFail = !g_perm.w 415 val g_instrPermFail = !g_perm.x 416 val ldGpf = (g_ldPermFail || gpf) && isLd 417 val stGpf = (g_stPermFail || gpf) && isSt 418 val instrGpf = (g_instrPermFail || gpf) && isInst 419 val s2_valid = portTranslateEnable(idx) && hasS2xlate && !onlyS1 420 421 val fault_valid = s1_valid || s2_valid 422 423 // when pf and gpf can't happens simultaneously 424 val hasPf = (ldPf || ldUpdate || stPf || stUpdate || instrPf || instrUpdate) && s1_valid && !af && !isFakePte && !isNonLeaf 425 // Only lsu need check related to high address truncation 426 when (RegNext(prepf || pregpf || preaf)) { 427 resp(idx).bits.isForVSnonLeafPTE := false.B 428 resp(idx).bits.excp(nDups).pf.ld := RegNext(prepf) && isLd 429 resp(idx).bits.excp(nDups).pf.st := RegNext(prepf) && isSt 430 resp(idx).bits.excp(nDups).pf.instr := false.B 431 432 resp(idx).bits.excp(nDups).gpf.ld := RegNext(pregpf) && isLd 433 resp(idx).bits.excp(nDups).gpf.st := RegNext(pregpf) && isSt 434 resp(idx).bits.excp(nDups).gpf.instr := false.B 435 436 resp(idx).bits.excp(nDups).af.ld := RegNext(preaf) && TlbCmd.isRead(cmd) 437 resp(idx).bits.excp(nDups).af.st := RegNext(preaf) && TlbCmd.isWrite(cmd) 438 resp(idx).bits.excp(nDups).af.instr := false.B 439 440 resp(idx).bits.excp(nDups).vaNeedExt := false.B 441 // overwrite miss & gpaddr when exception related to high address truncation happens 442 resp(idx).bits.miss := false.B 443 resp(idx).bits.gpaddr(nDups) := req_out(idx).fullva 444 } .otherwise { 445 // isForVSnonLeafPTE is used only when gpf happens and it caused by a G-stage translation which supports VS-stage translation 446 // it will be sent to CSR in order to modify the m/htinst. 447 // Ref: The RISC-V Instruction Set Manual: Volume II: Privileged Architecture - 19.6.3. Transformed Instruction or Pseudoinstruction for mtinst or htinst 448 val isForVSnonLeafPTE = isNonLeaf || isFakePte 449 resp(idx).bits.isForVSnonLeafPTE := isForVSnonLeafPTE 450 resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && s1_valid && !af && !isFakePte && !isNonLeaf 451 resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && s1_valid && !af && !isFakePte && !isNonLeaf 452 resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && s1_valid && !af && !isFakePte && !isNonLeaf 453 // NOTE: pf need && with !af, page fault has higher priority than access fault 454 // but ptw may also have access fault, then af happens, the translation is wrong. 455 // In this case, pf has lower priority than af 456 457 resp(idx).bits.excp(nDups).gpf.ld := (ldGpf || g_ldUpdate) && s2_valid && !af && !hasPf 458 resp(idx).bits.excp(nDups).gpf.st := (stGpf || g_stUpdate) && s2_valid && !af && !hasPf 459 resp(idx).bits.excp(nDups).gpf.instr := (instrGpf || g_instrUpdate) && s2_valid && !af && !hasPf 460 461 resp(idx).bits.excp(nDups).af.ld := af && TlbCmd.isRead(cmd) && fault_valid 462 resp(idx).bits.excp(nDups).af.st := af && TlbCmd.isWrite(cmd) && fault_valid 463 resp(idx).bits.excp(nDups).af.instr := af && TlbCmd.isExec(cmd) && fault_valid 464 465 resp(idx).bits.excp(nDups).vaNeedExt := true.B 466 } 467 468 resp(idx).bits.excp(nDups).isHyper := isHyperInst(idx) 469 } 470 471 def handle_nonblock(idx: Int): Unit = { 472 io.requestor(idx).resp.valid := req_out_v(idx) 473 io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true 474 XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B") 475 476 val req_need_gpa = hasGpf(idx) 477 val req_s2xlate = Wire(UInt(2.W)) 478 req_s2xlate := MuxCase(noS2xlate, Seq( 479 (!(virt_out(idx) || req_out(idx).hyperinst)) -> noS2xlate, 480 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 481 (csr.vsatp.mode === 0.U) -> onlyStage2, 482 (csr.hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 483 )) 484 485 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) 486 // TODO: RegNext enable: ptw.resp.valid ? req.valid 487 val ptw_resp_bits_reg = RegEnable(ptw.resp.bits, ptw.resp.valid) 488 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) 489 val ptw_getGpa = req_need_gpa && hitVec(idx) 490 val need_gpa_vpn_hit = need_gpa_vpn === get_pn(req_out(idx).vaddr) 491 492 io.ptw.req(idx).valid := false.B; 493 io.tlbreplay(idx) := false.B; 494 495 when (req_out_v(idx) && missVec(idx)) { 496 // NOTE: for an miss tlb request: either send a ptw request, or ask for a replay 497 when (ptw_just_back || ptw_already_back) { 498 io.tlbreplay(idx) := true.B; 499 } .elsewhen (need_gpa && !need_gpa_vpn_hit && !resp_gpa_refill) { 500 // not send any unrelated ptw request when l1tlb is in need_gpa state 501 io.tlbreplay(idx) := true.B; 502 } .otherwise { 503 io.ptw.req(idx).valid := true.B; 504 } 505 } 506 507 when (io.requestor(idx).req_kill && GatedValidRegNext(io.requestor(idx).req.fire)) { 508 io.ptw.req(idx).valid := false.B 509 io.tlbreplay(idx) := true.B 510 } 511 512 io.ptw.req(idx).bits.vpn := get_pn(req_out(idx).vaddr) 513 io.ptw.req(idx).bits.s2xlate := req_s2xlate 514 io.ptw.req(idx).bits.getGpa := ptw_getGpa 515 io.ptw.req(idx).bits.memidx := req_out(idx).memidx 516 } 517 518 def handle_block(idx: Int): Unit = { 519 // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid 520 io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire 521 // req_out_v for if there is a request, may long latency, fixme 522 523 // miss request entries 524 val req_need_gpa = hasGpf(idx) 525 val miss_req_vpn = get_pn(req_out(idx).vaddr) 526 val miss_req_memidx = req_out(idx).memidx 527 val miss_req_s2xlate = Wire(UInt(2.W)) 528 miss_req_s2xlate := MuxCase(noS2xlate, Seq( 529 (!(virt_out(idx) || req_out(idx).hyperinst)) -> noS2xlate, 530 (csr.vsatp.mode =/= 0.U && csr.hgatp.mode =/= 0.U) -> allStage, 531 (csr.vsatp.mode === 0.U) -> onlyStage2, 532 (csr.hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 533 )) 534 val miss_req_s2xlate_reg = RegEnable(miss_req_s2xlate, io.ptw.req(idx).fire) 535 val hasS2xlate = miss_req_s2xlate_reg =/= noS2xlate 536 val onlyS2 = miss_req_s2xlate_reg === onlyStage2 537 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) 538 val hit_s2 = io.ptw.resp.bits.s2.hit(miss_req_vpn, io.csr.hgatp.vmid) 539 val hit = Mux(onlyS2, hit_s2, hit_s1) && io.ptw.resp.valid && miss_req_s2xlate_reg === io.ptw.resp.bits.s2xlate 540 541 val new_coming_valid = WireInit(false.B) 542 new_coming_valid := req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx) 543 val new_coming = GatedValidRegNext(new_coming_valid) 544 val miss_wire = new_coming && missVec(idx) 545 val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire, flush_pipe(idx)) 546 val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe), 547 io.ptw.req(idx).fire || resp(idx).fire, flush_pipe(idx)) 548 549 // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu 550 resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx)) 551 when (io.ptw.resp.fire && hit && req_out_v(idx) && portTranslateEnable(idx)) { 552 val stage1 = io.ptw.resp.bits.s1 553 val stage2 = io.ptw.resp.bits.s2 554 val s2xlate = io.ptw.resp.bits.s2xlate 555 resp(idx).valid := true.B 556 resp(idx).bits.miss := false.B 557 val s1_paddr = Cat(stage1.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 558 val s2_paddr = Cat(stage2.genPPNS2(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 559 for (d <- 0 until nRespDups) { 560 resp(idx).bits.paddr(d) := Mux(s2xlate =/= noS2xlate, s2_paddr, s1_paddr) 561 resp(idx).bits.gpaddr(d) := s1_paddr 562 pbmt_check(idx, d, io.ptw.resp.bits.s1.entry.pbmt, io.ptw.resp.bits.s2.entry.pbmt, s2xlate) 563 perm_check(stage1, req_out(idx).cmd, idx, d, stage2, req_out(idx).hlvx, s2xlate) 564 } 565 pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, false.B, idx) 566 567 // NOTE: the unfiltered req would be handled by Repeater 568 } 569 assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must") 570 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") 571 572 val ptw_req = io.ptw.req(idx) 573 ptw_req.valid := miss_req_v 574 ptw_req.bits.vpn := miss_req_vpn 575 ptw_req.bits.s2xlate := miss_req_s2xlate 576 ptw_req.bits.getGpa := req_need_gpa && hitVec(idx) 577 ptw_req.bits.memidx := miss_req_memidx 578 579 io.tlbreplay(idx) := false.B 580 581 // NOTE: when flush pipe, tlb should abandon last req 582 // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp 583 // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it. 584 if (!q.outsideRecvFlush) { 585 when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) { 586 resp(idx).valid := true.B 587 for (d <- 0 until nRespDups) { 588 resp(idx).bits.pbmt(d) := 0.U 589 resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr 590 resp(idx).bits.excp(d).pf.st := true.B 591 resp(idx).bits.excp(d).pf.instr := true.B 592 } 593 } 594 } 595 } 596 597 // when ptw resp, tlb at refill_idx maybe set to miss by force. 598 // Bypass ptw resp to check. 599 def ptw_resp_bypass(vpn: UInt, s2xlate: UInt) = { 600 // TODO: RegNext enable: ptw.resp.valid 601 val hasS2xlate = s2xlate =/= noS2xlate 602 val onlyS2 = s2xlate === onlyStage2 603 val onlyS1 = s2xlate === onlyStage1 604 val s2xlate_hit = s2xlate === ptw.resp.bits.s2xlate 605 val resp_hit = ptw.resp.bits.hit(vpn, io.csr.satp.asid, io.csr.vsatp.asid, io.csr.hgatp.vmid, true, false) 606 val p_hit = GatedValidRegNext(resp_hit && io.ptw.resp.fire && s2xlate_hit) 607 val ppn_s1 = ptw.resp.bits.s1.genPPN(vpn) 608 val gvpn = Mux(onlyS2, vpn, ppn_s1) 609 val ppn_s2 = ptw.resp.bits.s2.genPPNS2(gvpn) 610 val p_ppn = RegEnable(Mux(s2xlate === onlyStage2 || s2xlate === allStage, ppn_s2, ppn_s1), io.ptw.resp.fire) 611 val p_pbmt = RegEnable(ptw.resp.bits.s1.entry.pbmt,io.ptw.resp.fire) 612 val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits.s1), io.ptw.resp.fire) 613 val p_gvpn = RegEnable(Mux(onlyS2, ptw.resp.bits.s2.entry.tag, ptw.resp.bits.s1.genGVPN(vpn)), io.ptw.resp.fire) 614 val p_g_pbmt = RegEnable(ptw.resp.bits.s2.entry.pbmt,io.ptw.resp.fire) 615 val p_g_perm = RegEnable(hptwresp_to_tlbperm(ptw.resp.bits.s2), io.ptw.resp.fire) 616 val p_s2xlate = RegEnable(ptw.resp.bits.s2xlate, io.ptw.resp.fire) 617 val p_s1_level = RegEnable(ptw.resp.bits.s1.entry.level.get, io.ptw.resp.fire) 618 val p_s1_isLeaf = RegEnable(ptw.resp.bits.s1.isLeaf(), io.ptw.resp.fire) 619 val p_s1_isFakePte = RegEnable(ptw.resp.bits.s1.isFakePte(), io.ptw.resp.fire) 620 (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) 621 } 622 623 // perf event 624 val result_ok = req_in.map(a => GatedValidRegNext(a.fire)) 625 val perfEvents = 626 Seq( 627 ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire else portTranslateEnable(i) && result_ok(i) })), 628 ("miss ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && result_ok(i) && missVec(i) else ptw.req(i).fire })), 629 ) 630 generatePerfEvent() 631 632 // perf log 633 for (i <- 0 until Width) { 634 if (Block(i)) { 635 XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i)) 636 XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i)) 637 } else { 638 XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid)) 639 XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i)) 640 XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegEnable(req(i).bits.debug.isFirstIssue, req(i).valid)) 641 XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i)) 642 } 643 } 644 XSPerfAccumulate("ptw_resp_count", ptw.resp.fire) 645 XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire && ptw.resp.bits.s1.pf) 646 647 // Log 648 for(i <- 0 until Width) { 649 XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n") 650 XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n") 651 } 652 653 XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n") 654 XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n") 655 for (i <- ptw.req.indices) { 656 XSDebug(ptw.req(i).fire, p"L2TLB req:${ptw.req(i).bits}\n") 657 } 658 XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n") 659 660 println(s"${q.name}: page: ${q.NWays} ${q.Associative} ${q.Replacer.get}") 661 662 if (env.EnableDifftest) { 663 for (i <- 0 until Width) { 664 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 665 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 666 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 667 val difftest = DifftestModule(new DiffL1TLBEvent) 668 difftest.coreid := io.hartId 669 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) 670 if (!Seq("itlb", "ldtlb", "sttlb").contains(q.name)) { 671 difftest.valid := false.B 672 } 673 difftest.index := TLBDiffId(p(XSCoreParamsKey).HartId).U 674 difftest.vpn := RegEnable(get_pn(req_in(i).bits.vaddr), req_in(i).valid) 675 difftest.ppn := get_pn(io.requestor(i).resp.bits.paddr(0)) 676 difftest.satp := Cat(io.csr.satp.mode, io.csr.satp.asid, io.csr.satp.ppn) 677 difftest.vsatp := Cat(io.csr.vsatp.mode, io.csr.vsatp.asid, io.csr.vsatp.ppn) 678 difftest.hgatp := Cat(io.csr.hgatp.mode, io.csr.hgatp.vmid, io.csr.hgatp.ppn) 679 val req_need_gpa = gpf 680 val req_s2xlate = Wire(UInt(2.W)) 681 req_s2xlate := MuxCase(noS2xlate, Seq( 682 (!RegNext(virt_in || req_in(i).bits.hyperinst)) -> noS2xlate, 683 (vsatp.mode =/= 0.U && hgatp.mode =/= 0.U) -> allStage, 684 (vsatp.mode === 0.U) -> onlyStage2, 685 (hgatp.mode === 0.U || req_need_gpa) -> onlyStage1 686 )) 687 difftest.s2xlate := req_s2xlate 688 } 689 } 690} 691 692object TLBDiffId { 693 var i: Int = 0 694 var lastHartId: Int = -1 695 def apply(hartId: Int): Int = { 696 if (lastHartId != hartId) { 697 i = 0 698 lastHartId = hartId 699 } 700 i += 1 701 i - 1 702 } 703} 704 705class TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q) 706class TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q) 707 708class TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule { 709 val io = IO(new TlbReplaceIO(Width, q)) 710 711 if (q.Associative == "fa") { 712 val re = ReplacementPolicy.fromString(q.Replacer, q.NWays) 713 re.access(io.page.access.map(_.touch_ways)) 714 io.page.refillIdx := re.way 715 } else { // set-acco && plru 716 val re = ReplacementPolicy.fromString(q.Replacer, q.NSets, q.NWays) 717 re.access(io.page.access.map(_.sets), io.page.access.map(_.touch_ways)) 718 io.page.refillIdx := { if (q.NWays == 1) 0.U else re.way(io.page.chosen_set) } 719 } 720} 721