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 chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.internal.naming.chiselName 22import chisel3.util._ 23import freechips.rocketchip.util.SRAMAnnotation 24import xiangshan._ 25import utils._ 26import xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig} 27import xiangshan.backend.rob.RobPtr 28import xiangshan.backend.fu.util.HasCSRConst 29import firrtl.FirrtlProtos.Firrtl.Module.ExternalModule.Parameter 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 41@chiselName 42class TLB(Width: Int, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule 43 with HasCSRConst 44 with HasPerfEvents 45{ 46 val io = IO(new TlbIO(Width, q)) 47 48 val req = io.requestor.map(_.req) 49 val resp = io.requestor.map(_.resp) 50 val ptw = io.ptw 51 val pmp = io.pmp 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 flush_mmu = DelayN(sfence.valid || csr.satp.changed, q.fenceDelay) 63 val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe 64 val flush_pipe = io.flushPipe 65 66 // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush. 67 // because, csr will influence tlb behavior. 68 val ifecth = if (q.fetchi) true.B else false.B 69 val mode = if (q.useDmode) csr.priv.dmode else csr.priv.imode 70 // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux... 71 val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U) 72 else (satp.mode === 8.U && (mode < ModeM)) 73 74 val req_in = req 75 val req_out = req.map(a => RegEnable(a.bits, a.fire())) 76 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))) 77 78 val refill = ptw.resp.fire() && !flush_mmu && vmEnable 79 val entries = Module(new TlbStorageWrapper(Width, q)) 80 entries.io.base_connect(sfence, csr, satp) 81 if (q.outReplace) { io.replace <> entries.io.replace } 82 for (i <- 0 until Width) { 83 entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i) 84 entries.io.w_apply(refill, ptw.resp.bits, io.ptw_replenish) 85 } 86 87 // read TLB, get hit/miss, paddr, perm bits 88 val readResult = (0 until Width).map(TLBRead(_)) 89 val hitVec = readResult.map(_._1) 90 val missVec = readResult.map(_._2) 91 val pmp_addr = readResult.map(_._3) 92 val static_pm = readResult.map(_._4) 93 val static_pm_v = readResult.map(_._5) 94 val perm = readResult.map(_._6) 95 96 // check pmp use paddr (for timing optization, use pmp_addr here) 97 // check permisson 98 (0 until Width).foreach{i => 99 pmp_check(pmp_addr(i), req_out(i).size, req_out(i).cmd, i) 100 perm_check(perm(i), req_out(i).cmd, static_pm(i), static_pm_v(i), i) 101 } 102 103 // handle block or non-block io 104 // for non-block io, just return the above result, send miss to ptw 105 // for block io, hold the request, send miss to ptw, 106 // when ptw back, return the result 107 (0 until Width) foreach {i => 108 if (Block(i)) handle_block(i) 109 else handle_nonblock(i) 110 } 111 io.ptw.resp.ready := true.B 112 113 /************************ main body above | method/log/perf below ****************************/ 114 115 def TLBRead(i: Int) = { 116 val (hit, ppn, perm, super_hit, super_ppn, static_pm) = entries.io.r_resp_apply(i) 117 118 val miss = !hit && vmEnable 119 val fast_miss = !super_hit && vmEnable 120 hit.suggestName(s"hit_read_${i}") 121 miss.suggestName(s"miss_read_${i}") 122 123 XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn)} perm:${perm}\n") 124 125 val paddr = Cat(ppn, get_off(req_out(i).vaddr)) 126 val vaddr = SignExt(req_out(i).vaddr, PAddrBits) 127 128 resp(i).bits.paddr := Mux(vmEnable, paddr, vaddr) 129 resp(i).bits.miss := miss 130 resp(i).bits.fast_miss := fast_miss 131 resp(i).bits.ptwBack := ptw.resp.fire() 132 133 // val pmp_paddr = Mux(vmEnable, Cat(super_ppn, get_off(req_out(i).vaddr)), vaddr) 134 // pmp_paddr seems same to paddr functionally. It abandons normal_ppn for timing optimization. 135 val pmp_paddr = Mux(vmEnable, paddr, vaddr) 136 val static_pm_valid = !super_hit && vmEnable && q.partialStaticPMP.B 137 138 (hit, miss, pmp_paddr, static_pm, static_pm_valid, perm) 139 } 140 141 def pmp_check(addr: UInt, size: UInt, cmd: UInt, idx: Int): Unit = { 142 pmp(idx).valid := resp(idx).valid 143 pmp(idx).bits.addr := addr 144 pmp(idx).bits.size := size 145 pmp(idx).bits.cmd := cmd 146 } 147 148 def perm_check(perm: TlbPermBundle, cmd: UInt, spm: TlbPMBundle, spm_v: Bool, idx: Int) = { 149 // for timing optimization, pmp check is divided into dynamic and static 150 // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done 151 // static: 4K pages (or sram entries) -> check pmp with pre-checked results 152 val af = perm.af 153 val pf = perm.pf 154 val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception 155 val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception 156 val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception 157 val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!io.csr.priv.sum || ifecth)) 158 val ldPermFail = !(modeCheck && (perm.r || io.csr.priv.mxr && perm.x)) 159 val stPermFail = !(modeCheck && perm.w) 160 val instrPermFail = !(modeCheck && perm.x) 161 val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd)) 162 val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 163 val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd) 164 val fault_valid = vmEnable 165 resp(idx).bits.excp.pf.ld := (ldPf || ldUpdate) && fault_valid && !af 166 resp(idx).bits.excp.pf.st := (stPf || stUpdate) && fault_valid && !af 167 resp(idx).bits.excp.pf.instr := (instrPf || instrUpdate) && fault_valid && !af 168 // NOTE: pf need && with !af, page fault has higher priority than access fault 169 // but ptw may also have access fault, then af happens, the translation is wrong. 170 // In this case, pf has lower priority than af 171 172 resp(idx).bits.excp.af.ld := (af || (spm_v && !spm.r)) && TlbCmd.isRead(cmd) && fault_valid 173 resp(idx).bits.excp.af.st := (af || (spm_v && !spm.w)) && TlbCmd.isWrite(cmd) && fault_valid 174 resp(idx).bits.excp.af.instr := (af || (spm_v && !spm.x)) && TlbCmd.isExec(cmd) && fault_valid 175 resp(idx).bits.static_pm.valid := spm_v && fault_valid // ls/st unit should use this mmio, not the result from pmp 176 resp(idx).bits.static_pm.bits := !spm.c 177 } 178 179 def handle_nonblock(idx: Int): Unit = { 180 io.requestor(idx).resp.valid := req_out_v(idx) 181 io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true 182 io.ptw.req(idx).valid := RegNext(req_out_v(idx) && missVec(idx), false.B) // TODO: remove the regnext, timing 183 io.ptw.req(idx).bits.vpn := RegNext(get_pn(req_out(idx).vaddr)) 184 } 185 186 def handle_block(idx: Int): Unit = { 187 // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid 188 io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire() 189 // req_out_v for if there is a request, may long latency, fixme 190 191 // miss request entries 192 val miss_req_vpn = get_pn(req_out(idx).vaddr) 193 val hit = io.ptw.resp.bits.entry.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid 194 195 val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B) 196 val miss_wire = new_coming && missVec(idx) 197 val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire(), flush_pipe(idx)) 198 val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe), 199 io.ptw.req(idx).fire() || resp(idx).fire(), flush_pipe(idx)) 200 201 // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu 202 resp(idx).valid := req_out_v(idx) && !(miss_v && vmEnable) 203 when (io.ptw.resp.fire() && hit && req_out_v(idx) && vmEnable) { 204 val pte = io.ptw.resp.bits 205 resp(idx).valid := true.B 206 resp(idx).bits.miss := false.B // for blocked tlb, this is useless 207 resp(idx).bits.paddr := Cat(pte.entry.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 208 209 perm_check(pte, req_out(idx).cmd, 0.U.asTypeOf(new TlbPMBundle), false.B, idx) 210 pmp_check(resp(idx).bits.paddr, req_out(idx).size, req_out(idx).cmd, idx) 211 // NOTE: the unfiltered req would be handled by Repeater 212 } 213 assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must") 214 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") 215 216 val ptw_req = io.ptw.req(idx) 217 ptw_req.valid := miss_req_v 218 ptw_req.bits.vpn := miss_req_vpn 219 220 // NOTE: when flush pipe, tlb should abandon last req 221 // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp 222 // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it. 223 if (!q.outsideRecvFlush) { 224 when (req_out_v(idx) && flush_pipe(idx) && vmEnable) { 225 resp(idx).valid := true.B 226 resp(idx).bits.excp.pf.ld := true.B // sfence happened, pf for not to use this addr 227 resp(idx).bits.excp.pf.st := true.B 228 resp(idx).bits.excp.pf.instr := true.B 229 } 230 } 231 } 232 // assert 233 for(i <- 0 until Width) { 234 TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.") 235 } 236 237 // perf event 238 val result_ok = req_in.map(a => RegNext(a.fire())) 239 val perfEvents = 240 Seq( 241 ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire() else vmEnable && result_ok(i) })), 242 ("miss ", PopCount((0 until Width).map{i => if (Block(i)) vmEnable && result_ok(i) && missVec(i) else ptw.req(i).fire() })), 243 ) 244 generatePerfEvent() 245 246 // perf log 247 for (i <- 0 until Width) { 248 if (Block(i)) { 249 XSPerfAccumulate(s"access${i}",result_ok(i) && vmEnable) 250 XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i)) 251 } else { 252 XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && vmEnable && RegNext(req(i).bits.debug.isFirstIssue)) 253 XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && vmEnable) 254 XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && vmEnable && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue)) 255 XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && vmEnable && missVec(i)) 256 } 257 } 258 XSPerfAccumulate("ptw_resp_count", ptw.resp.fire()) 259 XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire() && ptw.resp.bits.pf) 260 261 // Log 262 for(i <- 0 until Width) { 263 XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n") 264 XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n") 265 } 266 267 XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n") 268 XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n") 269 for (i <- ptw.req.indices) { 270 XSDebug(ptw.req(i).fire(), p"L2TLB req:${ptw.req(i).bits}\n") 271 } 272 XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n") 273 274 println(s"${q.name}: normal page: ${q.normalNWays} ${q.normalAssociative} ${q.normalReplacer.get} super page: ${q.superNWays} ${q.superAssociative} ${q.superReplacer.get}") 275 276} 277 278class TLBNonBlock(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, Seq.fill(Width)(false), q) 279class TLBBLock(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, Seq.fill(Width)(true), q) 280 281class TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule { 282 val io = IO(new TlbReplaceIO(Width, q)) 283 284 if (q.normalAssociative == "fa") { 285 val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays) 286 re.access(io.normalPage.access.map(_.touch_ways)) 287 io.normalPage.refillIdx := re.way 288 } else { // set-acco && plru 289 val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays) 290 re.access(io.normalPage.access.map(_.sets), io.normalPage.access.map(_.touch_ways)) 291 io.normalPage.refillIdx := { if (q.normalNWays == 1) 0.U else re.way(io.normalPage.chosen_set) } 292 } 293 294 if (q.superAssociative == "fa") { 295 val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays) 296 re.access(io.superPage.access.map(_.touch_ways)) 297 io.superPage.refillIdx := re.way 298 } else { // set-acco && plru 299 val re = ReplacementPolicy.fromString(q.superReplacer, q.superNSets, q.superNWays) 300 re.access(io.superPage.access.map(_.sets), io.superPage.access.map(_.touch_ways)) 301 io.superPage.refillIdx := { if (q.superNWays == 1) 0.U else re.way(io.superPage.chosen_set) } 302 } 303} 304