MissQueue.scala 20.7 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
import utils._
23
import freechips.rocketchip.tilelink._
24
import bus.tilelink.TLMessages._
25
import difftest._
26
import huancun.{AliasKey, DirtyKey, PreferCacheKey, PrefetchKey}
A
Allen 已提交
27

28
class MissReq(implicit p: Parameters) extends DCacheBundle
A
Allen 已提交
29 30 31 32 33
{
  val source = UInt(sourceTypeWidth.W)
  val cmd    = UInt(M_SZ.W)
  // must be aligned to block
  val addr   = UInt(PAddrBits.W)
34
  val vaddr  = UInt(VAddrBits.W)
A
Allen 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

  // store
  val store_data   = UInt((cfg.blockBytes * 8).W)
  val store_mask   = UInt(cfg.blockBytes.W)

  // which word does amo work on?
  val word_idx = UInt(log2Up(blockWords).W)
  val amo_data = UInt(DataBits.W)
  val amo_mask = UInt((DataBits/8).W)

  // coherence state
  val coh = new ClientMetadata
  val id  = UInt(reqIdWidth.W)

  def dump() = {
    XSDebug("MissReq source: %d cmd: %d addr: %x store_data: %x store_mask: %x word_idx: %d amo_data: %x amo_mask: %x coh: %d id: %d\n",
      source, cmd, addr, store_data, store_mask, word_idx, amo_data, amo_mask, coh.state, id)
  }
53

54
  def isLoad = (source === LOAD_SOURCE.U) || (source === SOFT_PREFETCH.U) //tjz
55
  def isStore = source === STORE_SOURCE.U
A
Allen 已提交
56 57 58
}

// One miss entry deals with one missed block
59
class MissEntry(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule {
A
Allen 已提交
60 61 62 63 64 65 66 67 68 69 70
  val io = IO(new Bundle {
    // MSHR ID
    val id = Input(UInt())

    // client requests
    // this entry is free and can be allocated to new reqs
    val primary_ready = Output(Bool())
    // this entry is busy, but it can merge the new req
    val secondary_ready = Output(Bool())
    // this entry is busy and it can not merge the new req
    val secondary_reject = Output(Bool())
71
    val req    = Flipped(ValidIO(new MissReq))
A
Allen 已提交
72 73 74 75 76 77 78 79 80
    val refill = ValidIO(new Refill)

    // bus
    val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle))
    val mem_grant   = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
    val mem_finish  = DecoupledIO(new TLBundleE(edge.bundle))

    val pipe_req  = DecoupledIO(new MainPipeReq)
    val pipe_resp = Flipped(ValidIO(new MainPipeResp))
81 82 83

    // block probe
    val block_addr = ValidIO(UInt(PAddrBits.W))
A
Allen 已提交
84
  })
85 86 87 88
  val tma_io = IO(new Bundle {
    val req    = Output(new MissReq)
    val state  = Output(UInt(5.W))
  })
A
Allen 已提交
89 90

  val req = Reg(new MissReq)
91
  val req_valid = RegInit(false.B)
A
Allen 已提交
92

93 94 95 96 97 98
  val s_acquire = RegInit(true.B)
  val s_grantack = RegInit(true.B)
  val s_pipe_req = RegInit(true.B)
  val w_grantfirst = RegInit(true.B)
  val w_grantlast = RegInit(true.B)
  val w_pipe_resp = RegInit(true.B)
A
Allen 已提交
99

100 101 102 103 104 105
  val no_schedule = s_grantack && s_pipe_req
  val no_wait = w_pipe_resp
  val release_entry = no_schedule && no_wait

  val acquire_not_sent = !s_acquire && !io.mem_acquire.ready
  val data_not_refilled = !w_grantlast
A
Allen 已提交
106 107

  // should we refill the data to load queue to wake up any missed load?
108 109
  val should_refill_data_reg = Reg(Bool())
  val should_refill_data = WireInit(should_refill_data_reg)
A
Allen 已提交
110

111
  val full_overwrite = req.isStore && req.store_mask.andR
A
Allen 已提交
112

113 114
  val (_, _, refill_done, refill_count) = edge.count(io.mem_grant)
  val grant_param = Reg(UInt(TLPermissions.bdWidth.W))
A
Allen 已提交
115

116 117
  val grant_beats = RegInit(0.U((beatBits).W))

