MainPipe.scala 30.6 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

A
Allen 已提交
17 18
package xiangshan.cache

19
import chipsalliance.rocketchip.config.Parameters
A
Allen 已提交
20 21
import chisel3._
import chisel3.util._
22 23 24
import freechips.rocketchip.tilelink.ClientStates._
import freechips.rocketchip.tilelink.MemoryOpCategories._
import freechips.rocketchip.tilelink.TLPermissions._
A
Allen 已提交
25
import freechips.rocketchip.tilelink.{ClientMetadata, ClientStates, TLPermissions}
Z
zhanglinjuan 已提交
26
import utils._
27
import xiangshan.L1CacheErrorInfo
A
Allen 已提交
28

Z
zhanglinjuan 已提交
29 30
class MainPipeReq(implicit p: Parameters) extends DCacheBundle {
  val miss = Bool() // only amo miss will refill in main pipe
A
Allen 已提交
31 32
  val miss_id = UInt(log2Up(cfg.nMissEntries).W)
  val miss_param = UInt(TLPermissions.bdWidth.W)
33
  val miss_dirty = Bool()
34
  val miss_way_en = UInt(DCacheWays.W)
A
Allen 已提交
35 36 37

  val probe = Bool()
  val probe_param = UInt(TLPermissions.bdWidth.W)
38
  val probe_need_data = Bool()
A
Allen 已提交
39 40

  // request info
Z
zhanglinjuan 已提交
41
  // reqs from Store, AMO use this
A
Allen 已提交
42 43
  // probe does not use this
  val source = UInt(sourceTypeWidth.W)
Z
zhanglinjuan 已提交
44
  val cmd = UInt(M_SZ.W)
45 46 47
  // if dcache size > 32KB, vaddr is also needed for store
  // vaddr is used to get extra index bits
  val vaddr  = UInt(VAddrBits.W)
A
Allen 已提交
48 49 50 51
  // must be aligned to block
  val addr   = UInt(PAddrBits.W)

  // store
Z
zhanglinjuan 已提交
52 53
  val store_data = UInt((cfg.blockBytes * 8).W)
  val store_mask = UInt(cfg.blockBytes.W)
A
Allen 已提交
54 55 56 57

  // which word does amo work on?
  val word_idx = UInt(log2Up(cfg.blockBytes * 8 / DataBits).W)
  val amo_data   = UInt(DataBits.W)
Z
zhanglinjuan 已提交
58 59
  val amo_mask   = UInt((DataBits / 8).W)

60 61 62
  // error
  val error = Bool()

63 64 65 66
  // replace
  val replace = Bool()
  val replace_way_en = UInt(DCacheWays.W)

Z
zhanglinjuan 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  val id = UInt(reqIdWidth.W)

  def isLoad: Bool = source === LOAD_SOURCE.U
  def isStore: Bool = source === STORE_SOURCE.U
  def isAMO: Bool = source === AMO_SOURCE.U

  def convertStoreReq(store: DCacheLineReq): MainPipeReq = {
    val req = Wire(new MainPipeReq)
    req := DontCare
    req.miss := false.B
    req.miss_dirty := false.B
    req.probe := false.B
    req.probe_need_data := false.B
    req.source := STORE_SOURCE.U
    req.cmd := store.cmd
    req.addr := store.addr
    req.vaddr := store.vaddr
    req.store_data := store.data
    req.store_mask := store.mask
86
    req.replace := false.B
87
    req.error := false.B
Z
zhanglinjuan 已提交
88 89
    req.id := store.id
    req
A
Allen 已提交
90 91 92
  }
}

