xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/Repeater.scala (revision a1ea7f76add43b40af78084f7f646a0010120cd7)
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 PTWRepeater(Width: Int = 1)(implicit p: Parameters) extends XSModule with HasPtwConst {
29  val io = IO(new Bundle {
30    val tlb = Flipped(new TlbPtwIO(Width))
31    val ptw = new TlbPtwIO
32    val sfence = Input(new SfenceBundle)
33  })
34  val req_in = if (Width == 1) {
35    io.tlb.req(0)
36  } else {
37    val arb = Module(new RRArbiter(io.tlb.req(0).bits.cloneType, Width))
38    arb.io.in <> io.tlb.req
39    arb.io.out
40  }
41  val (tlb, ptw, sfence) = (io.tlb, io.ptw, RegNext(io.sfence.valid))
42  val req = RegEnable(req_in.bits, req_in.fire())
43  val resp = RegEnable(ptw.resp.bits, ptw.resp.fire())
44  val haveOne = BoolStopWatch(req_in.fire(), tlb.resp.fire() || sfence)
45  val sent = BoolStopWatch(ptw.req(0).fire(), req_in.fire() || sfence)
46  val recv = BoolStopWatch(ptw.resp.fire(), req_in.fire() || sfence)
47
48  req_in.ready := !haveOne
49  ptw.req(0).valid := haveOne && !sent
50  ptw.req(0).bits := req
51
52  tlb.resp.bits := resp
53  tlb.resp.valid := haveOne && recv
54  ptw.resp.ready := !recv
55
56  XSPerfAccumulate("req_count", ptw.req(0).fire())
57  XSPerfAccumulate("tlb_req_cycle", BoolStopWatch(req_in.fire(), tlb.resp.fire() || sfence))
58  XSPerfAccumulate("ptw_req_cycle", BoolStopWatch(ptw.req(0).fire(), ptw.resp.fire() || sfence))
59
60  XSDebug(haveOne, p"haveOne:${haveOne} sent:${sent} recv:${recv} sfence:${sfence} req:${req} resp:${resp}")
61  XSDebug(req_in.valid || io.tlb.resp.valid, p"tlb: ${tlb}\n")
62  XSDebug(io.ptw.req(0).valid || io.ptw.resp.valid, p"ptw: ${ptw}\n")
63  assert(!RegNext(recv && io.ptw.resp.valid, init = false.B), "re-receive ptw.resp")
64}
65
66/* dtlb
67 *
68 */
69class PTWFilter(Width: Int, Size: Int)(implicit p: Parameters) extends XSModule with HasPtwConst {
70  val io = IO(new Bundle {
71    val tlb = Flipped(new BTlbPtwIO(Width))
72    val ptw = new TlbPtwIO()
73    val sfence = Input(new SfenceBundle)
74  })
75
76  require(Size >= Width)
77
78  val v = RegInit(VecInit(Seq.fill(Size)(false.B)))
79  val ports = Reg(Vec(Size, Vec(Width, Bool()))) // record which port(s) the entry come from, may not able to cover all the ports
80  val vpn = Reg(Vec(Size, UInt(vpnLen.W)))
81  val enqPtr = RegInit(0.U(log2Up(Size).W)) // Enq
82  val issPtr = RegInit(0.U(log2Up(Size).W)) // Iss to Ptw
83  val deqPtr = RegInit(0.U(log2Up(Size).W)) // Deq
84  val mayFullDeq = RegInit(false.B)
85  val mayFullIss = RegInit(false.B)
86  val counter = RegInit(0.U(log2Up(Size+1).W))
87
88  val sfence = RegNext(io.sfence)
89  val ptwResp = RegEnable(io.ptw.resp.bits, io.ptw.resp.fire())
90  val ptwResp_valid = RegNext(io.ptw.resp.valid, init = false.B)
91  val tlb_req = io.tlb.req
92  val oldMatchVec = tlb_req.map(a => vpn.zip(v).map{case (pi, vi) => vi && a.valid && pi === a.bits.vpn })
93  val newMatchVec = tlb_req.map(a => tlb_req.map(b => b.valid && a.valid && b.bits.vpn === a.bits.vpn ))
94  val ptwResp_newMatchVec = tlb_req.map(a => ptwResp_valid && ptwResp.entry.hit(a.bits.vpn, allType = true) && a.valid) // TODO: may have long latency
95  val ptwResp_oldMatchVec = vpn.zip(v).map{ case (pi, vi) => vi && ptwResp.entry.hit(pi, allType = true) }
96  val update_ports = v.indices.map(i => oldMatchVec.map(j => j(i)))
97  val ports_init = (0 until Width).map(i => (1 << i).U(Width.W))
98  val filter_ports = (0 until Width).map(i => ParallelMux(newMatchVec(i).zip(ports_init).drop(i)))
99  val resp_vector = ParallelMux(ptwResp_oldMatchVec zip ports)
100  val resp_still_valid = ParallelOR(ptwResp_oldMatchVec).asBool
101
102  def canMerge(index: Int) : Bool = {
103    ptwResp_newMatchVec(index) ||
104    Cat(oldMatchVec(index)).orR ||
105    Cat(newMatchVec(index).take(index)).orR
106  }
107
108  def filter_req() = {
109    val reqs =  tlb_req.indices.map{ i =>
110      val req = Wire(ValidIO(new PtwReq()))
111      val merge = canMerge(i)
112      req.bits := tlb_req(i).bits
113      req.valid := !merge && tlb_req(i).valid
114      req
115    }
116    reqs
117  }
118
119
120  val reqs = filter_req()
121  val req_ports = filter_ports
122  var enqPtr_next = WireInit(deqPtr)
123  val isFull = enqPtr === deqPtr && mayFullDeq
124  val isEmptyDeq = enqPtr === deqPtr && !mayFullDeq
125  val isEmptyIss = enqPtr === issPtr && !mayFullIss
126  val accumEnqNum = (0 until Width).map(i => PopCount(reqs.take(i).map(_.valid)))
127  val enqPtrVec = VecInit((0 until Width).map(i => enqPtr + accumEnqNum(i)))
128  val enqNum = PopCount(reqs.map(_.valid))
129  val canEnqueue = counter +& enqNum <= Size.U
130
131  io.tlb.req.map(_.ready := true.B) // NOTE: just drop un-fire reqs
132  io.tlb.resp.valid := ptwResp_valid && resp_still_valid
133  io.tlb.resp.bits.data := ptwResp
134  io.tlb.resp.bits.vector := resp_vector
135  io.ptw.req(0).valid := v(issPtr) && !isEmptyIss && !(ptwResp_valid && ptwResp.entry.hit(io.ptw.req(0).bits.vpn))
136  io.ptw.req(0).bits.vpn := vpn(issPtr)
137  io.ptw.resp.ready := true.B
138
139  reqs.zipWithIndex.map{
140    case (req, i) =>
141      when (req.valid && canEnqueue) {
142        v(enqPtrVec(i)) := true.B
143        vpn(enqPtrVec(i)) := req.bits.vpn
144        ports(enqPtrVec(i)) := req_ports(i).asBools
145      }
146  }
147  for (i <- ports.indices) {
148    when (v(i)) {
149      ports(i) := ports(i).zip(update_ports(i)).map(a => a._1 || a._2)
150    }
151  }
152
153  val do_enq = canEnqueue && Cat(reqs.map(_.valid)).orR
154  val do_deq = (!v(deqPtr) && !isEmptyDeq)
155  val do_iss = io.ptw.req(0).fire() || (!v(issPtr) && !isEmptyIss)
156  when (do_enq) {
157    enqPtr := enqPtr + enqNum
158  }
159  when (do_deq) {
160    deqPtr := deqPtr + 1.U
161  }
162  when (do_iss) {
163    issPtr := issPtr + 1.U
164  }
165  when (do_enq =/= do_deq) {
166    mayFullDeq := do_enq
167  }
168  when (do_enq =/= do_iss) {
169    mayFullIss := do_enq
170  }
171
172  when (ptwResp_valid) {
173    v.zip(ptwResp_oldMatchVec).map{ case (vi, mi) => when (mi) { vi := false.B }}
174  }
175
176  counter := counter - do_deq + Mux(do_enq, enqNum, 0.U)
177  assert(counter <= Size.U, "counter should be less than Size")
178  when (counter === 0.U) {
179    assert(!io.ptw.req(0).fire(), "when counter is 0, should not req")
180    assert(isEmptyDeq && isEmptyIss, "when counter is 0, should be empty")
181  }
182  when (counter === Size.U) {
183    assert(mayFullDeq, "when counter is Size, should be full")
184  }
185
186  when (sfence.valid) {
187    v.map(_ := false.B)
188    deqPtr := 0.U
189    enqPtr := 0.U
190    issPtr := 0.U
191    ptwResp_valid := false.B
192    mayFullDeq := false.B
193    mayFullIss := false.B
194    counter := 0.U
195  }
196
197
198  // perf
199  val inflight_counter = RegInit(0.U(log2Up(Size + 1).W))
200  when (io.ptw.req(0).fire() =/= io.ptw.resp.fire()) {
201    inflight_counter := Mux(io.ptw.req(0).fire(), inflight_counter + 1.U, inflight_counter - 1.U)
202  }
203  when (sfence.valid) {
204    inflight_counter := 0.U
205  }
206  XSPerfAccumulate("tlb_req_count", PopCount(Cat(io.tlb.req.map(_.valid))))
207  XSPerfAccumulate("tlb_req_count_filtered", Mux(do_enq, accumEnqNum(Width - 1), 0.U))
208  XSPerfAccumulate("ptw_req_count", io.ptw.req(0).fire())
209  XSPerfAccumulate("ptw_req_cycle", inflight_counter)
210  XSPerfAccumulate("tlb_resp_count", io.tlb.resp.fire())
211  XSPerfAccumulate("ptw_resp_count", io.ptw.resp.fire())
212  XSPerfAccumulate("inflight_cycle", !isEmptyDeq)
213  for (i <- 0 until Size + 1) {
214    XSPerfAccumulate(s"counter${i}", counter === i.U)
215  }
216}
217