118 119 120
  when (io.req.valid && io.primary_ready) {
    req_valid := true.B
    req := io.req.bits
121
    req.addr := get_block_addr(io.req.bits.addr)
A
Allen 已提交
122

123 124 125 126 127 128
    s_acquire := false.B
    s_grantack := false.B
    s_pipe_req := false.B
    w_grantfirst := false.B
    w_grantlast := false.B
    w_pipe_resp := false.B
A
Allen 已提交
129

130
    should_refill_data_reg := io.req.bits.isLoad
131 132

    grant_beats := 0.U
133 134
  }.elsewhen (release_entry) {
    req_valid := false.B
A
Allen 已提交
135 136
  }

137
  when (io.req.valid && io.secondary_ready) {
A
Allen 已提交
138 139 140 141 142 143 144 145
    // The merged reqs should never have higher permissions
    // which means the cache silently upgrade the permission of our block
    // without merge with this miss queue request!
    // Either our req come in with stale meta, or the req that upgrade the permission does not merge with this req.
    // Both cases are bugs of DCache.
    //
    // DCache can silently drop permission(eg, probed or evicted)
    // it should never silently upgrade permissions.
146
    assert (io.req.bits.coh.state <= req.coh.state)
A
Allen 已提交
147
    // use the most uptodate meta
148
    req.coh := io.req.bits.coh
A
Allen 已提交
149 150 151 152

    // when merging with store
    // we should remember its info into our req
    // or we will not be able to replay store
153 154
    when (io.req.bits.isStore) {
      req := io.req.bits
A
Allen 已提交
155 156
    }

157
    should_refill_data := should_refill_data_reg || io.req.bits.isLoad
158
    should_refill_data_reg := should_refill_data
A
Allen 已提交
159 160
  }

161 162 163 164
  // set state regs
  when (io.mem_acquire.fire()) {
    s_acquire := true.B
  }
A
Allen 已提交
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179
  val refill_data = Reg(Vec(blockRows, UInt(rowBits.W)))
  val refill_data_raw = Reg(Vec(blockBytes/beatBytes, UInt(beatBits.W)))
  val new_data = Wire(Vec(blockRows, UInt(rowBits.W)))
  val new_mask = Wire(Vec(blockRows, UInt(rowBytes.W)))
  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)
  }
  for (i <- 0 until blockRows) {
    new_data(i) := req.store_data(rowBits * (i + 1) - 1, rowBits * i)
    // we only need to merge data for Store
    new_mask(i) := Mux(req.isStore, req.store_mask(rowBytes * (i + 1) - 1, rowBytes * i), 0.U)
  }
  val hasData = RegInit(true.B)
180
  val isDirty = RegInit(false.B)
181 182 183 184 185 186 187 188 189 190
  when (io.mem_grant.fire()) {
    w_grantfirst := true.B
    grant_param := io.mem_grant.bits.param
    when (edge.hasData(io.mem_grant.bits)) {
      // GrantData
      for (i <- 0 until beatRows) {
        val idx = (refill_count << log2Floor(beatRows)) + i.U
        val grant_row = io.mem_grant.bits.data(rowBits * (i + 1) - 1, rowBits * i)
        refill_data(idx) := mergePutData(grant_row, new_data(idx), new_mask(idx))
      }
A
Allen 已提交
191

192 193 194
      w_grantlast := w_grantlast || refill_done

      hasData := true.B
195 196

      grant_beats := grant_beats + 1.U
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    }.otherwise {
      // Grant

      // since we do not sync between MissQueue and WritebackQueue
      // for a AcquireBlock BtoT, we can not protect our block from being replaced by another miss and written back by WritebackQueue
      // so AcquireBlock BtoT, we need L2 to give us GrantData, not Grant.
      // So that whether our block is replaced or not, we can always refill the block with valid data
      // So, if we enters here
      // we must be a AcquirePerm, not a AcquireBlock!!!
      assert (full_overwrite)
      // when we only acquire perm, not data
      // use Store's data
      for (i <- 0 until blockRows) {
        refill_data(i) := new_data(i)
      }
A
Allen 已提交
212

213
      w_grantlast := true.B
A
Allen 已提交
214

215
      hasData := false.B
A
Allen 已提交
216 217
    }

218
    refill_data_raw(refill_count) := io.mem_grant.bits.data
219
    isDirty := io.mem_grant.bits.echo.lift(DirtyKey).getOrElse(false.B)
220
  }
A
Allen 已提交
221

222 223 224
  when (io.mem_finish.fire()) {
    s_grantack := true.B
  }
A
Allen 已提交
225