93
class MainPipe(implicit p: Parameters) extends DCacheModule with HasPerfEvents {
Z
zhanglinjuan 已提交
94 95 96 97
  val io = IO(new Bundle() {
    // probe queue
    val probe_req = Flipped(DecoupledIO(new MainPipeReq))
    // store miss go to miss queue
W
William Wang 已提交
98
    val miss_req = DecoupledIO(new MissReq)
Z
zhanglinjuan 已提交
99 100 101 102 103 104 105 106
    // store buffer
    val store_req = Flipped(DecoupledIO(new DCacheLineReq))
    val store_replay_resp = ValidIO(new DCacheLineResp)
    val store_hit_resp = ValidIO(new DCacheLineResp)
    val release_update = ValidIO(new ReleaseUpdate)
    // atmoics
    val atomic_req = Flipped(DecoupledIO(new MainPipeReq))
    val atomic_resp = ValidIO(new AtomicsResp)
107 108 109
    // replace
    val replace_req = Flipped(DecoupledIO(new MainPipeReq))
    val replace_resp = ValidIO(UInt(log2Up(cfg.nMissEntries).W))
Z
zhanglinjuan 已提交
110 111 112
    // write-back queue
    val wb = DecoupledIO(new WritebackReq)

113
    val data_read_intend = Output(Bool())
Z
zhanglinjuan 已提交
114 115
    val data_read = DecoupledIO(new L1BankedDataReadLineReq)
    val data_resp = Input(Vec(DCacheBanks, new L1BankedDataReadResult()))
116
    val readline_error_delayed = Input(Bool())
Z
zhanglinjuan 已提交
117 118 119
    val data_write = DecoupledIO(new L1BankedDataWriteReq)

    val meta_read = DecoupledIO(new MetaReadReq)
120
    val meta_resp = Input(Vec(nWays, new Meta))
Z
zhanglinjuan 已提交
121
    val meta_write = DecoupledIO(new MetaWriteReq)
122 123
    val error_flag_resp = Input(Vec(nWays, Bool()))
    val error_flag_write = DecoupledIO(new ErrorWriteReq)
Z
zhanglinjuan 已提交
124 125

    val tag_read = DecoupledIO(new TagReadReq)
126
    val tag_resp = Input(Vec(nWays, UInt(encTagBits.W)))
Z
zhanglinjuan 已提交
127
    val tag_write = DecoupledIO(new TagWriteReq)
128

Z
zhanglinjuan 已提交
129 130 131 132 133 134 135 136 137 138 139 140
    // update state vec in replacement algo
    val replace_access = ValidIO(new ReplacementAccessBundle)
    // find the way to be replaced
    val replace_way = new ReplacementWayReqIO

    val status = new Bundle() {
      val s0_set = ValidIO(UInt(idxBits.W))
      val s1, s2, s3 = ValidIO(new Bundle() {
        val set = UInt(idxBits.W)
        val way_en = UInt(nWays.W)
      })
    }
141 142 143

    // lrsc locked block should block probe
    val lrsc_locked_block = Output(Valid(UInt(PAddrBits.W)))
Z
zhanglinjuan 已提交
144
    val invalid_resv_set = Input(Bool())
W
William Wang 已提交
145
    val update_resv_set = Output(Bool())
146
    val block_lr = Output(Bool())
147 148 149

    // ecc error
    val error = Output(new L1CacheErrorInfo())
150 151
  })

Z
zhanglinjuan 已提交
152 153 154
  // meta array is made of regs, so meta write or read should always be ready
  assert(RegNext(io.meta_read.ready))
  assert(RegNext(io.meta_write.ready))
155

Z
zhanglinjuan 已提交
156 157
  val s1_s0_set_conflict, s2_s0_set_conlict, s3_s0_set_conflict = Wire(Bool())
  val set_conflict = s1_s0_set_conflict || s2_s0_set_conlict || s3_s0_set_conflict
Z
zhanglinjuan 已提交
158 159 160 161
  // check sbuffer store req set_conflict in parallel with req arbiter
  // it will speed up the generation of store_req.ready, which is in crit. path
  val s1_s0_set_conflict_store, s2_s0_set_conlict_store, s3_s0_set_conflict_store = Wire(Bool())
  val store_set_conflict = s1_s0_set_conflict_store || s2_s0_set_conlict_store || s3_s0_set_conflict_store
162 163
  val s1_ready, s2_ready, s3_ready = Wire(Bool())

Z
zhanglinjuan 已提交
164 165 166 167 168 169 170 171
  // convert store req to main pipe req, and select a req from store and probe
  val store_req = Wire(DecoupledIO(new MainPipeReq))
  store_req.bits := (new MainPipeReq).convertStoreReq(io.store_req.bits)
  store_req.valid := io.store_req.valid
  io.store_req.ready := store_req.ready

  // s0: read meta and tag
  val req = Wire(DecoupledIO(new MainPipeReq))
172 173 174
  arbiter(
    in = Seq(
      io.probe_req,
W
William Wang 已提交
175
      io.replace_req,
Z
zhanglinjuan 已提交
176
      store_req, // Note: store_req.ready is now manually assigned for better timing
W
William Wang 已提交
177
      io.atomic_req
178 179 180 181
    ),
    out = req,
    name = Some("main_pipe_req")
  )
Z
zhanglinjuan 已提交
182 183 184 185 186 187

  val store_idx = get_idx(io.store_req.bits.vaddr)
  // manually assign store_req.ready for better timing
  // now store_req set conflict check is done in parallel with req arbiter
  store_req.ready := io.meta_read.ready && io.tag_read.ready && s1_ready && !store_set_conflict &&
    !io.probe_req.valid && !io.replace_req.valid
Z
zhanglinjuan 已提交
188 189
  val s0_req = req.bits
  val s0_idx = get_idx(s0_req.vaddr)
190
  val s0_need_tag = io.tag_read.valid
Z
zhanglinjuan 已提交
191 192
  val s0_can_go = io.meta_read.ready && io.tag_read.ready && s1_ready && !set_conflict
  val s0_fire = req.valid && s0_can_go
193

194 195 196
  val bank_write = VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, s0_req.store_mask).orR)).asUInt
  val bank_full_write = VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, s0_req.store_mask).andR)).asUInt
  val banks_full_overwrite = bank_full_write.andR
197

198 199 200
  val banked_store_rmask = bank_write & ~bank_full_write
  val banked_full_rmask = ~0.U(DCacheBanks.W)
  val banked_none_rmask = 0.U(DCacheBanks.W)
201

Z
zhanglinjuan 已提交
202
  val store_need_data = !s0_req.probe && s0_req.isStore && banked_store_rmask.orR
203
  val probe_need_data = s0_req.probe
Z
zhanglinjuan 已提交
204 205
  val amo_need_data = !s0_req.probe && s0_req.isAMO
  val miss_need_data = s0_req.miss
206
  val replace_need_data = s0_req.replace
Y
Yinan Xu 已提交
207

208
  val banked_need_data = store_need_data || probe_need_data || amo_need_data || miss_need_data || replace_need_data
209

Z
zhanglinjuan 已提交
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,
Y
Yinan Xu 已提交
212
      banked_full_rmask,
213 214
      banked_none_rmask
    ))
