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 xiangshan._ 23import utils._ 24import utility._ 25import freechips.rocketchip.tilelink._ 26import freechips.rocketchip.tilelink.ClientStates._ 27import freechips.rocketchip.tilelink.MemoryOpCategories._ 28import freechips.rocketchip.tilelink.TLPermissions._ 29import difftest._ 30import coupledL2.{AliasKey, DirtyKey, PrefetchKey} 31import utility.FastArbiter 32import mem.{AddPipelineReg} 33import mem.trace._ 34 35class MissReqWoStoreData(implicit p: Parameters) extends DCacheBundle { 36 val source = UInt(sourceTypeWidth.W) 37 val cmd = UInt(M_SZ.W) 38 val addr = UInt(PAddrBits.W) 39 val vaddr = UInt(VAddrBits.W) 40 val way_en = UInt(DCacheWays.W) 41 val pc = UInt(VAddrBits.W) 42 43 // store 44 val full_overwrite = Bool() 45 46 // which word does amo work on? 47 val word_idx = UInt(log2Up(blockWords).W) 48 val amo_data = UInt(DataBits.W) 49 val amo_mask = UInt((DataBits / 8).W) 50 51 val req_coh = new ClientMetadata 52 val replace_coh = new ClientMetadata 53 val replace_tag = UInt(tagBits.W) 54 val id = UInt(reqIdWidth.W) 55 56 // For now, miss queue entry req is actually valid when req.valid && !cancel 57 // * req.valid is fast to generate 58 // * cancel is slow to generate, it will not be used until the last moment 59 // 60 // cancel may come from the following sources: 61 // 1. miss req blocked by writeback queue: 62 // a writeback req of the same address is in progress 63 // 2. pmp check failed 64 val cancel = Bool() // cancel is slow to generate, it will cancel missreq.valid 65 66 // Req source decode 67 // Note that req source is NOT cmd type 68 // For instance, a req which isFromPrefetch may have R or W cmd 69 def isFromLoad = source === LOAD_SOURCE.U 70 def isFromStore = source === STORE_SOURCE.U 71 def isFromAMO = source === AMO_SOURCE.U 72 def isFromPrefetch = source >= DCACHE_PREFETCH_SOURCE.U 73 def hit = req_coh.isValid() 74} 75 76class MissReqStoreData(implicit p: Parameters) extends DCacheBundle { 77 // store data and store mask will be written to miss queue entry 78 // 1 cycle after req.fire() and meta write 79 val store_data = UInt((cfg.blockBytes * 8).W) 80 val store_mask = UInt(cfg.blockBytes.W) 81} 82 83class MissReq(implicit p: Parameters) extends MissReqWoStoreData { 84 // store data and store mask will be written to miss queue entry 85 // 1 cycle after req.fire() and meta write 86 val store_data = UInt((cfg.blockBytes * 8).W) 87 val store_mask = UInt(cfg.blockBytes.W) 88 89 def toMissReqStoreData(): MissReqStoreData = { 90 val out = Wire(new MissReqStoreData) 91 out.store_data := store_data 92 out.store_mask := store_mask 93 out 94 } 95 96 def toMissReqWoStoreData(): MissReqWoStoreData = { 97 val out = Wire(new MissReqWoStoreData) 98 out.source := source 99 out.cmd := cmd 100 out.addr := addr 101 out.vaddr := vaddr 102 out.way_en := way_en 103 out.full_overwrite := full_overwrite 104 out.word_idx := word_idx 105 out.amo_data := amo_data 106 out.amo_mask := amo_mask 107 out.req_coh := req_coh 108 out.replace_coh := replace_coh 109 out.replace_tag := replace_tag 110 out.id := id 111 out.cancel := cancel 112 out.pc := pc 113 out 114 } 115} 116 117class MissResp(implicit p: Parameters) extends DCacheBundle { 118 val id = UInt(log2Up(cfg.nMissEntries).W) 119 // cache req missed, merged into one of miss queue entries 120 // i.e. !miss_merged means this access is the first miss for this cacheline 121 val merged = Bool() 122 val repl_way_en = UInt(DCacheWays.W) 123} 124 125class MissEntry(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule { 126 val io = IO(new Bundle() { 127 val hartId = Input(UInt(8.W)) 128 // MSHR ID 129 val id = Input(UInt(log2Up(cfg.nMissEntries).W)) 130 // client requests 131 // MSHR update request, MSHR state and addr will be updated when req.fire() 132 val req = Flipped(ValidIO(new MissReqWoStoreData)) 133 // store data and mask will be write to miss queue entry 1 cycle after req.fire() 134 val req_data = Input(new MissReqStoreData) 135 // allocate this entry for new req 136 val primary_valid = Input(Bool()) 137 // this entry is free and can be allocated to new reqs 138 val primary_ready = Output(Bool()) 139 // this entry is busy, but it can merge the new req 140 val secondary_ready = Output(Bool()) 141 // this entry is busy and it can not merge the new req 142 val secondary_reject = Output(Bool()) 143 // way selected for replacing, used to support plru update 144 val repl_way_en = Output(UInt(DCacheWays.W)) 145 146 // bus 147 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 148 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 149 val mem_finish = DecoupledIO(new TLBundleE(edge.bundle)) 150 151 // send refill info to load queue 152 val refill_to_ldq = ValidIO(new Refill) 153 154 // refill pipe 155 val refill_pipe_req = DecoupledIO(new RefillPipeReq) 156 val refill_pipe_resp = Input(Bool()) 157 158 // replace pipe 159 val replace_pipe_req = DecoupledIO(new MainPipeReq) 160 val replace_pipe_resp = Input(Bool()) 161 162 // main pipe: amo miss 163 val main_pipe_req = DecoupledIO(new MainPipeReq) 164 val main_pipe_resp = Input(Bool()) 165 166 val block_addr = ValidIO(UInt(PAddrBits.W)) 167 168 val debug_early_replace = ValidIO(new Bundle() { 169 // info about the block that has been replaced 170 val idx = UInt(idxBits.W) // vaddr 171 val tag = UInt(tagBits.W) // paddr 172 }) 173 174 val req_handled_by_this_entry = Output(Bool()) 175 176 val forwardInfo = Output(new MissEntryForwardIO) 177 val l2_pf_store_only = Input(Bool()) 178 }) 179 180 assert(!RegNext(io.primary_valid && !io.primary_ready)) 181 182 val req = Reg(new MissReqWoStoreData) 183 val req_store_mask = Reg(UInt(cfg.blockBytes.W)) 184 val req_valid = RegInit(false.B) 185 val set = addr_to_dcache_set(req.vaddr) 186 187 val input_req_is_prefetch = isPrefetch(io.req.bits.cmd) 188 189 val s_acquire = RegInit(true.B) 190 val s_grantack = RegInit(true.B) 191 val s_replace_req = RegInit(true.B) 192 val s_refill = RegInit(true.B) 193 val s_mainpipe_req = RegInit(true.B) 194 val s_write_storedata = RegInit(true.B) 195 196 val w_grantfirst = RegInit(true.B) 197 val w_grantlast = RegInit(true.B) 198 val w_replace_resp = RegInit(true.B) 199 val w_refill_resp = RegInit(true.B) 200 val w_mainpipe_resp = RegInit(true.B) 201 202 val release_entry = s_grantack && w_refill_resp && w_mainpipe_resp 203 204 val acquire_not_sent = !s_acquire && !io.mem_acquire.ready 205 val data_not_refilled = !w_grantfirst 206 207 val error = RegInit(false.B) 208 val prefetch = RegInit(false.B) 209 val access = RegInit(false.B) 210 211 val should_refill_data_reg = Reg(Bool()) 212 val should_refill_data = WireInit(should_refill_data_reg) 213 214 // val full_overwrite = req.isFromStore && req_store_mask.andR 215 val full_overwrite = Reg(Bool()) 216 217 val (_, _, refill_done, refill_count) = edge.count(io.mem_grant) 218 val grant_param = Reg(UInt(TLPermissions.bdWidth.W)) 219 220 // refill data with store data, this reg will be used to store: 221 // 1. store data (if needed), before l2 refill data 222 // 2. store data and l2 refill data merged result (i.e. new cacheline taht will be write to data array) 223 val refill_and_store_data = Reg(Vec(blockRows, UInt(rowBits.W))) 224 // raw data refilled to l1 by l2 225 val refill_data_raw = Reg(Vec(blockBytes/beatBytes, UInt(beatBits.W))) 226 227 // allocate current miss queue entry for a miss req 228 val primary_fire = WireInit(io.req.valid && io.primary_ready && io.primary_valid && !io.req.bits.cancel) 229 // merge miss req to current miss queue entry 230 val secondary_fire = WireInit(io.req.valid && io.secondary_ready && !io.req.bits.cancel) 231 232 val req_handled_by_this_entry = primary_fire || secondary_fire 233 234 io.req_handled_by_this_entry := req_handled_by_this_entry 235 236 when (release_entry && req_valid) { 237 req_valid := false.B 238 } 239 240 when (!s_write_storedata && req_valid) { 241 // store data will be write to miss queue entry 1 cycle after req.fire() 242 s_write_storedata := true.B 243 assert(RegNext(primary_fire || secondary_fire)) 244 } 245 246 when (primary_fire) { 247 req_valid := true.B 248 req := io.req.bits 249 req.addr := get_block_addr(io.req.bits.addr) 250 251 s_acquire := false.B 252 s_grantack := false.B 253 254 w_grantfirst := false.B 255 w_grantlast := false.B 256 257 s_write_storedata := !io.req.bits.isFromStore // only store need to wait for data 258 full_overwrite := io.req.bits.isFromStore && io.req.bits.full_overwrite 259 260 when (!io.req.bits.isFromAMO) { 261 s_refill := false.B 262 w_refill_resp := false.B 263 } 264 265 when (!io.req.bits.hit && io.req.bits.replace_coh.isValid() && !io.req.bits.isFromAMO) { 266 s_replace_req := false.B 267 w_replace_resp := false.B 268 } 269 270 when (io.req.bits.isFromAMO) { 271 s_mainpipe_req := false.B 272 w_mainpipe_resp := false.B 273 } 274 275 should_refill_data_reg := io.req.bits.isFromLoad 276 error := false.B 277 prefetch := input_req_is_prefetch 278 access := false.B 279 } 280 281 when (secondary_fire) { 282 assert(io.req.bits.req_coh.state <= req.req_coh.state || (prefetch && !access)) 283 assert(!(io.req.bits.isFromAMO || req.isFromAMO)) 284 // use the most uptodate meta 285 req.req_coh := io.req.bits.req_coh 286 287 when (io.req.bits.isFromStore) { 288 req := io.req.bits 289 req.addr := get_block_addr(io.req.bits.addr) 290 req.way_en := req.way_en 291 req.replace_coh := req.replace_coh 292 req.replace_tag := req.replace_tag 293 s_write_storedata := false.B // only store need to wait for data 294 full_overwrite := io.req.bits.isFromStore && io.req.bits.full_overwrite 295 } 296 297 should_refill_data := should_refill_data_reg || io.req.bits.isFromLoad 298 should_refill_data_reg := should_refill_data 299 when (!input_req_is_prefetch) { 300 access := true.B // when merge non-prefetch req, set access bit 301 } 302 } 303 304 when (io.mem_acquire.fire()) { 305 s_acquire := true.B 306 } 307 308 // store data and mask write 309 when (!s_write_storedata && req_valid) { 310 req_store_mask := io.req_data.store_mask 311 for (i <- 0 until blockRows) { 312 refill_and_store_data(i) := io.req_data.store_data(rowBits * (i + 1) - 1, rowBits * i) 313 } 314 } 315 316 // merge data refilled by l2 and store data, update miss queue entry, gen refill_req 317 val new_data = Wire(Vec(blockRows, UInt(rowBits.W))) 318 val new_mask = Wire(Vec(blockRows, UInt(rowBytes.W))) 319 // merge refilled data and store data (if needed) 320 def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = { 321 val full_wmask = FillInterleaved(8, wmask) 322 (~full_wmask & old_data | full_wmask & new_data) 323 } 324 for (i <- 0 until blockRows) { 325 // new_data(i) := req.store_data(rowBits * (i + 1) - 1, rowBits * i) 326 new_data(i) := refill_and_store_data(i) 327 // we only need to merge data for Store 328 new_mask(i) := Mux(req.isFromStore, req_store_mask(rowBytes * (i + 1) - 1, rowBytes * i), 0.U) 329 } 330 331 val hasData = RegInit(true.B) 332 val isDirty = RegInit(false.B) 333 when (io.mem_grant.fire()) { 334 w_grantfirst := true.B 335 grant_param := io.mem_grant.bits.param 336 when (edge.hasData(io.mem_grant.bits)) { 337 // GrantData 338 for (i <- 0 until beatRows) { 339 val idx = (refill_count << log2Floor(beatRows)) + i.U 340 val grant_row = io.mem_grant.bits.data(rowBits * (i + 1) - 1, rowBits * i) 341 refill_and_store_data(idx) := mergePutData(grant_row, new_data(idx), new_mask(idx)) 342 } 343 w_grantlast := w_grantlast || refill_done 344 hasData := true.B 345 }.otherwise { 346 // Grant 347 assert(full_overwrite) 348 for (i <- 0 until blockRows) { 349 refill_and_store_data(i) := new_data(i) 350 } 351 w_grantlast := true.B 352 hasData := false.B 353 } 354 355 error := io.mem_grant.bits.denied || io.mem_grant.bits.corrupt || error 356 357 refill_data_raw(refill_count) := io.mem_grant.bits.data 358 isDirty := io.mem_grant.bits.echo.lift(DirtyKey).getOrElse(false.B) 359 } 360 361 when (io.mem_finish.fire()) { 362 s_grantack := true.B 363 } 364 365 when (io.replace_pipe_req.fire()) { 366 s_replace_req := true.B 367 } 368 369 when (io.replace_pipe_resp) { 370 w_replace_resp := true.B 371 } 372 373 when (io.refill_pipe_req.fire()) { 374 s_refill := true.B 375 } 376 377 when (io.refill_pipe_resp) { 378 w_refill_resp := true.B 379 } 380 381 when (io.main_pipe_req.fire()) { 382 s_mainpipe_req := true.B 383 } 384 385 when (io.main_pipe_resp) { 386 w_mainpipe_resp := true.B 387 } 388 389 def before_req_sent_can_merge(new_req: MissReqWoStoreData): Bool = { 390 acquire_not_sent && (req.isFromLoad || req.isFromPrefetch) && (new_req.isFromLoad || new_req.isFromStore) 391 } 392 393 def before_data_refill_can_merge(new_req: MissReqWoStoreData): Bool = { 394 data_not_refilled && (req.isFromLoad || req.isFromStore || req.isFromPrefetch) && new_req.isFromLoad 395 } 396 397 // Note that late prefetch will be ignored 398 399 def should_merge(new_req: MissReqWoStoreData): Bool = { 400 val block_match = get_block(req.addr) === get_block(new_req.addr) 401 block_match && 402 ( 403 before_req_sent_can_merge(new_req) || 404 before_data_refill_can_merge(new_req) 405 ) 406 } 407 408 // store can be merged before io.mem_acquire.fire() 409 // store can not be merged the cycle that io.mem_acquire.fire() 410 // load can be merged before io.mem_grant.fire() 411 // 412 // TODO: merge store if possible? mem_acquire may need to be re-issued, 413 // but sbuffer entry can be freed 414 def should_reject(new_req: MissReqWoStoreData): Bool = { 415 val block_match = get_block(req.addr) === get_block(new_req.addr) 416 val set_match = set === addr_to_dcache_set(new_req.vaddr) 417 418 req_valid && 419 Mux( 420 block_match, 421 !before_req_sent_can_merge(new_req) && 422 !before_data_refill_can_merge(new_req), 423 set_match && new_req.way_en === req.way_en 424 ) 425 } 426 427 io.primary_ready := !req_valid 428 io.secondary_ready := should_merge(io.req.bits) 429 io.secondary_reject := should_reject(io.req.bits) 430 io.repl_way_en := req.way_en 431 432 // should not allocate, merge or reject at the same time 433 assert(RegNext(PopCount(Seq(io.primary_ready, io.secondary_ready, io.secondary_reject)) <= 1.U)) 434 435 val refill_data_splited = WireInit(VecInit(Seq.tabulate(cfg.blockBytes * 8 / l1BusDataWidth)(i => { 436 val data = refill_and_store_data.asUInt 437 data((i + 1) * l1BusDataWidth - 1, i * l1BusDataWidth) 438 }))) 439 // when granted data is all ready, wakeup lq's miss load 440 io.refill_to_ldq.valid := RegNext(!w_grantlast && io.mem_grant.fire()) && should_refill_data_reg 441 io.refill_to_ldq.bits.addr := RegNext(req.addr + (refill_count << refillOffBits)) 442 io.refill_to_ldq.bits.data := refill_data_splited(RegNext(refill_count)) 443 io.refill_to_ldq.bits.error := RegNext(io.mem_grant.bits.corrupt || io.mem_grant.bits.denied) 444 io.refill_to_ldq.bits.refill_done := RegNext(refill_done && io.mem_grant.fire()) 445 io.refill_to_ldq.bits.hasdata := hasData 446 io.refill_to_ldq.bits.data_raw := refill_data_raw.asUInt 447 io.refill_to_ldq.bits.id := io.id 448 449 io.mem_acquire.valid := !s_acquire 450 val grow_param = req.req_coh.onAccess(req.cmd)._2 451 val acquireBlock = edge.AcquireBlock( 452 fromSource = io.id, 453 toAddress = req.addr, 454 lgSize = (log2Up(cfg.blockBytes)).U, 455 growPermissions = grow_param 456 )._2 457 val acquirePerm = edge.AcquirePerm( 458 fromSource = io.id, 459 toAddress = req.addr, 460 lgSize = (log2Up(cfg.blockBytes)).U, 461 growPermissions = grow_param 462 )._2 463 io.mem_acquire.bits := Mux(full_overwrite, acquirePerm, acquireBlock) 464 // resolve cache alias by L2 465 io.mem_acquire.bits.user.lift(AliasKey).foreach( _ := req.vaddr(13, 12)) 466 // trigger prefetch 467 io.mem_acquire.bits.user.lift(PrefetchKey).foreach(_ := Mux(io.l2_pf_store_only, req.isFromStore, true.B)) 468 require(nSets <= 256) 469 470 io.mem_grant.ready := !w_grantlast && s_acquire 471 472 val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire()) 473 assert(RegNext(!io.mem_grant.fire() || edge.isRequest(io.mem_grant.bits))) 474 io.mem_finish.valid := !s_grantack && w_grantfirst 475 io.mem_finish.bits := grantack 476 477 io.replace_pipe_req.valid := !s_replace_req 478 val replace = io.replace_pipe_req.bits 479 replace := DontCare 480 replace.miss := false.B 481 replace.miss_id := io.id 482 replace.miss_dirty := false.B 483 replace.probe := false.B 484 replace.probe_need_data := false.B 485 replace.source := LOAD_SOURCE.U 486 replace.vaddr := req.vaddr // only untag bits are needed 487 replace.addr := Cat(req.replace_tag, 0.U(pgUntagBits.W)) // only tag bits are needed 488 replace.store_mask := 0.U 489 replace.replace := true.B 490 replace.replace_way_en := req.way_en 491 replace.error := false.B 492 493 io.refill_pipe_req.valid := !s_refill && w_replace_resp && w_grantlast 494 val refill = io.refill_pipe_req.bits 495 refill.source := req.source 496 refill.addr := req.addr 497 refill.way_en := req.way_en 498 refill.wmask := Mux( 499 hasData || req.isFromLoad, 500 ~0.U(DCacheBanks.W), 501 VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, req_store_mask).orR)).asUInt 502 ) 503 refill.data := refill_and_store_data.asTypeOf((new RefillPipeReq).data) 504 refill.miss_id := io.id 505 refill.id := req.id 506 def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = { 507 val c = categorize(cmd) 508 MuxLookup(Cat(c, param, dirty), Nothing, Seq( 509 //(effect param) -> (next) 510 Cat(rd, toB, false.B) -> Branch, 511 Cat(rd, toB, true.B) -> Branch, 512 Cat(rd, toT, false.B) -> Trunk, 513 Cat(rd, toT, true.B) -> Dirty, 514 Cat(wi, toT, false.B) -> Trunk, 515 Cat(wi, toT, true.B) -> Dirty, 516 Cat(wr, toT, false.B) -> Dirty, 517 Cat(wr, toT, true.B) -> Dirty)) 518 } 519 refill.meta.coh := ClientMetadata(missCohGen(req.cmd, grant_param, isDirty)) 520 refill.error := error 521 refill.prefetch := prefetch 522 refill.access := access 523 refill.alias := req.vaddr(13, 12) // TODO 524 525 io.main_pipe_req.valid := !s_mainpipe_req && w_grantlast 526 io.main_pipe_req.bits := DontCare 527 io.main_pipe_req.bits.miss := true.B 528 io.main_pipe_req.bits.miss_id := io.id 529 io.main_pipe_req.bits.miss_param := grant_param 530 io.main_pipe_req.bits.miss_dirty := isDirty 531 io.main_pipe_req.bits.miss_way_en := req.way_en 532 io.main_pipe_req.bits.probe := false.B 533 io.main_pipe_req.bits.source := req.source 534 io.main_pipe_req.bits.cmd := req.cmd 535 io.main_pipe_req.bits.vaddr := req.vaddr 536 io.main_pipe_req.bits.addr := req.addr 537 io.main_pipe_req.bits.store_data := refill_and_store_data.asUInt 538 io.main_pipe_req.bits.store_mask := ~0.U(blockBytes.W) 539 io.main_pipe_req.bits.word_idx := req.word_idx 540 io.main_pipe_req.bits.amo_data := req.amo_data 541 io.main_pipe_req.bits.amo_mask := req.amo_mask 542 io.main_pipe_req.bits.error := error 543 io.main_pipe_req.bits.id := req.id 544 545 io.block_addr.valid := req_valid && w_grantlast && !w_refill_resp 546 io.block_addr.bits := req.addr 547 548 io.debug_early_replace.valid := BoolStopWatch(io.replace_pipe_resp, io.refill_pipe_req.fire()) 549 io.debug_early_replace.bits.idx := addr_to_dcache_set(req.vaddr) 550 io.debug_early_replace.bits.tag := req.replace_tag 551 552 io.forwardInfo.apply(req_valid, req.addr, refill_data_raw, w_grantfirst, w_grantlast) 553 554 XSPerfAccumulate("miss_req_primary", primary_fire) 555 XSPerfAccumulate("miss_req_merged", secondary_fire) 556 XSPerfAccumulate("load_miss_penalty_to_use", 557 should_refill_data && 558 BoolStopWatch(primary_fire, io.refill_to_ldq.valid, true) 559 ) 560 XSPerfAccumulate("main_pipe_penalty", BoolStopWatch(io.main_pipe_req.fire(), io.main_pipe_resp)) 561 XSPerfAccumulate("penalty_blocked_by_channel_A", io.mem_acquire.valid && !io.mem_acquire.ready) 562 XSPerfAccumulate("penalty_waiting_for_channel_D", s_acquire && !w_grantlast && !io.mem_grant.valid) 563 XSPerfAccumulate("penalty_waiting_for_channel_E", io.mem_finish.valid && !io.mem_finish.ready) 564 XSPerfAccumulate("penalty_from_grant_to_refill", !w_refill_resp && w_grantlast) 565 XSPerfAccumulate("prefetch_req_primary", primary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 566 XSPerfAccumulate("prefetch_req_merged", secondary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 567 568 val (mshr_penalty_sample, mshr_penalty) = TransactionLatencyCounter(RegNext(primary_fire), release_entry) 569 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 0, 20, 1, true, true) 570 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 20, 100, 10, true, false) 571 572 val load_miss_begin = primary_fire && io.req.bits.isFromLoad 573 val refill_finished = RegNext(!w_grantlast && refill_done) && should_refill_data 574 val (load_miss_penalty_sample, load_miss_penalty) = TransactionLatencyCounter(load_miss_begin, refill_finished) // not real refill finish time 575 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 0, 20, 1, true, true) 576 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 20, 100, 10, true, false) 577 578 val (a_to_d_penalty_sample, a_to_d_penalty) = TransactionLatencyCounter(io.mem_acquire.fire(), io.mem_grant.fire() && refill_done) 579 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 0, 20, 1, true, true) 580 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 20, 100, 10, true, false) 581} 582 583class MissQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasPerfEvents { 584 val io = IO(new Bundle { 585 val hartId = Input(UInt(8.W)) 586 val req = Flipped(DecoupledIO(new MissReq)) 587 val resp = Output(new MissResp) 588 val refill_to_ldq = ValidIO(new Refill) 589 590 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 591 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 592 val mem_finish = DecoupledIO(new TLBundleE(edge.bundle)) 593 594 val refill_pipe_req = DecoupledIO(new RefillPipeReq) 595 val refill_pipe_req_dup = Vec(nDupStatus, DecoupledIO(new RefillPipeReqCtrl)) 596 val refill_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 597 598 val replace_pipe_req = DecoupledIO(new MainPipeReq) 599 val replace_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 600 601 val main_pipe_req = DecoupledIO(new MainPipeReq) 602 val main_pipe_resp = Flipped(ValidIO(new AtomicsResp)) 603 604 // block probe 605 val probe_addr = Input(UInt(PAddrBits.W)) 606 val probe_block = Output(Bool()) 607 608 val full = Output(Bool()) 609 610 // only for performance counter 611 // This is valid when an mshr has finished replacing a block (w_replace_resp), 612 // but hasn't received Grant from L2 (!w_grantlast) 613 val debug_early_replace = Vec(cfg.nMissEntries, ValidIO(new Bundle() { 614 // info about the block that has been replaced 615 val idx = UInt(idxBits.W) // vaddr 616 val tag = UInt(tagBits.W) // paddr 617 })) 618 619 // forward missqueue 620 val forward = Vec(LoadPipelineWidth, new LduToMissqueueForwardIO) 621 val l2_pf_store_only = Input(Bool()) 622 }) 623 624 // 128KBL1: FIXME: provide vaddr for l2 625 626 val entries = Seq.fill(cfg.nMissEntries)(Module(new MissEntry(edge))) 627 628 val req_data_gen = io.req.bits.toMissReqStoreData() 629 val req_data_buffer = RegEnable(req_data_gen, io.req.valid) 630 631 val primary_ready_vec = entries.map(_.io.primary_ready) 632 val secondary_ready_vec = entries.map(_.io.secondary_ready) 633 val secondary_reject_vec = entries.map(_.io.secondary_reject) 634 val probe_block_vec = entries.map { case e => e.io.block_addr.valid && e.io.block_addr.bits === io.probe_addr } 635 636 val merge = Cat(secondary_ready_vec).orR 637 val reject = Cat(secondary_reject_vec).orR 638 val alloc = !reject && !merge && Cat(primary_ready_vec).orR 639 val accept = alloc || merge 640 641 val req_handled_vec = entries.map(_.io.req_handled_by_this_entry) 642 assert(PopCount(req_handled_vec) <= 1.U, "Only one mshr can handle a req") 643 io.resp.id := OHToUInt(req_handled_vec) 644 io.resp.merged := merge 645 io.resp.repl_way_en := Mux1H(secondary_ready_vec, entries.map(_.io.repl_way_en)) 646 647 val forwardInfo_vec = VecInit(entries.map(_.io.forwardInfo)) 648 (0 until LoadPipelineWidth).map(i => { 649 val id = io.forward(i).mshrid 650 val req_valid = io.forward(i).valid 651 val paddr = io.forward(i).paddr 652 653 val (forward_mshr, forwardData) = forwardInfo_vec(id).forward(req_valid, paddr) 654 io.forward(i).forward_result_valid := forwardInfo_vec(id).check(req_valid, paddr) 655 io.forward(i).forward_mshr := forward_mshr 656 io.forward(i).forwardData := forwardData 657 }) 658 659 assert(RegNext(PopCount(secondary_ready_vec) <= 1.U)) 660// assert(RegNext(PopCount(secondary_reject_vec) <= 1.U)) 661 // It is possible that one mshr wants to merge a req, while another mshr wants to reject it. 662 // That is, a coming req has the same paddr as that of mshr_0 (merge), 663 // while it has the same set and the same way as mshr_1 (reject). 664 // In this situation, the coming req should be merged by mshr_0 665// assert(RegNext(PopCount(Seq(merge, reject)) <= 1.U)) 666 667 def select_valid_one[T <: Bundle]( 668 in: Seq[DecoupledIO[T]], 669 out: DecoupledIO[T], 670 name: Option[String] = None): Unit = { 671 672 if (name.nonEmpty) { out.suggestName(s"${name.get}_select") } 673 out.valid := Cat(in.map(_.valid)).orR 674 out.bits := ParallelMux(in.map(_.valid) zip in.map(_.bits)) 675 in.map(_.ready := out.ready) 676 assert(!RegNext(out.valid && PopCount(Cat(in.map(_.valid))) > 1.U)) 677 } 678 679 io.mem_grant.ready := false.B 680 681 entries.zipWithIndex.foreach { 682 case (e, i) => 683 val former_primary_ready = if(i == 0) 684 false.B 685 else 686 Cat((0 until i).map(j => entries(j).io.primary_ready)).orR 687 688 e.io.hartId := io.hartId 689 e.io.id := i.U 690 e.io.l2_pf_store_only := io.l2_pf_store_only 691 e.io.req.valid := io.req.valid 692 e.io.primary_valid := io.req.valid && 693 !merge && 694 !reject && 695 !former_primary_ready && 696 e.io.primary_ready 697 e.io.req.bits := io.req.bits.toMissReqWoStoreData() 698 e.io.req_data := req_data_buffer 699 700 e.io.mem_grant.valid := false.B 701 e.io.mem_grant.bits := DontCare 702 when (io.mem_grant.bits.source === i.U) { 703 e.io.mem_grant <> io.mem_grant 704 } 705 706 e.io.refill_pipe_resp := io.refill_pipe_resp.valid && io.refill_pipe_resp.bits === i.U 707 e.io.replace_pipe_resp := io.replace_pipe_resp.valid && io.replace_pipe_resp.bits === i.U 708 e.io.main_pipe_resp := io.main_pipe_resp.valid && io.main_pipe_resp.bits.ack_miss_queue && io.main_pipe_resp.bits.miss_id === i.U 709 710 io.debug_early_replace(i) := e.io.debug_early_replace 711 } 712 713 io.req.ready := accept 714 io.refill_to_ldq.valid := Cat(entries.map(_.io.refill_to_ldq.valid)).orR 715 io.refill_to_ldq.bits := ParallelMux(entries.map(_.io.refill_to_ldq.valid) zip entries.map(_.io.refill_to_ldq.bits)) 716 717 TLArbiter.lowest(edge, io.mem_acquire, entries.map(_.io.mem_acquire):_*) 718 TLArbiter.lowest(edge, io.mem_finish, entries.map(_.io.mem_finish):_*) 719 720 // arbiter_with_pipereg_N_dup(entries.map(_.io.refill_pipe_req), io.refill_pipe_req, 721 // io.refill_pipe_req_dup, 722 // Some("refill_pipe_req")) 723 val out_refill_pipe_req = Wire(Decoupled(new RefillPipeReq)) 724 val out_refill_pipe_req_ctrl = Wire(Decoupled(new RefillPipeReqCtrl)) 725 out_refill_pipe_req_ctrl.valid := out_refill_pipe_req.valid 726 out_refill_pipe_req_ctrl.bits := out_refill_pipe_req.bits.getCtrl 727 out_refill_pipe_req.ready := out_refill_pipe_req_ctrl.ready 728 arbiter(entries.map(_.io.refill_pipe_req), out_refill_pipe_req, Some("refill_pipe_req")) 729 for (dup <- io.refill_pipe_req_dup) { 730 AddPipelineReg(out_refill_pipe_req_ctrl, dup, false.B) 731 } 732 AddPipelineReg(out_refill_pipe_req, io.refill_pipe_req, false.B) 733 734 arbiter_with_pipereg(entries.map(_.io.replace_pipe_req), io.replace_pipe_req, Some("replace_pipe_req")) 735 736 fastArbiter(entries.map(_.io.main_pipe_req), io.main_pipe_req, Some("main_pipe_req")) 737 738 io.probe_block := Cat(probe_block_vec).orR 739 740 io.full := ~Cat(entries.map(_.io.primary_ready)).andR 741 742 // L1MissTrace Chisel DB 743 val debug_miss_trace = Wire(new L1MissTrace) 744 debug_miss_trace.vaddr := io.req.bits.vaddr 745 debug_miss_trace.paddr := io.req.bits.addr 746 debug_miss_trace.source := io.req.bits.source 747 debug_miss_trace.pc := io.req.bits.pc 748 749 val isWriteL1MissQMissTable = WireInit(Constantin.createRecord("isWriteL1MissQMissTable" + p(XSCoreParamsKey).HartId.toString)) 750 val table = ChiselDB.createTable("L1MissQMissTrace_hart"+ p(XSCoreParamsKey).HartId.toString, new L1MissTrace) 751 table.log(debug_miss_trace, isWriteL1MissQMissTable.orR && io.req.valid && !io.req.bits.cancel && alloc, "MissQueue", clock, reset) 752 753 // Difftest 754 if (env.EnableDifftest) { 755 val difftest = Module(new DifftestRefillEvent) 756 difftest.io.clock := clock 757 difftest.io.coreid := io.hartId 758 difftest.io.cacheid := 1.U 759 difftest.io.valid := io.refill_to_ldq.valid && io.refill_to_ldq.bits.hasdata && io.refill_to_ldq.bits.refill_done 760 difftest.io.addr := io.refill_to_ldq.bits.addr 761 difftest.io.data := io.refill_to_ldq.bits.data_raw.asTypeOf(difftest.io.data) 762 } 763 764 // Perf count 765 XSPerfAccumulate("miss_req", io.req.fire()) 766 XSPerfAccumulate("miss_req_allocate", io.req.fire() && alloc) 767 XSPerfAccumulate("miss_req_merge_load", io.req.fire() && merge && io.req.bits.isFromLoad) 768 XSPerfAccumulate("miss_req_reject_load", io.req.valid && reject && io.req.bits.isFromLoad) 769 XSPerfAccumulate("probe_blocked_by_miss", io.probe_block) 770 XSPerfAccumulate("prefetch_primary_fire", io.req.fire() && alloc && io.req.bits.isFromPrefetch) 771 XSPerfAccumulate("prefetch_secondary_fire", io.req.fire() && merge && io.req.bits.isFromPrefetch) 772 val max_inflight = RegInit(0.U((log2Up(cfg.nMissEntries) + 1).W)) 773 val num_valids = PopCount(~Cat(primary_ready_vec).asUInt) 774 when (num_valids > max_inflight) { 775 max_inflight := num_valids 776 } 777 // max inflight (average) = max_inflight_total / cycle cnt 778 XSPerfAccumulate("max_inflight", max_inflight) 779 QueuePerf(cfg.nMissEntries, num_valids, num_valids === cfg.nMissEntries.U) 780 io.full := num_valids === cfg.nMissEntries.U 781 XSPerfHistogram("num_valids", num_valids, true.B, 0, cfg.nMissEntries, 1) 782 783 val perfValidCount = RegNext(PopCount(entries.map(entry => (!entry.io.primary_ready)))) 784 val perfEvents = Seq( 785 ("dcache_missq_req ", io.req.fire()), 786 ("dcache_missq_1_4_valid", (perfValidCount < (cfg.nMissEntries.U/4.U))), 787 ("dcache_missq_2_4_valid", (perfValidCount > (cfg.nMissEntries.U/4.U)) & (perfValidCount <= (cfg.nMissEntries.U/2.U))), 788 ("dcache_missq_3_4_valid", (perfValidCount > (cfg.nMissEntries.U/2.U)) & (perfValidCount <= (cfg.nMissEntries.U*3.U/4.U))), 789 ("dcache_missq_4_4_valid", (perfValidCount > (cfg.nMissEntries.U*3.U/4.U))), 790 ) 791 generatePerfEvent() 792} 793