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 chisel3.experimental.chiselName 23import freechips.rocketchip.util.SRAMAnnotation 24import xiangshan._ 25import utils._ 26 27@chiselName 28class TLBFA( 29 sameCycle: Boolean, 30 ports: Int, 31 nSets: Int, 32 nWays: Int, 33 sramSinglePort: Boolean, 34 normalPage: Boolean, 35 superPage: Boolean 36)(implicit p: Parameters) extends TlbModule{ 37 38 val io = IO(new TlbStorageIO(nSets, nWays, ports)) 39 io.r.req.map(_.ready := true.B) 40 41 val v = RegInit(VecInit(Seq.fill(nWays)(false.B))) 42 val entries = Reg(Vec(nWays, new TlbEntry(normalPage, superPage))) 43 val g = entries.map(_.perm.g) 44 45 for (i <- 0 until ports) { 46 val req = io.r.req(i) 47 val resp = io.r.resp(i) 48 val access = io.access(i) 49 50 val vpn = req.bits.vpn 51 val vpn_reg = if (sameCycle) vpn else RegEnable(vpn, req.fire()) 52 53 val refill_mask = if (sameCycle) 0.U(nWays.W) else Mux(io.w.valid, UIntToOH(io.w.bits.wayIdx), 0.U(nWays.W)) 54 val hitVec = VecInit((entries.zipWithIndex).zip(v zip refill_mask.asBools).map{case (e, m) => e._1.hit(vpn, io.csr.satp.asid) && m._1 && !m._2 }) 55 56 hitVec.suggestName("hitVec") 57 58 val hitVecReg = if (sameCycle) hitVec else RegEnable(hitVec, req.fire()) 59 60 resp.valid := { if (sameCycle) req.valid else RegNext(req.valid) } 61 resp.bits.hit := Cat(hitVecReg).orR 62 resp.bits.ppn := ParallelMux(hitVecReg zip entries.map(_.genPPN(vpn_reg))) 63 resp.bits.perm := ParallelMux(hitVecReg zip entries.map(_.perm)) 64 io.r.resp_hit_sameCycle(i) := Cat(hitVec).orR 65 66 access.sets := get_set_idx(vpn_reg, nSets) // no use 67 access.touch_ways.valid := resp.valid && Cat(hitVecReg).orR 68 access.touch_ways.bits := OHToUInt(hitVecReg) 69 70 resp.bits.hit.suggestName("hit") 71 resp.bits.ppn.suggestName("ppn") 72 resp.bits.perm.suggestName("perm") 73 } 74 75 when (io.w.valid) { 76 v(io.w.bits.wayIdx) := true.B 77 entries(io.w.bits.wayIdx).apply(io.w.bits.data, io.csr.satp.asid) 78 } 79 80 val refill_vpn_reg = RegNext(io.w.bits.data.entry.tag) 81 val refill_wayIdx_reg = RegNext(io.w.bits.wayIdx) 82 when (RegNext(io.w.valid)) { 83 io.access.map { access => 84 access.sets := get_set_idx(refill_vpn_reg, nSets) 85 access.touch_ways.valid := true.B 86 access.touch_ways.bits := refill_wayIdx_reg 87 } 88 } 89 90 val sfence = io.sfence 91 val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn 92 val sfenceHit = entries.map(_.hit(sfence_vpn, sfence.bits.asid)) 93 val sfenceHit_noasid = entries.map(_.hit(sfence_vpn, sfence.bits.asid, ignoreAsid = true)) 94 when (io.sfence.valid) { 95 when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U) 96 when (sfence.bits.rs2) { // asid, but i do not want to support asid, *.rs2 <- (rs2===0.U) 97 // all addr and all asid 98 v.map(_ := false.B) 99 }.otherwise { 100 // all addr but specific asid 101 v.zipWithIndex.map{ case (a,i) => a := a & (g(i) | !(entries(i).asid === sfence.bits.asid)) } 102 } 103 }.otherwise { 104 when (sfence.bits.rs2) { 105 // specific addr but all asid 106 v.zipWithIndex.map{ case (a,i) => a := a & !sfenceHit_noasid(i) } 107 }.otherwise { 108 // specific addr and specific asid 109 v.zipWithIndex.map{ case (a,i) => a := a & !(sfenceHit(i) && !g(i)) } 110 } 111 } 112 } 113 114 val victim_idx = io.w.bits.wayIdx 115 io.victim.out.valid := v(victim_idx) && io.w.valid && entries(victim_idx).level.getOrElse(3.U) === 2.U 116 io.victim.out.bits.entry := ns_to_n(entries(victim_idx)) 117 118 def ns_to_n(ns: TlbEntry): TlbEntry = { 119 val n = Wire(new TlbEntry(pageNormal = true, pageSuper = false)) 120 n.perm := ns.perm 121 n.ppn := ns.ppn 122 n.tag := ns.tag 123 n.asid := ns.asid 124 n 125 } 126 127 XSPerfAccumulate(s"access", io.r.resp.map(_.valid.asUInt()).fold(0.U)(_ + _)) 128 XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt())) 129 130 for (i <- 0 until nWays) { 131 XSPerfAccumulate(s"access${i}", io.r.resp.zip(io.access.map(acc => UIntToOH(acc.touch_ways.bits))).map{ case (a, b) => 132 a.valid && a.bits.hit && b(i)}.fold(0.U)(_.asUInt() + _.asUInt())) 133 } 134 for (i <- 0 until nWays) { 135 XSPerfAccumulate(s"refill${i}", io.w.valid && io.w.bits.wayIdx === i.U) 136 } 137 138 val perfinfo = IO(new Bundle(){ 139 val perfEvents = Output(new PerfEventsBundle(2)) 140 }) 141 val perfEvents = Seq( 142 ("tlbstore_access ", io.r.resp.map(_.valid.asUInt()).fold(0.U)(_ + _) ), 143 ("tlbstore_hit ", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt())), 144 ) 145 146 for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) { 147 perf_out.incr_step := RegNext(perf) 148 } 149 150 println(s"tlb_fa: nSets${nSets} nWays:${nWays}") 151} 152 153@chiselName 154class TLBSA( 155 sameCycle: Boolean, 156 ports: Int, 157 nSets: Int, 158 nWays: Int, 159 sramSinglePort: Boolean, 160 normalPage: Boolean, 161 superPage: Boolean 162)(implicit p: Parameters) extends TlbModule { 163 require(!superPage, "super page should use reg/fa") 164 require(!sameCycle, "sram needs next cycle") 165 166 val io = IO(new TlbStorageIO(nSets, nWays, ports)) 167 168 io.r.req.map(_.ready := { if (sramSinglePort) !io.w.valid else true.B }) 169 val v = RegInit(VecInit(Seq.fill(nSets)(VecInit(Seq.fill(nWays)(false.B))))) 170 171 for (i <- 0 until ports) { // duplicate sram 172 val entries = Module(new SRAMTemplate( 173 new TlbEntry(normalPage, superPage), 174 set = nSets, 175 way = nWays, 176 singlePort = sramSinglePort 177 )) 178 179 val req = io.r.req(i) 180 val resp = io.r.resp(i) 181 val access = io.access(i) 182 183 val vpn = req.bits.vpn 184 val vpn_reg = RegEnable(vpn, req.fire()) 185 186 val ridx = get_set_idx(vpn, nSets) 187 val vidx = RegNext(Mux(req.fire(), v(ridx), VecInit(Seq.fill(nWays)(false.B)))) 188 entries.io.r.req.valid := req.valid 189 entries.io.r.req.bits.apply(setIdx = ridx) 190 191 val data = entries.io.r.resp.data 192 val hitVec = VecInit(data.zip(vidx).map { case (e, vi) => e.hit(vpn_reg, io.csr.satp.asid, nSets) && vi }) 193 resp.bits.hit := Cat(hitVec).orR && RegNext(req.ready, init = false.B) 194 resp.bits.ppn := ParallelMux(hitVec zip data.map(_.genPPN(vpn_reg))) 195 resp.bits.perm := ParallelMux(hitVec zip data.map(_.perm)) 196 io.r.resp_hit_sameCycle(i) := DontCare 197 198 resp.valid := { 199 if (sramSinglePort) RegNext(req.fire()) else RegNext(req.valid) 200 } 201 resp.bits.hit.suggestName("hit") 202 resp.bits.ppn.suggestName("ppn") 203 resp.bits.perm.suggestName("perm") 204 205 access.sets := get_set_idx(vpn_reg, nSets) // no use 206 access.touch_ways.valid := resp.valid && Cat(hitVec).orR 207 access.touch_ways.bits := OHToUInt(hitVec) 208 209 entries.io.w.apply( 210 valid = io.w.valid || io.victim.in.valid, 211 setIdx = Mux(io.w.valid, get_set_idx(io.w.bits.data.entry.tag, nSets), get_set_idx(io.victim.in.bits.entry.tag, nSets)), 212 data = Mux(io.w.valid, (Wire(new TlbEntry(normalPage, superPage)).apply(io.w.bits.data, io.csr.satp.asid)), io.victim.in.bits.entry), 213 waymask = UIntToOH(io.w.bits.wayIdx) 214 ) 215 } 216 217 when (io.victim.in.valid) { 218 v(get_set_idx(io.victim.in.bits.entry.tag, nSets))(io.w.bits.wayIdx) := true.B 219 } 220 // w has higher priority than victim 221 when (io.w.valid) { 222 v(get_set_idx(io.w.bits.data.entry.tag, nSets))(io.w.bits.wayIdx) := true.B 223 } 224 225 val refill_vpn_reg = RegNext(Mux(io.victim.in.valid, io.victim.in.bits.entry.tag, io.w.bits.data.entry.tag)) 226 val refill_wayIdx_reg = RegNext(io.w.bits.wayIdx) 227 when (RegNext(io.w.valid || io.victim.in.valid)) { 228 io.access.map { access => 229 access.sets := get_set_idx(refill_vpn_reg, nSets) 230 access.touch_ways.valid := true.B 231 access.touch_ways.bits := refill_wayIdx_reg 232 } 233 } 234 235 val sfence = io.sfence 236 val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn 237 when (io.sfence.valid) { 238 when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U) 239 v.map(a => a.map(b => b := false.B)) 240 }.otherwise { 241 // specific addr but all asid 242 v(get_set_idx(sfence_vpn, nSets)).map(_ := false.B) 243 } 244 } 245 246 io.victim.out := DontCare 247 248 XSPerfAccumulate(s"access", io.r.req.map(_.valid.asUInt()).fold(0.U)(_ + _)) 249 XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt())) 250 251 for (i <- 0 until nSets) { 252 for (j <- 0 until nWays) { 253 XSPerfAccumulate(s"refill${i}_${j}", (io.w.valid || io.victim.in.valid) && 254 (Mux(io.w.valid, get_set_idx(io.w.bits.data.entry.tag, nSets), get_set_idx(io.victim.in.bits.entry.tag, nSets)) === i.U) && 255 (j.U === io.w.bits.wayIdx) 256 ) 257 } 258 } 259 260 for (i <- 0 until nSets) { 261 for (j <- 0 until nWays) { 262 XSPerfAccumulate(s"hit${i}_${j}", io.r.resp.map(_.valid) 263 .zip(io.access.map(a => UIntToOH(a.touch_ways.bits)(j))) 264 .map{case(vi, hi) => vi && hi } 265 .zip(io.r.req.map(a => RegNext(get_set_idx(a.bits.vpn, nSets)) === i.U)) 266 .map{a => (a._1 && a._2).asUInt()} 267 .fold(0.U)(_ + _) 268 ) 269 } 270 } 271 272 for (i <- 0 until nSets) { 273 XSPerfAccumulate(s"access${i}", io.r.resp.map(_.valid) 274 .zip(io.r.req.map(a => RegNext(get_set_idx(a.bits.vpn, nSets)) === i.U)) 275 .map{a => (a._1 && a._2).asUInt()} 276 .fold(0.U)(_ + _) 277 ) 278 } 279 280 println(s"tlb_sa: nSets:${nSets} nWays:${nWays}") 281} 282 283object TlbStorage { 284 def apply 285 ( 286 name: String, 287 associative: String, 288 sameCycle: Boolean, 289 ports: Int, 290 nSets: Int, 291 nWays: Int, 292 sramSinglePort: Boolean, 293 normalPage: Boolean, 294 superPage: Boolean 295 )(implicit p: Parameters) = { 296 if (associative == "fa") { 297 val storage = Module(new TLBFA(sameCycle, ports, nSets, nWays, sramSinglePort, normalPage, superPage)) 298 storage.suggestName(s"tlb_${name}_fa") 299 storage.io 300 } else { 301 val storage = Module(new TLBSA(sameCycle, ports, nSets, nWays, sramSinglePort, normalPage, superPage)) 302 storage.suggestName(s"tlb_${name}_sa") 303 storage.io 304 } 305 } 306} 307