xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/L2TLBMissQueue.scala (revision b6982e83d6fe4f8c3d111ebc70665f115e470ddf)
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.internal.naming.chiselName
23import xiangshan._
24import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
25import utils._
26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
27import freechips.rocketchip.tilelink._
28import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
29
30/* Miss Queue dont care about duplicate req, which is done by PtwFilter
31 * PtwMissQueue is just a Queue inside Chisel with flush
32 */
33
34class L2TlbMQEntry(implicit p: Parameters) extends XSBundle with HasPtwConst {
35  val vpn = UInt(vpnLen.W)
36  val source = UInt(bPtwWidth.W)
37  val ppn = UInt(ppnLen.W)
38  val wait_id = UInt(log2Up(MSHRSize).W)
39  val af = Bool()
40}
41
42class L2TlbMQInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst {
43  val vpn = Output(UInt(vpnLen.W))
44  val source = Output(UInt(bPtwWidth.W))
45  val l3 = Valid(Output(UInt(PAddrBits.W)))
46}
47
48class L2TlbMQCacheBundle(implicit p: Parameters) extends XSBundle with HasPtwConst {
49  val vpn = Output(UInt(vpnLen.W))
50  val source = Output(UInt(bPtwWidth.W))
51}
52
53class L2TlbMQIO(implicit p: Parameters) extends XSBundle with HasPtwConst {
54  val in = Flipped(Decoupled(new L2TlbMQInBundle()))
55  val sfence = Input(new SfenceBundle)
56  val cache = Decoupled(new L2TlbMQCacheBundle())
57  val fsm_done = Input(Bool())
58  val out = DecoupledIO(new Bundle {
59    val source = Output(UInt(bPtwWidth.W))
60    val id = Output(UInt(bMemID.W))
61    val vpn = Output(UInt(vpnLen.W))
62    val af = Output(Bool())
63  })
64  val mem = new Bundle {
65    val req = DecoupledIO(new L2TlbMemReqBundle())
66    val resp = Flipped(Valid(new Bundle {
67      val id = Output(UInt(log2Up(MSHRSize).W))
68    }))
69    val enq_ptr = Output(UInt(log2Ceil(MSHRSize).W))
70    val buffer_it = Output(Vec(MSHRSize, Bool()))
71    val refill_vpn = Output(UInt(vpnLen.W))
72    val req_mask = Input(Vec(MSHRSize, Bool()))
73  }
74  val pmp = new Bundle {
75    val req = Valid(new PMPReqBundle())
76    val resp = Flipped(new PMPRespBundle())
77  }
78}
79
80@chiselName
81class L2TlbMissQueue(implicit p: Parameters) extends XSModule with HasPtwConst {
82  val io = IO(new L2TlbMQIO())
83
84  val entries = Reg(Vec(MSHRSize, new L2TlbMQEntry()))
85  val state_idle :: state_cache_high :: state_cache_low :: state_addr_check :: state_mem_req :: state_mem_waiting :: state_mem_out :: Nil = Enum(7)
86  val state = RegInit(VecInit(Seq.fill(MSHRSize)(state_idle)))
87  val is_emptys = state.map(_ === state_idle)
88  val is_caches_high = state.map(_ === state_cache_high)
89  val is_caches_low = state.map(_ === state_cache_low)
90  val is_mems = state.map(_ === state_mem_req)
91  val is_waiting = state.map(_ === state_mem_waiting)
92  val is_having = state.map(_ === state_mem_out)
93
94  val full = !ParallelOR(is_emptys).asBool()
95  val enq_ptr = ParallelPriorityEncoder(is_emptys)
96  val cache_high_ptr = ParallelPriorityEncoder(is_caches_high)
97  val cache_low_ptr = ParallelPriorityEncoder(is_caches_low)
98
99  val cache_arb = Module(new Arbiter(new L2TlbMQCacheBundle(), 2))
100  cache_arb.io.in(0).valid := Cat(is_caches_high).orR && io.fsm_done // fsm busy, required l1/l2 pte is not ready
101  cache_arb.io.in(0).bits.vpn := entries(cache_high_ptr).vpn
102  cache_arb.io.in(0).bits.source := entries(cache_high_ptr).source
103  cache_arb.io.in(1).valid := Cat(is_caches_low).orR
104  cache_arb.io.in(1).bits.vpn := entries(cache_low_ptr).vpn
105  cache_arb.io.in(1).bits.source := entries(cache_low_ptr).source
106  cache_arb.io.out.ready := io.cache.ready
107  val cache_ptr = Mux(cache_arb.io.chosen === 0.U, cache_high_ptr, cache_low_ptr)
108
109  val mem_ptr = ParallelPriorityEncoder(is_having)
110  val mem_arb = Module(new RRArbiter(new L2TlbMQEntry(), MSHRSize))
111  for (i <- 0 until MSHRSize) {
112    mem_arb.io.in(i).bits := entries(i)
113    mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i)
114  }
115
116  // duplicate req
117  // to_wait: wait for the last to access mem, set to mem_resp
118  // to_cache: the last is back just right now, set to mem_cache
119  def dup(vpn1: UInt, vpn2: UInt): Bool = {
120    dropL3SectorBits(vpn1) === dropL3SectorBits(vpn2)
121  }
122  val dup_vec = state.indices.map(i =>
123    dup(io.in.bits.vpn, entries(i).vpn)
124  )
125  val dup_req_fire = mem_arb.io.out.fire() && dup(io.in.bits.vpn, mem_arb.io.out.bits.vpn) // dup with the req fire entry
126  val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entres, sending mem req already
127  val dup_vec_having = dup_vec.zipWithIndex.map{case (d, i) => d && is_having(i)} // dup with the "mem_out" entry recv the data just now
128  val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id)))
129  val dup_wait_resp = io.mem.resp.fire() && VecInit(dup_vec_wait)(io.mem.resp.bits.id) // dup with the entry that data coming next cycle
130  val to_wait = Cat(dup_vec_wait).orR || dup_req_fire
131  val to_mem_out = dup_wait_resp
132  val to_cache_low = Cat(dup_vec_having).orR
133  assert(RegNext(!(dup_req_fire && Cat(dup_vec_wait).orR), init = true.B), "mem req but some entries already waiting, should not happed")
134
135  val mem_resp_hit = RegInit(VecInit(Seq.fill(MSHRSize)(false.B)))
136  val enq_state = Mux(to_mem_out, state_mem_out, // same to the blew, but the mem resp now
137    Mux(to_cache_low, state_cache_low, // same to the below, but the mem resp last cycle
138    Mux(to_wait, state_mem_waiting, // wait for the prev mem resp
139    Mux(io.in.bits.l3.valid, state_addr_check, state_cache_high))))
140  when (io.in.fire()) {
141    state(enq_ptr) := enq_state
142    entries(enq_ptr).vpn := io.in.bits.vpn
143    entries(enq_ptr).ppn := io.in.bits.l3.bits
144    entries(enq_ptr).source := io.in.bits.source
145    entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr)
146    entries(enq_ptr).af := false.B
147    mem_resp_hit(enq_ptr) := to_mem_out
148  }
149  when (mem_arb.io.out.fire()) {
150    for (i <- state.indices) {
151      when (state(i) =/= state_idle && dup(entries(i).vpn, mem_arb.io.out.bits.vpn)) {
152        // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait"
153        state(i) := state_mem_waiting
154        entries(i).wait_id := mem_arb.io.chosen
155      }
156    }
157  }
158  when (io.mem.resp.fire()) {
159    state.indices.map{i =>
160      when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) {
161        state(i) := state_mem_out
162        mem_resp_hit(i) := true.B
163      }
164    }
165  }
166  when (io.out.fire()) {
167    assert(state(mem_ptr) === state_mem_out)
168    state(mem_ptr) := state_idle
169  }
170  when (io.cache.fire()) {
171    state(cache_ptr) := state_idle
172  }
173
174  mem_resp_hit.map(a => when (a) { a := false.B } )
175
176  val enq_ptr_reg = RegNext(enq_ptr)
177
178  io.pmp.req.valid := RegNext(enq_state === state_addr_check)
179  io.pmp.req.bits.addr := MakeAddr(entries(enq_ptr_reg).ppn, getVpnn(entries(enq_ptr_reg).vpn, 0))
180  io.pmp.req.bits.cmd := TlbCmd.read
181  io.pmp.req.bits.size := 3.U // TODO: fix it
182  val pmp_resp_valid = io.pmp.req.valid // same cycle
183  when (pmp_resp_valid && (state(enq_ptr_reg) === state_addr_check) &&
184    !(mem_arb.io.out.fire && dup(entries(enq_ptr_reg).vpn, mem_arb.io.out.bits.vpn))) {
185    // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before
186    //       when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare
187    entries(enq_ptr_reg).af := io.pmp.resp.ld
188    state(enq_ptr_reg) := Mux(io.pmp.resp.ld, state_mem_out, state_mem_req)
189  }
190
191  when (io.sfence.valid) {
192    state.map(_ := state_idle)
193  }
194
195  io.in.ready := !full
196  io.cache.valid := cache_arb.io.out.valid
197  io.cache.bits.vpn := cache_arb.io.out.bits.vpn
198  io.cache.bits.source := cache_arb.io.out.bits.source
199
200  io.out.valid := ParallelOR(is_having).asBool()
201  io.out.bits.source := entries(mem_ptr).source
202  io.out.bits.vpn := entries(mem_ptr).vpn
203  io.out.bits.id := mem_ptr
204  io.out.bits.af := entries(mem_ptr).af
205
206  io.mem.req.valid := mem_arb.io.out.valid
207  io.mem.req.bits.addr := MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.vpn, 0))
208  io.mem.req.bits.id := mem_arb.io.chosen
209  mem_arb.io.out.ready := io.mem.req.ready
210  io.mem.refill_vpn := entries(RegNext(io.mem.resp.bits.id(log2Up(MSHRSize)-1, 0))).vpn
211  io.mem.buffer_it := mem_resp_hit
212  io.mem.enq_ptr := enq_ptr
213
214  XSPerfAccumulate("mq_in_count", io.in.fire())
215  XSPerfAccumulate("mq_in_block", io.in.valid && !io.in.ready)
216  for (i <- 0 until 7) {
217    XSPerfAccumulate(s"enq_state${i}", io.in.fire() && enq_state === i.U)
218  }
219  for (i <- 0 until (MSHRSize + 1)) {
220    XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U)
221    XSPerfAccumulate(s"cache_high_util${i}", PopCount(is_caches_high) === i.U)
222    XSPerfAccumulate(s"cache_low_util${i}", PopCount(is_caches_low) === i.U)
223    XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U)
224    XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U)
225  }
226  XSPerfAccumulate("mem_count", io.mem.req.fire())
227  XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U)
228
229  for (i <- 0 until MSHRSize) {
230    TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}")
231  }
232  assert(!io.in.valid || io.in.ready, "when io.in.valid, should always ready")
233}