215 216

  // generate wmask here and use it in stage 2
217 218 219
  val banked_store_wmask = bank_write
  val banked_full_wmask = ~0.U(DCacheBanks.W)
  val banked_none_wmask = 0.U(DCacheBanks.W)
220

Z
zhanglinjuan 已提交
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 226
  val s1_banked_rmask = RegEnable(s0_banked_rmask, s0_fire)
  val s1_banked_store_wmask = RegEnable(banked_store_wmask, s0_fire)
227
  val s1_need_tag = RegEnable(s0_need_tag, s0_fire)
Z
zhanglinjuan 已提交
228 229 230
  val s1_can_go = s2_ready && (io.data_read.ready || !s1_need_data)
  val s1_fire = s1_valid && s1_can_go
  val s1_idx = get_idx(s1_req.vaddr)
231 232 233 234 235
  when (s0_fire) {
    s1_valid := true.B
  }.elsewhen (s1_fire) {
    s1_valid := false.B
  }
Z
zhanglinjuan 已提交
236 237
  s1_ready := !s1_valid || s1_can_go
  s1_s0_set_conflict := s1_valid && s0_idx === s1_idx
Z
zhanglinjuan 已提交
238
  s1_s0_set_conflict_store := s1_valid && store_idx === s1_idx
239

240
  val meta_resp = Wire(Vec(nWays, (new Meta).asUInt()))
Z
zhanglinjuan 已提交
241
  val tag_resp = Wire(Vec(nWays, UInt(tagBits.W)))
242 243 244 245 246 247
  val ecc_resp = Wire(Vec(nWays, UInt(eccTagBits.W)))
  meta_resp := Mux(RegNext(s0_fire), VecInit(io.meta_resp.map(_.asUInt)), RegNext(meta_resp))
  tag_resp := Mux(RegNext(s0_fire), VecInit(io.tag_resp.map(r => r(tagBits - 1, 0))), RegNext(tag_resp))
  ecc_resp := Mux(RegNext(s0_fire), VecInit(io.tag_resp.map(r => r(encTagBits - 1, tagBits))), RegNext(ecc_resp))
  val enc_tag_resp = Wire(io.tag_resp.cloneType)
  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))
Z
zhanglinjuan 已提交
250 251
  val s1_tag_eq_way = wayMap((w: Int) => tag_resp(w) === get_tag(s1_req.addr)).asUInt
  val s1_tag_match_way = wayMap((w: Int) => s1_tag_eq_way(w) && Meta(meta_resp(w)).coh.isValid()).asUInt
252 253
  val s1_tag_match = s1_tag_match_way.orR

Z
zhanglinjuan 已提交
254 255
  val s1_hit_tag = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap(w => tag_resp(w))), get_tag(s1_req.addr))
  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))
Z
zhanglinjuan 已提交
263 264 265
  s1_repl_way_en := Mux(RegNext(s0_fire), UIntToOH(io.replace_way.way), RegNext(s1_repl_way_en))
  val s1_repl_tag = Mux1H(s1_repl_way_en, wayMap(w => tag_resp(w)))
  val s1_repl_coh = Mux1H(s1_repl_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
266 267
  val s1_miss_tag = Mux1H(s1_req.miss_way_en, wayMap(w => tag_resp(w)))
  val s1_miss_coh = Mux1H(s1_req.miss_way_en, wayMap(w => meta_resp(w))).asTypeOf(new ClientMetadata)
268

269 270 271
  val s1_repl_way_raw = WireInit(0.U(log2Up(nWays).W))
  s1_repl_way_raw := Mux(RegNext(s0_fire), io.replace_way.way, RegNext(s1_repl_way_raw))

Z
zhanglinjuan 已提交
272
  val s1_need_replacement = (s1_req.miss || s1_req.isStore && !s1_req.probe) && !s1_tag_match
273 274 275 276 277 278 279 280 281 282 283 284 285
  val s1_way_en = Mux(
    s1_req.replace,
    s1_req.replace_way_en,
    Mux(
      s1_req.miss,
      s1_req.miss_way_en,
      Mux(
        s1_need_replacement,
        s1_repl_way_en,
        s1_tag_match_way
      )
    )
  )
286
  assert(!RegNext(s1_fire && PopCount(s1_way_en) > 1.U))
287 288 289 290 291 292 293 294 295
  val s1_tag = Mux(
    s1_req.replace,
    get_tag(s1_req.addr),
    Mux(
      s1_req.miss,
      s1_miss_tag,
      Mux(s1_need_replacement, s1_repl_tag, s1_hit_tag)
    )
  )
296 297 298
  val s1_coh = Mux(
    s1_req.replace,
    Mux1H(s1_req.replace_way_en, meta_resp.map(ClientMetadata(_))),
299 300 301 302 303
    Mux(
      s1_req.miss,
      s1_miss_coh,
      Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh)
    )
304
  )
305

W
William Wang 已提交
306 307
  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
  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
W
William Wang 已提交
309

Z
zhanglinjuan 已提交
310 311 312
  // s2: select data, return resp if this is a store miss
  val s2_valid = RegInit(false.B)
  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)
Z
zhanglinjuan 已提交
316
  val (s2_has_permission, _, s2_new_hit_coh) = s2_hit_coh.onAccess(s2_req.cmd)
317

Z
zhanglinjuan 已提交
318 319
  val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire)
  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)
Z
zhanglinjuan 已提交
323
  val s2_idx = get_idx(s2_req.vaddr)