226 227
  when (io.pipe_req.fire()) {
    s_pipe_req := true.B
228
    assert(!io.pipe_req.bits.vaddr === 0.U)
A
Allen 已提交
229 230
  }

231 232
  when (io.pipe_resp.valid) {
    w_pipe_resp := true.B
A
Allen 已提交
233 234
  }

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
//  def can_merge(new_req: MissReq): Bool = {
//    // caution: do not merge with AMO
//    // we can not do amoalu calculation in MissQueue
//    // so, we do not know the result after AMO calculation
//    // so do not merge with AMO
//
//    // before read acquire is fired, we can merge read or write
//    val before_read_sent = acquire_not_sent && req.source === LOAD_SOURCE.U && (new_req.source === LOAD_SOURCE.U || new_req.source === STORE_SOURCE.U)
//    // before read/write refills data to LoadQueue, we can merge any read
//    val before_data_refill = data_not_refilled && (req.source === LOAD_SOURCE.U || req.source === STORE_SOURCE.U) && new_req.source === LOAD_SOURCE.U
//
//    before_read_sent || before_data_refill
//  }

  def before_read_sent_can_merge(new_req: MissReq): Bool = {
250
    acquire_not_sent && (req.source === LOAD_SOURCE.U || req.source === SOFT_PREFETCH.U) && (new_req.source === LOAD_SOURCE.U || new_req.source === STORE_SOURCE.U || req.source === SOFT_PREFETCH.U) //tjz
251
  }
A
Allen 已提交
252

253
  def before_data_refill_can_merge(new_req: MissReq): Bool = {
254
    data_not_refilled && (req.source === LOAD_SOURCE.U || req.source === STORE_SOURCE.U || req.source === SOFT_PREFETCH.U) && (new_req.source === LOAD_SOURCE.U || req.source === SOFT_PREFETCH.U)
255 256 257
  }

  def should_merge(new_req: MissReq): Bool = {
258
    val block_match = req.addr === get_block_addr(new_req.addr)
259 260
    val beat_match = new_req.addr(blockOffBits - 1, beatOffBits) >= grant_beats
    block_match && (before_read_sent_can_merge(new_req) || beat_match && before_data_refill_can_merge(new_req))
A
Allen 已提交
261 262
  }

263
  def should_reject(new_req: MissReq): Bool = {
264
    val block_match = req.addr === get_block_addr(new_req.addr)
265
    // do not reject any req when we are in s_invalid
266
    block_match && !should_merge(new_req) && req_valid // TODO: optimize this
267 268 269 270 271 272 273 274 275 276
  }

  io.primary_ready := !req_valid
  io.secondary_ready := should_merge(io.req.bits)
  io.secondary_reject := should_reject(io.req.bits)

  // should not allocate, merge or reject at the same time
  // one at a time
  OneHot.checkOneHot(Seq(io.primary_ready, io.secondary_ready, io.secondary_reject))

A
Allen 已提交
277 278 279
  // put should_refill_data out of RegNext
  // so that when load miss are merged at refill_done
  // we can still refill data back
280 281 282 283 284 285 286
  //
  // Now refill to load queue width l1BusDataWidth, to load queue refill req
  // will be issued as soon as data is ready (stored in regs in miss queue)
  val refill_data_splited = WireInit(VecInit(Seq.tabulate(cfg.blockBytes * 8 / l1BusDataWidth)(i => {
    val data = refill_data.asUInt
    data((i + 1) * l1BusDataWidth - 1, i * l1BusDataWidth)
  })))
287
  io.refill.valid := RegNext(!w_grantlast && s_acquire && io.mem_grant.fire()) && should_refill_data
288 289
  io.refill.bits.addr := RegNext(req.addr + (refill_count << refillOffBits))
  io.refill.bits.data := refill_data_splited(RegNext(refill_count))
290
  io.refill.bits.refill_done := RegNext(refill_done && io.mem_grant.fire())
291
  io.refill.bits.hasdata := hasData
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  io.refill.bits.data_raw := refill_data_raw.asUInt

  io.mem_acquire.valid := !s_acquire
  val grow_param = req.coh.onAccess(req.cmd)._2
  val acquireBlock = edge.AcquireBlock(
    fromSource = io.id,
    toAddress = req.addr,
    lgSize = (log2Up(cfg.blockBytes)).U,
    growPermissions = grow_param
  )._2
  val acquirePerm = edge.AcquirePerm(
    fromSource = io.id,
    toAddress = req.addr,
    lgSize = (log2Up(cfg.blockBytes)).U,
    growPermissions = grow_param
  )._2
  io.mem_acquire.bits := Mux(full_overwrite, acquirePerm, acquireBlock)
