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.util._ 22import xiangshan._ 23import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 24import utils._ 25import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 26import freechips.rocketchip.tilelink._ 27 28class PTWReapterIO(Width: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 29 val tlb = Flipped(new TlbPtwIO(Width)) 30 val ptw = new TlbPtwIO 31 32 def apply(tlb: TlbPtwIO, ptw: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 33 this.tlb <> tlb 34 this.ptw <> ptw 35 this.sfence <> sfence 36 this.csr <> csr 37 } 38 39 def apply(tlb: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 40 this.tlb <> tlb 41 this.sfence <> sfence 42 this.csr <> csr 43 } 44 45} 46 47class PTWRepeater(Width: Int = 1, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 48 val io = IO(new PTWReapterIO(Width)) 49 50 val req_in = if (Width == 1) { 51 io.tlb.req(0) 52 } else { 53 val arb = Module(new RRArbiter(io.tlb.req(0).bits.cloneType, Width)) 54 arb.io.in <> io.tlb.req 55 arb.io.out 56 } 57 val (tlb, ptw, flush) = (io.tlb, io.ptw, DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)) 58 val req = RegEnable(req_in.bits, req_in.fire()) 59 val resp = RegEnable(ptw.resp.bits, ptw.resp.fire()) 60 val haveOne = BoolStopWatch(req_in.fire(), tlb.resp.fire() || flush) 61 val sent = BoolStopWatch(ptw.req(0).fire(), req_in.fire() || flush) 62 val recv = BoolStopWatch(ptw.resp.fire() && haveOne, req_in.fire() || flush) 63 64 req_in.ready := !haveOne 65 ptw.req(0).valid := haveOne && !sent 66 ptw.req(0).bits := req 67 68 tlb.resp.bits := resp 69 tlb.resp.valid := haveOne && recv 70 ptw.resp.ready := !recv 71 72 XSPerfAccumulate("req_count", ptw.req(0).fire()) 73 XSPerfAccumulate("tlb_req_cycle", BoolStopWatch(req_in.fire(), tlb.resp.fire() || flush)) 74 XSPerfAccumulate("ptw_req_cycle", BoolStopWatch(ptw.req(0).fire(), ptw.resp.fire() || flush)) 75 76 XSDebug(haveOne, p"haveOne:${haveOne} sent:${sent} recv:${recv} sfence:${flush} req:${req} resp:${resp}") 77 XSDebug(req_in.valid || io.tlb.resp.valid, p"tlb: ${tlb}\n") 78 XSDebug(io.ptw.req(0).valid || io.ptw.resp.valid, p"ptw: ${ptw}\n") 79 assert(!RegNext(recv && io.ptw.resp.valid, init = false.B), "re-receive ptw.resp") 80 XSError(io.ptw.req(0).valid && io.ptw.resp.valid && !flush, "ptw repeater recv resp when sending") 81 XSError(io.ptw.resp.valid && (req.vpn =/= io.ptw.resp.bits.entry.tag), "ptw repeater recv resp with wrong tag") 82 XSError(io.ptw.resp.valid && !io.ptw.resp.ready, "ptw repeater's ptw resp back, but not ready") 83 TimeOutAssert(sent && !recv, timeOutThreshold, "Repeater doesn't recv resp in time") 84} 85 86/* dtlb 87 * 88 */ 89 90class PTWRepeaterNB(Width: Int = 1, passReady: Boolean = false, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 91 val io = IO(new PTWReapterIO(Width)) 92 93 val req_in = if (Width == 1) { 94 io.tlb.req(0) 95 } else { 96 val arb = Module(new RRArbiter(io.tlb.req(0).bits.cloneType, Width)) 97 arb.io.in <> io.tlb.req 98 arb.io.out 99 } 100 val (tlb, ptw, flush) = (io.tlb, io.ptw, DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay)) 101 /* sent: tlb -> repeater -> ptw 102 * recv: ptw -> repeater -> tlb 103 * different from PTWRepeater 104 */ 105 106 // tlb -> repeater -> ptw 107 val req = RegEnable(req_in.bits, req_in.fire()) 108 val sent = BoolStopWatch(req_in.fire(), ptw.req(0).fire() || flush) 109 req_in.ready := !sent || { if (passReady) ptw.req(0).ready else false.B } 110 ptw.req(0).valid := sent 111 ptw.req(0).bits := req 112 113 // ptw -> repeater -> tlb 114 val resp = RegEnable(ptw.resp.bits, ptw.resp.fire()) 115 val recv = BoolStopWatch(ptw.resp.fire(), tlb.resp.fire() || flush) 116 ptw.resp.ready := !recv || { if (passReady) tlb.resp.ready else false.B } 117 tlb.resp.valid := recv 118 tlb.resp.bits := resp 119 120 XSPerfAccumulate("req", req_in.fire()) 121 XSPerfAccumulate("resp", tlb.resp.fire()) 122 if (!passReady) { 123 XSPerfAccumulate("req_blank", req_in.valid && sent && ptw.req(0).ready) 124 XSPerfAccumulate("resp_blank", ptw.resp.valid && recv && tlb.resp.ready) 125 XSPerfAccumulate("req_blank_ignore_ready", req_in.valid && sent) 126 XSPerfAccumulate("resp_blank_ignore_ready", ptw.resp.valid && recv) 127 } 128 XSDebug(req_in.valid || io.tlb.resp.valid, p"tlb: ${tlb}\n") 129 XSDebug(io.ptw.req(0).valid || io.ptw.resp.valid, p"ptw: ${ptw}\n") 130} 131 132class PTWFilterIO(Width: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 133 val tlb = Flipped(new VectorTlbPtwIO(Width)) 134 val ptw = new TlbPtwIO() 135 136 def apply(tlb: VectorTlbPtwIO, ptw: TlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 137 this.tlb <> tlb 138 this.ptw <> ptw 139 this.sfence <> sfence 140 this.csr <> csr 141 } 142 143 def apply(tlb: VectorTlbPtwIO, sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 144 this.tlb <> tlb 145 this.sfence <> sfence 146 this.csr <> csr 147 } 148 149} 150 151class PTWFilter(Width: Int, Size: Int, FenceDelay: Int)(implicit p: Parameters) extends XSModule with HasPtwConst { 152 require(Size >= Width) 153 154 val io = IO(new PTWFilterIO(Width)) 155 156 val v = RegInit(VecInit(Seq.fill(Size)(false.B))) 157 val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports 158 val vpn = Reg(Vec(Size, UInt(vpnLen.W))) 159 val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq 160 val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw 161 val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq 162 val mayFullDeq = RegInit(false.B) 163 val mayFullIss = RegInit(false.B) 164 val counter = RegInit(0.U(log2Up(Size+1).W)) 165 166 val flush = DelayN(io.sfence.valid || io.csr.satp.changed, FenceDelay) 167 val tlb_req = WireInit(io.tlb.req) // NOTE: tlb_req is not io.tlb.req, see below codes, just use cloneType 168 tlb_req.suggestName("tlb_req") 169 170 val inflight_counter = RegInit(0.U(log2Up(Size + 1).W)) 171 val inflight_full = inflight_counter === Size.U 172 when (io.ptw.req(0).fire() =/= io.ptw.resp.fire()) { 173 inflight_counter := Mux(io.ptw.req(0).fire(), inflight_counter + 1.U, inflight_counter - 1.U) 174 } 175 176 val canEnqueue = Wire(Bool()) // NOTE: actually enqueue 177 val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire()) 178 val ptwResp_OldMatchVec = vpn.zip(v).map{ case (pi, vi) => 179 vi && io.ptw.resp.bits.entry.hit(pi, io.csr.satp.asid, true, true)} 180 val ptwResp_valid = RegNext(io.ptw.resp.fire() && Cat(ptwResp_OldMatchVec).orR, init = false.B) 181 val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).map{ case (pi, vi) => vi && pi === a.bits.vpn}) 182 val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn && canEnqueue}) 183 val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn)) 184 185 (0 until Width) foreach { i => 186 tlb_req(i).valid := RegNext(io.tlb.req(i).valid && 187 !(ptwResp_valid && ptwResp.entry.hit(io.tlb.req(i).bits.vpn, 0.U, true, true)) && 188 !Cat(lastReqMatchVec_early(i)).orR, 189 init = false.B) 190 tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid) 191 } 192 193 val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR)) 194 val newMatchVec = (0 until Width).map(i => (0 until Width).map(j => 195 RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid 196 )) 197 val ptwResp_newMatchVec = tlb_req.map(a => 198 ptwResp_valid && ptwResp.entry.hit(a.bits.vpn, 0.U, allType = true, true)) 199 200 val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid)) 201 val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i))) 202 val ports_init = (0 until Width).map(i => (1 << i).U(Width.W)) 203 val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i))) 204 val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire()) 205 206 def canMerge(index: Int) : Bool = { 207 ptwResp_newMatchVec(index) || oldMatchVec(index) || 208 Cat(newMatchVec(index).take(index)).orR 209 } 210 211 def filter_req() = { 212 val reqs = tlb_req.indices.map{ i => 213 val req = Wire(ValidIO(new PtwReq())) 214 val merge = canMerge(i) 215 req.bits := tlb_req(i).bits 216 req.valid := !merge && tlb_req(i).valid 217 req 218 } 219 reqs 220 } 221 222 val reqs = filter_req() 223 val req_ports = filter_ports 224 val isFull = enqPtr === deqPtr && mayFullDeq 225 val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq 226 val isEmptyIss = enqPtr === issPtr && !mayFullIss 227 val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid))) 228 val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U)) 229 val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i)))) 230 val enqNum = PopCount(reqs.map(_.valid)) 231 canEnqueue := counter +& enqNum <= Size.U 232 233 // the req may recv false ready, but actually received. Filter and TLB will handle it. 234 val enqNum_fake = PopCount(io.tlb.req.map(_.valid)) 235 val canEnqueue_fake = counter +& enqNum_fake <= Size.U 236 io.tlb.req.map(_.ready := canEnqueue_fake) // NOTE: just drop un-fire reqs 237 238 // tlb req flushed by ptw resp: last ptw resp && current ptw resp 239 // the flushed tlb req will fakely enq, with a false valid 240 val tlb_req_flushed = reqs.map(a => io.ptw.resp.valid && io.ptw.resp.bits.entry.hit(a.bits.vpn, 0.U, true, true)) 241 242 io.tlb.resp.valid := ptwResp_valid 243 io.tlb.resp.bits.data := ptwResp 244 io.tlb.resp.bits.vector := resp_vector 245 246 val issue_valid = v(issPtr) && !isEmptyIss && !inflight_full 247 val issue_filtered = ptwResp_valid && ptwResp.entry.hit(io.ptw.req(0).bits.vpn, io.csr.satp.asid, allType=true, ignoreAsid=true) 248 val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/)) 249 io.ptw.req(0).valid := issue_valid && !issue_filtered 250 io.ptw.req(0).bits.vpn := vpn(issPtr) 251 io.ptw.resp.ready := true.B 252 253 reqs.zipWithIndex.map{ 254 case (req, i) => 255 when (req.valid && canEnqueue) { 256 v(enqPtrVec(i)) := !tlb_req_flushed(i) 257 vpn(enqPtrVec(i)) := req.bits.vpn 258 ports(enqPtrVec(i)) := req_ports(i).asBools 259 } 260 } 261 for (i <- ports.indices) { 262 when (v(i)) { 263 ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2) 264 } 265 } 266 267 val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR 268 val do_deq = (!v(deqPtr) && !isEmptyDeq) 269 val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss) 270 when (do_enq) { 271 enqPtr := enqPtr + enqNum 272 } 273 when (do_deq) { 274 deqPtr := deqPtr + 1.U 275 } 276 when (do_iss) { 277 issPtr := issPtr + 1.U 278 } 279 when (issue_fire_fake && issue_filtered) { // issued but is filtered 280 v(issPtr) := false.B 281 } 282 when (do_enq =/= do_deq) { 283 mayFullDeq := do_enq 284 } 285 when (do_enq =/= do_iss) { 286 mayFullIss := do_enq 287 } 288 289 when (io.ptw.resp.fire()) { 290 v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }} 291 } 292 293 counter := counter - do_deq + Mux(do_enq, enqNum, 0.U) 294 assert(counter <= Size.U, "counter should be no more than Size") 295 assert(inflight_counter <= Size.U, "inflight should be no more than Size") 296 when (counter === 0.U) { 297 assert(!io.ptw.req(0).fire(), "when counter is 0, should not req") 298 assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty") 299 } 300 when (counter === Size.U) { 301 assert(mayFullDeq, "when counter is Size, should be full") 302 } 303 304 when (flush) { 305 v.map(_ := false.B) 306 deqPtr := 0.U 307 enqPtr := 0.U 308 issPtr := 0.U 309 ptwResp_valid := false.B 310 mayFullDeq := false.B 311 mayFullIss := false.B 312 counter := 0.U 313 inflight_counter := 0.U 314 } 315 316 // perf 317 XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid)))) 318 XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U)) 319 XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire()) 320 XSPerfAccumulate("ptw_req_cycle", inflight_counter) 321 XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire()) 322 XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire()) 323 XSPerfAccumulate("inflight_cycle", !isEmptyDeq) 324 for (i <- 0 until Size + 1) { 325 XSPerfAccumulate(s"counter${i}", counter === i.U) 326 } 327 328 for (i <- 0 until Size) { 329 TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time") 330 } 331} 332 333object PTWRepeater { 334 def apply(fenceDelay: Int, 335 tlb: TlbPtwIO, 336 sfence: SfenceBundle, 337 csr: TlbCsrBundle 338 )(implicit p: Parameters) = { 339 val width = tlb.req.size 340 val repeater = Module(new PTWRepeater(width, fenceDelay)) 341 repeater.io.apply(tlb, sfence, csr) 342 repeater 343 } 344 345 def apply(fenceDelay: Int, 346 tlb: TlbPtwIO, 347 ptw: TlbPtwIO, 348 sfence: SfenceBundle, 349 csr: TlbCsrBundle 350 )(implicit p: Parameters) = { 351 val width = tlb.req.size 352 val repeater = Module(new PTWRepeater(width, fenceDelay)) 353 repeater.io.apply(tlb, ptw, sfence, csr) 354 repeater 355 } 356} 357 358object PTWRepeaterNB { 359 def apply(passReady: Boolean, fenceDelay: Int, 360 tlb: TlbPtwIO, 361 sfence: SfenceBundle, 362 csr: TlbCsrBundle 363 )(implicit p: Parameters) = { 364 val width = tlb.req.size 365 val repeater = Module(new PTWRepeaterNB(width, passReady,fenceDelay)) 366 repeater.io.apply(tlb, sfence, csr) 367 repeater 368 } 369 370 def apply(passReady: Boolean, fenceDelay: Int, 371 tlb: TlbPtwIO, 372 ptw: TlbPtwIO, 373 sfence: SfenceBundle, 374 csr: TlbCsrBundle 375 )(implicit p: Parameters) = { 376 val width = tlb.req.size 377 val repeater = Module(new PTWRepeaterNB(width, passReady, fenceDelay)) 378 repeater.io.apply(tlb, ptw, sfence, csr) 379 repeater 380 } 381} 382 383object PTWFilter { 384 def apply(fenceDelay: Int, 385 tlb: VectorTlbPtwIO, 386 ptw: TlbPtwIO, 387 sfence: SfenceBundle, 388 csr: TlbCsrBundle, 389 size: Int 390 )(implicit p: Parameters) = { 391 val width = tlb.req.size 392 val filter = Module(new PTWFilter(width, size, fenceDelay)) 393 filter.io.apply(tlb, ptw, sfence, csr) 394 filter 395 } 396 397 def apply(fenceDelay: Int, 398 tlb: VectorTlbPtwIO, 399 sfence: SfenceBundle, 400 csr: TlbCsrBundle, 401 size: Int 402 )(implicit p: Parameters) = { 403 val width = tlb.req.size 404 val filter = Module(new PTWFilter(width, size, fenceDelay)) 405 filter.io.apply(tlb, sfence, csr) 406 filter 407 } 408 409} 410