xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/ICacheMissUnit.scala (revision 9672f0b7124446b0dbe8f0a1e831208f22e01305)
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.frontend.icache
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.diplomacy.IdRange
23import freechips.rocketchip.tilelink.ClientStates._
24import freechips.rocketchip.tilelink.TLPermissions._
25import freechips.rocketchip.tilelink._
26import xiangshan._
27import xiangshan.cache._
28import utils._
29import utility._
30import difftest._
31
32
33abstract class ICacheMissUnitModule(implicit p: Parameters) extends XSModule
34  with HasICacheParameters
35
36abstract class ICacheMissUnitBundle(implicit p: Parameters) extends XSBundle
37  with HasICacheParameters
38
39class ICacheMissReq(implicit p: Parameters) extends ICacheBundle
40{
41    val paddr      = UInt(PAddrBits.W)
42    val vaddr      = UInt(VAddrBits.W)
43    val waymask   = UInt(nWays.W)
44
45    def getVirSetIdx = get_idx(vaddr)
46    def getPhyTag    = get_phy_tag(paddr)
47}
48
49
50class ICacheMissResp(implicit p: Parameters) extends ICacheBundle
51{
52    val data     = UInt(blockBits.W)
53    val corrupt  = Bool()
54}
55
56class ICacheMissBundle(implicit p: Parameters) extends ICacheBundle{
57    val req       =   Vec(2, Flipped(DecoupledIO(new ICacheMissReq)))
58    val resp      =   Vec(2,ValidIO(new ICacheMissResp))
59    val flush     =   Input(Bool())
60}
61
62
63class ICacheMissEntry(edge: TLEdgeOut, id: Int)(implicit p: Parameters) extends ICacheMissUnitModule
64  with MemoryOpConstants
65{
66  val io = IO(new Bundle {
67    val id = Input(UInt(log2Ceil(PortNumber).W))
68
69    val req = Flipped(DecoupledIO(new ICacheMissReq))
70    val resp = ValidIO(new ICacheMissResp)
71
72    //tilelink channel
73    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
74    val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
75
76    val meta_write = DecoupledIO(new ICacheMetaWriteBundle)
77    val data_write = DecoupledIO(new ICacheDataWriteBundle)
78
79    val ongoing_req    = Output(new FilterInfo)
80    val fencei = Input(Bool())
81  })
82
83  /** default value for control signals */
84  io.resp := DontCare
85  io.mem_acquire.bits := DontCare
86  io.mem_grant.ready := true.B
87  io.meta_write.bits := DontCare
88  io.data_write.bits := DontCare
89
90  val s_idle  :: s_send_mem_aquire :: s_wait_mem_grant :: s_write_back_wait_resp :: s_write_back :: s_wait_resp :: Nil = Enum(6)
91  val state = RegInit(s_idle)
92  /** control logic transformation */
93  //request register
94  val req = Reg(new ICacheMissReq)
95  val req_idx = req.getVirSetIdx //virtual index
96  val req_tag = req.getPhyTag //physical tag
97  val req_waymask = req.waymask
98  val req_corrupt = RegInit(false.B)
99
100  val (_, _, refill_done, refill_address_inc) = edge.addr_inc(io.mem_grant)
101
102  val needflush_r = RegInit(false.B)
103  when (state === s_idle) { needflush_r := false.B }
104  when (state =/= s_idle && io.fencei) { needflush_r := true.B }
105  val needflush = needflush_r | io.fencei
106
107  //cacheline register
108  val readBeatCnt = Reg(UInt(log2Up(refillCycles).W))
109  val respDataReg = Reg(Vec(refillCycles, UInt(beatBits.W)))
110
111  //initial
112  io.resp.bits := DontCare
113  io.mem_acquire.bits := DontCare
114  io.mem_grant.ready := true.B
115  io.meta_write.bits := DontCare
116  io.data_write.bits := DontCare
117
118  io.req.ready := (state === s_idle)
119  io.mem_acquire.valid := (state === s_send_mem_aquire)
120
121  io.ongoing_req.valid := (state =/= s_idle)
122  io.ongoing_req.paddr :=  addrAlign(req.paddr, blockBytes, PAddrBits)
123
124  //state change
125  switch(state) {
126    is(s_idle) {
127      when(io.req.fire) {
128        readBeatCnt := 0.U
129        state := s_send_mem_aquire
130        req := io.req.bits
131      }
132    }
133
134    // memory request
135    is(s_send_mem_aquire) {
136      when(io.mem_acquire.fire) {
137        state := s_wait_mem_grant
138      }
139    }
140
141    is(s_wait_mem_grant) {
142      when(edge.hasData(io.mem_grant.bits)) {
143        when(io.mem_grant.fire) {
144          readBeatCnt := readBeatCnt + 1.U
145          respDataReg(readBeatCnt) := io.mem_grant.bits.data
146          req_corrupt := io.mem_grant.bits.corrupt // TODO: seems has bug
147          when(readBeatCnt === (refillCycles - 1).U) {
148            assert(refill_done, "refill not done!")
149            state := s_write_back_wait_resp
150          }
151        }
152      }
153    }
154
155    is(s_write_back_wait_resp) {
156      when((io.meta_write.fire && io.data_write.fire || needflush) && io.resp.fire) {
157        state := s_idle
158      }.elsewhen(io.meta_write.fire && io.data_write.fire || needflush) {
159        state := s_wait_resp
160      }.elsewhen(io.resp.fire) {
161        state := s_write_back
162      }
163    }
164
165    is(s_write_back) {
166      when(io.meta_write.fire && io.data_write.fire || needflush) {
167        state := s_idle
168      }
169    }
170
171    is(s_wait_resp) {
172      when(io.resp.fire) {
173        state := s_idle
174      }
175    }
176  }
177
178  /** refill write and meta write */
179
180  val getBlock = edge.Get(
181    fromSource = io.id,
182    toAddress = addrAlign(req.paddr, blockBytes, PAddrBits),
183    lgSize = (log2Up(cacheParams.blockBytes)).U
184  )._2
185
186  io.mem_acquire.bits := getBlock // getBlock
187  // req source
188  io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUInst.id.U)
189  require(nSets <= 256) // icache size should not be more than 128KB
190
191  //resp to ifu
192  io.resp.valid := (state === s_wait_resp) || (state === s_write_back_wait_resp)
193
194  io.resp.bits.data := respDataReg.asUInt
195  io.resp.bits.corrupt := req_corrupt
196
197  io.meta_write.valid := (((state === s_write_back) || (state === s_write_back_wait_resp)) && !needflush)
198  io.meta_write.bits.generate(tag = req_tag, idx = req_idx, waymask = req_waymask, bankIdx = req_idx(0))
199
200  io.data_write.valid := (((state === s_write_back) || (state === s_write_back_wait_resp)) && !needflush)
201  io.data_write.bits.generate(data = respDataReg.asUInt,
202                              idx  = req_idx,
203                              waymask = req_waymask,
204                              bankIdx = req_idx(0),
205                              paddr = req.paddr)
206
207  XSPerfAccumulate(
208    "entryPenalty" + Integer.toString(id, 10),
209    BoolStopWatch(
210      start = io.req.fire,
211      stop = io.resp.valid,
212      startHighPriority = true)
213  )
214  XSPerfAccumulate("entryReq" + Integer.toString(id, 10), io.req.fire)
215
216  // Statistics on the latency distribution of MSHR
217  val cntLatency = RegInit(0.U(32.W))
218  cntLatency := Mux(io.mem_acquire.fire, 1.U, cntLatency + 1.U)
219  // the condition is same as the transition from s_wait_mem_grant to s_write_back
220  val cntEnable = (state === s_wait_mem_grant) && edge.hasData(io.mem_grant.bits) &&
221                  io.mem_grant.fire && (readBeatCnt === (refillCycles - 1).U)
222  XSPerfHistogram("icache_mshr_latency_" + id.toString(), cntLatency, cntEnable, 0, 300, 10, right_strict = true)
223}
224
225
226class ICacheMissUnit(edge: TLEdgeOut)(implicit p: Parameters) extends ICacheMissUnitModule
227{
228  val io = IO(new Bundle{
229    val hartId      = Input(UInt(8.W))
230    val req         = Vec(2, Flipped(DecoupledIO(new ICacheMissReq)))
231    val resp        = Vec(2, ValidIO(new ICacheMissResp))
232
233    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
234    val mem_grant   = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
235
236    val fdip_acquire = Flipped(DecoupledIO(new TLBundleA(edge.bundle)))
237    val fdip_grant   = DecoupledIO(new TLBundleD(edge.bundle))
238
239    val meta_write  = DecoupledIO(new ICacheMetaWriteBundle)
240    val data_write  = DecoupledIO(new ICacheDataWriteBundle)
241
242    val ICacheMissUnitInfo = new ICacheMissUnitInfo
243    val fencei = Input(Bool())
244  })
245  // assign default values to output signals
246  io.mem_grant.ready := false.B
247
248  val meta_write_arb = Module(new Arbiter(new ICacheMetaWriteBundle,  PortNumber))
249  val refill_arb     = Module(new Arbiter(new ICacheDataWriteBundle,  PortNumber))
250
251  io.mem_grant.ready := true.B
252
253  val entries = (0 until PortNumber) map { i =>
254    val entry = Module(new ICacheMissEntry(edge, i))
255
256    entry.io.id := i.U
257
258    // entry req
259    entry.io.req.valid := io.req(i).valid
260    entry.io.req.bits  := io.req(i).bits
261    io.req(i).ready    := entry.io.req.ready
262
263    // entry resp
264    meta_write_arb.io.in(i)     <>  entry.io.meta_write
265    refill_arb.io.in(i)         <>  entry.io.data_write
266
267    entry.io.mem_grant.valid := false.B
268    entry.io.mem_grant.bits  := DontCare
269    when (io.mem_grant.bits.source === i.U) {
270      entry.io.mem_grant <> io.mem_grant
271    }
272
273    io.resp(i) <> entry.io.resp
274    io.ICacheMissUnitInfo.mshr(i) <> entry.io.ongoing_req
275    entry.io.fencei := io.fencei
276//    XSPerfAccumulate(
277//      "entryPenalty" + Integer.toString(i, 10),
278//      BoolStopWatch(
279//        start = entry.io.req.fire,
280//        stop = entry.io.resp.fire,
281//        startHighPriority = true)
282//    )
283//    XSPerfAccumulate("entryReq" + Integer.toString(i, 10), entry.io.req.fire)
284
285    entry
286  }
287
288  io.fdip_grant.valid := false.B
289  io.fdip_grant.bits  := DontCare
290  when (io.mem_grant.bits.source === PortNumber.U) {
291    io.fdip_grant <> io.mem_grant
292  }
293
294  /**
295    ******************************************************************************
296    * Register 2 cycle meta write info for IPrefetchPipe filter
297    ******************************************************************************
298    */
299  val meta_write_buffer = InitQueue(new FilterInfo, size = 2)
300  meta_write_buffer(0).valid := io.meta_write.fire
301  meta_write_buffer(0).paddr := io.data_write.bits.paddr
302  meta_write_buffer(1)       := meta_write_buffer(0)
303  (0 until 2).foreach (i => {
304    io.ICacheMissUnitInfo.recentWrite(i) := meta_write_buffer(i)
305  })
306
307  val tl_a_chanel = entries.map(_.io.mem_acquire) :+ io.fdip_acquire
308  TLArbiter.lowest(edge, io.mem_acquire, tl_a_chanel:_*)
309
310  io.meta_write     <> meta_write_arb.io.out
311  io.data_write     <> refill_arb.io.out
312
313  if (env.EnableDifftest) {
314    val difftest = DifftestModule(new DiffRefillEvent, dontCare = true)
315    difftest.coreid := io.hartId
316    difftest.index := 0.U
317    difftest.valid := refill_arb.io.out.valid
318    difftest.addr := refill_arb.io.out.bits.paddr
319    difftest.data := refill_arb.io.out.bits.data.asTypeOf(difftest.data)
320    difftest.idtfr := DontCare
321  }
322
323  (0 until nWays).map{ w =>
324    XSPerfAccumulate("line_0_refill_way_" + Integer.toString(w, 10),  entries(0).io.meta_write.valid && OHToUInt(entries(0).io.meta_write.bits.waymask)  === w.U)
325    XSPerfAccumulate("line_1_refill_way_" + Integer.toString(w, 10),  entries(1).io.meta_write.valid && OHToUInt(entries(1).io.meta_write.bits.waymask)  === w.U)
326  }
327
328}
329