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