xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLBStorage.scala (revision c3abb8b6b92c14ec0f3dbbac60a8caa531994a95)
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
49    val vpn = req.bits.vpn
50    val vpn_reg = if (sameCycle) vpn else RegEnable(vpn, req.fire())
51
52    val refill_mask = if (sameCycle) 0.U(nWays.W) else Mux(io.w.valid, UIntToOH(io.w.bits.wayIdx), 0.U(nWays.W))
53    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 })
54
55    hitVec.suggestName("hitVec")
56
57    val hitVecReg = if (sameCycle) hitVec else RegEnable(hitVec, req.fire())
58
59    resp.valid := { if (sameCycle) req.valid else RegNext(req.valid) }
60    resp.bits.hit := Cat(hitVecReg).orR
61    resp.bits.ppn := ParallelMux(hitVecReg zip entries.map(_.genPPN(vpn_reg)))
62    resp.bits.perm := ParallelMux(hitVecReg zip entries.map(_.perm))
63    resp.bits.hitVec := hitVecReg.asUInt
64
65    resp.bits.hit.suggestName("hit")
66    resp.bits.ppn.suggestName("ppn")
67    resp.bits.perm.suggestName("perm")
68    resp.bits.hitVec.suggestName("hitVec")
69  }
70
71  when (io.w.valid) {
72    v(io.w.bits.wayIdx) := true.B
73    entries(io.w.bits.wayIdx).apply(io.w.bits.data, io.csr.satp.asid)
74  }
75
76  val sfence = io.sfence
77  val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn
78  val sfenceHit = entries.map(_.hit(sfence_vpn, sfence.bits.asid))
79  val sfenceHit_noasid = entries.map(_.hit(sfence_vpn, sfence.bits.asid, ignoreAsid = true))
80  when (io.sfence.valid) {
81    when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U)
82      when (sfence.bits.rs2) { // asid, but i do not want to support asid, *.rs2 <- (rs2===0.U)
83        // all addr and all asid
84        v.map(_ := false.B)
85      }.otherwise {
86        // all addr but specific asid
87        v.zipWithIndex.map{ case (a,i) => a := a & (g(i) | !(entries(i).asid === sfence.bits.asid)) }
88      }
89    }.otherwise {
90      when (sfence.bits.rs2) {
91        // specific addr but all asid
92        v.zipWithIndex.map{ case (a,i) => a := a & !sfenceHit_noasid(i) }
93      }.otherwise {
94        // specific addr and specific asid
95        v.zipWithIndex.map{ case (a,i) => a := a & !(sfenceHit(i) && !g(i)) }
96      }
97    }
98  }
99
100  val victim_idx = io.w.bits.wayIdx
101  io.victim.out.valid := v(victim_idx) && io.w.valid && entries(victim_idx).level.getOrElse(3.U) === 2.U
102  io.victim.out.bits.entry := ns_to_n(entries(victim_idx))
103
104  def ns_to_n(ns: TlbEntry): TlbEntry = {
105    val n = Wire(new TlbEntry(pageNormal = true, pageSuper = false))
106    n.perm := ns.perm
107    n.ppn := ns.ppn
108    n.tag := ns.tag
109    n.asid := ns.asid
110    n
111  }
112
113  XSPerfAccumulate(s"access", io.r.resp.map(_.valid.asUInt()).fold(0.U)(_ + _))
114  XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt()))
115
116  for (i <- 0 until nWays) {
117    XSPerfAccumulate(s"access${i}", io.r.resp.map(a => a.valid && a.bits.hit && a.bits.hitVec(i)).fold(0.U)(_.asUInt
118    () + _.asUInt()))
119  }
120  for (i <- 0 until nWays) {
121    XSPerfAccumulate(s"refill${i}", io.w.valid && io.w.bits.wayIdx === i.U)
122  }
123
124  println(s"tlb_fa: nSets${nSets} nWays:${nWays}")
125}
126
127@chiselName
128class TLBSA(
129  sameCycle: Boolean,
130  ports: Int,
131  nSets: Int,
132  nWays: Int,
133  sramSinglePort: Boolean,
134  normalPage: Boolean,
135  superPage: Boolean
136)(implicit p: Parameters) extends TlbModule {
137  require(!superPage, "super page should use reg/fa")
138  require(!sameCycle, "sram needs next cycle")
139
140  val io = IO(new TlbStorageIO(nSets, nWays, ports))
141
142  io.r.req.map(_.ready := { if (sramSinglePort) !io.w.valid else true.B })
143  val v = RegInit(VecInit(Seq.fill(nSets)(VecInit(Seq.fill(nWays)(false.B)))))
144
145  for (i <- 0 until ports) { // duplicate sram
146    val entries = Module(new SRAMTemplate(
147      new TlbEntry(normalPage, superPage),
148      set = nSets,
149      way = nWays,
150      singlePort = sramSinglePort
151    ))
152
153    val req = io.r.req(i)
154    val resp = io.r.resp(i)
155
156    val vpn = req.bits.vpn
157    val vpn_reg = RegEnable(vpn, req.fire())
158
159    val ridx = get_idx(vpn, nSets)
160    val vidx = RegNext(Mux(req.fire(), v(ridx), VecInit(Seq.fill(nWays)(false.B))))
161    entries.io.r.req.valid := req.valid
162    entries.io.r.req.bits.apply(setIdx = ridx)
163
164    val data = entries.io.r.resp.data
165    val hitVec = VecInit(data.zip(vidx).map { case (e, vi) => e.hit(vpn_reg, io.csr.satp.asid) && vi })
166    resp.bits.hit := Cat(hitVec).orR && RegNext(req.ready, init = false.B)
167    resp.bits.ppn := ParallelMux(hitVec zip data.map(_.genPPN(vpn_reg)))
168    resp.bits.perm := ParallelMux(hitVec zip data.map(_.perm))
169    resp.bits.hitVec := hitVec.asUInt
170
171    resp.valid := {
172      if (sramSinglePort) RegNext(req.fire()) else RegNext(req.valid)
173    }
174    resp.bits.hit.suggestName("hit")
175    resp.bits.ppn.suggestName("ppn")
176    resp.bits.perm.suggestName("perm")
177    resp.bits.hitVec.suggestName("hitVec")
178
179    entries.io.w.apply(
180      valid = io.w.valid || io.victim.in.valid,
181      setIdx = Mux(io.w.valid, get_idx(io.w.bits.data.entry.tag, nSets), get_idx(io.victim.in.bits.entry.tag, nSets)),
182      data = Mux(io.w.valid, (Wire(new TlbEntry(normalPage, superPage)).apply(io.w.bits.data, io.csr.satp.asid)), io.victim.in.bits.entry),
183      waymask = UIntToOH(io.w.bits.wayIdx)
184    )
185  }
186  when (io.w.valid) {
187    v(get_idx(io.w.bits.data.entry.tag, nSets))(io.w.bits.wayIdx) := true.B
188  }
189  when (io.victim.in.valid) {
190    v(get_idx(io.victim.in.bits.entry.tag, nSets))(io.w.bits.wayIdx) := true.B
191  }
192
193  val sfence = io.sfence
194  val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn
195  when (io.sfence.valid) {
196    when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U)
197        v.map(a => a.map(b => b := false.B))
198    }.otherwise {
199        // specific addr but all asid
200        v(get_idx(sfence_vpn, nSets)).map(_ := false.B)
201    }
202  }
203
204  io.victim.out := DontCare
205
206  XSPerfAccumulate(s"access", io.r.req.map(_.valid.asUInt()).fold(0.U)(_ + _))
207  XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt()))
208
209  for (i <- 0 until nSets) {
210    for (j <- 0 until nWays) {
211      XSPerfAccumulate(s"refill${i}_${j}", (io.w.valid || io.victim.in.valid) &&
212        (Mux(io.w.valid, get_idx(io.w.bits.data.entry.tag, nSets), get_idx(io.victim.in.bits.entry.tag, nSets)) === i.U) &&
213        (j.U === io.w.bits.wayIdx)
214      )
215    }
216  }
217
218  for (i <- 0 until nSets) {
219    for (j <- 0 until nWays) {
220      XSPerfAccumulate(s"hit${i}_${j}", io.r.resp.map(_.valid)
221        .zip(io.r.resp.map(_.bits.hitVec(j)))
222        .map{case(vi, hi) => vi && hi }
223        .zip(io.r.req.map(a => RegNext(get_idx(a.bits.vpn, nSets)) === i.U))
224        .map{a => (a._1 && a._2).asUInt()}
225        .fold(0.U)(_ + _)
226      )
227    }
228  }
229
230  for (i <- 0 until nSets) {
231    XSPerfAccumulate(s"access${i}", io.r.resp.map(_.valid)
232      .zip(io.r.req.map(a => RegNext(get_idx(a.bits.vpn, nSets)) === i.U))
233      .map{a => (a._1 && a._2).asUInt()}
234      .fold(0.U)(_ + _)
235    )
236  }
237
238  println(s"tlb_sa: nSets:${nSets} nWays:${nWays}")
239}
240
241object TlbStorage {
242  def apply
243  (
244    name: String,
245    associative: String,
246    sameCycle: Boolean,
247    ports: Int,
248    nSets: Int,
249    nWays: Int,
250    sramSinglePort: Boolean,
251    normalPage: Boolean,
252    superPage: Boolean
253  )(implicit p: Parameters) = {
254    if (associative == "fa") {
255       val storage = Module(new TLBFA(sameCycle, ports, nSets, nWays, sramSinglePort, normalPage, superPage))
256       storage.suggestName(s"tlb_${name}_fa")
257       storage.io
258    } else {
259       val storage = Module(new TLBSA(sameCycle, ports, nSets, nWays, sramSinglePort, normalPage, superPage))
260       storage.suggestName(s"tlb_${name}_sa")
261       storage.io
262    }
263  }
264}