309 310 311 312 313 314 315
  // resolve cache alias by L2
  io.mem_acquire.bits.user.lift(AliasKey).foreach( _ := req.vaddr(13, 12))
  // trigger prefetch
  io.mem_acquire.bits.user.lift(PrefetchKey).foreach(_ := true.B)
  // prefer not to cache data in L2 by default
  io.mem_acquire.bits.user.lift(PreferCacheKey).foreach(_ := false.B)
  require(nSets <= 256) // dcache size should not be more than 128KB
316 317 318 319 320 321 322 323 324 325 326
  io.mem_grant.ready := !w_grantlast && s_acquire
  val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire())
  val is_grant = RegEnable(edge.isRequest(io.mem_grant.bits), io.mem_grant.fire())
  io.mem_finish.valid := !s_grantack && w_grantfirst && is_grant
  io.mem_finish.bits := grantack

  io.pipe_req.valid := !s_pipe_req && w_grantlast
  val pipe_req = io.pipe_req.bits
  pipe_req.miss := true.B
  pipe_req.miss_id := io.id
  pipe_req.miss_param := grant_param
327
  pipe_req.miss_dirty := isDirty
328 329 330

  pipe_req.probe := false.B
  pipe_req.probe_param := DontCare
331
  pipe_req.probe_need_data := false.B
332 333 334 335

  pipe_req.source := req.source
  pipe_req.cmd    := req.cmd
  pipe_req.addr   := req.addr
336
  pipe_req.vaddr   := req.vaddr
337 338 339 340 341 342 343 344 345 346
  pipe_req.store_data := refill_data.asUInt
  // full overwrite
  pipe_req.store_mask := Fill(cfg.blockBytes, "b1".U)
  pipe_req.word_idx := req.word_idx
  pipe_req.amo_data   := req.amo_data
  pipe_req.amo_mask   := req.amo_mask
  pipe_req.id     := req.id

  io.block_addr.valid := req_valid && w_grantlast && !release_entry
  io.block_addr.bits := req.addr
347

348 349
  tma_io.req := req
  tma_io.state := DontCare // TODO
350

351 352 353
  XSPerfAccumulate("miss_req", io.req.valid && io.primary_ready)
  XSPerfAccumulate("miss_penalty", BoolStopWatch(io.req.valid && io.primary_ready, release_entry))
  XSPerfAccumulate("load_miss_penalty_to_use", should_refill_data && BoolStopWatch(io.req.valid && io.primary_ready, io.refill.valid, true))
354 355
  XSPerfAccumulate("pipeline_penalty", BoolStopWatch(io.pipe_req.fire(), io.pipe_resp.fire()))
  XSPerfAccumulate("penalty_blocked_by_channel_A", io.mem_acquire.valid && !io.mem_acquire.ready)
356
  XSPerfAccumulate("penalty_waiting_for_channel_D", s_acquire && !w_grantlast && !io.mem_grant.valid)
357 358
  XSPerfAccumulate("penalty_blocked_by_channel_E", io.mem_finish.valid && !io.mem_finish.ready)
  XSPerfAccumulate("penalty_blocked_by_pipeline", io.pipe_req.valid && !io.pipe_req.ready)
359
  XSPerfAccumulate("soft_prefetch_number", io.req.valid && io.primary_ready && io.req.bits.source === SOFT_PREFETCH.U)
360

361
  val (mshr_penalty_sample, mshr_penalty) = TransactionLatencyCounter(RegNext(io.req.valid && io.primary_ready), release_entry)
362
  XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 0, 100, 10)
363

364 365
  val load_miss_begin = io.req.valid && io.primary_ready && io.req.bits.isLoad
  val refill_finished = RegNext(!w_grantlast && refill_done) && should_refill_data
366
  val (load_miss_penalty_sample, load_miss_penalty) = TransactionLatencyCounter(load_miss_begin, refill_finished) // not real refill finish time
367
  XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 0, 100, 10)
368 369

  val (a_to_d_penalty_sample, a_to_d_penalty) = TransactionLatencyCounter(io.mem_acquire.fire(), io.mem_grant.fire() && refill_done)
370
  XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 0, 100, 10)
A
Allen 已提交
371 372
}