324
  val s2_way_en = RegEnable(s1_way_en, s1_fire)
Z
zhanglinjuan 已提交
325 326 327
  val s2_tag = RegEnable(s1_tag, s1_fire)
  val s2_coh = RegEnable(s1_coh, s1_fire)
  val s2_banked_store_wmask = RegEnable(s1_banked_store_wmask, s1_fire)
328 329
  val s2_flag_error = RegEnable(s1_flag_error, s1_fire)
  val s2_tag_error = RegEnable(s1_tag_error, s1_fire)
330
  val s2_l2_error = s2_req.error
331 332 333
  val s2_error = s2_flag_error || s2_tag_error || s2_l2_error // data_error not included

  val s2_may_report_data_error = s2_need_data && s2_coh.state =/= ClientStates.Nothing
334 335

  val s2_hit = s2_tag_match && s2_has_permission
Z
zhanglinjuan 已提交
336 337 338 339
  val s2_amo_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isAMO
  val s2_store_hit = s2_hit && !s2_req.probe && !s2_req.miss && s2_req.isStore

  s2_s0_set_conlict := s2_valid && s0_idx === s2_idx
Z
zhanglinjuan 已提交
340
  s2_s0_set_conlict_store := s2_valid && store_idx === s2_idx
Z
zhanglinjuan 已提交
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
W
William Wang 已提交
344
  val s2_can_go_to_mq = RegEnable(s1_pregen_can_go_to_mq, s1_fire)
Z
zhanglinjuan 已提交
345 346 347 348 349 350 351 352
  assert(RegNext(!(s2_valid && s2_can_go_to_s3 && s2_can_go_to_mq)))
  val s2_can_go = s2_can_go_to_s3 || s2_can_go_to_mq
  val s2_fire = s2_valid && s2_can_go
  val s2_fire_to_s3 = s2_valid && s2_can_go_to_s3
  when (s1_fire) {
    s2_valid := true.B
  }.elsewhen (s2_fire) {
    s2_valid := false.B
353
  }
Z
zhanglinjuan 已提交
354
  s2_ready := !s2_valid || s2_can_go
W
William Wang 已提交
355
  val replay = !io.miss_req.ready
356

Z
zhanglinjuan 已提交
357 358
  val data_resp = Wire(io.data_resp.cloneType)
  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 362 363 364

  def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = {
    val full_wmask = FillInterleaved(8, wmask)
    ((~full_wmask & old_data) | (full_wmask & new_data))
  }
Y
Yinan Xu 已提交
365

