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 difftest._ 24import freechips.rocketchip.util.SRAMAnnotation 25import xiangshan._ 26import utils._ 27import utility._ 28import xiangshan.backend.fu.{PMPChecker, PMPReqBundle, PMPConfig => XSPMPConfig} 29import xiangshan.backend.rob.RobPtr 30import xiangshan.backend.fu.util.HasCSRConst 31import firrtl.FirrtlProtos.Firrtl.Module.ExternalModule.Parameter 32import freechips.rocketchip.rocket.PMPConfig 33 34/** TLB module 35 * support block request and non-block request io at the same time 36 * return paddr at next cycle, then go for pmp/pma check 37 * @param Width: The number of requestors 38 * @param Block: Blocked or not for each requestor ports 39 * @param q: TLB Parameters, like entry number, each TLB has its own parameters 40 * @param p: XiangShan Paramemters, like XLEN 41 */ 42 43@chiselName 44class TLB(Width: Int, nRespDups: Int = 1, Block: Seq[Boolean], q: TLBParameters)(implicit p: Parameters) extends TlbModule 45 with HasCSRConst 46 with HasPerfEvents 47{ 48 val io = IO(new TlbIO(Width, nRespDups, q)) 49 50 val req = io.requestor.map(_.req) 51 val resp = io.requestor.map(_.resp) 52 val ptw = io.ptw 53 val pmp = io.pmp 54 val refill_to_mem = io.refill_to_mem 55 56 /** Sfence.vma & Svinval 57 * Sfence.vma will 1. flush old entries 2. flush inflight 3. flush pipe 58 * Svinval will 1. flush old entries 2. flush inflight 59 * So, Svinval will not flush pipe, which means 60 * it should not drop reqs from pipe and should return right resp 61 */ 62 val sfence = DelayN(io.sfence, q.fenceDelay) 63 val csr = io.csr 64 val satp = DelayN(io.csr.satp, q.fenceDelay) 65 val flush_mmu = DelayN(sfence.valid || csr.satp.changed, q.fenceDelay) 66 val mmu_flush_pipe = DelayN(sfence.valid && sfence.bits.flushPipe, q.fenceDelay) // for svinval, won't flush pipe 67 val flush_pipe = io.flushPipe 68 69 // ATTENTION: csr and flush from backend are delayed. csr should not be later than flush. 70 // because, csr will influence tlb behavior. 71 val ifecth = if (q.fetchi) true.B else false.B 72 val mode = if (q.useDmode) csr.priv.dmode else csr.priv.imode 73 // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux... 74 val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U) 75 else (satp.mode === 8.U && (mode < ModeM)) 76 val portTranslateEnable = (0 until Width).map(i => vmEnable && RegNext(!req(i).bits.no_translate)) 77 78 val req_in = req 79 val req_out = req.map(a => RegEnable(a.bits, a.fire())) 80 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))) 81 82 val refill = ptw.resp.fire() && !flush_mmu && vmEnable 83 refill_to_mem.valid := refill 84 refill_to_mem.memidx := ptw.resp.bits.memidx 85 86 val entries = Module(new TlbStorageWrapper(Width, q, nRespDups)) 87 entries.io.base_connect(sfence, csr, satp) 88 if (q.outReplace) { io.replace <> entries.io.replace } 89 for (i <- 0 until Width) { 90 entries.io.r_req_apply(io.requestor(i).req.valid, get_pn(req_in(i).bits.vaddr), i) 91 entries.io.w_apply(refill, ptw.resp.bits) 92 resp(i).bits.debug.isFirstIssue := RegNext(req(i).bits.debug.isFirstIssue) 93 resp(i).bits.debug.robIdx := RegNext(req(i).bits.debug.robIdx) 94 } 95 96 // read TLB, get hit/miss, paddr, perm bits 97 val readResult = (0 until Width).map(TLBRead(_)) 98 val hitVec = readResult.map(_._1) 99 val missVec = readResult.map(_._2) 100 val pmp_addr = readResult.map(_._3) 101 val perm = readResult.map(_._4) 102 103 // check pmp use paddr (for timing optization, use pmp_addr here) 104 // check permisson 105 (0 until Width).foreach{i => 106 pmp_check(pmp_addr(i), req_out(i).size, req_out(i).cmd, i) 107 for (d <- 0 until nRespDups) { 108 perm_check(perm(i)(d), req_out(i).cmd, i, d) 109 } 110 } 111 112 // handle block or non-block io 113 // for non-block io, just return the above result, send miss to ptw 114 // for block io, hold the request, send miss to ptw, 115 // when ptw back, return the result 116 (0 until Width) foreach {i => 117 if (Block(i)) handle_block(i) 118 else handle_nonblock(i) 119 } 120 io.ptw.resp.ready := true.B 121 122 /************************ main body above | method/log/perf below ****************************/ 123 def TLBRead(i: Int) = { 124 val (e_hit, e_ppn, e_perm) = entries.io.r_resp_apply(i) 125 val (p_hit, p_ppn, p_perm) = ptw_resp_bypass(get_pn(req_in(i).bits.vaddr)) 126 val enable = portTranslateEnable(i) 127 128 val hit = e_hit || p_hit 129 val miss = !hit && enable 130 hit.suggestName(s"hit_read_${i}") 131 miss.suggestName(s"miss_read_${i}") 132 133 val vaddr = SignExt(req_out(i).vaddr, PAddrBits) 134 resp(i).bits.miss := miss 135 resp(i).bits.ptwBack := ptw.resp.fire() 136 resp(i).bits.memidx := RegNext(req_in(i).bits.memidx) 137 138 val ppn = WireInit(VecInit(Seq.fill(nRespDups)(0.U(ppnLen.W)))) 139 val perm = WireInit(VecInit(Seq.fill(nRespDups)(0.U.asTypeOf(new TlbPermBundle)))) 140 141 for (d <- 0 until nRespDups) { 142 ppn(d) := Mux(p_hit, p_ppn, e_ppn(d)) 143 perm(d) := Mux(p_hit, p_perm, e_perm(d)) 144 145 val paddr = Cat(ppn(d), get_off(req_out(i).vaddr)) 146 resp(i).bits.paddr(d) := Mux(enable, paddr, vaddr) 147 } 148 149 XSDebug(req_out_v(i), p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn(0))} perm:${perm(0)}\n") 150 151 val pmp_paddr = resp(i).bits.paddr(0) 152 153 (hit, miss, pmp_paddr, perm) 154 } 155 156 def pmp_check(addr: UInt, size: UInt, cmd: UInt, idx: Int): Unit = { 157 pmp(idx).valid := resp(idx).valid 158 pmp(idx).bits.addr := addr 159 pmp(idx).bits.size := size 160 pmp(idx).bits.cmd := cmd 161 } 162 163 def perm_check(perm: TlbPermBundle, cmd: UInt, idx: Int, nDups: Int) = { 164 // for timing optimization, pmp check is divided into dynamic and static 165 // dynamic: superpage (or full-connected reg entries) -> check pmp when translation done 166 // static: 4K pages (or sram entries) -> check pmp with pre-checked results 167 val af = perm.af 168 val pf = perm.pf 169 val ldUpdate = !perm.a && TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) // update A/D through exception 170 val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) // update A/D through exception 171 val instrUpdate = !perm.a && TlbCmd.isExec(cmd) // update A/D through exception 172 val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!io.csr.priv.sum || ifecth)) 173 val ldPermFail = !(modeCheck && (perm.r || io.csr.priv.mxr && perm.x)) 174 val stPermFail = !(modeCheck && perm.w) 175 val instrPermFail = !(modeCheck && perm.x) 176 val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd)) 177 val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) 178 val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmd) 179 val fault_valid = portTranslateEnable(idx) 180 resp(idx).bits.excp(nDups).pf.ld := (ldPf || ldUpdate) && fault_valid && !af 181 resp(idx).bits.excp(nDups).pf.st := (stPf || stUpdate) && fault_valid && !af 182 resp(idx).bits.excp(nDups).pf.instr := (instrPf || instrUpdate) && fault_valid && !af 183 // NOTE: pf need && with !af, page fault has higher priority than access fault 184 // but ptw may also have access fault, then af happens, the translation is wrong. 185 // In this case, pf has lower priority than af 186 187 resp(idx).bits.excp(nDups).af.ld := af && TlbCmd.isRead(cmd) && fault_valid 188 resp(idx).bits.excp(nDups).af.st := af && TlbCmd.isWrite(cmd) && fault_valid 189 resp(idx).bits.excp(nDups).af.instr := af && TlbCmd.isExec(cmd) && fault_valid 190 } 191 192 def handle_nonblock(idx: Int): Unit = { 193 io.requestor(idx).resp.valid := req_out_v(idx) 194 io.requestor(idx).req.ready := io.requestor(idx).resp.ready // should always be true 195 XSError(!io.requestor(idx).resp.ready, s"${q.name} port ${idx} is non-block, resp.ready must be true.B") 196 197 val ptw_just_back = ptw.resp.fire && ptw.resp.bits.hit(get_pn(req_out(idx).vaddr), asid = io.csr.satp.asid, allType = true) 198 io.ptw.req(idx).valid := RegNext(req_out_v(idx) && missVec(idx) && !ptw_just_back, false.B) // TODO: remove the regnext, timing 199 when (RegEnable(io.requestor(idx).req_kill, RegNext(io.requestor(idx).req.fire))) { 200 io.ptw.req(idx).valid := false.B 201 } 202 io.ptw.req(idx).bits.vpn := RegNext(get_pn(req_out(idx).vaddr)) 203 io.ptw.req(idx).bits.memidx := RegNext(req_out(idx).memidx) 204 } 205 206 def handle_block(idx: Int): Unit = { 207 // three valid: 1.if exist a entry; 2.if sent to ptw; 3.unset resp.valid 208 io.requestor(idx).req.ready := !req_out_v(idx) || io.requestor(idx).resp.fire() 209 // req_out_v for if there is a request, may long latency, fixme 210 211 // miss request entries 212 val miss_req_vpn = get_pn(req_out(idx).vaddr) 213 val miss_req_memidx = req_out(idx).memidx 214 val hit = io.ptw.resp.bits.hit(miss_req_vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.valid 215 216 val new_coming = RegNext(req_in(idx).fire && !req_in(idx).bits.kill && !flush_pipe(idx), false.B) 217 val miss_wire = new_coming && missVec(idx) 218 val miss_v = ValidHoldBypass(miss_wire, resp(idx).fire(), flush_pipe(idx)) 219 val miss_req_v = ValidHoldBypass(miss_wire || (miss_v && flush_mmu && !mmu_flush_pipe), 220 io.ptw.req(idx).fire() || resp(idx).fire(), flush_pipe(idx)) 221 222 // when ptw resp, check if hit, reset miss_v, resp to lsu/ifu 223 resp(idx).valid := req_out_v(idx) && !(miss_v && portTranslateEnable(idx)) 224 when (io.ptw.resp.fire() && hit && req_out_v(idx) && portTranslateEnable(idx)) { 225 val pte = io.ptw.resp.bits 226 resp(idx).valid := true.B 227 resp(idx).bits.miss := false.B // for blocked tlb, this is useless 228 for (d <- 0 until nRespDups) { 229 resp(idx).bits.paddr(d) := Cat(pte.genPPN(get_pn(req_out(idx).vaddr)), get_off(req_out(idx).vaddr)) 230 perm_check(pte, req_out(idx).cmd, idx, d) 231 } 232 pmp_check(resp(idx).bits.paddr(0), req_out(idx).size, req_out(idx).cmd, idx) 233 234 // NOTE: the unfiltered req would be handled by Repeater 235 } 236 assert(RegNext(!resp(idx).valid || resp(idx).ready, true.B), "when tlb resp valid, ready should be true, must") 237 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") 238 239 val ptw_req = io.ptw.req(idx) 240 ptw_req.valid := miss_req_v 241 ptw_req.bits.vpn := miss_req_vpn 242 ptw_req.bits.memidx := miss_req_memidx 243 244 // NOTE: when flush pipe, tlb should abandon last req 245 // however, some outside modules like icache, dont care flushPipe, and still waiting for tlb resp 246 // just resp valid and raise page fault to go through. The pipe(ifu) will abandon it. 247 if (!q.outsideRecvFlush) { 248 when (req_out_v(idx) && flush_pipe(idx) && portTranslateEnable(idx)) { 249 resp(idx).valid := true.B 250 for (d <- 0 until nRespDups) { 251 resp(idx).bits.excp(d).pf.ld := true.B // sfence happened, pf for not to use this addr 252 resp(idx).bits.excp(d).pf.st := true.B 253 resp(idx).bits.excp(d).pf.instr := true.B 254 } 255 } 256 } 257 } 258 259 // when ptw resp, tlb at refill_idx maybe set to miss by force. 260 // Bypass ptw resp to check. 261 def ptw_resp_bypass(vpn: UInt) = { 262 val p_hit = RegNext(ptw.resp.bits.hit(vpn, io.csr.satp.asid, allType = true) && io.ptw.resp.fire) 263 val p_ppn = RegEnable(ptw.resp.bits.genPPN(vpn), io.ptw.resp.fire) 264 val p_perm = RegEnable(ptwresp_to_tlbperm(ptw.resp.bits), io.ptw.resp.fire) 265 (p_hit, p_ppn, p_perm) 266 } 267 268 // assert 269 for(i <- 0 until Width) { 270 TimeOutAssert(req_out_v(i) && !resp(i).valid, timeOutThreshold, s"{q.name} port{i} long time no resp valid.") 271 } 272 273 // perf event 274 val result_ok = req_in.map(a => RegNext(a.fire())) 275 val perfEvents = 276 Seq( 277 ("access", PopCount((0 until Width).map{i => if (Block(i)) io.requestor(i).req.fire() else portTranslateEnable(i) && result_ok(i) })), 278 ("miss ", PopCount((0 until Width).map{i => if (Block(i)) portTranslateEnable(i) && result_ok(i) && missVec(i) else ptw.req(i).fire() })), 279 ) 280 generatePerfEvent() 281 282 // perf log 283 for (i <- 0 until Width) { 284 if (Block(i)) { 285 XSPerfAccumulate(s"access${i}",result_ok(i) && portTranslateEnable(i)) 286 XSPerfAccumulate(s"miss${i}", result_ok(i) && missVec(i)) 287 } else { 288 XSPerfAccumulate("first_access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && RegNext(req(i).bits.debug.isFirstIssue)) 289 XSPerfAccumulate("access" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i)) 290 XSPerfAccumulate("first_miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue)) 291 XSPerfAccumulate("miss" + Integer.toString(i, 10), result_ok(i) && portTranslateEnable(i) && missVec(i)) 292 } 293 } 294 XSPerfAccumulate("ptw_resp_count", ptw.resp.fire()) 295 XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire() && ptw.resp.bits.pf) 296 297 // Log 298 for(i <- 0 until Width) { 299 XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n") 300 XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n") 301 } 302 303 XSDebug(io.sfence.valid, p"Sfence: ${io.sfence}\n") 304 XSDebug(ParallelOR(req_out_v) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n") 305 for (i <- ptw.req.indices) { 306 XSDebug(ptw.req(i).fire(), p"L2TLB req:${ptw.req(i).bits}\n") 307 } 308 XSDebug(ptw.resp.valid, p"L2TLB resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n") 309 310 println(s"${q.name}: page: ${q.NWays} ${q.Associative} ${q.Replacer.get}") 311 312 if (env.EnableDifftest) { 313 for (i <- 0 until Width) { 314 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 315 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 316 val difftest = DifftestModule(new DiffL1TLBEvent) 317 difftest.clock := clock 318 difftest.coreid := p(XSCoreParamsKey).HartId.asUInt 319 difftest.valid := RegNext(io.requestor(i).req.fire) && !RegNext(io.requestor(i).req_kill) && io.requestor(i).resp.fire && !io.requestor(i).resp.bits.miss && !pf && !af && portTranslateEnable(i) 320 if (!Seq("itlb", "ldtlb", "sttlb").contains(q.name)) { 321 difftest.valid := false.B 322 } 323 difftest.index := TLBDiffId(p(XSCoreParamsKey).HartId).U 324 difftest.satp := io.csr.satp.ppn 325 difftest.vpn := RegNext(get_pn(req_in(i).bits.vaddr)) 326 difftest.ppn := get_pn(io.requestor(i).resp.bits.paddr(0)) 327 } 328 } 329} 330 331object TLBDiffId { 332 var i: Int = 0 333 var lastHartId: Int = -1 334 def apply(hartId: Int): Int = { 335 if (lastHartId != hartId) { 336 i = 0 337 lastHartId = hartId 338 } 339 i += 1 340 i - 1 341 } 342} 343 344class TLBNonBlock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(false), q) 345class TLBBLock(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends TLB(Width, nRespDups, Seq.fill(Width)(true), q) 346 347class TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule { 348 val io = IO(new TlbReplaceIO(Width, q)) 349 350 if (q.Associative == "fa") { 351 val re = ReplacementPolicy.fromString(q.Replacer, q.NWays) 352 re.access(io.page.access.map(_.touch_ways)) 353 io.page.refillIdx := re.way 354 } else { // set-acco && plru 355 val re = ReplacementPolicy.fromString(q.Replacer, q.NSets, q.NWays) 356 re.access(io.page.access.map(_.sets), io.page.access.map(_.touch_ways)) 357 io.page.refillIdx := { if (q.NWays == 1) 0.U else re.way(io.page.chosen_set) } 358 } 359} 360