373
class MissQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasTLDump
A
Allen 已提交
374 375 376 377 378 379 380 381 382 383 384
{
  val io = IO(new Bundle {
    val req    = Flipped(DecoupledIO(new MissReq))
    val refill = ValidIO(new Refill)

    val mem_acquire = Decoupled(new TLBundleA(edge.bundle))
    val mem_grant   = Flipped(DecoupledIO(new TLBundleD(edge.bundle)))
    val mem_finish  = Decoupled(new TLBundleE(edge.bundle))

    val pipe_req  = DecoupledIO(new MainPipeReq)
    val pipe_resp = Flipped(ValidIO(new MainPipeResp))
385 386 387 388

    // block probe
    val probe_req = Input(UInt(PAddrBits.W))
    val probe_block = Output(Bool())
389 390

    val full = Output(Bool())
A
Allen 已提交
391 392
  })

393 394
  // 128KBL1: FIXME: provide vaddr for l2

395
  val pipe_req_arb = Module(new RRArbiter(new MainPipeReq, cfg.nMissEntries))
A
allen 已提交
396
  val refill_arb   = Module(new Arbiter(new Refill, cfg.nMissEntries))
A
Allen 已提交
397 398 399 400 401

  // dispatch req to MSHR
  val primary_ready  = Wire(Vec(cfg.nMissEntries, Bool()))
  val secondary_ready  = Wire(Vec(cfg.nMissEntries, Bool()))
  val secondary_reject  = Wire(Vec(cfg.nMissEntries, Bool()))
402
  val probe_block_vec = Wire(Vec(cfg.nMissEntries, Bool()))
A
Allen 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

  // try merging with existing reqs
  val merge = secondary_ready.asUInt.orR
  val merge_idx = PriorityEncoder(secondary_ready)
  // some req says the request can not be merged
  val reject = secondary_reject.asUInt.orR
  // allocate a new entry for this req
  val allocate = !reject && !merge && primary_ready.asUInt.orR
  val alloc_idx = PriorityEncoder(primary_ready)

  // will this req be accepted
  val accept = (merge || allocate) && !reject
  // if it's accepted, which entry will it enter
  val entry_idx = Mux(allocate, alloc_idx, merge_idx)

  // for one block, their should be only one MSHR
  // one block should not be stay in multiple MSHRs
  // if we a req can not merge with existing reqs
  // block it!
  OneHot.checkOneHot(secondary_ready)
  OneHot.checkOneHot(secondary_reject)
  // should not merge and reject at the same time
  OneHot.checkOneHot(Seq(merge, reject))

  io.req.ready := accept
  io.mem_grant.ready := false.B

  val entries = (0 until cfg.nMissEntries) map { i =>
    val entry = Module(new MissEntry(edge))

    entry.io.id := i.U(log2Up(cfg.nMissEntries).W)

    // entry req
436
    entry.io.req.valid  := (i.U === entry_idx) && accept && io.req.valid
A
Allen 已提交
437 438 439
    primary_ready(i)    := entry.io.primary_ready
    secondary_ready(i)  := entry.io.secondary_ready
    secondary_reject(i) := entry.io.secondary_reject
440
    probe_block_vec(i)  := entry.io.block_addr.valid && entry.io.block_addr.bits === io.probe_req
441
    entry.io.req.bits   := io.req.bits
A
Allen 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

    // entry refill
    refill_arb.io.in(i).valid := entry.io.refill.valid
    refill_arb.io.in(i).bits  := entry.io.refill.bits

    // pipe_req
    pipe_req_arb.io.in(i)     <> entry.io.pipe_req

    // pipe_req
    entry.io.pipe_resp.valid  := false.B
    entry.io.pipe_resp.bits   := DontCare
    when (io.pipe_resp.bits.id === i.U) {
      entry.io.pipe_resp <> io.pipe_resp
    }

    entry.io.mem_grant.valid := false.B
    entry.io.mem_grant.bits  := DontCare
    when (io.mem_grant.bits.source === i.U) {
      entry.io.mem_grant <> io.mem_grant
    }

    /*
464 465 466
    XSPerf(
      "perfCntDCacheMissQueuePenaltyEntry" + Integer.toString(i, 10),
      BoolStopWatch(
Y
Yinan Xu 已提交
467
        start = entry.io.req.fire(),
468 469 470
        stop = entry.io.resp.fire(),
        startHighPriority = true)
    )
A
Allen 已提交
471 472 473 474 475
    */

    entry
  }

476 477
  val pendingVec = entries.map(entry => (entry.tma_io.req.source =/= STORE_SOURCE.U) && (entry.tma_io.state =/= 0.U))

A
Allen 已提交
478 479 480 481
  io.refill.valid := refill_arb.io.out.valid
  io.refill.bits  := refill_arb.io.out.bits
  refill_arb.io.out.ready := true.B

482 483 484 485 486 487 488 489 490
  if (!env.FPGAPlatform) {
    val difftest = Module(new DifftestRefillEvent)
    difftest.io.clock := clock
    difftest.io.coreid := hardId.U
    difftest.io.valid := io.refill.valid && io.refill.bits.hasdata && io.refill.bits.refill_done
    difftest.io.addr := io.refill.bits.addr
    difftest.io.data := io.refill.bits.data_raw.asTypeOf(difftest.io.data)
  }

A
Allen 已提交
491 492 493
  // one refill at a time
  OneHot.checkOneHot(refill_arb.io.in.map(r => r.valid))

494 495
  TLArbiter.lowest(edge, io.mem_acquire, entries.map(_.io.mem_acquire):_*)
  TLArbiter.lowest(edge, io.mem_finish,  entries.map(_.io.mem_finish):_*)
A
Allen 已提交
496 497 498

  io.pipe_req <> pipe_req_arb.io.out

499
  io.probe_block := probe_block_vec.asUInt.orR
A
Allen 已提交
500 501 502 503 504 505 506 507

  // print all input/output requests for debug purpose

  when (io.req.fire()) {
    io.req.bits.dump()
    // sanity check
    val source = io.req.bits.source
    val cmd = io.req.bits.cmd
508 509
    when (source === LOAD_SOURCE.U || source === SOFT_PREFETCH.U) {
      assert (cmd === M_XRD || cmd === M_PFR || cmd === M_PFW)
A
Allen 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    }
    when (source === STORE_SOURCE.U) {
      assert (cmd === M_XWR)
    }

    when (source === AMO_SOURCE.U) {
      assert (
        cmd === M_XA_SWAP ||
        cmd === M_XLR     ||
        cmd === M_XSC     ||
        cmd === M_XA_ADD  ||
        cmd === M_XA_XOR  ||
        cmd === M_XA_OR   ||
        cmd === M_XA_AND  ||
        cmd === M_XA_MIN  ||
        cmd === M_XA_MAX  ||
        cmd === M_XA_MINU ||
        cmd === M_XA_MAXU)
    }
    // req addr must be aligned to block boundary
530
//    assert (io.req.bits.addr(blockOffBits - 1, 0) === 0.U)
A
Allen 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  }

  when (io.refill.fire()) {
    io.refill.bits.dump()
  }

  when (io.mem_acquire.fire()) {
    XSDebug("mem_acquire ")
    io.mem_acquire.bits.dump
  }

  when (io.mem_grant.fire()) {
    XSDebug("mem_grant ")
    io.mem_grant.bits.dump
  }

  when (io.mem_finish.fire()) {
    XSDebug("mem_finish ")
    io.mem_finish.bits.dump
  }

552 553 554 555
  when (io.probe_block) {
    XSDebug(p"block probe req ${Hexadecimal(io.probe_req)}\n")
  }

556
  XSPerfAccumulate("miss_req", io.req.fire())
557 558 559
  XSPerfAccumulate("miss_req_allocate", io.req.fire() && allocate)
  XSPerfAccumulate("miss_req_merge_load", io.req.fire() && merge && !reject && io.req.bits.isLoad)
  XSPerfAccumulate("miss_req_reject_load", io.req.valid && reject && io.req.bits.isLoad)
560
  XSPerfAccumulate("probe_blocked_by_miss", io.probe_block)
561 562 563 564 565 566
  val max_inflight = RegInit(0.U((log2Up(cfg.nMissEntries) + 1).W))
  val num_valids = PopCount(~primary_ready.asUInt)
  when (num_valids > max_inflight) {
    max_inflight := num_valids
  }
  // max inflight (average) = max_inflight_total / cycle cnt
567
  XSPerfAccumulate("max_inflight", max_inflight)
568
  QueuePerf(cfg.nMissEntries, num_valids, num_valids === cfg.nMissEntries.U)
569
  io.full := num_valids === cfg.nMissEntries.U
570
  XSPerfHistogram("num_valids", num_valids, true.B, 0, cfg.nMissEntries, 1)
A
Allen 已提交
571
}