366
  val s2_data = WireInit(VecInit((0 until DCacheBanks).map(i => {
Z
zhanglinjuan 已提交
367
    data_resp(i).raw_data
368 369 370 371 372 373 374 375 376
  })))

  for (i <- 0 until DCacheBanks) {
    val old_data = s2_data(i)
    val new_data = get_data_of_bank(i, s2_req.store_data)
    // for amo hit, we should use read out SRAM data
    // do not merge with store data
    val wmask = Mux(s2_amo_hit, 0.U(wordBytes.W), get_mask_of_bank(i, s2_req.store_mask))
    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

Z
zhanglinjuan 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  // s3: write data, meta and tag
  val s3_valid = RegInit(false.B)
  val s3_req = RegEnable(s2_req, s2_fire_to_s3)
  val s3_idx = get_idx(s3_req.vaddr)
  val s3_tag = RegEnable(s2_tag, s2_fire_to_s3)
  val s3_tag_match = RegEnable(s2_tag_match, s2_fire_to_s3)
  val s3_coh = RegEnable(s2_coh, s2_fire_to_s3)
  val s3_hit = RegEnable(s2_hit, s2_fire_to_s3)
  val s3_amo_hit = RegEnable(s2_amo_hit, s2_fire_to_s3)
  val s3_store_hit = RegEnable(s2_store_hit, s2_fire_to_s3)
  val s3_hit_coh = RegEnable(s2_hit_coh, s2_fire_to_s3)
  val s3_new_hit_coh = RegEnable(s2_new_hit_coh, s2_fire_to_s3)
  val s3_way_en = RegEnable(s2_way_en, s2_fire_to_s3)
  val s3_banked_store_wmask = RegEnable(s2_banked_store_wmask, s2_fire_to_s3)
  val s3_store_data_merged = RegEnable(s2_store_data_merged, s2_fire_to_s3)
  val s3_data_word = RegEnable(s2_data_word, s2_fire_to_s3)
  val s3_data = RegEnable(s2_data, s2_fire_to_s3)
398
  val s3_l2_error = s3_req.error
399 400 401 402 403 404 405 406 407
  // data_error will be reported by data array 1 cycle after data read resp
  val s3_data_error = Wire(Bool())
  s3_data_error := Mux(RegNext(RegNext(s1_fire)), // ecc check result is generated 2 cycle after read req
    io.readline_error_delayed && RegNext(s2_may_report_data_error), 
    RegNext(s3_data_error) // do not update s3_data_error if !s1_fire
  )
  // error signal for amo inst
  // s3_error = s3_flag_error || s3_tag_error || s3_l2_error || s3_data_error
  val s3_error = RegEnable(s2_error, s2_fire_to_s3) || s3_data_error
Z
zhanglinjuan 已提交
408 409
  val (probe_has_dirty_data, probe_shrink_param, probe_new_coh) = s3_coh.onProbe(s3_req.probe_param)
  val s3_need_replacement = RegEnable(s2_need_replacement, s2_fire_to_s3)
410

Z
zhanglinjuan 已提交
411 412 413 414
  val miss_update_meta = s3_req.miss
  val probe_update_meta = s3_req.probe && s3_tag_match && s3_coh =/= probe_new_coh
  val store_update_meta = s3_req.isStore && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
  val amo_update_meta = s3_req.isAMO && !s3_req.probe && s3_hit_coh =/= s3_new_hit_coh
W
William Wang 已提交
415
  val amo_wait_amoalu = s3_req.isAMO && s3_req.cmd =/= M_XLR && s3_req.cmd =/= M_XSC
416
  val update_meta = (miss_update_meta || probe_update_meta || store_update_meta || amo_update_meta) && !s3_req.replace
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431
  def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = {
    val c = categorize(cmd)
    MuxLookup(Cat(c, param, dirty), Nothing, Seq(
      //(effect param) -> (next)
      Cat(rd, toB, false.B)  -> Branch,
      Cat(rd, toB, true.B)   -> Branch,
      Cat(rd, toT, false.B)  -> Trunk,
      Cat(rd, toT, true.B)   -> Dirty,
      Cat(wi, toT, false.B)  -> Trunk,
      Cat(wi, toT, true.B)   -> Dirty,
      Cat(wr, toT, false.B)  -> Dirty,
      Cat(wr, toT, true.B)   -> Dirty))
  }
  val miss_new_coh = ClientMetadata(missCohGen(s3_req.cmd, s3_req.miss_param, s3_req.miss_dirty))
432

Z
zhanglinjuan 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445
  val new_coh = Mux(
    miss_update_meta,
    miss_new_coh,
    Mux(
      probe_update_meta,
      probe_new_coh,
      Mux(
        store_update_meta || amo_update_meta,
        s3_new_hit_coh,
        ClientMetadata.onReset
      )
    )
  )
446 447 448 449 450

  // LR, SC and AMO
  val debug_sc_fail_addr = RegInit(0.U)
  val debug_sc_fail_cnt  = RegInit(0.U(8.W))

451 452
  val lrsc_count = RegInit(0.U(log2Ceil(LRSCCycles).W))
  val lrsc_valid = lrsc_count > LRSCBackOff.U
453
  val lrsc_addr  = Reg(UInt())
Z
zhanglinjuan 已提交
454 455
  val s3_lr = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XLR
  val s3_sc = !s3_req.probe && s3_req.isAMO && s3_req.cmd === M_XSC
456 457 458 459 460 461
  val s3_lrsc_addr_match = lrsc_valid && lrsc_addr === get_block_addr(s3_req.addr)
  val s3_sc_fail = s3_sc && !s3_lrsc_addr_match
  val s3_sc_resp = Mux(s3_sc_fail, 1.U, 0.U)

  val s3_can_do_amo = (s3_req.miss && !s3_req.probe && s3_req.source === AMO_SOURCE.U) || s3_amo_hit
  val s3_can_do_amo_write = s3_can_do_amo && isWrite(s3_req.cmd) && !s3_sc_fail
Z
zhanglinjuan 已提交
462

463 464
  when (s3_valid && (s3_lr || s3_sc)) {
    when (s3_can_do_amo && s3_lr) {
465
      lrsc_count := (LRSCCycles - 1).U
466 467 468 469 470 471 472 473 474 475
      lrsc_addr := get_block_addr(s3_req.addr)
    } .otherwise {
      lrsc_count := 0.U
    }
  } .elsewhen (lrsc_count > 0.U) {
    lrsc_count := lrsc_count - 1.U
  }

  io.lrsc_locked_block.valid := lrsc_valid
  io.lrsc_locked_block.bits  := lrsc_addr
476
  io.block_lr := RegNext(lrsc_count > 0.U)
477

W
William Wang 已提交
478 479 480 481 482
  // When we update update_resv_set, block all probe req in the next cycle
  // It should give Probe reservation set addr compare an independent cycle,
  // which will lead to better timing
  io.update_resv_set := s3_valid && s3_lr && s3_can_do_amo

483 484
  // when we release this block,
  // we invalidate this reservation set
Z
zhanglinjuan 已提交
485 486
  when (io.invalid_resv_set) {
    lrsc_count := 0.U
487 488 489 490 491 492 493 494 495 496 497 498 499
  }

  when (s3_valid) {
    when (s3_req.addr === debug_sc_fail_addr) {
      when (s3_sc_fail) {
        debug_sc_fail_cnt := debug_sc_fail_cnt + 1.U
      } .elsewhen (s3_sc) {
        debug_sc_fail_cnt := 0.U
      }
    } .otherwise {
      when (s3_sc_fail) {
        debug_sc_fail_addr := s3_req.addr
        debug_sc_fail_cnt  := 1.U
500
        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")
501 502 503
      }
    }
  }
504
  // assert(debug_sc_fail_cnt < 100.U, "L1DCache failed too many SCs in a row")
Z
zhanglinjuan 已提交
505

506
  val banked_amo_wmask = UIntToOH(s3_req.word_idx)
Z
zhanglinjuan 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520
//  val banked_wmask = s3_banked_store_wmask
  val banked_wmask = Mux(
    s3_req.miss,
    banked_full_wmask,
    Mux(
      s3_store_hit,
      s3_banked_store_wmask,
      Mux(
        s3_can_do_amo_write,
        banked_amo_wmask,
        banked_none_wmask
      )
    )
  )
521 522
  val update_data = s3_req.miss || s3_store_hit || s3_can_do_amo_write
  assert(!(banked_wmask.orR && !update_data))
523 524 525

  // generate write data
  // AMO hits
W
William Wang 已提交
526 527
  val s3_s_amoalu = RegInit(false.B)
  val do_amoalu = amo_wait_amoalu && s3_valid && !s3_s_amoalu
528 529 530 531 532 533 534
  val amoalu   = Module(new AMOALU(wordBits))
  amoalu.io.mask := s3_req.amo_mask
  amoalu.io.cmd  := s3_req.cmd
  amoalu.io.lhs  := s3_data_word
  amoalu.io.rhs  := s3_req.amo_data

  // merge amo write data
535
//  val amo_bitmask = FillInterleaved(8, s3_req.amo_mask)
536
  val s3_amo_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
W
William Wang 已提交
537
  val s3_sc_data_merged = Wire(Vec(DCacheBanks, UInt(DCacheSRAMRowBits.W)))
538 539 540
  for (i <- 0 until DCacheBanks) {
    val old_data = s3_store_data_merged(i)
    val new_data = amoalu.io.out
W
William Wang 已提交
541 542 543 544 545
    val wmask = Mux(
      s3_req.word_idx === i.U,
      ~0.U(wordBytes.W),
      0.U(wordBytes.W)
    )
546
    s3_amo_data_merged(i) := mergePutData(old_data, new_data, wmask)
547 548 549 550
//    s3_sc_data_merged(i) := amo_bitmask & s3_req.amo_data | ~amo_bitmask & old_data
    s3_sc_data_merged(i) := mergePutData(old_data, s3_req.amo_data,
      Mux(s3_req.word_idx === i.U && !s3_sc_fail, s3_req.amo_mask, 0.U(wordBytes.W))
    )
W
William Wang 已提交
551 552 553 554
  }
  val s3_amo_data_merged_reg = RegEnable(s3_amo_data_merged, do_amoalu)
  when(do_amoalu){
    s3_s_amoalu := true.B
555 556
  }

Z
zhanglinjuan 已提交
557 558
  val miss_wb = s3_req.miss && s3_need_replacement && s3_coh.state =/= ClientStates.Nothing
  val probe_wb = s3_req.probe
559 560
  val replace_wb = s3_req.replace
  val need_wb = miss_wb || probe_wb || replace_wb
561 562

  val (_, miss_shrink_param, _) = s3_coh.onCacheControl(M_FLUSH)
563
  val writeback_param = Mux(probe_wb, probe_shrink_param, miss_shrink_param)
564 565
  val writeback_data = if (dcacheParameters.alwaysReleaseData) {
    s3_tag_match && s3_req.probe && s3_req.probe_need_data ||
566
      s3_coh === ClientStates.Dirty || (miss_wb || replace_wb) && s3_coh.state =/= ClientStates.Nothing
567 568 569
  } else {
    s3_tag_match && s3_req.probe && s3_req.probe_need_data || s3_coh === ClientStates.Dirty
  }
570

Z
zhanglinjuan 已提交
571 572
  val s3_probe_can_go = s3_req.probe && io.wb.ready && (io.meta_write.ready || !probe_update_meta)
  val s3_store_can_go = s3_req.isStore && !s3_req.probe && (io.meta_write.ready || !store_update_meta) && (io.data_write.ready || !update_data)
W
William Wang 已提交
573
  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)
