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