xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/ICacheMissUnit.scala (revision 3a6db8a39a25f02047d1fb2b257c89be0b2c36dc)
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 chipsalliance.rocketchip.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 huancun.{AliasKey, DirtyKey}
28import xiangshan.cache._
29import utils._
30
31
32abstract class ICacheMissUnitModule(implicit p: Parameters) extends XSModule
33  with HasICacheParameters
34
35abstract class ICacheMissUnitBundle(implicit p: Parameters) extends XSBundle
36  with HasICacheParameters
37
38class ICacheMissReq(implicit p: Parameters) extends ICacheBundle
39{
40    val paddr      = UInt(PAddrBits.W)
41    val vaddr      = UInt(VAddrBits.W)
42    val waymask   = UInt(nWays.W)
43    val coh       = new ClientMetadata
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    val mem_finish = DecoupledIO(new TLBundleE(edge.bundle))
76
77    val meta_write = DecoupledIO(new ICacheMetaWriteBundle)
78    val data_write = DecoupledIO(new ICacheDataWriteBundle)
79
80    val release_req    =  DecoupledIO(new ReplacePipeReq)
81    val release_resp   =  Flipped(ValidIO(UInt(ReplaceIdWid.W)))
82    val victimInfor        =  Output(new ICacheVictimInfor())
83
84  })
85
86  /** default value for control signals */
87  io.resp := DontCare
88  io.mem_acquire.bits := DontCare
89  io.mem_grant.ready := true.B
90  io.meta_write.bits := DontCare
91  io.data_write.bits := DontCare
92
93  val s_idle  :: s_send_mem_aquire :: s_wait_mem_grant :: s_write_back :: s_send_grant_ack :: s_send_replace :: s_wait_replace :: s_wait_resp :: Nil = Enum(8)
94  val state = RegInit(s_idle)
95  /** control logic transformation */
96  //request register
97  val req = Reg(new ICacheMissReq)
98  val req_idx = req.getVirSetIdx //virtual index
99  val req_tag = req.getPhyTag //physical tag
100  val req_waymask = req.waymask
101  val release_id  = Cat(MainPipeKey.U, id.U)
102  val req_corrupt = RegInit(false.B)
103
104  io.victimInfor.valid := state === s_send_replace || state === s_wait_replace || state === s_wait_resp
105  io.victimInfor.vidx  := req_idx
106
107  val (_, _, refill_done, refill_address_inc) = edge.addr_inc(io.mem_grant)
108
109  //cacheline register
110  val readBeatCnt = Reg(UInt(log2Up(refillCycles).W))
111  val respDataReg = Reg(Vec(refillCycles, UInt(beatBits.W)))
112
113  //initial
114  io.resp.bits := DontCare
115  io.mem_acquire.bits := DontCare
116  io.mem_grant.ready := true.B
117  io.meta_write.bits := DontCare
118  io.data_write.bits := DontCare
119
120  io.release_req.bits.paddr := req.paddr
121  io.release_req.bits.vaddr := req.vaddr
122  io.release_req.bits.voluntary := true.B
123  io.release_req.bits.waymask   := req.waymask
124  io.release_req.bits.id   := release_id
125  io.release_req.bits.param := DontCare //release will not care tilelink param
126
127  io.req.ready := (state === s_idle)
128  io.mem_acquire.valid := (state === s_send_mem_aquire)
129  io.release_req.valid := (state === s_send_replace)
130
131  val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire())
132  val grant_param = Reg(UInt(TLPermissions.bdWidth.W))
133  val is_dirty = RegInit(false.B)
134  val is_grant = RegEnable(edge.isRequest(io.mem_grant.bits), io.mem_grant.fire())
135
136  //state change
137  switch(state) {
138    is(s_idle) {
139      when(io.req.fire()) {
140        readBeatCnt := 0.U
141        state := s_send_mem_aquire
142        req := io.req.bits
143      }
144    }
145
146    // memory request
147    is(s_send_mem_aquire) {
148      when(io.mem_acquire.fire()) {
149        state := s_wait_mem_grant
150      }
151    }
152
153    is(s_wait_mem_grant) {
154      when(edge.hasData(io.mem_grant.bits)) {
155        when(io.mem_grant.fire()) {
156          readBeatCnt := readBeatCnt + 1.U
157          respDataReg(readBeatCnt) := io.mem_grant.bits.data
158          req_corrupt := io.mem_grant.bits.corrupt
159          grant_param := io.mem_grant.bits.param
160          is_dirty    := io.mem_grant.bits.echo.lift(DirtyKey).getOrElse(false.B)
161          when(readBeatCnt === (refillCycles - 1).U) {
162            assert(refill_done, "refill not done!")
163            state := s_send_grant_ack
164          }
165        }
166      }
167    }
168
169    is(s_send_grant_ack) {
170      when(io.mem_finish.fire()) {
171        state := s_send_replace
172      }
173    }
174
175    is(s_send_replace){
176      when(io.release_req.fire()){
177        state := s_wait_replace
178      }
179    }
180
181    is(s_wait_replace){
182      when(io.release_resp.valid && io.release_resp.bits === release_id){
183        state := s_write_back
184      }
185    }
186
187    is(s_write_back) {
188      state := Mux(io.meta_write.fire() && io.data_write.fire(), s_wait_resp, s_write_back)
189    }
190
191    is(s_wait_resp) {
192      io.resp.bits.data := respDataReg.asUInt
193      io.resp.bits.corrupt := req_corrupt
194      when(io.resp.fire()) {
195        state := s_idle
196      }
197    }
198  }
199
200  /** refill write and meta write */
201  val missCoh    = ClientMetadata(Nothing)
202  val grow_param = missCoh.onAccess(M_XRD)._2
203  val acquireBlock = edge.AcquireBlock(
204    fromSource = io.id,
205    toAddress = addrAlign(req.paddr, blockBytes, PAddrBits),
206    lgSize = (log2Up(cacheParams.blockBytes)).U,
207    growPermissions = grow_param
208  )._2
209  io.mem_acquire.bits := acquireBlock
210  // resolve cache alias by L2
211  io.mem_acquire.bits.user.lift(AliasKey).foreach(_ := req.vaddr(13, 12))
212  require(nSets <= 256) // icache size should not be more than 128KB
213
214  /** Grant ACK */
215  io.mem_finish.valid := (state === s_send_grant_ack) && is_grant
216  io.mem_finish.bits := grantack
217
218  //resp to ifu
219  io.resp.valid := state === s_wait_resp
220  /** update coh meta */
221  def missCohGen(param: UInt, dirty: Bool): UInt = {
222    MuxLookup(Cat(param, dirty), Nothing, Seq(
223      Cat(toB, false.B) -> Branch,
224      Cat(toB, true.B)  -> Branch,
225      Cat(toT, false.B) -> Trunk,
226      Cat(toT, true.B)  -> Dirty))
227  }
228
229  val miss_new_coh = ClientMetadata(missCohGen(grant_param, is_dirty))
230
231  io.meta_write.valid := (state === s_write_back)
232  io.meta_write.bits.generate(tag = req_tag, coh = miss_new_coh, idx = req_idx, waymask = req_waymask, bankIdx = req_idx(0))
233
234  io.data_write.valid := (state === s_write_back)
235  io.data_write.bits.generate(data = respDataReg.asUInt, idx = req_idx, waymask = req_waymask, bankIdx = req_idx(0))
236
237  XSPerfAccumulate(
238    "entryPenalty" + Integer.toString(id, 10),
239    BoolStopWatch(
240      start = io.req.fire(),
241      stop = io.resp.valid,
242      startHighPriority = true)
243  )
244  XSPerfAccumulate("entryReq" + Integer.toString(id, 10), io.req.fire())
245
246}
247
248
249class ICacheMissUnit(edge: TLEdgeOut)(implicit p: Parameters) extends ICacheMissUnitModule
250{
251  val io = IO(new Bundle{
252    val req         = Vec(2, Flipped(DecoupledIO(new ICacheMissReq)))
253    val resp        = Vec(2, ValidIO(new ICacheMissResp))
254
255    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
256    val mem_grant   = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
257    val mem_finish  = DecoupledIO(new TLBundleE(edge.bundle))
258
259    val meta_write  = DecoupledIO(new ICacheMetaWriteBundle)
260    val data_write  = DecoupledIO(new ICacheDataWriteBundle)
261
262    val release_req    =  DecoupledIO(new ReplacePipeReq)
263    val release_resp   =  Flipped(ValidIO(UInt(ReplaceIdWid.W)))
264
265    val victimInfor = Vec(PortNumber, Output(new ICacheVictimInfor()))
266
267    val prefetch_req =  Flipped(DecoupledIO(new PIQReq))
268
269  })
270  // assign default values to output signals
271  io.mem_grant.ready := false.B
272
273  val meta_write_arb = Module(new Arbiter(new ICacheMetaWriteBundle,  PortNumber))
274  val refill_arb     = Module(new Arbiter(new ICacheDataWriteBundle,  PortNumber))
275  val release_arb    = Module(new Arbiter(new ReplacePipeReq,  PortNumber))
276
277  io.mem_grant.ready := true.B
278
279  val entries = (0 until PortNumber) map { i =>
280    val entry = Module(new ICacheMissEntry(edge, i))
281
282    entry.io.id := i.U
283
284    // entry req
285    entry.io.req.valid := io.req(i).valid
286    entry.io.req.bits  := io.req(i).bits
287    io.req(i).ready    := entry.io.req.ready
288
289    // entry resp
290    meta_write_arb.io.in(i)     <>  entry.io.meta_write
291    refill_arb.io.in(i)         <>  entry.io.data_write
292    release_arb.io.in(i)        <>  entry.io.release_req
293
294    entry.io.mem_grant.valid := false.B
295    entry.io.mem_grant.bits  := DontCare
296    when (io.mem_grant.bits.source === i.U) {
297      entry.io.mem_grant <> io.mem_grant
298    }
299
300    io.resp(i) <> entry.io.resp
301
302    io.victimInfor(i) := entry.io.victimInfor
303
304    entry.io.release_resp <> io.release_resp
305
306    XSPerfAccumulate(
307      "entryPenalty" + Integer.toString(i, 10),
308      BoolStopWatch(
309        start = entry.io.req.fire(),
310        stop = entry.io.resp.fire(),
311        startHighPriority = true)
312    )
313    XSPerfAccumulate("entryReq" + Integer.toString(i, 10), entry.io.req.fire())
314
315    entry
316  }
317
318  val alloc = Wire(UInt(log2Ceil(nPrefetchEntries).W))
319
320  val prefEntries = (PortNumber until PortNumber + nPrefetchEntries) map { i =>
321    val prefetchEntry = Module(new IPrefetchEntry(edge, PortNumber))
322
323    prefetchEntry.io.mem_hint_ack.valid := false.B
324    prefetchEntry.io.mem_hint_ack.bits := DontCare
325
326    when(io.mem_grant.bits.source === PortNumber.U) {
327      prefetchEntry.io.mem_hint_ack <> io.mem_grant
328    }
329
330    prefetchEntry.io.req.valid := io.prefetch_req.valid && ((i-PortNumber).U === alloc)
331    prefetchEntry.io.req.bits  := io.prefetch_req.bits
332
333    prefetchEntry.io.id := i.U
334
335    prefetchEntry
336  }
337
338  alloc := PriorityEncoder(prefEntries.map(_.io.req.ready))
339  io.prefetch_req.ready := ParallelOR(prefEntries.map(_.io.req.ready))
340  val tl_a_chanel = entries.map(_.io.mem_acquire) ++ prefEntries.map(_.io.mem_hint)
341  TLArbiter.lowest(edge, io.mem_acquire, tl_a_chanel:_*)
342
343  TLArbiter.lowest(edge, io.mem_finish,  entries.map(_.io.mem_finish):_*)
344
345  io.meta_write     <> meta_write_arb.io.out
346  io.data_write     <> refill_arb.io.out
347  io.release_req        <> release_arb.io.out
348
349  (0 until nWays).map{ w =>
350    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)
351    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)
352  }
353
354}
355
356
357
358