Z
zhanglinjuan 已提交
574 575 576
  val s3_miss_can_go = s3_req.miss &&
    (io.meta_write.ready || !amo_update_meta) &&
    (io.data_write.ready || !update_data) &&
W
William Wang 已提交
577
    (s3_s_amoalu || !amo_wait_amoalu) &&
Z
zhanglinjuan 已提交
578 579
    io.tag_write.ready &&
    io.wb.ready
580 581 582
  val s3_replace_nothing = s3_req.replace && s3_coh.state === ClientStates.Nothing
  val s3_replace_can_go = s3_req.replace && (s3_replace_nothing || io.wb.ready)
  val s3_can_go = s3_probe_can_go || s3_store_can_go || s3_amo_can_go || s3_miss_can_go || s3_replace_can_go
583
  val s3_update_data_cango = s3_store_can_go || s3_amo_can_go || s3_miss_can_go // used to speed up data_write gen
Z
zhanglinjuan 已提交
584 585 586 587 588
  val s3_fire = s3_valid && s3_can_go
  when (s2_fire_to_s3) {
    s3_valid := true.B
  }.elsewhen (s3_fire) {
    s3_valid := false.B
589
  }
Z
zhanglinjuan 已提交
590 591
  s3_ready := !s3_valid || s3_can_go
  s3_s0_set_conflict := s3_valid && s3_idx === s0_idx
Z
zhanglinjuan 已提交
592
  s3_s0_set_conflict_store := s3_valid && s3_idx === store_idx
Z
zhanglinjuan 已提交
593 594
  assert(RegNext(!s3_valid || !(s3_req.isStore && !s3_req.probe) || s3_hit)) // miss store should never come to s3

W
William Wang 已提交
595 596 597
  when(s3_fire) {
    s3_s_amoalu := false.B
  }
