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