xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/Repeater.scala (revision 7a2fc509e2d355879c4db3dc3f17a6ccacd3d09e)
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 ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire())
168  val ptwResp_OldMatchVec = vpn.zip(v).map{ case (pi, vi) =>
169    vi && io.ptw.resp.bits.entry.hit(pi, io.csr.satp.asid, true, true)}
170  val ptwResp_valid = RegNext(io.ptw.resp.fire() && Cat(ptwResp_OldMatchVec).orR, init = false.B)
171  val oldMatchVec_early = io.tlb.req.map(a => vpn.zip(v).map{ case (pi, vi) => vi && pi === a.bits.vpn})
172  val lastReqMatchVec_early = io.tlb.req.map(a => tlb_req.map{ b => b.valid && b.bits.vpn === a.bits.vpn})
173  val newMatchVec_early = io.tlb.req.map(a => io.tlb.req.map(b => a.bits.vpn === b.bits.vpn))
174
175  (0 until Width) foreach { i =>
176    tlb_req(i).valid := RegNext(io.tlb.req(i).valid &&
177      !(ptwResp_valid && ptwResp.entry.hit(io.tlb.req(i).bits.vpn, 0.U, true, true)) &&
178      !Cat(lastReqMatchVec_early(i)).orR,
179      init = false.B)
180    tlb_req(i).bits := RegEnable(io.tlb.req(i).bits, io.tlb.req(i).valid)
181  }
182
183  val oldMatchVec = oldMatchVec_early.map(a => RegNext(Cat(a).orR))
184  val newMatchVec = (0 until Width).map(i => (0 until Width).map(j =>
185    RegNext(newMatchVec_early(i)(j)) && tlb_req(j).valid
186  ))
187  val ptwResp_newMatchVec = tlb_req.map(a =>
188    ptwResp_valid && ptwResp.entry.hit(a.bits.vpn, 0.U, allType = true, true))
189
190  val oldMatchVec2 = (0 until Width).map(i => oldMatchVec_early(i).map(RegNext(_)).map(_ & tlb_req(i).valid))
191  val update_ports = v.indices.map(i => oldMatchVec2.map(j => j(i)))
192  val ports_init = (0 until Width).map(i => (1 << i).U(Width.W))
193  val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i)))
194  val resp_vector = RegEnable(ParallelMux(ptwResp_OldMatchVec zip ports), io.ptw.resp.fire())
195
196  def canMerge(index: Int) : Bool = {
197    ptwResp_newMatchVec(index) || oldMatchVec(index) ||
198    Cat(newMatchVec(index).take(index)).orR
199  }
200
201  def filter_req() = {
202    val reqs =  tlb_req.indices.map{ i =>
203      val req = Wire(ValidIO(new PtwReq()))
204      val merge = canMerge(i)
205      req.bits := tlb_req(i).bits
206      req.valid := !merge && tlb_req(i).valid
207      req
208    }
209    reqs
210  }
211
212  val reqs = filter_req()
213  val req_ports = filter_ports
214  val isFull = enqPtr === deqPtr && mayFullDeq
215  val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq
216  val isEmptyIss = enqPtr === issPtr && !mayFullIss
217  val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid)))
218  val enqPtrVecInit = VecInit((0 until Width).map(i => enqPtr + i.U))
219  val enqPtrVec = VecInit((0 until Width).map(i => enqPtrVecInit(accumEnqNum(i))))
220  val enqNum = PopCount(reqs.map(_.valid))
221  val canEnqueue = counter +& enqNum <= Size.U
222
223  io.tlb.req.map(_.ready := true.B) // NOTE: just drop un-fire reqs
224  io.tlb.resp.valid := ptwResp_valid
225  io.tlb.resp.bits.data := ptwResp
226  io.tlb.resp.bits.vector := resp_vector
227
228  val issue_valid = v(issPtr) && !isEmptyIss
229  val issue_filtered = ptwResp_valid && ptwResp.entry.hit(io.ptw.req(0).bits.vpn, io.csr.satp.asid, allType=true, ignoreAsid=true)
230  val issue_fire_fake = issue_valid && (io.ptw.req(0).ready || (issue_filtered && false.B /*timing-opt*/))
231  io.ptw.req(0).valid := issue_valid && !issue_filtered
232  io.ptw.req(0).bits.vpn := vpn(issPtr)
233  io.ptw.resp.ready := true.B
234
235  reqs.zipWithIndex.map{
236    case (req, i) =>
237      when (req.valid && canEnqueue) {
238        v(enqPtrVec(i)) := true.B
239        vpn(enqPtrVec(i)) := req.bits.vpn
240        ports(enqPtrVec(i)) := req_ports(i).asBools
241      }
242  }
243  for (i <- ports.indices) {
244    when (v(i)) {
245      ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2)
246    }
247  }
248
249  val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR
250  val do_deq = (!v(deqPtr) && !isEmptyDeq)
251  val do_iss = issue_fire_fake || (!v(issPtr) && !isEmptyIss)
252  when (do_enq) {
253    enqPtr := enqPtr + enqNum
254  }
255  when (do_deq) {
256    deqPtr := deqPtr + 1.U
257  }
258  when (do_iss) {
259    issPtr := issPtr + 1.U
260  }
261  when (issue_fire_fake && issue_filtered) { // issued but is filtered
262    v(issPtr) := false.B
263  }
264  when (do_enq =/= do_deq) {
265    mayFullDeq := do_enq
266  }
267  when (do_enq =/= do_iss) {
268    mayFullIss := do_enq
269  }
270
271  when (io.ptw.resp.fire()) {
272    v.zip(ptwResp_OldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
273  }
274
275  counter := counter - do_deq + Mux(do_enq, enqNum, 0.U)
276  assert(counter <= Size.U, "counter should be less than Size")
277  when (counter === 0.U) {
278    assert(!io.ptw.req(0).fire(), "when counter is 0, should not req")
279    assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty")
280  }
281  when (counter === Size.U) {
282    assert(mayFullDeq, "when counter is Size, should be full")
283  }
284
285  when (flush) {
286    v.map(_ := false.B)
287    deqPtr := 0.U
288    enqPtr := 0.U
289    issPtr := 0.U
290    ptwResp_valid := false.B
291    mayFullDeq := false.B
292    mayFullIss := false.B
293    counter := 0.U
294  }
295
296  // perf
297  val inflight_counter = RegInit(0.U(log2Up(Size + 1).W))
298  when (io.ptw.req(0).fire() =/= io.ptw.resp.fire()) {
299    inflight_counter := Mux(io.ptw.req(0).fire(), inflight_counter + 1.U, inflight_counter - 1.U)
300  }
301  when (flush) {
302    inflight_counter := 0.U
303  }
304  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
305  XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U))
306  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire())
307  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
308  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire())
309  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire())
310  XSPerfAccumulate("inflight_cycle", !isEmptyDeq)
311  for (i <- 0 until Size + 1) {
312    XSPerfAccumulate(s"counter${i}", counter === i.U)
313  }
314
315  for (i <- 0 until Size) {
316    TimeOutAssert(v(i), timeOutThreshold, s"Filter ${i} doesn't recv resp in time")
317  }
318}
319
320object PTWRepeater {
321  def apply(
322    tlb: TlbPtwIO,
323    sfence: SfenceBundle,
324    csr: TlbCsrBundle
325  )(implicit p: Parameters) = {
326    val width = tlb.req.size
327    val repeater = Module(new PTWRepeater(width))
328    repeater.io.apply(tlb, sfence, csr)
329    repeater
330  }
331
332  def apply(
333    tlb: TlbPtwIO,
334    ptw: TlbPtwIO,
335    sfence: SfenceBundle,
336    csr: TlbCsrBundle
337  )(implicit p: Parameters) = {
338    val width = tlb.req.size
339    val repeater = Module(new PTWRepeater(width))
340    repeater.io.apply(tlb, ptw, sfence, csr)
341    repeater
342  }
343}
344
345object PTWRepeaterNB {
346  def apply(passReady: Boolean,
347    tlb: TlbPtwIO,
348    sfence: SfenceBundle,
349    csr: TlbCsrBundle
350  )(implicit p: Parameters) = {
351    val width = tlb.req.size
352    val repeater = Module(new PTWRepeaterNB(width, passReady))
353    repeater.io.apply(tlb, sfence, csr)
354    repeater
355  }
356
357  def apply(passReady: Boolean,
358    tlb: TlbPtwIO,
359    ptw: TlbPtwIO,
360    sfence: SfenceBundle,
361    csr: TlbCsrBundle
362  )(implicit p: Parameters) = {
363    val width = tlb.req.size
364    val repeater = Module(new PTWRepeaterNB(width, passReady))
365    repeater.io.apply(tlb, ptw, sfence, csr)
366    repeater
367  }
368}
369
370object PTWFilter {
371  def apply(
372    tlb: BTlbPtwIO,
373    ptw: TlbPtwIO,
374    sfence: SfenceBundle,
375    csr: TlbCsrBundle,
376    size: Int
377  )(implicit p: Parameters) = {
378    val width = tlb.req.size
379    val filter = Module(new PTWFilter(width, size))
380    filter.io.apply(tlb, ptw, sfence, csr)
381    filter
382  }
383
384  def apply(
385    tlb: BTlbPtwIO,
386    sfence: SfenceBundle,
387    csr: TlbCsrBundle,
388    size: Int
389  )(implicit p: Parameters) = {
390    val width = tlb.req.size
391    val filter = Module(new PTWFilter(width, size))
392    filter.io.apply(tlb, sfence, csr)
393    filter
394  }
395
396}
397