Z
zhanglinjuan 已提交
598 599 600 601 602

  req.ready := s0_can_go

  io.meta_read.valid := req.valid && s1_ready && !set_conflict
  io.meta_read.bits.idx := get_idx(s0_req.vaddr)
603
  io.meta_read.bits.way_en := Mux(s0_req.replace, s0_req.replace_way_en, ~0.U(nWays.W))
Z
zhanglinjuan 已提交
604

605
  io.tag_read.valid := req.valid && s1_ready && !set_conflict && !s0_req.replace
Z
zhanglinjuan 已提交
606 607 608
  io.tag_read.bits.idx := get_idx(s0_req.vaddr)
  io.tag_read.bits.way_en := ~0.U(nWays.W)

609
  io.data_read_intend := s1_valid && s1_need_data
Z
zhanglinjuan 已提交
610 611 612 613 614
  io.data_read.valid := s1_valid && s1_need_data && s2_ready
  io.data_read.bits.rmask := s1_banked_rmask
  io.data_read.bits.way_en := s1_way_en
  io.data_read.bits.addr := s1_req.vaddr

W
William Wang 已提交
615 616 617 618 619 620 621
  io.miss_req.valid := s2_valid && s2_can_go_to_mq
  val miss_req = io.miss_req.bits
  miss_req := DontCare
  miss_req.source := s2_req.source
  miss_req.cmd := s2_req.cmd
  miss_req.addr := s2_req.addr
  miss_req.vaddr := s2_req.vaddr
622
  miss_req.way_en := Mux(s2_tag_match, s2_tag_match_way, s2_repl_way_en)
W
William Wang 已提交
623 624 625 626 627 628 629 630 631 632
  miss_req.store_data := s2_req.store_data
  miss_req.store_mask := s2_req.store_mask
  miss_req.word_idx := s2_req.word_idx
  miss_req.amo_data := s2_req.amo_data
  miss_req.amo_mask := s2_req.amo_mask
  miss_req.req_coh := s2_hit_coh
  miss_req.replace_coh := s2_repl_coh
  miss_req.replace_tag := s2_repl_tag
  miss_req.id := s2_req.id
  miss_req.cancel := false.B
Z
zhanglinjuan 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

  io.store_replay_resp.valid := s2_valid && s2_can_go_to_mq && replay && s2_req.isStore
  io.store_replay_resp.bits.data := DontCare
  io.store_replay_resp.bits.miss := true.B
  io.store_replay_resp.bits.replay := true.B
  io.store_replay_resp.bits.id := s2_req.id

  io.store_hit_resp.valid := s3_valid && s3_store_can_go
  io.store_hit_resp.bits.data := DontCare
  io.store_hit_resp.bits.miss := false.B
  io.store_hit_resp.bits.replay := false.B
  io.store_hit_resp.bits.id := s3_req.id

  io.release_update.valid := s3_valid && (s3_store_can_go || s3_amo_can_go) && s3_hit && update_data
  io.release_update.bits.addr := s3_req.addr
  io.release_update.bits.mask := Mux(s3_store_hit, s3_banked_store_wmask, banked_amo_wmask)
W
William Wang 已提交
649 650 651 652 653 654 655 656 657
  io.release_update.bits.data := Mux(
    amo_wait_amoalu, 
    s3_amo_data_merged_reg, 
    Mux(
      s3_sc,
      s3_sc_data_merged,
      s3_store_data_merged
    )
  ).asUInt
Z
zhanglinjuan 已提交
658 659 660 661 662

  val atomic_hit_resp = Wire(new AtomicsResp)
  atomic_hit_resp.data := Mux(s3_sc, s3_sc_resp, s3_data_word)
  atomic_hit_resp.miss := false.B
  atomic_hit_resp.miss_id := s3_req.miss_id
663
  atomic_hit_resp.error := s3_error
Z
zhanglinjuan 已提交
664 665 666 667 668 669 670
  atomic_hit_resp.replay := false.B
  atomic_hit_resp.ack_miss_queue := s3_req.miss
  atomic_hit_resp.id := lrsc_valid
  val atomic_replay_resp = Wire(new AtomicsResp)
  atomic_replay_resp.data := DontCare
  atomic_replay_resp.miss := true.B
  atomic_replay_resp.miss_id := DontCare
671
  atomic_replay_resp.error := false.B
Z
zhanglinjuan 已提交
672 673 674 675 676 677 678
  atomic_replay_resp.replay := true.B
  atomic_replay_resp.ack_miss_queue := false.B
  atomic_replay_resp.id := DontCare
  val atomic_replay_resp_valid = s2_valid && s2_can_go_to_mq && replay && s2_req.isAMO
  val atomic_hit_resp_valid = s3_valid && (s3_amo_can_go || s3_miss_can_go && s3_req.isAMO)
  io.atomic_resp.valid := atomic_replay_resp_valid || atomic_hit_resp_valid
  io.atomic_resp.bits := Mux(atomic_replay_resp_valid, atomic_replay_resp, atomic_hit_resp)
679

680 681 682
  io.replace_resp.valid := s3_fire && s3_req.replace
  io.replace_resp.bits := s3_req.miss_id

Z
zhanglinjuan 已提交
683 684 685 686 687
  io.meta_write.valid := s3_fire && update_meta
  io.meta_write.bits.idx := s3_idx
  io.meta_write.bits.way_en := s3_way_en
  io.meta_write.bits.meta.coh := new_coh

688
  io.error_flag_write.valid := s3_fire && update_meta && s3_l2_error
