xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/mainpipe/MainPipe.scala (revision fa9d712c89878ecee3ecf56223b1bb1f63fc78e9)
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 = 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_tag_error = dcacheParameters.tagCode.decode(s1_encTag).error && s1_need_tag
259  val s1_l2_error = s1_req.error
260
261  // replacement policy
262  val s1_repl_way_en = WireInit(0.U(nWays.W))
263  s1_repl_way_en := Mux(RegNext(s0_fire), UIntToOH(io.replace_way.way), RegNext(s1_repl_way_en))
264  val s1_repl_tag = Mux1H(s1_repl_way_en, wayMap(w => tag_resp(w)))
265  val s1_repl_coh = Mux1H(s1_repl_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
266  val s1_miss_tag = Mux1H(s1_req.miss_way_en, wayMap(w => tag_resp(w)))
267  val s1_miss_coh = Mux1H(s1_req.miss_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
268
269  val s1_repl_way_raw = WireInit(0.U(log2Up(nWays).W))
270  s1_repl_way_raw := Mux(RegNext(s0_fire), io.replace_way.way, RegNext(s1_repl_way_raw))
271
272  val s1_need_replacement = (s1_req.miss || s1_req.isStore && !s1_req.probe) && !s1_tag_match
273  val s1_way_en = Mux(
274    s1_req.replace,
275    s1_req.replace_way_en,
276    Mux(
277      s1_req.miss,
278      s1_req.miss_way_en,
279      Mux(
280        s1_need_replacement,
281        s1_repl_way_en,
282        s1_tag_match_way
283      )
284    )
285  )
286  assert(!RegNext(s1_fire && PopCount(s1_way_en) > 1.U))
287  val s1_tag = Mux(
288    s1_req.replace,
289    get_tag(s1_req.addr),
290    Mux(
291      s1_req.miss,
292      s1_miss_tag,
293      Mux(s1_need_replacement, s1_repl_tag, s1_hit_tag)
294    )
295  )
296  val s1_coh = Mux(
297    s1_req.replace,
298    Mux1H(s1_req.replace_way_en, meta_resp.map(ClientMetadata(_))),
299    Mux(
300      s1_req.miss,
301      s1_miss_coh,
302      Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh)
303    )
304  )
305
306  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
307  val s1_hit = s1_tag_match && s1_has_permission
308  val s1_pregen_can_go_to_mq = !s1_req.replace && !s1_req.probe && !s1_req.miss && (s1_req.isStore || s1_req.isAMO) && !s1_hit
309
310  // s2: select data, return resp if this is a store miss
311  val s2_valid = RegInit(false.B)
312  val s2_req = RegEnable(s1_req, s1_fire)
313  val s2_tag_match = RegEnable(s1_tag_match, s1_fire)
314  val s2_tag_match_way = RegEnable(s1_tag_match_way, s1_fire)
315  val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire)
316  val (s2_has_permission, _, s2_new_hit_coh) = s2_hit_coh.onAccess(s2_req.cmd)
317
318  val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire)
319  val s2_repl_coh = RegEnable(s1_repl_coh, s1_fire)
320  val s2_repl_way_en = RegEnable(s1_repl_way_en, s1_fire)
321  val s2_need_replacement = RegEnable(s1_need_replacement, s1_fire)
322  val s2_need_data = RegEnable(s1_need_data, s1_fire)
323  val s2_idx = get_idx(s2_req.vaddr)
324  val s2_way_en = RegEnable(s1_way_en, s1_fire)
325  val s2_tag = RegEnable(s1_tag, s1_fire)
326  val s2_coh = RegEnable(s1_coh, s1_fire)
327  val s2_banked_store_wmask = RegEnable(s1_banked_store_wmask, s1_fire)
328  val s2_flag_error = RegEnable(s1_flag_error, s1_fire)
329  val s2_tag_error = RegEnable(s1_tag_error, s1_fire)
330  val s2_l2_error = s2_req.error
331  // s2_data_error will be reported by data array
332  val s2_data_error = io.readline_error && s2_need_data && s2_coh.state =/= ClientStates.Nothing
333  val s2_error = s2_flag_error || s2_tag_error || s2_data_error || s2_l2_error
334
335  val s2_hit = s2_tag_match && s2_has_permission
336  val s2_amo_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isAMO
337  val s2_store_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isStore
338
339  s2_s0_set_conlict := s2_valid && s0_idx === s2_idx
340  s2_s0_set_conlict_store := s2_valid && store_idx === s2_idx
341
342  // For a store req, it either hits and goes to s3, or miss and enter miss queue immediately
343  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
344  val s2_can_go_to_mq = RegEnable(s1_pregen_can_go_to_mq, s1_fire)
345  assert(RegNext(!(s2_valid && s2_can_go_to_s3 && s2_can_go_to_mq)))
346  val s2_can_go = s2_can_go_to_s3 || s2_can_go_to_mq
347  val s2_fire = s2_valid && s2_can_go
348  val s2_fire_to_s3 = s2_valid && s2_can_go_to_s3
349  when (s1_fire) {
350    s2_valid := true.B
351  }.elsewhen (s2_fire) {
352    s2_valid := false.B
353  }
354  s2_ready := !s2_valid || s2_can_go
355  val replay = !io.miss_req.ready
356
357  val data_resp = Wire(io.data_resp.cloneType)
358  data_resp := Mux(RegNext(s1_fire), io.data_resp, RegNext(data_resp))
359  val s2_store_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
360
361  def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = {
362    val full_wmask = FillInterleaved(8, wmask)
363    ((~full_wmask & old_data) | (full_wmask & new_data))
364  }
365
366  val s2_data = WireInit(VecInit((0 until DCacheBanks).map(i => {
367    data_resp(i).raw_data
368  })))
369
370  for (i <- 0 until DCacheBanks) {
371    val old_data = s2_data(i)
372    val new_data = get_data_of_bank(i, s2_req.store_data)
373    // for amo hit, we should use read out SRAM data
374    // do not merge with store data
375    val wmask = Mux(s2_amo_hit, 0.U(wordBytes.W), get_mask_of_bank(i, s2_req.store_mask))
376    s2_store_data_merged(i) := mergePutData(old_data, new_data, wmask)
377  }
378
379  val s2_data_word = s2_store_data_merged(s2_req.word_idx)
380
381  // s3: write data, meta and tag
382  val s3_valid = RegInit(false.B)
383  val s3_req = RegEnable(s2_req, s2_fire_to_s3)
384  val s3_idx = get_idx(s3_req.vaddr)
385  val s3_tag = RegEnable(s2_tag, s2_fire_to_s3)
386  val s3_tag_match = RegEnable(s2_tag_match, s2_fire_to_s3)
387  val s3_coh = RegEnable(s2_coh, s2_fire_to_s3)
388  val s3_hit = RegEnable(s2_hit, s2_fire_to_s3)
389  val s3_amo_hit = RegEnable(s2_amo_hit, s2_fire_to_s3)
390  val s3_store_hit = RegEnable(s2_store_hit, s2_fire_to_s3)
391  val s3_hit_coh = RegEnable(s2_hit_coh, s2_fire_to_s3)
392  val s3_new_hit_coh = RegEnable(s2_new_hit_coh, s2_fire_to_s3)
393  val s3_way_en = RegEnable(s2_way_en, s2_fire_to_s3)
394  val s3_banked_store_wmask = RegEnable(s2_banked_store_wmask, s2_fire_to_s3)
395  val s3_store_data_merged = RegEnable(s2_store_data_merged, s2_fire_to_s3)
396  val s3_data_word = RegEnable(s2_data_word, s2_fire_to_s3)
397  val s3_data = RegEnable(s2_data, s2_fire_to_s3)
398  val s3_l2_error = s3_req.error
399  val s3_error = RegEnable(s2_error, s2_fire_to_s3)
400  val (probe_has_dirty_data, probe_shrink_param, probe_new_coh) = s3_coh.onProbe(s3_req.probe_param)
401  val s3_need_replacement = RegEnable(s2_need_replacement, s2_fire_to_s3)
402
403  val miss_update_meta = s3_req.miss
404  val probe_update_meta = s3_req.probe && s3_tag_match && s3_coh =/= probe_new_coh
405  val store_update_meta = s3_req.isStore && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
406  val amo_update_meta = s3_req.isAMO && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
407  val amo_wait_amoalu = s3_req.isAMO && s3_req.cmd =/= M_XLR && s3_req.cmd =/= M_XSC
408  val update_meta = (miss_update_meta || probe_update_meta || store_update_meta || amo_update_meta) && !s3_req.replace
409
410  def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = {
411    val c = categorize(cmd)
412    MuxLookup(Cat(c, param, dirty), Nothing, Seq(
413      //(effect param) -> (next)
414      Cat(rd, toB, false.B)  -> Branch,
415      Cat(rd, toB, true.B)   -> Branch,
416      Cat(rd, toT, false.B)  -> Trunk,
417      Cat(rd, toT, true.B)   -> Dirty,
418      Cat(wi, toT, false.B)  -> Trunk,
419      Cat(wi, toT, true.B)   -> Dirty,
420      Cat(wr, toT, false.B)  -> Dirty,
421      Cat(wr, toT, true.B)   -> Dirty))
422  }
423  val miss_new_coh = ClientMetadata(missCohGen(s3_req.cmd, s3_req.miss_param, s3_req.miss_dirty))
424
425  val new_coh = Mux(
426    miss_update_meta,
427    miss_new_coh,
428    Mux(
429      probe_update_meta,
430      probe_new_coh,
431      Mux(
432        store_update_meta || amo_update_meta,
433        s3_new_hit_coh,
434        ClientMetadata.onReset
435      )
436    )
437  )
438
439  // LR, SC and AMO
440  val debug_sc_fail_addr = RegInit(0.U)
441  val debug_sc_fail_cnt  = RegInit(0.U(8.W))
442
443  val lrsc_count = RegInit(0.U(log2Ceil(LRSCCycles).W))
444  val lrsc_valid = lrsc_count > LRSCBackOff.U
445  val lrsc_addr  = Reg(UInt())
446  val s3_lr = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XLR
447  val s3_sc = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XSC
448  val s3_lrsc_addr_match = lrsc_valid && lrsc_addr === get_block_addr(s3_req.addr)
449  val s3_sc_fail = s3_sc && !s3_lrsc_addr_match
450  val s3_sc_resp = Mux(s3_sc_fail, 1.U, 0.U)
451
452  val s3_can_do_amo = (s3_req.miss && !s3_req.probe && s3_req.source === AMO_SOURCE.U) || s3_amo_hit
453  val s3_can_do_amo_write = s3_can_do_amo && isWrite(s3_req.cmd) && !s3_sc_fail
454
455  when (s3_valid && (s3_lr || s3_sc)) {
456    when (s3_can_do_amo && s3_lr) {
457      lrsc_count := (LRSCCycles - 1).U
458      lrsc_addr := get_block_addr(s3_req.addr)
459    } .otherwise {
460      lrsc_count := 0.U
461    }
462  } .elsewhen (lrsc_count > 0.U) {
463    lrsc_count := lrsc_count - 1.U
464  }
465
466  io.lrsc_locked_block.valid := lrsc_valid
467  io.lrsc_locked_block.bits  := lrsc_addr
468  io.block_lr := RegNext(lrsc_count > 0.U)
469
470  // When we update update_resv_set, block all probe req in the next cycle
471  // It should give Probe reservation set addr compare an independent cycle,
472  // which will lead to better timing
473  io.update_resv_set := s3_valid && s3_lr && s3_can_do_amo
474
475  // when we release this block,
476  // we invalidate this reservation set
477  when (io.invalid_resv_set) {
478    lrsc_count := 0.U
479  }
480
481  when (s3_valid) {
482    when (s3_req.addr === debug_sc_fail_addr) {
483      when (s3_sc_fail) {
484        debug_sc_fail_cnt := debug_sc_fail_cnt + 1.U
485      } .elsewhen (s3_sc) {
486        debug_sc_fail_cnt := 0.U
487      }
488    } .otherwise {
489      when (s3_sc_fail) {
490        debug_sc_fail_addr := s3_req.addr
491        debug_sc_fail_cnt  := 1.U
492        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")
493      }
494    }
495  }
496  // assert(debug_sc_fail_cnt < 100.U, "L1DCache failed too many SCs in a row")
497
498  val banked_amo_wmask = UIntToOH(s3_req.word_idx)
499//  val banked_wmask = s3_banked_store_wmask
500  val banked_wmask = Mux(
501    s3_req.miss,
502    banked_full_wmask,
503    Mux(
504      s3_store_hit,
505      s3_banked_store_wmask,
506      Mux(
507        s3_can_do_amo_write,
508        banked_amo_wmask,
509        banked_none_wmask
510      )
511    )
512  )
513  val update_data = s3_req.miss || s3_store_hit || s3_can_do_amo_write
514  assert(!(banked_wmask.orR && !update_data))
515
516  // generate write data
517  // AMO hits
518  val s3_s_amoalu = RegInit(false.B)
519  val do_amoalu = amo_wait_amoalu && s3_valid && !s3_s_amoalu
520  val amoalu   = Module(new AMOALU(wordBits))
521  amoalu.io.mask := s3_req.amo_mask
522  amoalu.io.cmd  := s3_req.cmd
523  amoalu.io.lhs  := s3_data_word
524  amoalu.io.rhs  := s3_req.amo_data
525
526  // merge amo write data
527//  val amo_bitmask = FillInterleaved(8, s3_req.amo_mask)
528  val s3_amo_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
529  val s3_sc_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
530  for (i <- 0 until DCacheBanks) {
531    val old_data = s3_store_data_merged(i)
532    val new_data = amoalu.io.out
533    val wmask = Mux(
534      s3_req.word_idx === i.U,
535      ~0.U(wordBytes.W),
536      0.U(wordBytes.W)
537    )
538    s3_amo_data_merged(i) := mergePutData(old_data, new_data, wmask)
539//    s3_sc_data_merged(i) := amo_bitmask & s3_req.amo_data | ~amo_bitmask & old_data
540    s3_sc_data_merged(i) := mergePutData(old_data, s3_req.amo_data,
541      Mux(s3_req.word_idx === i.U && !s3_sc_fail, s3_req.amo_mask, 0.U(wordBytes.W))
542    )
543  }
544  val s3_amo_data_merged_reg = RegEnable(s3_amo_data_merged, do_amoalu)
545  when(do_amoalu){
546    s3_s_amoalu := true.B
547  }
548
549  val miss_wb = s3_req.miss && s3_need_replacement && s3_coh.state =/= ClientStates.Nothing
550  val probe_wb = s3_req.probe
551  val replace_wb = s3_req.replace
552  val need_wb = miss_wb || probe_wb || replace_wb
553
554  val (_, miss_shrink_param, _) = s3_coh.onCacheControl(M_FLUSH)
555  val writeback_param = Mux(probe_wb, probe_shrink_param, miss_shrink_param)
556  val writeback_data = if (dcacheParameters.alwaysReleaseData) {
557    s3_tag_match && s3_req.probe && s3_req.probe_need_data ||
558      s3_coh === ClientStates.Dirty || (miss_wb || replace_wb) && s3_coh.state =/= ClientStates.Nothing
559  } else {
560    s3_tag_match && s3_req.probe && s3_req.probe_need_data || s3_coh === ClientStates.Dirty
561  }
562
563  val s3_probe_can_go = s3_req.probe && io.wb.ready && (io.meta_write.ready || !probe_update_meta)
564  val s3_store_can_go = s3_req.isStore && !s3_req.probe && (io.meta_write.ready || !store_update_meta) && (io.data_write.ready || !update_data)
565  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)
566  val s3_miss_can_go = s3_req.miss &&
567    (io.meta_write.ready || !amo_update_meta) &&
568    (io.data_write.ready || !update_data) &&
569    (s3_s_amoalu || !amo_wait_amoalu) &&
570    io.tag_write.ready &&
571    io.wb.ready
572  val s3_replace_nothing = s3_req.replace && s3_coh.state === ClientStates.Nothing
573  val s3_replace_can_go = s3_req.replace && (s3_replace_nothing || io.wb.ready)
574  val s3_can_go = s3_probe_can_go || s3_store_can_go || s3_amo_can_go || s3_miss_can_go || s3_replace_can_go
575  val s3_update_data_cango = s3_store_can_go || s3_amo_can_go || s3_miss_can_go // used to speed up data_write gen
576  val s3_fire = s3_valid && s3_can_go
577  when (s2_fire_to_s3) {
578    s3_valid := true.B
579  }.elsewhen (s3_fire) {
580    s3_valid := false.B
581  }
582  s3_ready := !s3_valid || s3_can_go
583  s3_s0_set_conflict := s3_valid && s3_idx === s0_idx
584  s3_s0_set_conflict_store := s3_valid && s3_idx === store_idx
585  assert(RegNext(!s3_valid || !(s3_req.isStore && !s3_req.probe) || s3_hit)) // miss store should never come to s3
586
587  when(s3_fire) {
588    s3_s_amoalu := false.B
589  }
590
591  req.ready := s0_can_go
592
593  io.meta_read.valid := req.valid && s1_ready && !set_conflict
594  io.meta_read.bits.idx := get_idx(s0_req.vaddr)
595  io.meta_read.bits.way_en := Mux(s0_req.replace, s0_req.replace_way_en, ~0.U(nWays.W))
596
597  io.tag_read.valid := req.valid && s1_ready && !set_conflict && !s0_req.replace
598  io.tag_read.bits.idx := get_idx(s0_req.vaddr)
599  io.tag_read.bits.way_en := ~0.U(nWays.W)
600
601  io.data_read_intend := s1_valid && s1_need_data
602  io.data_read.valid := s1_valid && s1_need_data && s2_ready
603  io.data_read.bits.rmask := s1_banked_rmask
604  io.data_read.bits.way_en := s1_way_en
605  io.data_read.bits.addr := s1_req.vaddr
606
607  io.miss_req.valid := s2_valid && s2_can_go_to_mq
608  val miss_req = io.miss_req.bits
609  miss_req := DontCare
610  miss_req.source := s2_req.source
611  miss_req.cmd := s2_req.cmd
612  miss_req.addr := s2_req.addr
613  miss_req.vaddr := s2_req.vaddr
614  miss_req.way_en := Mux(s2_tag_match, s2_tag_match_way, s2_repl_way_en)
615  miss_req.store_data := s2_req.store_data
616  miss_req.store_mask := s2_req.store_mask
617  miss_req.word_idx := s2_req.word_idx
618  miss_req.amo_data := s2_req.amo_data
619  miss_req.amo_mask := s2_req.amo_mask
620  miss_req.req_coh := s2_hit_coh
621  miss_req.replace_coh := s2_repl_coh
622  miss_req.replace_tag := s2_repl_tag
623  miss_req.id := s2_req.id
624  miss_req.cancel := false.B
625
626  io.store_replay_resp.valid := s2_valid && s2_can_go_to_mq && replay && s2_req.isStore
627  io.store_replay_resp.bits.data := DontCare
628  io.store_replay_resp.bits.miss := true.B
629  io.store_replay_resp.bits.replay := true.B
630  io.store_replay_resp.bits.id := s2_req.id
631
632  io.store_hit_resp.valid := s3_valid && s3_store_can_go
633  io.store_hit_resp.bits.data := DontCare
634  io.store_hit_resp.bits.miss := false.B
635  io.store_hit_resp.bits.replay := false.B
636  io.store_hit_resp.bits.id := s3_req.id
637
638  io.release_update.valid := s3_valid && (s3_store_can_go || s3_amo_can_go) && s3_hit && update_data
639  io.release_update.bits.addr := s3_req.addr
640  io.release_update.bits.mask := Mux(s3_store_hit, s3_banked_store_wmask, banked_amo_wmask)
641  io.release_update.bits.data := Mux(
642    amo_wait_amoalu,
643    s3_amo_data_merged_reg,
644    Mux(
645      s3_sc,
646      s3_sc_data_merged,
647      s3_store_data_merged
648    )
649  ).asUInt
650
651  val atomic_hit_resp = Wire(new AtomicsResp)
652  atomic_hit_resp.data := Mux(s3_sc, s3_sc_resp, s3_data_word)
653  atomic_hit_resp.miss := false.B
654  atomic_hit_resp.miss_id := s3_req.miss_id
655  atomic_hit_resp.error := s3_error
656  atomic_hit_resp.replay := false.B
657  atomic_hit_resp.ack_miss_queue := s3_req.miss
658  atomic_hit_resp.id := lrsc_valid
659  val atomic_replay_resp = Wire(new AtomicsResp)
660  atomic_replay_resp.data := DontCare
661  atomic_replay_resp.miss := true.B
662  atomic_replay_resp.miss_id := DontCare
663  atomic_replay_resp.error := false.B
664  atomic_replay_resp.replay := true.B
665  atomic_replay_resp.ack_miss_queue := false.B
666  atomic_replay_resp.id := DontCare
667  val atomic_replay_resp_valid = s2_valid && s2_can_go_to_mq && replay && s2_req.isAMO
668  val atomic_hit_resp_valid = s3_valid && (s3_amo_can_go || s3_miss_can_go && s3_req.isAMO)
669  io.atomic_resp.valid := atomic_replay_resp_valid || atomic_hit_resp_valid
670  io.atomic_resp.bits := Mux(atomic_replay_resp_valid, atomic_replay_resp, atomic_hit_resp)
671
672  io.replace_resp.valid := s3_fire && s3_req.replace
673  io.replace_resp.bits := s3_req.miss_id
674
675  io.meta_write.valid := s3_fire && update_meta
676  io.meta_write.bits.idx := s3_idx
677  io.meta_write.bits.way_en := s3_way_en
678  io.meta_write.bits.meta.coh := new_coh
679
680  io.error_flag_write.valid := s3_fire && update_meta && s3_l2_error
681  io.error_flag_write.bits.idx := s3_idx
682  io.error_flag_write.bits.way_en := s3_way_en
683  io.error_flag_write.bits.error := s3_l2_error
684
685  io.tag_write.valid := s3_fire && s3_req.miss
686  io.tag_write.bits.idx := s3_idx
687  io.tag_write.bits.way_en := s3_way_en
688  io.tag_write.bits.tag := get_tag(s3_req.addr)
689
690  io.data_write.valid := s3_valid && s3_update_data_cango && update_data
691  io.data_write.bits.way_en := s3_way_en
692  io.data_write.bits.addr := s3_req.vaddr
693  io.data_write.bits.wmask := banked_wmask
694  io.data_write.bits.data := Mux(
695    amo_wait_amoalu,
696    s3_amo_data_merged_reg,
697    Mux(
698      s3_sc,
699      s3_sc_data_merged,
700      s3_store_data_merged
701    )
702  )
703  assert(RegNext(!io.meta_write.valid || !s3_req.replace))
704  assert(RegNext(!io.tag_write.valid || !s3_req.replace))
705  assert(RegNext(!io.data_write.valid || !s3_req.replace))
706
707  io.wb.valid := s3_valid && (
708    // replace
709    s3_req.replace && !s3_replace_nothing ||
710    // probe can go to wbq
711    s3_req.probe && (io.meta_write.ready || !probe_update_meta) ||
712      // amo miss can go to wbq
713      s3_req.miss &&
714        (io.meta_write.ready || !amo_update_meta) &&
715        (io.data_write.ready || !update_data) &&
716        (s3_s_amoalu || !amo_wait_amoalu) &&
717        io.tag_write.ready
718    ) && need_wb
719  io.wb.bits.addr := get_block_addr(Cat(s3_tag, get_untag(s3_req.vaddr)))
720  io.wb.bits.param := writeback_param
721  io.wb.bits.voluntary := s3_req.miss || s3_req.replace
722  io.wb.bits.hasData := writeback_data
723  io.wb.bits.dirty := s3_coh === ClientStates.Dirty
724  io.wb.bits.data := s3_data.asUInt()
725  io.wb.bits.delay_release := s3_req.replace
726  io.wb.bits.miss_id := s3_req.miss_id
727
728  io.replace_access.valid := RegNext(s1_fire && (s1_req.isAMO || s1_req.isStore) && !s1_req.probe)
729  io.replace_access.bits.set := s2_idx
730  io.replace_access.bits.way := RegNext(OHToUInt(s1_way_en))
731
732  io.replace_way.set.valid := RegNext(s0_fire)
733  io.replace_way.set.bits := s1_idx
734
735  // TODO: consider block policy of a finer granularity
736  io.status.s0_set.valid := req.valid
737  io.status.s0_set.bits := get_idx(s0_req.vaddr)
738  io.status.s1.valid := s1_valid
739  io.status.s1.bits.set := s1_idx
740  io.status.s1.bits.way_en := s1_way_en
741  io.status.s2.valid := s2_valid && !s2_req.replace
742  io.status.s2.bits.set := s2_idx
743  io.status.s2.bits.way_en := s2_way_en
744  io.status.s3.valid := s3_valid && !s3_req.replace
745  io.status.s3.bits.set := s3_idx
746  io.status.s3.bits.way_en := s3_way_en
747
748  io.error := 0.U.asTypeOf(new L1CacheErrorInfo())
749  io.error.report_to_beu := RegNext((s2_tag_error || s2_data_error) && s2_fire)
750  io.error.paddr := RegNext(s2_req.addr)
751  io.error.source.tag := RegNext(s2_tag_error)
752  io.error.source.data := RegNext(s2_data_error)
753  io.error.source.l2 := RegNext(s2_flag_error || s2_l2_error)
754  io.error.opType.store := RegNext(s2_req.isStore && !s2_req.probe)
755  io.error.opType.probe := RegNext(s2_req.probe)
756  io.error.opType.release := RegNext(s2_req.replace)
757  io.error.opType.atom := RegNext(s2_req.isAMO && !s2_req.probe)
758  io.error.valid := RegNext(s2_error && s2_fire)
759
760  val perfEvents = Seq(
761    ("dcache_mp_req          ", s0_fire                                                      ),
762    ("dcache_mp_total_penalty", PopCount(VecInit(Seq(s0_fire, s1_valid, s2_valid, s3_valid))))
763  )
764  generatePerfEvent()
765}
766