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 org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import freechips.rocketchip.formal.PropertyClass 25import xiangshan.backend.fu.util.HasCSRConst 26 27import scala.math.min 28 29// For Direct-map TLBs, we do not use it now 30class BankedAsyncDataModuleTemplateWithDup[T <: Data]( 31 gen: T, 32 numEntries: Int, 33 numRead: Int, 34 numDup: Int, 35 numBanks: Int 36) extends Module { 37 val io = IO(new Bundle { 38 val raddr = Vec(numRead, Input(UInt(log2Ceil(numEntries).W))) 39 val rdata = Vec(numRead, Vec(numDup, Output(gen))) 40 val wen = Input(Bool()) 41 val waddr = Input(UInt(log2Ceil(numEntries).W)) 42 val wdata = Input(gen) 43 }) 44 require(numBanks > 1) 45 require(numEntries > numBanks) 46 47 val numBankEntries = numEntries / numBanks 48 def bankOffset(address: UInt): UInt = { 49 address(log2Ceil(numBankEntries) - 1, 0) 50 } 51 52 def bankIndex(address: UInt): UInt = { 53 address(log2Ceil(numEntries) - 1, log2Ceil(numBankEntries)) 54 } 55 56 val dataBanks = Seq.tabulate(numBanks)(i => { 57 val bankEntries = if (i < numBanks - 1) numBankEntries else (numEntries - (i * numBankEntries)) 58 Mem(bankEntries, gen) 59 }) 60 61 // async read, but regnext 62 for (i <- 0 until numRead) { 63 val data_read = Reg(Vec(numDup, Vec(numBanks, gen))) 64 val bank_index = Reg(Vec(numDup, UInt(numBanks.W))) 65 for (j <- 0 until numDup) { 66 bank_index(j) := UIntToOH(bankIndex(io.raddr(i))) 67 for (k <- 0 until numBanks) { 68 data_read(j)(k) := Mux(io.wen && (io.waddr === io.raddr(i)), 69 io.wdata, dataBanks(k)(bankOffset(io.raddr(i)))) 70 } 71 } 72 // next cycle 73 for (j <- 0 until numDup) { 74 io.rdata(i)(j) := Mux1H(bank_index(j), data_read(j)) 75 } 76 } 77 78 // write 79 for (i <- 0 until numBanks) { 80 when (io.wen && (bankIndex(io.waddr) === i.U)) { 81 dataBanks(i)(bankOffset(io.waddr)) := io.wdata 82 } 83 } 84} 85 86class TLBFA( 87 parentName: String, 88 ports: Int, 89 nDups: Int, 90 nSets: Int, 91 nWays: Int, 92 saveLevel: Boolean = false, 93 normalPage: Boolean, 94 superPage: Boolean 95)(implicit p: Parameters) extends TlbModule with HasPerfEvents { 96 97 val io = IO(new TlbStorageIO(nSets, nWays, ports, nDups)) 98 io.r.req.map(_.ready := true.B) 99 100 val v = RegInit(VecInit(Seq.fill(nWays)(false.B))) 101 val entries = Reg(Vec(nWays, new TlbSectorEntry(normalPage, superPage))) 102 val g = entries.map(_.perm.g) 103 104 for (i <- 0 until ports) { 105 val req = io.r.req(i) 106 val resp = io.r.resp(i) 107 val access = io.access(i) 108 109 val vpn = req.bits.vpn 110 val vpn_reg = RegEnable(vpn, req.fire) 111 val hasS2xlate = req.bits.s2xlate =/= noS2xlate 112 val OnlyS2 = req.bits.s2xlate === onlyStage2 113 val OnlyS1 = req.bits.s2xlate === onlyStage1 114 val refill_mask = Mux(io.w.valid, UIntToOH(io.w.bits.wayIdx), 0.U(nWays.W)) 115 val hitVec = VecInit((entries.zipWithIndex).zip(v zip refill_mask.asBools).map{ 116 case (e, m) => { 117 val s2xlate_hit = e._1.s2xlate === req.bits.s2xlate 118 val hit = e._1.hit(vpn, Mux(hasS2xlate, io.csr.vsatp.asid, io.csr.satp.asid), vmid = io.csr.hgatp.asid, hasS2xlate = hasS2xlate, onlyS2 = OnlyS2, onlyS1 = OnlyS1) 119 s2xlate_hit && hit && m._1 && !m._2 120 } 121 }) 122 123 hitVec.suggestName("hitVec") 124 125 val hitVecReg = RegEnable(hitVec, req.fire) 126 // Sector tlb may trigger multi-hit, see def "wbhit" 127 XSPerfAccumulate(s"port${i}_multi_hit", !(!resp.valid || (PopCount(hitVecReg) === 0.U || PopCount(hitVecReg) === 1.U))) 128 129 resp.valid := GatedValidRegNext(req.valid) 130 resp.bits.hit := Cat(hitVecReg).orR 131 val ppnReg = RegEnable(VecInit(entries.map(_.genPPN(saveLevel, req.valid)(vpn))), req.fire) 132 val permReg = RegEnable(VecInit(entries.map(_.perm)), req.fire) 133 val gPermReg = RegEnable(VecInit(entries.map(_.g_perm)), req.fire) 134 val s2xLate = RegEnable(VecInit(entries.map(_.s2xlate)), req.fire) 135 if (nWays == 1) { 136 for (d <- 0 until nDups) { 137 resp.bits.ppn(d) := ppnReg(0) 138 resp.bits.perm(d) := permReg(0) 139 resp.bits.g_perm(d) := gPermReg(0) 140 resp.bits.s2xlate(d) := s2xLate(0) 141 } 142 } else { 143 for (d <- 0 until nDups) { 144 resp.bits.ppn(d) := Mux1H(hitVecReg zip ppnReg) 145 resp.bits.perm(d) := Mux1H(hitVecReg zip permReg) 146 resp.bits.g_perm(d) := Mux1H(hitVecReg zip gPermReg) 147 resp.bits.s2xlate(d) := Mux1H(hitVecReg zip s2xLate) 148 } 149 } 150 151 access.sets := get_set_idx(vpn_reg(vpn_reg.getWidth - 1, sectortlbwidth), nSets) // no use 152 access.touch_ways.valid := resp.valid && Cat(hitVecReg).orR 153 access.touch_ways.bits := OHToUInt(hitVecReg) 154 155 resp.bits.hit.suggestName("hit") 156 resp.bits.ppn.suggestName("ppn") 157 resp.bits.perm.suggestName("perm") 158 resp.bits.g_perm.suggestName("g_perm") 159 } 160 161 when (io.w.valid) { 162 v(io.w.bits.wayIdx) := true.B 163 entries(io.w.bits.wayIdx).apply(io.w.bits.data) 164 } 165 // write assert, should not duplicate with the existing entries 166 val w_hit_vec = VecInit(entries.zip(v).map{case (e, vi) => e.wbhit(io.w.bits.data, Mux(io.w.bits.data.s2xlate =/= noS2xlate, io.csr.vsatp.asid, io.csr.satp.asid), s2xlate = io.w.bits.data.s2xlate) && vi }) 167 XSError(io.w.valid && Cat(w_hit_vec).orR, s"${parentName} refill, duplicate with existing entries") 168 169 val refill_vpn_reg = RegEnable(io.w.bits.data.s1.entry.tag, io.w.valid) 170 val refill_wayIdx_reg = RegEnable(io.w.bits.wayIdx, io.w.valid) 171 when (GatedValidRegNext(io.w.valid)) { 172 io.access.map { access => 173 access.sets := get_set_idx(refill_vpn_reg, nSets) 174 access.touch_ways.valid := true.B 175 access.touch_ways.bits := refill_wayIdx_reg 176 } 177 } 178 179 val sfence = io.sfence 180 val sfence_valid = sfence.valid && !sfence.bits.hg && !sfence.bits.hv 181 val sfence_vpn = sfence.bits.addr(VAddrBits - 1, offLen) 182 val sfenceHit = entries.map(_.hit(sfence_vpn, sfence.bits.id, vmid = io.csr.hgatp.asid, hasS2xlate = io.csr.priv.virt)) 183 val sfenceHit_noasid = entries.map(_.hit(sfence_vpn, sfence.bits.id, ignoreAsid = true, vmid = io.csr.hgatp.asid, hasS2xlate = io.csr.priv.virt)) 184 // Sfence will flush all sectors of an entry when hit 185 when (sfence_valid) { 186 when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U) 187 when (sfence.bits.rs2) { // asid, but i do not want to support asid, *.rs2 <- (rs2===0.U) 188 // all addr and all asid 189 v.zipWithIndex.map{ case(a, i) => a := a && !((io.csr.priv.virt === false.B && entries(i).s2xlate === noS2xlate) || 190 (io.csr.priv.virt && entries(i).s2xlate =/= noS2xlate && entries(i).vmid === io.csr.hgatp.asid))} 191 }.otherwise { 192 // all addr but specific asid 193 v.zipWithIndex.map{ case (a, i) => a := a && !(!g(i) && ((!io.csr.priv.virt && entries(i).s2xlate === noS2xlate && entries(i).asid === sfence.bits.id) || 194 (io.csr.priv.virt && entries(i).s2xlate =/= noS2xlate && entries(i).asid === sfence.bits.id && entries(i).vmid === io.csr.hgatp.asid)))} 195 } 196 }.otherwise { 197 when (sfence.bits.rs2) { 198 // specific addr but all asid 199 v.zipWithIndex.map{ case (a, i) => a := a & !sfenceHit_noasid(i) } 200 }.otherwise { 201 // specific addr and specific asid 202 v.zipWithIndex.map{ case (a, i) => a := a & !(sfenceHit(i) && !g(i)) } 203 } 204 } 205 } 206 207 val hfencev_valid = sfence.valid && sfence.bits.hv 208 val hfenceg_valid = sfence.valid && sfence.bits.hg 209 val hfencev = io.sfence 210 val hfencev_vpn = sfence_vpn 211 val hfencevHit = entries.map(_.hit(hfencev_vpn, hfencev.bits.id, vmid = io.csr.hgatp.asid, hasS2xlate = true.B)) 212 val hfencevHit_noasid = entries.map(_.hit(hfencev_vpn, 0.U, ignoreAsid = true, vmid = io.csr.hgatp.asid, hasS2xlate = true.B)) 213 when (hfencev_valid) { 214 when (hfencev.bits.rs1) { 215 when (hfencev.bits.rs2) { 216 v.zipWithIndex.map { case (a, i) => a := a && !(entries(i).s2xlate =/= noS2xlate && entries(i).vmid === io.csr.hgatp.asid)} 217 }.otherwise { 218 v.zipWithIndex.map { case (a, i) => a := a && !(!g(i) && (entries(i).s2xlate =/= noS2xlate && entries(i).asid === sfence.bits.id && entries(i).vmid === io.csr.hgatp.asid)) 219 } 220 } 221 }.otherwise { 222 when (hfencev.bits.rs2) { 223 v.zipWithIndex.map{ case (a, i) => a := a && !hfencevHit_noasid(i) } 224 }.otherwise { 225 v.zipWithIndex.map{ case (a, i) => a := a && !(hfencevHit(i) && !g(i)) } 226 } 227 } 228 } 229 230 231 val hfenceg = io.sfence 232 val hfenceg_gvpn = (sfence.bits.addr << 2)(VAddrBits - 1, offLen) 233 when (hfenceg_valid) { 234 when(hfenceg.bits.rs2) { 235 v.zipWithIndex.map { case (a, i) => a := a && !(entries(i).s2xlate =/= noS2xlate) } 236 }.otherwise { 237 v.zipWithIndex.map { case (a, i) => a := a && !(entries(i).s2xlate =/= noS2xlate && entries(i).vmid === sfence.bits.id) } 238 } 239 } 240 241 XSPerfAccumulate(s"access", io.r.resp.map(_.valid.asUInt).fold(0.U)(_ + _)) 242 XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt + _.asUInt)) 243 244 for (i <- 0 until nWays) { 245 XSPerfAccumulate(s"access${i}", io.r.resp.zip(io.access.map(acc => UIntToOH(acc.touch_ways.bits))).map{ case (a, b) => 246 a.valid && a.bits.hit && b(i)}.fold(0.U)(_.asUInt + _.asUInt)) 247 } 248 for (i <- 0 until nWays) { 249 XSPerfAccumulate(s"refill${i}", io.w.valid && io.w.bits.wayIdx === i.U) 250 } 251 252 val perfEvents = Seq( 253 ("tlbstore_access", io.r.resp.map(_.valid.asUInt).fold(0.U)(_ + _) ), 254 ("tlbstore_hit ", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt + _.asUInt)), 255 ) 256 generatePerfEvent() 257 258 println(s"${parentName} tlb_fa: nSets${nSets} nWays:${nWays}") 259} 260 261class TLBFakeFA( 262 ports: Int, 263 nDups: Int, 264 nSets: Int, 265 nWays: Int, 266 useDmode: Boolean = false 267 )(implicit p: Parameters) extends TlbModule with HasCSRConst{ 268 269 val io = IO(new TlbStorageIO(nSets, nWays, ports, nDups)) 270 io.r.req.map(_.ready := true.B) 271 val mode = if (useDmode) io.csr.priv.dmode else io.csr.priv.imode 272 val vmEnable = if (EnbaleTlbDebug) (io.csr.satp.mode === 8.U) 273 else (io.csr.satp.mode === 8.U && (mode < ModeM)) 274 275 for (i <- 0 until ports) { 276 val req = io.r.req(i) 277 val resp = io.r.resp(i) 278 279 val helper = Module(new PTEHelper()) 280 helper.clock := clock 281 helper.satp := io.csr.satp.ppn 282 helper.enable := req.fire && vmEnable 283 helper.vpn := req.bits.vpn 284 285 val pte = helper.pte.asTypeOf(new PteBundle) 286 val ppn = pte.ppn 287 val vpn_reg = RegEnable(req.bits.vpn, req.valid) 288 val pf = helper.pf 289 val level = helper.level 290 291 resp.valid := GatedValidRegNext(req.valid) 292 resp.bits.hit := true.B 293 for (d <- 0 until nDups) { 294 resp.bits.perm(d).pf := pf 295 resp.bits.perm(d).af := false.B 296 resp.bits.perm(d).d := pte.perm.d 297 resp.bits.perm(d).a := pte.perm.a 298 resp.bits.perm(d).g := pte.perm.g 299 resp.bits.perm(d).u := pte.perm.u 300 resp.bits.perm(d).x := pte.perm.x 301 resp.bits.perm(d).w := pte.perm.w 302 resp.bits.perm(d).r := pte.perm.r 303 304 resp.bits.ppn(d) := MuxLookup(level, 0.U)(Seq( 305 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn_reg(vpnnLen*2-1, 0)), 306 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn_reg(vpnnLen-1, 0)), 307 2.U -> ppn) 308 ) 309 } 310 } 311 312 io.access := DontCare 313} 314 315object TlbStorage { 316 def apply 317 ( 318 parentName: String, 319 associative: String, 320 ports: Int, 321 nDups: Int = 1, 322 nSets: Int, 323 nWays: Int, 324 saveLevel: Boolean = false, 325 normalPage: Boolean, 326 superPage: Boolean, 327 useDmode: Boolean, 328 SoftTLB: Boolean 329 )(implicit p: Parameters) = { 330 if (SoftTLB) { 331 val storage = Module(new TLBFakeFA(ports, nDups, nSets, nWays, useDmode)) 332 storage.suggestName(s"${parentName}_fake_fa") 333 storage.io 334 } else { 335 val storage = Module(new TLBFA(parentName, ports, nDups, nSets, nWays, saveLevel, normalPage, superPage)) 336 storage.suggestName(s"${parentName}_fa") 337 storage.io 338 } 339 } 340} 341 342class TlbStorageWrapper(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends TlbModule { 343 val io = IO(new TlbStorageWrapperIO(ports, q, nDups)) 344 345 val page = TlbStorage( 346 parentName = q.name + "_storage", 347 associative = q.Associative, 348 ports = ports, 349 nDups = nDups, 350 nSets = q.NSets, 351 nWays = q.NWays, 352 normalPage = true, 353 superPage = true, 354 useDmode = q.useDmode, 355 SoftTLB = coreParams.softTLB 356 ) 357 358 for (i <- 0 until ports) { 359 page.r_req_apply( 360 valid = io.r.req(i).valid, 361 vpn = io.r.req(i).bits.vpn, 362 i = i, 363 s2xlate = io.r.req(i).bits.s2xlate 364 ) 365 } 366 367 for (i <- 0 until ports) { 368 val q = page.r.req(i) 369 val p = page.r.resp(i) 370 val rq = io.r.req(i) 371 val rp = io.r.resp(i) 372 rq.ready := q.ready // actually, not used 373 rp.valid := p.valid // actually, not used 374 rp.bits.hit := p.bits.hit 375 for (d <- 0 until nDups) { 376 rp.bits.ppn(d) := p.bits.ppn(d) 377 rp.bits.perm(d).pf := p.bits.perm(d).pf 378 rp.bits.perm(d).af := p.bits.perm(d).af 379 rp.bits.perm(d).d := p.bits.perm(d).d 380 rp.bits.perm(d).a := p.bits.perm(d).a 381 rp.bits.perm(d).g := p.bits.perm(d).g 382 rp.bits.perm(d).u := p.bits.perm(d).u 383 rp.bits.perm(d).x := p.bits.perm(d).x 384 rp.bits.perm(d).w := p.bits.perm(d).w 385 rp.bits.perm(d).r := p.bits.perm(d).r 386 rp.bits.s2xlate(d) := p.bits.s2xlate(d) 387 rp.bits.g_perm(d) := p.bits.g_perm(d) 388 } 389 } 390 391 page.sfence <> io.sfence 392 page.csr <> io.csr 393 394 val refill_idx = if (q.outReplace) { 395 io.replace.page.access <> page.access 396 io.replace.page.chosen_set := DontCare 397 io.replace.page.refillIdx 398 } else { 399 val re = ReplacementPolicy.fromString(q.Replacer, q.NWays) 400 re.access(page.access.map(_.touch_ways)) 401 re.way 402 } 403 404 page.w_apply( 405 valid = io.w.valid, 406 wayIdx = refill_idx, 407 data = io.w.bits.data 408 ) 409 410 // replacement 411 def get_access(one_hot: UInt, valid: Bool): Valid[UInt] = { 412 val res = Wire(Valid(UInt(log2Up(one_hot.getWidth).W))) 413 res.valid := Cat(one_hot).orR && valid 414 res.bits := OHToUInt(one_hot) 415 res 416 } 417} 418