689 690
  io.error_flag_write.bits.idx := s3_idx
  io.error_flag_write.bits.way_en := s3_way_en
691
  io.error_flag_write.bits.error := s3_l2_error
692

Z
zhanglinjuan 已提交
693 694 695 696 697
  io.tag_write.valid := s3_fire && s3_req.miss
  io.tag_write.bits.idx := s3_idx
  io.tag_write.bits.way_en := s3_way_en
  io.tag_write.bits.tag := get_tag(s3_req.addr)

698
  io.data_write.valid := s3_valid && s3_update_data_cango && update_data
Z
zhanglinjuan 已提交
699 700 701
  io.data_write.bits.way_en := s3_way_en
  io.data_write.bits.addr := s3_req.vaddr
  io.data_write.bits.wmask := banked_wmask
W
William Wang 已提交
702 703 704 705 706 707 708 709 710
  io.data_write.bits.data := Mux(
    amo_wait_amoalu, 
    s3_amo_data_merged_reg, 
    Mux(
      s3_sc,
      s3_sc_data_merged,
      s3_store_data_merged
    )
  )
711 712 713
  assert(RegNext(!io.meta_write.valid || !s3_req.replace))
  assert(RegNext(!io.tag_write.valid || !s3_req.replace))
  assert(RegNext(!io.data_write.valid || !s3_req.replace))
Z
zhanglinjuan 已提交
714 715

  io.wb.valid := s3_valid && (
716 717
    // replace
    s3_req.replace && !s3_replace_nothing ||
Z
zhanglinjuan 已提交
718 719 720 721 722 723
    // probe can go to wbq
    s3_req.probe && (io.meta_write.ready || !probe_update_meta) ||
      // amo miss can go to wbq
      s3_req.miss &&
        (io.meta_write.ready || !amo_update_meta) &&
        (io.data_write.ready || !update_data) &&
724
        (s3_s_amoalu || !amo_wait_amoalu) &&
Z
zhanglinjuan 已提交
725 726 727 728
        io.tag_write.ready
    ) && need_wb
  io.wb.bits.addr := get_block_addr(Cat(s3_tag, get_untag(s3_req.vaddr)))
  io.wb.bits.param := writeback_param
729
  io.wb.bits.voluntary := s3_req.miss || s3_req.replace
Z
zhanglinjuan 已提交
730 731 732
  io.wb.bits.hasData := writeback_data
  io.wb.bits.dirty := s3_coh === ClientStates.Dirty
  io.wb.bits.data := s3_data.asUInt()
733 734
  io.wb.bits.delay_release := s3_req.replace
  io.wb.bits.miss_id := s3_req.miss_id
Z
zhanglinjuan 已提交
735

736
  io.replace_access.valid := RegNext(s1_fire && (s1_req.isAMO || s1_req.isStore) && !s1_req.probe)
Z
zhanglinjuan 已提交
737 738 739 740 741 742 743 744 745 746 747 748
  io.replace_access.bits.set := s2_idx
  io.replace_access.bits.way := RegNext(OHToUInt(s1_way_en))

  io.replace_way.set.valid := RegNext(s0_fire)
  io.replace_way.set.bits := s1_idx

  // TODO: consider block policy of a finer granularity
  io.status.s0_set.valid := req.valid
  io.status.s0_set.bits := get_idx(s0_req.vaddr)
  io.status.s1.valid := s1_valid
  io.status.s1.bits.set := s1_idx
  io.status.s1.bits.way_en := s1_way_en
749
  io.status.s2.valid := s2_valid && !s2_req.replace
Z
zhanglinjuan 已提交
750 751
  io.status.s2.bits.set := s2_idx
  io.status.s2.bits.way_en := s2_way_en
752
  io.status.s3.valid := s3_valid && !s3_req.replace
Z
zhanglinjuan 已提交
753 754
  io.status.s3.bits.set := s3_idx
  io.status.s3.bits.way_en := s3_way_en
755

756
  // report error to beu and csr, 1 cycle after read data resp
757
  io.error := 0.U.asTypeOf(new L1CacheErrorInfo())
758 759 760 761 762 763 764 765 766 767 768 769 770
  // report error, update error csr
  io.error.valid := s3_error && RegNext(s2_fire)
  // only tag_error and data_error will be reported to beu
  // l2_error should not be reported (l2 will report that) 
  io.error.report_to_beu := (RegEnable(s2_tag_error, s2_fire) || s3_data_error) && RegNext(s2_fire)
  io.error.paddr := RegEnable(s2_req.addr, s2_fire)
  io.error.source.tag := RegEnable(s2_tag_error, s2_fire)
  io.error.source.data := s3_data_error
  io.error.source.l2 := RegEnable(s2_flag_error || s2_l2_error, s2_fire)
  io.error.opType.store := RegEnable(s2_req.isStore && !s2_req.probe, s2_fire)
  io.error.opType.probe := RegEnable(s2_req.probe, s2_fire)
  io.error.opType.release := RegEnable(s2_req.replace, s2_fire)
  io.error.opType.atom := RegEnable(s2_req.isAMO && !s2_req.probe, s2_fire)
771

772
  val perfEvents = Seq(
773 774
    ("dcache_mp_req          ", s0_fire                                                      ),
    ("dcache_mp_total_penalty", PopCount(VecInit(Seq(s0_fire, s1_valid, s2_valid, s3_valid))))
775
  )
776
  generatePerfEvent()
777
}