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