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