xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/mainpipe/MainPipe.scala (revision b56f947ea6e9fe50fd06047a225356a808f2a3b1)
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
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.tilelink.ClientStates._
23import freechips.rocketchip.tilelink.MemoryOpCategories._
24import freechips.rocketchip.tilelink.TLPermissions._
25import freechips.rocketchip.tilelink.{ClientMetadata, ClientStates, TLPermissions}
26import utils._
27import xiangshan.L1CacheErrorInfo
28
29class MainPipeReq(implicit p: Parameters) extends DCacheBundle {
30  val miss = Bool() // only amo miss will refill in main pipe
31  val miss_id = UInt(log2Up(cfg.nMissEntries).W)
32  val miss_param = UInt(TLPermissions.bdWidth.W)
33  val miss_dirty = Bool()
34  val miss_way_en = UInt(DCacheWays.W)
35
36  val probe = Bool()
37  val probe_param = UInt(TLPermissions.bdWidth.W)
38  val probe_need_data = Bool()
39
40  // request info
41  // reqs from Store, AMO use this
42  // probe does not use this
43  val source = UInt(sourceTypeWidth.W)
44  val cmd = UInt(M_SZ.W)
45  // if dcache size > 32KB, vaddr is also needed for store
46  // vaddr is used to get extra index bits
47  val vaddr  = UInt(VAddrBits.W)
48  // must be aligned to block
49  val addr   = UInt(PAddrBits.W)
50
51  // store
52  val store_data = UInt((cfg.blockBytes * 8).W)
53  val store_mask = UInt(cfg.blockBytes.W)
54
55  // which word does amo work on?
56  val word_idx = UInt(log2Up(cfg.blockBytes * 8 / DataBits).W)
57  val amo_data   = UInt(DataBits.W)
58  val amo_mask   = UInt((DataBits / 8).W)
59
60  // error
61  val error = Bool()
62
63  // replace
64  val replace = Bool()
65  val replace_way_en = UInt(DCacheWays.W)
66
67  val id = UInt(reqIdWidth.W)
68
69  def isLoad: Bool = source === LOAD_SOURCE.U
70  def isStore: Bool = source === STORE_SOURCE.U
71  def isAMO: Bool = source === AMO_SOURCE.U
72
73  def convertStoreReq(store: DCacheLineReq): MainPipeReq = {
74    val req = Wire(new MainPipeReq)
75    req := DontCare
76    req.miss := false.B
77    req.miss_dirty := false.B
78    req.probe := false.B
79    req.probe_need_data := false.B
80    req.source := STORE_SOURCE.U
81    req.cmd := store.cmd
82    req.addr := store.addr
83    req.vaddr := store.vaddr
84    req.store_data := store.data
85    req.store_mask := store.mask
86    req.replace := false.B
87    req.error := false.B
88    req.id := store.id
89    req
90  }
91}
92
93class MainPipe(implicit p: Parameters) extends DCacheModule with HasPerfEvents {
94  val io = IO(new Bundle() {
95    // probe queue
96    val probe_req = Flipped(DecoupledIO(new MainPipeReq))
97    // store miss go to miss queue
98    val miss_req = DecoupledIO(new MissReq)
99    // store buffer
100    val store_req = Flipped(DecoupledIO(new DCacheLineReq))
101    val store_replay_resp = ValidIO(new DCacheLineResp)
102    val store_hit_resp = ValidIO(new DCacheLineResp)
103    val release_update = ValidIO(new ReleaseUpdate)
104    // atmoics
105    val atomic_req = Flipped(DecoupledIO(new MainPipeReq))
106    val atomic_resp = ValidIO(new AtomicsResp)
107    // replace
108    val replace_req = Flipped(DecoupledIO(new MainPipeReq))
109    val replace_resp = ValidIO(UInt(log2Up(cfg.nMissEntries).W))
110    // write-back queue
111    val wb = DecoupledIO(new WritebackReq)
112
113    val data_read_intend = Output(Bool())
114    val data_read = DecoupledIO(new L1BankedDataReadLineReq)
115    val data_resp = Input(Vec(DCacheBanks, new L1BankedDataReadResult()))
116    val readline_error_delayed = Input(Bool())
117    val data_write = DecoupledIO(new L1BankedDataWriteReq)
118
119    val meta_read = DecoupledIO(new MetaReadReq)
120    val meta_resp = Input(Vec(nWays, new Meta))
121    val meta_write = DecoupledIO(new MetaWriteReq)
122    val error_flag_resp = Input(Vec(nWays, Bool()))
123    val error_flag_write = DecoupledIO(new ErrorWriteReq)
124
125    val tag_read = DecoupledIO(new TagReadReq)
126    val tag_resp = Input(Vec(nWays, UInt(encTagBits.W)))
127    val tag_write = DecoupledIO(new TagWriteReq)
128
129    // update state vec in replacement algo
130    val replace_access = ValidIO(new ReplacementAccessBundle)
131    // find the way to be replaced
132    val replace_way = new ReplacementWayReqIO
133
134    val status = new Bundle() {
135      val s0_set = ValidIO(UInt(idxBits.W))
136      val s1, s2, s3 = ValidIO(new Bundle() {
137        val set = UInt(idxBits.W)
138        val way_en = UInt(nWays.W)
139      })
140    }
141
142    // lrsc locked block should block probe
143    val lrsc_locked_block = Output(Valid(UInt(PAddrBits.W)))
144    val invalid_resv_set = Input(Bool())
145    val update_resv_set = Output(Bool())
146    val block_lr = Output(Bool())
147
148    // ecc error
149    val error = Output(new L1CacheErrorInfo())
150  })
151
152  // meta array is made of regs, so meta write or read should always be ready
153  assert(RegNext(io.meta_read.ready))
154  assert(RegNext(io.meta_write.ready))
155
156  val s1_s0_set_conflict, s2_s0_set_conlict, s3_s0_set_conflict = Wire(Bool())
157  val set_conflict = s1_s0_set_conflict || s2_s0_set_conlict || s3_s0_set_conflict
158  // check sbuffer store req set_conflict in parallel with req arbiter
159  // it will speed up the generation of store_req.ready, which is in crit. path
160  val s1_s0_set_conflict_store, s2_s0_set_conlict_store, s3_s0_set_conflict_store = Wire(Bool())
161  val store_set_conflict = s1_s0_set_conflict_store || s2_s0_set_conlict_store || s3_s0_set_conflict_store
162  val s1_ready, s2_ready, s3_ready = Wire(Bool())
163
164  // convert store req to main pipe req, and select a req from store and probe
165  val store_req = Wire(DecoupledIO(new MainPipeReq))
166  store_req.bits := (new MainPipeReq).convertStoreReq(io.store_req.bits)
167  store_req.valid := io.store_req.valid
168  io.store_req.ready := store_req.ready
169
170  // s0: read meta and tag
171  val req = Wire(DecoupledIO(new MainPipeReq))
172  arbiter(
173    in = Seq(
174      io.probe_req,
175      io.replace_req,
176      store_req, // Note: store_req.ready is now manually assigned for better timing
177      io.atomic_req
178    ),
179    out = req,
180    name = Some("main_pipe_req")
181  )
182
183  val store_idx = get_idx(io.store_req.bits.vaddr)
184  // manually assign store_req.ready for better timing
185  // now store_req set conflict check is done in parallel with req arbiter
186  store_req.ready := io.meta_read.ready && io.tag_read.ready && s1_ready && !store_set_conflict &&
187    !io.probe_req.valid && !io.replace_req.valid
188  val s0_req = req.bits
189  val s0_idx = get_idx(s0_req.vaddr)
190  val s0_need_tag = io.tag_read.valid
191  val s0_can_go = io.meta_read.ready && io.tag_read.ready && s1_ready && !set_conflict
192  val s0_fire = req.valid && s0_can_go
193
194  val bank_write = VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, s0_req.store_mask).orR)).asUInt
195  val bank_full_write = VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, s0_req.store_mask).andR)).asUInt
196  val banks_full_overwrite = bank_full_write.andR
197
198  val banked_store_rmask = bank_write & ~bank_full_write
199  val banked_full_rmask = ~0.U(DCacheBanks.W)
200  val banked_none_rmask = 0.U(DCacheBanks.W)
201
202  val store_need_data = !s0_req.probe && s0_req.isStore && banked_store_rmask.orR
203  val probe_need_data = s0_req.probe
204  val amo_need_data = !s0_req.probe && s0_req.isAMO
205  val miss_need_data = s0_req.miss
206  val replace_need_data = s0_req.replace
207
208  val banked_need_data = store_need_data || probe_need_data || amo_need_data || miss_need_data || replace_need_data
209
210  val s0_banked_rmask = Mux(store_need_data, banked_store_rmask,
211    Mux(probe_need_data || amo_need_data || miss_need_data || replace_need_data,
212      banked_full_rmask,
213      banked_none_rmask
214    ))
215
216  // generate wmask here and use it in stage 2
217  val banked_store_wmask = bank_write
218  val banked_full_wmask = ~0.U(DCacheBanks.W)
219  val banked_none_wmask = 0.U(DCacheBanks.W)
220
221  // s1: read data
222  val s1_valid = RegInit(false.B)
223  val s1_need_data = RegEnable(banked_need_data, s0_fire)
224  val s1_req = RegEnable(s0_req, s0_fire)
225  val s1_banked_rmask = RegEnable(s0_banked_rmask, s0_fire)
226  val s1_banked_store_wmask = RegEnable(banked_store_wmask, s0_fire)
227  val s1_need_tag = RegEnable(s0_need_tag, s0_fire)
228  val s1_can_go = s2_ready && (io.data_read.ready || !s1_need_data)
229  val s1_fire = s1_valid && s1_can_go
230  val s1_idx = get_idx(s1_req.vaddr)
231  when (s0_fire) {
232    s1_valid := true.B
233  }.elsewhen (s1_fire) {
234    s1_valid := false.B
235  }
236  s1_ready := !s1_valid || s1_can_go
237  s1_s0_set_conflict := s1_valid && s0_idx === s1_idx
238  s1_s0_set_conflict_store := s1_valid && store_idx === s1_idx
239
240  val meta_resp = Wire(Vec(nWays, (new Meta).asUInt()))
241  val tag_resp = Wire(Vec(nWays, UInt(tagBits.W)))
242  val ecc_resp = Wire(Vec(nWays, UInt(eccTagBits.W)))
243  meta_resp := Mux(RegNext(s0_fire), VecInit(io.meta_resp.map(_.asUInt)), RegNext(meta_resp))
244  tag_resp := Mux(RegNext(s0_fire), VecInit(io.tag_resp.map(r => r(tagBits - 1, 0))), RegNext(tag_resp))
245  ecc_resp := Mux(RegNext(s0_fire), VecInit(io.tag_resp.map(r => r(encTagBits - 1, tagBits))), RegNext(ecc_resp))
246  val enc_tag_resp = Wire(io.tag_resp.cloneType)
247  enc_tag_resp := Mux(RegNext(s0_fire), io.tag_resp, RegNext(enc_tag_resp))
248
249  def wayMap[T <: Data](f: Int => T) = VecInit((0 until nWays).map(f))
250  val s1_tag_eq_way = wayMap((w: Int) => tag_resp(w) === get_tag(s1_req.addr)).asUInt
251  val s1_tag_match_way = wayMap((w: Int) => s1_tag_eq_way(w) && Meta(meta_resp(w)).coh.isValid()).asUInt
252  val s1_tag_match = s1_tag_match_way.orR
253
254  val s1_hit_tag = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap(w => tag_resp(w))), get_tag(s1_req.addr))
255  val s1_hit_coh = ClientMetadata(Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap(w => meta_resp(w))), 0.U))
256  val s1_encTag = Mux1H(s1_tag_match_way, wayMap((w: Int) => enc_tag_resp(w)))
257  val s1_flag_error = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap(w => io.error_flag_resp(w))), false.B)
258  val s1_l2_error = s1_req.error
259
260  // replacement policy
261  val s1_repl_way_en = WireInit(0.U(nWays.W))
262  s1_repl_way_en := Mux(RegNext(s0_fire), UIntToOH(io.replace_way.way), RegNext(s1_repl_way_en))
263  val s1_repl_tag = Mux1H(s1_repl_way_en, wayMap(w => tag_resp(w)))
264  val s1_repl_coh = Mux1H(s1_repl_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
265  val s1_miss_tag = Mux1H(s1_req.miss_way_en, wayMap(w => tag_resp(w)))
266  val s1_miss_coh = Mux1H(s1_req.miss_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
267
268  val s1_repl_way_raw = WireInit(0.U(log2Up(nWays).W))
269  s1_repl_way_raw := Mux(RegNext(s0_fire), io.replace_way.way, RegNext(s1_repl_way_raw))
270
271  val s1_need_replacement = (s1_req.miss || s1_req.isStore && !s1_req.probe) && !s1_tag_match
272  val s1_way_en = Mux(
273    s1_req.replace,
274    s1_req.replace_way_en,
275    Mux(
276      s1_req.miss,
277      s1_req.miss_way_en,
278      Mux(
279        s1_need_replacement,
280        s1_repl_way_en,
281        s1_tag_match_way
282      )
283    )
284  )
285  assert(!RegNext(s1_fire && PopCount(s1_way_en) > 1.U))
286  val s1_tag = Mux(
287    s1_req.replace,
288    get_tag(s1_req.addr),
289    Mux(
290      s1_req.miss,
291      s1_miss_tag,
292      Mux(s1_need_replacement, s1_repl_tag, s1_hit_tag)
293    )
294  )
295  val s1_coh = Mux(
296    s1_req.replace,
297    Mux1H(s1_req.replace_way_en, meta_resp.map(ClientMetadata(_))),
298    Mux(
299      s1_req.miss,
300      s1_miss_coh,
301      Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh)
302    )
303  )
304
305  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
306  val s1_hit = s1_tag_match && s1_has_permission
307  val s1_pregen_can_go_to_mq = !s1_req.replace && !s1_req.probe && !s1_req.miss && (s1_req.isStore || s1_req.isAMO) && !s1_hit
308
309  // s2: select data, return resp if this is a store miss
310  val s2_valid = RegInit(false.B)
311  val s2_req = RegEnable(s1_req, s1_fire)
312  val s2_tag_match = RegEnable(s1_tag_match, s1_fire)
313  val s2_tag_match_way = RegEnable(s1_tag_match_way, s1_fire)
314  val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire)
315  val (s2_has_permission, _, s2_new_hit_coh) = s2_hit_coh.onAccess(s2_req.cmd)
316
317  val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire)
318  val s2_repl_coh = RegEnable(s1_repl_coh, s1_fire)
319  val s2_repl_way_en = RegEnable(s1_repl_way_en, s1_fire)
320  val s2_need_replacement = RegEnable(s1_need_replacement, s1_fire)
321  val s2_need_data = RegEnable(s1_need_data, s1_fire)
322  val s2_need_tag = RegEnable(s1_need_tag, s1_fire)
323  val s2_encTag = RegEnable(s1_encTag, s1_fire)
324  val s2_idx = get_idx(s2_req.vaddr)
325  val s2_way_en = RegEnable(s1_way_en, s1_fire)
326  val s2_tag = RegEnable(s1_tag, s1_fire)
327  val s2_coh = RegEnable(s1_coh, s1_fire)
328  val s2_banked_store_wmask = RegEnable(s1_banked_store_wmask, s1_fire)
329  val s2_flag_error = RegEnable(s1_flag_error, s1_fire)
330  val s2_tag_error = dcacheParameters.tagCode.decode(s2_encTag).error && s2_need_tag
331  val s2_l2_error = s2_req.error
332  val s2_error = s2_flag_error || s2_tag_error || s2_l2_error // data_error not included
333
334  val s2_may_report_data_error = s2_need_data && s2_coh.state =/= ClientStates.Nothing
335
336  val s2_hit = s2_tag_match && s2_has_permission
337  val s2_amo_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isAMO
338  val s2_store_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isStore
339
340  s2_s0_set_conlict := s2_valid && s0_idx === s2_idx
341  s2_s0_set_conlict_store := s2_valid && store_idx === s2_idx
342
343  // For a store req, it either hits and goes to s3, or miss and enter miss queue immediately
344  val s2_can_go_to_s3 = (s2_req.replace || s2_req.probe || s2_req.miss || (s2_req.isStore || s2_req.isAMO) && s2_hit) && s3_ready
345  val s2_can_go_to_mq = RegEnable(s1_pregen_can_go_to_mq, s1_fire)
346  assert(RegNext(!(s2_valid && s2_can_go_to_s3 && s2_can_go_to_mq)))
347  val s2_can_go = s2_can_go_to_s3 || s2_can_go_to_mq
348  val s2_fire = s2_valid && s2_can_go
349  val s2_fire_to_s3 = s2_valid && s2_can_go_to_s3
350  when (s1_fire) {
351    s2_valid := true.B
352  }.elsewhen (s2_fire) {
353    s2_valid := false.B
354  }
355  s2_ready := !s2_valid || s2_can_go
356  val replay = !io.miss_req.ready
357
358  val data_resp = Wire(io.data_resp.cloneType)
359  data_resp := Mux(RegNext(s1_fire), io.data_resp, RegNext(data_resp))
360  val s2_store_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
361
362  def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = {
363    val full_wmask = FillInterleaved(8, wmask)
364    ((~full_wmask & old_data) | (full_wmask & new_data))
365  }
366
367  val s2_data = WireInit(VecInit((0 until DCacheBanks).map(i => {
368    data_resp(i).raw_data
369  })))
370
371  for (i <- 0 until DCacheBanks) {
372    val old_data = s2_data(i)
373    val new_data = get_data_of_bank(i, s2_req.store_data)
374    // for amo hit, we should use read out SRAM data
375    // do not merge with store data
376    val wmask = Mux(s2_amo_hit, 0.U(wordBytes.W), get_mask_of_bank(i, s2_req.store_mask))
377    s2_store_data_merged(i) := mergePutData(old_data, new_data, wmask)
378  }
379
380  val s2_data_word = s2_store_data_merged(s2_req.word_idx)
381
382  // s3: write data, meta and tag
383  val s3_valid = RegInit(false.B)
384  val s3_req = RegEnable(s2_req, s2_fire_to_s3)
385  val s3_idx = get_idx(s3_req.vaddr)
386  val s3_tag = RegEnable(s2_tag, s2_fire_to_s3)
387  val s3_tag_match = RegEnable(s2_tag_match, s2_fire_to_s3)
388  val s3_coh = RegEnable(s2_coh, s2_fire_to_s3)
389  val s3_hit = RegEnable(s2_hit, s2_fire_to_s3)
390  val s3_amo_hit = RegEnable(s2_amo_hit, s2_fire_to_s3)
391  val s3_store_hit = RegEnable(s2_store_hit, s2_fire_to_s3)
392  val s3_hit_coh = RegEnable(s2_hit_coh, s2_fire_to_s3)
393  val s3_new_hit_coh = RegEnable(s2_new_hit_coh, s2_fire_to_s3)
394  val s3_way_en = RegEnable(s2_way_en, s2_fire_to_s3)
395  val s3_banked_store_wmask = RegEnable(s2_banked_store_wmask, s2_fire_to_s3)
396  val s3_store_data_merged = RegEnable(s2_store_data_merged, s2_fire_to_s3)
397  val s3_data_word = RegEnable(s2_data_word, s2_fire_to_s3)
398  val s3_data = RegEnable(s2_data, s2_fire_to_s3)
399  val s3_l2_error = s3_req.error
400  // data_error will be reported by data array 1 cycle after data read resp
401  val s3_data_error = Wire(Bool())
402  s3_data_error := Mux(RegNext(RegNext(s1_fire)), // ecc check result is generated 2 cycle after read req
403    io.readline_error_delayed && RegNext(s2_may_report_data_error),
404    RegNext(s3_data_error) // do not update s3_data_error if !s1_fire
405  )
406  // error signal for amo inst
407  // s3_error = s3_flag_error || s3_tag_error || s3_l2_error || s3_data_error
408  val s3_error = RegEnable(s2_error, s2_fire_to_s3) || s3_data_error
409  val (probe_has_dirty_data, probe_shrink_param, probe_new_coh) = s3_coh.onProbe(s3_req.probe_param)
410  val s3_need_replacement = RegEnable(s2_need_replacement, s2_fire_to_s3)
411
412  val miss_update_meta = s3_req.miss
413  val probe_update_meta = s3_req.probe && s3_tag_match && s3_coh =/= probe_new_coh
414  val store_update_meta = s3_req.isStore && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
415  val amo_update_meta = s3_req.isAMO && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
416  val amo_wait_amoalu = s3_req.isAMO && s3_req.cmd =/= M_XLR && s3_req.cmd =/= M_XSC
417  val update_meta = (miss_update_meta || probe_update_meta || store_update_meta || amo_update_meta) && !s3_req.replace
418
419  def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = {
420    val c = categorize(cmd)
421    MuxLookup(Cat(c, param, dirty), Nothing, Seq(
422      //(effect param) -> (next)
423      Cat(rd, toB, false.B)  -> Branch,
424      Cat(rd, toB, true.B)   -> Branch,
425      Cat(rd, toT, false.B)  -> Trunk,
426      Cat(rd, toT, true.B)   -> Dirty,
427      Cat(wi, toT, false.B)  -> Trunk,
428      Cat(wi, toT, true.B)   -> Dirty,
429      Cat(wr, toT, false.B)  -> Dirty,
430      Cat(wr, toT, true.B)   -> Dirty))
431  }
432  val miss_new_coh = ClientMetadata(missCohGen(s3_req.cmd, s3_req.miss_param, s3_req.miss_dirty))
433
434  val new_coh = Mux(
435    miss_update_meta,
436    miss_new_coh,
437    Mux(
438      probe_update_meta,
439      probe_new_coh,
440      Mux(
441        store_update_meta || amo_update_meta,
442        s3_new_hit_coh,
443        ClientMetadata.onReset
444      )
445    )
446  )
447
448  // LR, SC and AMO
449  val debug_sc_fail_addr = RegInit(0.U)
450  val debug_sc_fail_cnt  = RegInit(0.U(8.W))
451
452  val lrsc_count = RegInit(0.U(log2Ceil(LRSCCycles).W))
453  val lrsc_valid = lrsc_count > LRSCBackOff.U
454  val lrsc_addr  = Reg(UInt())
455  val s3_lr = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XLR
456  val s3_sc = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XSC
457  val s3_lrsc_addr_match = lrsc_valid && lrsc_addr === get_block_addr(s3_req.addr)
458  val s3_sc_fail = s3_sc && !s3_lrsc_addr_match
459  val s3_sc_resp = Mux(s3_sc_fail, 1.U, 0.U)
460
461  val s3_can_do_amo = (s3_req.miss && !s3_req.probe && s3_req.source === AMO_SOURCE.U) || s3_amo_hit
462  val s3_can_do_amo_write = s3_can_do_amo && isWrite(s3_req.cmd) && !s3_sc_fail
463
464  when (s3_valid && (s3_lr || s3_sc)) {
465    when (s3_can_do_amo && s3_lr) {
466      lrsc_count := (LRSCCycles - 1).U
467      lrsc_addr := get_block_addr(s3_req.addr)
468    } .otherwise {
469      lrsc_count := 0.U
470    }
471  } .elsewhen (lrsc_count > 0.U) {
472    lrsc_count := lrsc_count - 1.U
473  }
474
475  io.lrsc_locked_block.valid := lrsc_valid
476  io.lrsc_locked_block.bits  := lrsc_addr
477  io.block_lr := RegNext(lrsc_count > 0.U)
478
479  // When we update update_resv_set, block all probe req in the next cycle
480  // It should give Probe reservation set addr compare an independent cycle,
481  // which will lead to better timing
482  io.update_resv_set := s3_valid && s3_lr && s3_can_do_amo
483
484  // when we release this block,
485  // we invalidate this reservation set
486  when (io.invalid_resv_set) {
487    lrsc_count := 0.U
488  }
489
490  when (s3_valid) {
491    when (s3_req.addr === debug_sc_fail_addr) {
492      when (s3_sc_fail) {
493        debug_sc_fail_cnt := debug_sc_fail_cnt + 1.U
494      } .elsewhen (s3_sc) {
495        debug_sc_fail_cnt := 0.U
496      }
497    } .otherwise {
498      when (s3_sc_fail) {
499        debug_sc_fail_addr := s3_req.addr
500        debug_sc_fail_cnt  := 1.U
501        XSWarn(s3_sc_fail === 100.U, p"L1DCache failed too many SCs in a row 0x${Hexadecimal(debug_sc_fail_addr)}, check if sth went wrong\n")
502      }
503    }
504  }
505  // assert(debug_sc_fail_cnt < 100.U, "L1DCache failed too many SCs in a row")
506
507  val banked_amo_wmask = UIntToOH(s3_req.word_idx)
508//  val banked_wmask = s3_banked_store_wmask
509  val banked_wmask = Mux(
510    s3_req.miss,
511    banked_full_wmask,
512    Mux(
513      s3_store_hit,
514      s3_banked_store_wmask,
515      Mux(
516        s3_can_do_amo_write,
517        banked_amo_wmask,
518        banked_none_wmask
519      )
520    )
521  )
522  val update_data = s3_req.miss || s3_store_hit || s3_can_do_amo_write
523  assert(!(banked_wmask.orR && !update_data))
524
525  // generate write data
526  // AMO hits
527  val s3_s_amoalu = RegInit(false.B)
528  val do_amoalu = amo_wait_amoalu && s3_valid && !s3_s_amoalu
529  val amoalu   = Module(new AMOALU(wordBits))
530  amoalu.io.mask := s3_req.amo_mask
531  amoalu.io.cmd  := s3_req.cmd
532  amoalu.io.lhs  := s3_data_word
533  amoalu.io.rhs  := s3_req.amo_data
534
535  // merge amo write data
536//  val amo_bitmask = FillInterleaved(8, s3_req.amo_mask)
537  val s3_amo_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
538  val s3_sc_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
539  for (i <- 0 until DCacheBanks) {
540    val old_data = s3_store_data_merged(i)
541    val new_data = amoalu.io.out
542    val wmask = Mux(
543      s3_req.word_idx === i.U,
544      ~0.U(wordBytes.W),
545      0.U(wordBytes.W)
546    )
547    s3_amo_data_merged(i) := mergePutData(old_data, new_data, wmask)
548//    s3_sc_data_merged(i) := amo_bitmask & s3_req.amo_data | ~amo_bitmask & old_data
549    s3_sc_data_merged(i) := mergePutData(old_data, s3_req.amo_data,
550      Mux(s3_req.word_idx === i.U && !s3_sc_fail, s3_req.amo_mask, 0.U(wordBytes.W))
551    )
552  }
553  val s3_amo_data_merged_reg = RegEnable(s3_amo_data_merged, do_amoalu)
554  when(do_amoalu){
555    s3_s_amoalu := true.B
556  }
557
558  val miss_wb = s3_req.miss && s3_need_replacement && s3_coh.state =/= ClientStates.Nothing
559  val probe_wb = s3_req.probe
560  val replace_wb = s3_req.replace
561  val need_wb = miss_wb || probe_wb || replace_wb
562
563  val (_, miss_shrink_param, _) = s3_coh.onCacheControl(M_FLUSH)
564  val writeback_param = Mux(probe_wb, probe_shrink_param, miss_shrink_param)
565  val writeback_data = if (dcacheParameters.alwaysReleaseData) {
566    s3_tag_match && s3_req.probe && s3_req.probe_need_data ||
567      s3_coh === ClientStates.Dirty || (miss_wb || replace_wb) && s3_coh.state =/= ClientStates.Nothing
568  } else {
569    s3_tag_match && s3_req.probe && s3_req.probe_need_data || s3_coh === ClientStates.Dirty
570  }
571
572  val s3_probe_can_go = s3_req.probe && io.wb.ready && (io.meta_write.ready || !probe_update_meta)
573  val s3_store_can_go = s3_req.isStore && !s3_req.probe && (io.meta_write.ready || !store_update_meta) && (io.data_write.ready || !update_data)
574  val s3_amo_can_go = s3_amo_hit && (io.meta_write.ready || !amo_update_meta) && (io.data_write.ready || !update_data) && (s3_s_amoalu || !amo_wait_amoalu)
575  val s3_miss_can_go = s3_req.miss &&
576    (io.meta_write.ready || !amo_update_meta) &&
577    (io.data_write.ready || !update_data) &&
578    (s3_s_amoalu || !amo_wait_amoalu) &&
579    io.tag_write.ready &&
580    io.wb.ready
581  val s3_replace_nothing = s3_req.replace && s3_coh.state === ClientStates.Nothing
582  val s3_replace_can_go = s3_req.replace && (s3_replace_nothing || io.wb.ready)
583  val s3_can_go = s3_probe_can_go || s3_store_can_go || s3_amo_can_go || s3_miss_can_go || s3_replace_can_go
584  val s3_update_data_cango = s3_store_can_go || s3_amo_can_go || s3_miss_can_go // used to speed up data_write gen
585  val s3_fire = s3_valid && s3_can_go
586  when (s2_fire_to_s3) {
587    s3_valid := true.B
588  }.elsewhen (s3_fire) {
589    s3_valid := false.B
590  }
591  s3_ready := !s3_valid || s3_can_go
592  s3_s0_set_conflict := s3_valid && s3_idx === s0_idx
593  s3_s0_set_conflict_store := s3_valid && s3_idx === store_idx
594  assert(RegNext(!s3_valid || !(s3_req.isStore && !s3_req.probe) || s3_hit)) // miss store should never come to s3
595
596  when(s3_fire) {
597    s3_s_amoalu := false.B
598  }
599
600  req.ready := s0_can_go
601
602  io.meta_read.valid := req.valid && s1_ready && !set_conflict
603  io.meta_read.bits.idx := get_idx(s0_req.vaddr)
604  io.meta_read.bits.way_en := Mux(s0_req.replace, s0_req.replace_way_en, ~0.U(nWays.W))
605
606  io.tag_read.valid := req.valid && s1_ready && !set_conflict && !s0_req.replace
607  io.tag_read.bits.idx := get_idx(s0_req.vaddr)
608  io.tag_read.bits.way_en := ~0.U(nWays.W)
609
610  io.data_read_intend := s1_valid && s1_need_data
611  io.data_read.valid := s1_valid && s1_need_data && s2_ready
612  io.data_read.bits.rmask := s1_banked_rmask
613  io.data_read.bits.way_en := s1_way_en
614  io.data_read.bits.addr := s1_req.vaddr
615
616  io.miss_req.valid := s2_valid && s2_can_go_to_mq
617  val miss_req = io.miss_req.bits
618  miss_req := DontCare
619  miss_req.source := s2_req.source
620  miss_req.cmd := s2_req.cmd
621  miss_req.addr := s2_req.addr
622  miss_req.vaddr := s2_req.vaddr
623  miss_req.way_en := Mux(s2_tag_match, s2_tag_match_way, s2_repl_way_en)
624  miss_req.store_data := s2_req.store_data
625  miss_req.store_mask := s2_req.store_mask
626  miss_req.word_idx := s2_req.word_idx
627  miss_req.amo_data := s2_req.amo_data
628  miss_req.amo_mask := s2_req.amo_mask
629  miss_req.req_coh := s2_hit_coh
630  miss_req.replace_coh := s2_repl_coh
631  miss_req.replace_tag := s2_repl_tag
632  miss_req.id := s2_req.id
633  miss_req.cancel := false.B
634
635  io.store_replay_resp.valid := s2_valid && s2_can_go_to_mq && replay && s2_req.isStore
636  io.store_replay_resp.bits.data := DontCare
637  io.store_replay_resp.bits.miss := true.B
638  io.store_replay_resp.bits.replay := true.B
639  io.store_replay_resp.bits.id := s2_req.id
640
641  io.store_hit_resp.valid := s3_valid && s3_store_can_go
642  io.store_hit_resp.bits.data := DontCare
643  io.store_hit_resp.bits.miss := false.B
644  io.store_hit_resp.bits.replay := false.B
645  io.store_hit_resp.bits.id := s3_req.id
646
647  io.release_update.valid := s3_valid && (s3_store_can_go || s3_amo_can_go) && s3_hit && update_data
648  io.release_update.bits.addr := s3_req.addr
649  io.release_update.bits.mask := Mux(s3_store_hit, s3_banked_store_wmask, banked_amo_wmask)
650  io.release_update.bits.data := Mux(
651    amo_wait_amoalu,
652    s3_amo_data_merged_reg,
653    Mux(
654      s3_sc,
655      s3_sc_data_merged,
656      s3_store_data_merged
657    )
658  ).asUInt
659
660  val atomic_hit_resp = Wire(new AtomicsResp)
661  atomic_hit_resp.data := Mux(s3_sc, s3_sc_resp, s3_data_word)
662  atomic_hit_resp.miss := false.B
663  atomic_hit_resp.miss_id := s3_req.miss_id
664  atomic_hit_resp.error := s3_error
665  atomic_hit_resp.replay := false.B
666  atomic_hit_resp.ack_miss_queue := s3_req.miss
667  atomic_hit_resp.id := lrsc_valid
668  val atomic_replay_resp = Wire(new AtomicsResp)
669  atomic_replay_resp.data := DontCare
670  atomic_replay_resp.miss := true.B
671  atomic_replay_resp.miss_id := DontCare
672  atomic_replay_resp.error := false.B
673  atomic_replay_resp.replay := true.B
674  atomic_replay_resp.ack_miss_queue := false.B
675  atomic_replay_resp.id := DontCare
676  val atomic_replay_resp_valid = s2_valid && s2_can_go_to_mq && replay && s2_req.isAMO
677  val atomic_hit_resp_valid = s3_valid && (s3_amo_can_go || s3_miss_can_go && s3_req.isAMO)
678  io.atomic_resp.valid := atomic_replay_resp_valid || atomic_hit_resp_valid
679  io.atomic_resp.bits := Mux(atomic_replay_resp_valid, atomic_replay_resp, atomic_hit_resp)
680
681  io.replace_resp.valid := s3_fire && s3_req.replace
682  io.replace_resp.bits := s3_req.miss_id
683
684  io.meta_write.valid := s3_fire && update_meta
685  io.meta_write.bits.idx := s3_idx
686  io.meta_write.bits.way_en := s3_way_en
687  io.meta_write.bits.meta.coh := new_coh
688
689  io.error_flag_write.valid := s3_fire && update_meta && s3_l2_error
690  io.error_flag_write.bits.idx := s3_idx
691  io.error_flag_write.bits.way_en := s3_way_en
692  io.error_flag_write.bits.error := s3_l2_error
693
694  io.tag_write.valid := s3_fire && s3_req.miss
695  io.tag_write.bits.idx := s3_idx
696  io.tag_write.bits.way_en := s3_way_en
697  io.tag_write.bits.tag := get_tag(s3_req.addr)
698
699  io.data_write.valid := s3_valid && s3_update_data_cango && update_data
700  io.data_write.bits.way_en := s3_way_en
701  io.data_write.bits.addr := s3_req.vaddr
702  io.data_write.bits.wmask := banked_wmask
703  io.data_write.bits.data := Mux(
704    amo_wait_amoalu,
705    s3_amo_data_merged_reg,
706    Mux(
707      s3_sc,
708      s3_sc_data_merged,
709      s3_store_data_merged
710    )
711  )
712  assert(RegNext(!io.meta_write.valid || !s3_req.replace))
713  assert(RegNext(!io.tag_write.valid || !s3_req.replace))
714  assert(RegNext(!io.data_write.valid || !s3_req.replace))
715
716  io.wb.valid := s3_valid && (
717    // replace
718    s3_req.replace && !s3_replace_nothing ||
719    // probe can go to wbq
720    s3_req.probe && (io.meta_write.ready || !probe_update_meta) ||
721      // amo miss can go to wbq
722      s3_req.miss &&
723        (io.meta_write.ready || !amo_update_meta) &&
724        (io.data_write.ready || !update_data) &&
725        (s3_s_amoalu || !amo_wait_amoalu) &&
726        io.tag_write.ready
727    ) && need_wb
728  io.wb.bits.addr := get_block_addr(Cat(s3_tag, get_untag(s3_req.vaddr)))
729  io.wb.bits.param := writeback_param
730  io.wb.bits.voluntary := s3_req.miss || s3_req.replace
731  io.wb.bits.hasData := writeback_data
732  io.wb.bits.dirty := s3_coh === ClientStates.Dirty
733  io.wb.bits.data := s3_data.asUInt()
734  io.wb.bits.delay_release := s3_req.replace
735  io.wb.bits.miss_id := s3_req.miss_id
736
737  io.replace_access.valid := RegNext(s1_fire && (s1_req.isAMO || s1_req.isStore) && !s1_req.probe)
738  io.replace_access.bits.set := s2_idx
739  io.replace_access.bits.way := RegNext(OHToUInt(s1_way_en))
740
741  io.replace_way.set.valid := RegNext(s0_fire)
742  io.replace_way.set.bits := s1_idx
743
744  // TODO: consider block policy of a finer granularity
745  io.status.s0_set.valid := req.valid
746  io.status.s0_set.bits := get_idx(s0_req.vaddr)
747  io.status.s1.valid := s1_valid
748  io.status.s1.bits.set := s1_idx
749  io.status.s1.bits.way_en := s1_way_en
750  io.status.s2.valid := s2_valid && !s2_req.replace
751  io.status.s2.bits.set := s2_idx
752  io.status.s2.bits.way_en := s2_way_en
753  io.status.s3.valid := s3_valid && !s3_req.replace
754  io.status.s3.bits.set := s3_idx
755  io.status.s3.bits.way_en := s3_way_en
756
757  // report error to beu and csr, 1 cycle after read data resp
758  io.error := 0.U.asTypeOf(new L1CacheErrorInfo())
759  // report error, update error csr
760  io.error.valid := s3_error && RegNext(s2_fire)
761  // only tag_error and data_error will be reported to beu
762  // l2_error should not be reported (l2 will report that)
763  io.error.report_to_beu := (RegEnable(s2_tag_error, s2_fire) || s3_data_error) && RegNext(s2_fire)
764  io.error.paddr := RegEnable(s2_req.addr, s2_fire)
765  io.error.source.tag := RegEnable(s2_tag_error, s2_fire)
766  io.error.source.data := s3_data_error
767  io.error.source.l2 := RegEnable(s2_flag_error || s2_l2_error, s2_fire)
768  io.error.opType.store := RegEnable(s2_req.isStore && !s2_req.probe, s2_fire)
769  io.error.opType.probe := RegEnable(s2_req.probe, s2_fire)
770  io.error.opType.release := RegEnable(s2_req.replace, s2_fire)
771  io.error.opType.atom := RegEnable(s2_req.isAMO && !s2_req.probe, s2_fire)
772
773  val perfEvents = Seq(
774    ("dcache_mp_req          ", s0_fire                                                      ),
775    ("dcache_mp_total_penalty", PopCount(VecInit(Seq(s0_fire, s1_valid, s2_valid, s3_valid))))
776  )
777  generatePerfEvent()
778}
779