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 chisel3._ 20import chisel3.util._ 21import coupledL2.VaddrKey 22import difftest._ 23import freechips.rocketchip.tilelink.ClientStates._ 24import freechips.rocketchip.tilelink.MemoryOpCategories._ 25import freechips.rocketchip.tilelink.TLPermissions._ 26import freechips.rocketchip.tilelink._ 27import huancun.{AliasKey, DirtyKey, PrefetchKey} 28import org.chipsalliance.cde.config.Parameters 29import utility._ 30import utils._ 31import xiangshan._ 32import xiangshan.mem.AddPipelineReg 33import xiangshan.mem.prefetch._ 34import xiangshan.mem.trace._ 35 36class MissReqWoStoreData(implicit p: Parameters) extends DCacheBundle { 37 val source = UInt(sourceTypeWidth.W) 38 val pf_source = UInt(L1PfSourceBits.W) 39 val cmd = UInt(M_SZ.W) 40 val addr = UInt(PAddrBits.W) 41 val vaddr = UInt(VAddrBits.W) 42 val way_en = UInt(DCacheWays.W) 43 val pc = UInt(VAddrBits.W) 44 45 // store 46 val full_overwrite = Bool() 47 48 // which word does amo work on? 49 val word_idx = UInt(log2Up(blockWords).W) 50 val amo_data = UInt(DataBits.W) 51 val amo_mask = UInt((DataBits / 8).W) 52 53 val req_coh = new ClientMetadata 54 val replace_coh = new ClientMetadata 55 val replace_tag = UInt(tagBits.W) 56 val id = UInt(reqIdWidth.W) 57 58 val replace_pf = UInt(L1PfSourceBits.W) 59 60 // For now, miss queue entry req is actually valid when req.valid && !cancel 61 // * req.valid is fast to generate 62 // * cancel is slow to generate, it will not be used until the last moment 63 // 64 // cancel may come from the following sources: 65 // 1. miss req blocked by writeback queue: 66 // a writeback req of the same address is in progress 67 // 2. pmp check failed 68 val cancel = Bool() // cancel is slow to generate, it will cancel missreq.valid 69 70 // Req source decode 71 // Note that req source is NOT cmd type 72 // For instance, a req which isFromPrefetch may have R or W cmd 73 def isFromLoad = source === LOAD_SOURCE.U 74 def isFromStore = source === STORE_SOURCE.U 75 def isFromAMO = source === AMO_SOURCE.U 76 def isFromPrefetch = source >= DCACHE_PREFETCH_SOURCE.U 77 def isPrefetchWrite = source === DCACHE_PREFETCH_SOURCE.U && cmd === MemoryOpConstants.M_PFW 78 def isPrefetchRead = source === DCACHE_PREFETCH_SOURCE.U && cmd === MemoryOpConstants.M_PFR 79 def hit = req_coh.isValid() 80} 81 82class MissReqStoreData(implicit p: Parameters) extends DCacheBundle { 83 // store data and store mask will be written to miss queue entry 84 // 1 cycle after req.fire() and meta write 85 val store_data = UInt((cfg.blockBytes * 8).W) 86 val store_mask = UInt(cfg.blockBytes.W) 87} 88 89class MissReq(implicit p: Parameters) extends MissReqWoStoreData { 90 // store data and store mask will be written to miss queue entry 91 // 1 cycle after req.fire() and meta write 92 val store_data = UInt((cfg.blockBytes * 8).W) 93 val store_mask = UInt(cfg.blockBytes.W) 94 95 def toMissReqStoreData(): MissReqStoreData = { 96 val out = Wire(new MissReqStoreData) 97 out.store_data := store_data 98 out.store_mask := store_mask 99 out 100 } 101 102 def toMissReqWoStoreData(): MissReqWoStoreData = { 103 val out = Wire(new MissReqWoStoreData) 104 out.source := source 105 out.replace_pf := replace_pf 106 out.pf_source := pf_source 107 out.cmd := cmd 108 out.addr := addr 109 out.vaddr := vaddr 110 out.way_en := way_en 111 out.full_overwrite := full_overwrite 112 out.word_idx := word_idx 113 out.amo_data := amo_data 114 out.amo_mask := amo_mask 115 out.req_coh := req_coh 116 out.replace_coh := replace_coh 117 out.replace_tag := replace_tag 118 out.id := id 119 out.cancel := cancel 120 out.pc := pc 121 out 122 } 123} 124 125class MissResp(implicit p: Parameters) extends DCacheBundle { 126 val id = UInt(log2Up(cfg.nMissEntries).W) 127 // cache miss request is handled by miss queue, either merged or newly allocated 128 val handled = Bool() 129 // cache req missed, merged into one of miss queue entries 130 // i.e. !miss_merged means this access is the first miss for this cacheline 131 val merged = Bool() 132 val repl_way_en = UInt(DCacheWays.W) 133} 134 135 136/** 137 * miss queue enq logic: enq is now splited into 2 cycles 138 * +---------------------------------------------------------------------+ pipeline reg +-------------------------+ 139 * + s0: enq source arbiter, judge mshr alloc or merge + +-------+ + s1: real alloc or merge + 140 * + +-----+ primary_fire? -> + | alloc | + + 141 * + mainpipe -> req0 -> | | secondary_fire? -> + | merge | + + 142 * + loadpipe0 -> req1 -> | arb | -> req -> + -> | req | -> + + 143 * + loadpipe1 -> req2 -> | | mshr id -> + | id | + + 144 * + +-----+ + +-------+ + + 145 * +---------------------------------------------------------------------+ +-------------------------+ 146 */ 147 148// a pipeline reg between MissReq and MissEntry 149class MissReqPipeRegBundle(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheBundle { 150 val req = new MissReq 151 // this request is about to merge to an existing mshr 152 val merge = Bool() 153 // this request is about to allocate a new mshr 154 val alloc = Bool() 155 val mshr_id = UInt(log2Up(cfg.nMissEntries).W) 156 157 def reg_valid(): Bool = { 158 (merge || alloc) 159 } 160 161 def matched(new_req: MissReq): Bool = { 162 val block_match = get_block(req.addr) === get_block(new_req.addr) 163 block_match && reg_valid() && !(req.isFromPrefetch) 164 } 165 166 def prefetch_late_en(new_req: MissReqWoStoreData, new_req_valid: Bool): Bool = { 167 val block_match = get_block(req.addr) === get_block(new_req.addr) 168 new_req_valid && alloc && block_match && (req.isFromPrefetch) && !(new_req.isFromPrefetch) 169 } 170 171 def reject_req(new_req: MissReq): Bool = { 172 val block_match = get_block(req.addr) === get_block(new_req.addr) 173 val alias_match = is_alias_match(req.vaddr, new_req.vaddr) 174 val merge_load = (req.isFromLoad || req.isFromStore || req.isFromPrefetch) && new_req.isFromLoad 175 // store merge to a store is disabled, sbuffer should avoid this situation, as store to same address should preserver their program order to match memory model 176 val merge_store = (req.isFromLoad || req.isFromPrefetch) && new_req.isFromStore 177 178 val set_match = addr_to_dcache_set(req.vaddr) === addr_to_dcache_set(new_req.vaddr) 179 val way_match = req.way_en === new_req.way_en 180 Mux( 181 alloc, 182 Mux( 183 block_match, 184 !alias_match || !(merge_load || merge_store), 185 set_match && way_match 186 ), 187 false.B 188 ) 189 } 190 191 def merge_req(new_req: MissReq): Bool = { 192 val block_match = get_block(req.addr) === get_block(new_req.addr) 193 val alias_match = is_alias_match(req.vaddr, new_req.vaddr) 194 val merge_load = (req.isFromLoad || req.isFromStore || req.isFromPrefetch) && new_req.isFromLoad 195 // store merge to a store is disabled, sbuffer should avoid this situation, as store to same address should preserver their program order to match memory model 196 val merge_store = (req.isFromLoad || req.isFromPrefetch) && new_req.isFromStore 197 Mux( 198 alloc, 199 block_match && alias_match && (merge_load || merge_store), 200 false.B 201 ) 202 } 203 204 // send out acquire as soon as possible 205 // if a new store miss req is about to merge into this pipe reg, don't send acquire now 206 def can_send_acquire(valid: Bool, new_req: MissReq): Bool = { 207 alloc && !(valid && merge_req(new_req) && new_req.isFromStore) 208 } 209 210 def get_acquire(l2_pf_store_only: Bool): TLBundleA = { 211 val acquire = Wire(new TLBundleA(edge.bundle)) 212 val grow_param = req.req_coh.onAccess(req.cmd)._2 213 val acquireBlock = edge.AcquireBlock( 214 fromSource = mshr_id, 215 toAddress = get_block_addr(req.addr), 216 lgSize = (log2Up(cfg.blockBytes)).U, 217 growPermissions = grow_param 218 )._2 219 val acquirePerm = edge.AcquirePerm( 220 fromSource = mshr_id, 221 toAddress = get_block_addr(req.addr), 222 lgSize = (log2Up(cfg.blockBytes)).U, 223 growPermissions = grow_param 224 )._2 225 acquire := Mux(req.full_overwrite, acquirePerm, acquireBlock) 226 // resolve cache alias by L2 227 acquire.user.lift(AliasKey).foreach(_ := req.vaddr(13, 12)) 228 // pass vaddr to l2 229 acquire.user.lift(VaddrKey).foreach(_ := req.vaddr(VAddrBits - 1, blockOffBits)) 230 // trigger prefetch 231 acquire.user.lift(PrefetchKey).foreach(_ := Mux(l2_pf_store_only, req.isFromStore, true.B)) 232 // req source 233 when(req.isFromLoad) { 234 acquire.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPULoadData.id.U) 235 }.elsewhen(req.isFromStore) { 236 acquire.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUStoreData.id.U) 237 }.elsewhen(req.isFromAMO) { 238 acquire.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUAtomicData.id.U) 239 }.otherwise { 240 acquire.user.lift(ReqSourceKey).foreach(_ := MemReqSource.L1DataPrefetch.id.U) 241 } 242 243 acquire 244 } 245} 246 247class MissEntry(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule { 248 val io = IO(new Bundle() { 249 val hartId = Input(UInt(8.W)) 250 // MSHR ID 251 val id = Input(UInt(log2Up(cfg.nMissEntries).W)) 252 // client requests 253 // MSHR update request, MSHR state and addr will be updated when req.fire 254 val req = Flipped(ValidIO(new MissReqWoStoreData)) 255 // pipeline reg 256 val miss_req_pipe_reg = Input(new MissReqPipeRegBundle(edge)) 257 // allocate this entry for new req 258 val primary_valid = Input(Bool()) 259 // this entry is free and can be allocated to new reqs 260 val primary_ready = Output(Bool()) 261 // this entry is busy, but it can merge the new req 262 val secondary_ready = Output(Bool()) 263 // this entry is busy and it can not merge the new req 264 val secondary_reject = Output(Bool()) 265 // way selected for replacing, used to support plru update 266 val repl_way_en = Output(UInt(DCacheWays.W)) 267 268 // bus 269 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 270 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 271 val mem_finish = DecoupledIO(new TLBundleE(edge.bundle)) 272 273 // send refill info to load queue 274 val refill_to_ldq = ValidIO(new Refill) 275 276 // refill pipe 277 val refill_pipe_req = DecoupledIO(new RefillPipeReq) 278 val refill_pipe_resp = Input(Bool()) 279 280 // replace pipe 281 val replace_pipe_req = DecoupledIO(new MainPipeReq) 282 val replace_pipe_resp = Input(Bool()) 283 284 // main pipe: amo miss 285 val main_pipe_req = DecoupledIO(new MainPipeReq) 286 val main_pipe_resp = Input(Bool()) 287 288 val block_addr = ValidIO(UInt(PAddrBits.W)) 289 290 val debug_early_replace = ValidIO(new Bundle() { 291 // info about the block that has been replaced 292 val idx = UInt(idxBits.W) // vaddr 293 val tag = UInt(tagBits.W) // paddr 294 }) 295 296 val req_handled_by_this_entry = Output(Bool()) 297 298 val forwardInfo = Output(new MissEntryForwardIO) 299 val l2_pf_store_only = Input(Bool()) 300 301 val sms_agt_evict_req = ValidIO(new AGTEvictReq) 302 303 // whether the pipeline reg has send out an acquire 304 val acquire_fired_by_pipe_reg = Input(Bool()) 305 val memSetPattenDetected = Input(Bool()) 306 307 val perf_pending_prefetch = Output(Bool()) 308 val perf_pending_normal = Output(Bool()) 309 310 val rob_head_query = new DCacheBundle { 311 val vaddr = Input(UInt(VAddrBits.W)) 312 val query_valid = Input(Bool()) 313 314 val resp = Output(Bool()) 315 316 def hit(e_vaddr: UInt): Bool = { 317 require(e_vaddr.getWidth == VAddrBits) 318 query_valid && vaddr(VAddrBits - 1, DCacheLineOffset) === e_vaddr(VAddrBits - 1, DCacheLineOffset) 319 } 320 } 321 322 val latency_monitor = new DCacheBundle { 323 val load_miss_refilling = Output(Bool()) 324 val store_miss_refilling = Output(Bool()) 325 val amo_miss_refilling = Output(Bool()) 326 val pf_miss_refilling = Output(Bool()) 327 } 328 329 val prefetch_info = new DCacheBundle { 330 val late_prefetch = Output(Bool()) 331 } 332 val nMaxPrefetchEntry = Input(UInt(64.W)) 333 val matched = Output(Bool()) 334 }) 335 336 assert(!RegNext(io.primary_valid && !io.primary_ready)) 337 338 val req = Reg(new MissReqWoStoreData) 339 val req_primary_fire = Reg(new MissReqWoStoreData) // for perf use 340 val req_store_mask = Reg(UInt(cfg.blockBytes.W)) 341 val req_valid = RegInit(false.B) 342 val set = addr_to_dcache_set(req.vaddr) 343 344 val miss_req_pipe_reg_bits = io.miss_req_pipe_reg.req 345 346 val input_req_is_prefetch = isPrefetch(miss_req_pipe_reg_bits.cmd) 347 348 val s_acquire = RegInit(true.B) 349 val s_grantack = RegInit(true.B) 350 val s_replace_req = RegInit(true.B) 351 val s_refill = RegInit(true.B) 352 val s_mainpipe_req = RegInit(true.B) 353 354 val w_grantfirst = RegInit(true.B) 355 val w_grantlast = RegInit(true.B) 356 val w_replace_resp = RegInit(true.B) 357 val w_refill_resp = RegInit(true.B) 358 val w_mainpipe_resp = RegInit(true.B) 359 360 val release_entry = s_grantack && w_refill_resp && w_mainpipe_resp 361 362 val acquire_not_sent = !s_acquire && !io.mem_acquire.ready 363 val data_not_refilled = !w_grantfirst 364 365 val error = RegInit(false.B) 366 val prefetch = RegInit(false.B) 367 val access = RegInit(false.B) 368 369 val should_refill_data_reg = Reg(Bool()) 370 val should_refill_data = WireInit(should_refill_data_reg) 371 372 val should_replace = RegInit(false.B) 373 374 // val full_overwrite = req.isFromStore && req_store_mask.andR 375 val full_overwrite = Reg(Bool()) 376 377 val (_, _, refill_done, refill_count) = edge.count(io.mem_grant) 378 val grant_param = Reg(UInt(TLPermissions.bdWidth.W)) 379 380 // refill data with store data, this reg will be used to store: 381 // 1. store data (if needed), before l2 refill data 382 // 2. store data and l2 refill data merged result (i.e. new cacheline taht will be write to data array) 383 val refill_and_store_data = Reg(Vec(blockRows, UInt(rowBits.W))) 384 // raw data refilled to l1 by l2 385 val refill_data_raw = Reg(Vec(blockBytes/beatBytes, UInt(beatBits.W))) 386 387 // allocate current miss queue entry for a miss req 388 val primary_fire = WireInit(io.req.valid && io.primary_ready && io.primary_valid && !io.req.bits.cancel) 389 // merge miss req to current miss queue entry 390 val secondary_fire = WireInit(io.req.valid && io.secondary_ready && !io.req.bits.cancel) 391 392 val req_handled_by_this_entry = primary_fire || secondary_fire 393 394 // for perf use 395 val secondary_fired = RegInit(false.B) 396 397 io.perf_pending_prefetch := req_valid && prefetch && !secondary_fired 398 io.perf_pending_normal := req_valid && (!prefetch || secondary_fired) 399 400 io.rob_head_query.resp := io.rob_head_query.hit(req.vaddr) && req_valid 401 402 io.req_handled_by_this_entry := req_handled_by_this_entry 403 404 when (release_entry && req_valid) { 405 req_valid := false.B 406 } 407 408 when (io.miss_req_pipe_reg.alloc) { 409 assert(RegNext(primary_fire), "after 1 cycle of primary_fire, entry will be allocated") 410 req_valid := true.B 411 412 req := miss_req_pipe_reg_bits.toMissReqWoStoreData() 413 req_primary_fire := miss_req_pipe_reg_bits.toMissReqWoStoreData() 414 req.addr := get_block_addr(miss_req_pipe_reg_bits.addr) 415 416 s_acquire := io.acquire_fired_by_pipe_reg 417 s_grantack := false.B 418 419 w_grantfirst := false.B 420 w_grantlast := false.B 421 422 when(miss_req_pipe_reg_bits.isFromStore) { 423 req_store_mask := miss_req_pipe_reg_bits.store_mask 424 for (i <- 0 until blockRows) { 425 refill_and_store_data(i) := miss_req_pipe_reg_bits.store_data(rowBits * (i + 1) - 1, rowBits * i) 426 } 427 } 428 full_overwrite := miss_req_pipe_reg_bits.isFromStore && miss_req_pipe_reg_bits.full_overwrite 429 430 when (!miss_req_pipe_reg_bits.isFromAMO) { 431 s_refill := false.B 432 w_refill_resp := false.B 433 } 434 435 when (!miss_req_pipe_reg_bits.hit && miss_req_pipe_reg_bits.replace_coh.isValid() && !miss_req_pipe_reg_bits.isFromAMO) { 436 s_replace_req := false.B 437 w_replace_resp := false.B 438 should_replace := true.B 439 }.otherwise { 440 should_replace := false.B 441 } 442 443 when (miss_req_pipe_reg_bits.isFromAMO) { 444 s_mainpipe_req := false.B 445 w_mainpipe_resp := false.B 446 } 447 448 should_refill_data_reg := miss_req_pipe_reg_bits.isFromLoad 449 error := false.B 450 prefetch := input_req_is_prefetch && !io.miss_req_pipe_reg.prefetch_late_en(io.req.bits, io.req.valid) 451 access := false.B 452 secondary_fired := false.B 453 } 454 455 when (io.miss_req_pipe_reg.merge) { 456 assert(RegNext(secondary_fire) || RegNext(RegNext(primary_fire)), "after 1 cycle of secondary_fire or 2 cycle of primary_fire, entry will be merged") 457 assert(miss_req_pipe_reg_bits.req_coh.state <= req.req_coh.state || (prefetch && !access)) 458 assert(!(miss_req_pipe_reg_bits.isFromAMO || req.isFromAMO)) 459 // use the most uptodate meta 460 req.req_coh := miss_req_pipe_reg_bits.req_coh 461 462 assert(!miss_req_pipe_reg_bits.isFromPrefetch, "can not merge a prefetch req, late prefetch should always be ignored!") 463 464 when (miss_req_pipe_reg_bits.isFromStore) { 465 req := miss_req_pipe_reg_bits 466 req.addr := get_block_addr(miss_req_pipe_reg_bits.addr) 467 req.way_en := req.way_en 468 req.replace_coh := req.replace_coh 469 req.replace_tag := req.replace_tag 470 req_store_mask := miss_req_pipe_reg_bits.store_mask 471 for (i <- 0 until blockRows) { 472 refill_and_store_data(i) := miss_req_pipe_reg_bits.store_data(rowBits * (i + 1) - 1, rowBits * i) 473 } 474 full_overwrite := miss_req_pipe_reg_bits.isFromStore && miss_req_pipe_reg_bits.full_overwrite 475 assert(is_alias_match(req.vaddr, miss_req_pipe_reg_bits.vaddr), "alias bits should be the same when merging store") 476 } 477 478 should_refill_data := should_refill_data_reg || miss_req_pipe_reg_bits.isFromLoad 479 should_refill_data_reg := should_refill_data 480 when (!input_req_is_prefetch) { 481 access := true.B // when merge non-prefetch req, set access bit 482 } 483 secondary_fired := true.B 484 } 485 486 when (io.mem_acquire.fire) { 487 s_acquire := true.B 488 } 489 490 // merge data refilled by l2 and store data, update miss queue entry, gen refill_req 491 val new_data = Wire(Vec(blockRows, UInt(rowBits.W))) 492 val new_mask = Wire(Vec(blockRows, UInt(rowBytes.W))) 493 // merge refilled data and store data (if needed) 494 def mergePutData(old_data: UInt, new_data: UInt, wmask: UInt): UInt = { 495 val full_wmask = FillInterleaved(8, wmask) 496 (~full_wmask & old_data | full_wmask & new_data) 497 } 498 for (i <- 0 until blockRows) { 499 // new_data(i) := req.store_data(rowBits * (i + 1) - 1, rowBits * i) 500 new_data(i) := refill_and_store_data(i) 501 // we only need to merge data for Store 502 new_mask(i) := Mux(req.isFromStore, req_store_mask(rowBytes * (i + 1) - 1, rowBytes * i), 0.U) 503 } 504 505 val hasData = RegInit(true.B) 506 val isDirty = RegInit(false.B) 507 when (io.mem_grant.fire) { 508 w_grantfirst := true.B 509 grant_param := io.mem_grant.bits.param 510 when (edge.hasData(io.mem_grant.bits)) { 511 // GrantData 512 for (i <- 0 until beatRows) { 513 val idx = (refill_count << log2Floor(beatRows)) + i.U 514 val grant_row = io.mem_grant.bits.data(rowBits * (i + 1) - 1, rowBits * i) 515 refill_and_store_data(idx) := mergePutData(grant_row, new_data(idx), new_mask(idx)) 516 } 517 w_grantlast := w_grantlast || refill_done 518 hasData := true.B 519 }.otherwise { 520 // Grant 521 assert(full_overwrite) 522 for (i <- 0 until blockRows) { 523 refill_and_store_data(i) := new_data(i) 524 } 525 w_grantlast := true.B 526 hasData := false.B 527 } 528 529 error := io.mem_grant.bits.denied || io.mem_grant.bits.corrupt || error 530 531 refill_data_raw(refill_count) := io.mem_grant.bits.data 532 isDirty := io.mem_grant.bits.echo.lift(DirtyKey).getOrElse(false.B) 533 } 534 535 when (io.mem_finish.fire) { 536 s_grantack := true.B 537 } 538 539 when (io.replace_pipe_req.fire) { 540 s_replace_req := true.B 541 } 542 543 when (io.replace_pipe_resp) { 544 w_replace_resp := true.B 545 } 546 547 when (io.refill_pipe_req.fire) { 548 s_refill := true.B 549 } 550 551 when (io.refill_pipe_resp) { 552 w_refill_resp := true.B 553 } 554 555 when (io.main_pipe_req.fire) { 556 s_mainpipe_req := true.B 557 } 558 559 when (io.main_pipe_resp) { 560 w_mainpipe_resp := true.B 561 } 562 563 def before_req_sent_can_merge(new_req: MissReqWoStoreData): Bool = { 564 acquire_not_sent && (req.isFromLoad || req.isFromPrefetch) && (new_req.isFromLoad || new_req.isFromStore) 565 } 566 567 def before_data_refill_can_merge(new_req: MissReqWoStoreData): Bool = { 568 data_not_refilled && (req.isFromLoad || req.isFromStore || req.isFromPrefetch) && new_req.isFromLoad 569 } 570 571 // Note that late prefetch will be ignored 572 573 def should_merge(new_req: MissReqWoStoreData): Bool = { 574 val block_match = get_block(req.addr) === get_block(new_req.addr) 575 val alias_match = is_alias_match(req.vaddr, new_req.vaddr) 576 block_match && alias_match && 577 ( 578 before_req_sent_can_merge(new_req) || 579 before_data_refill_can_merge(new_req) 580 ) 581 } 582 583 // store can be merged before io.mem_acquire.fire 584 // store can not be merged the cycle that io.mem_acquire.fire 585 // load can be merged before io.mem_grant.fire 586 // 587 // TODO: merge store if possible? mem_acquire may need to be re-issued, 588 // but sbuffer entry can be freed 589 def should_reject(new_req: MissReqWoStoreData): Bool = { 590 val block_match = get_block(req.addr) === get_block(new_req.addr) 591 val set_match = set === addr_to_dcache_set(new_req.vaddr) 592 val alias_match = is_alias_match(req.vaddr, new_req.vaddr) 593 594 req_valid && 595 Mux( 596 block_match, 597 (!before_req_sent_can_merge(new_req) && !before_data_refill_can_merge(new_req)) || !alias_match, 598 set_match && new_req.way_en === req.way_en 599 ) 600 } 601 602 // req_valid will be updated 1 cycle after primary_fire, so next cycle, this entry cannot accept a new req 603 when(RegNext(io.id >= ((cfg.nMissEntries).U - io.nMaxPrefetchEntry))) { 604 // can accept prefetch req 605 io.primary_ready := !req_valid && !RegNext(primary_fire) 606 }.otherwise { 607 // cannot accept prefetch req except when a memset patten is detected 608 io.primary_ready := !req_valid && (!io.req.bits.isFromPrefetch || io.memSetPattenDetected) && !RegNext(primary_fire) 609 } 610 io.secondary_ready := should_merge(io.req.bits) 611 io.secondary_reject := should_reject(io.req.bits) 612 io.repl_way_en := req.way_en 613 614 // should not allocate, merge or reject at the same time 615 assert(RegNext(PopCount(Seq(io.primary_ready, io.secondary_ready, io.secondary_reject)) <= 1.U)) 616 617 val refill_data_splited = WireInit(VecInit(Seq.tabulate(cfg.blockBytes * 8 / l1BusDataWidth)(i => { 618 val data = refill_and_store_data.asUInt 619 data((i + 1) * l1BusDataWidth - 1, i * l1BusDataWidth) 620 }))) 621 // when granted data is all ready, wakeup lq's miss load 622 io.refill_to_ldq.valid := RegNext(!w_grantlast && io.mem_grant.fire) 623 io.refill_to_ldq.bits.addr := RegNext(req.addr + (refill_count << refillOffBits)) 624 io.refill_to_ldq.bits.data := refill_data_splited(RegNext(refill_count)) 625 io.refill_to_ldq.bits.error := RegNext(io.mem_grant.bits.corrupt || io.mem_grant.bits.denied) 626 io.refill_to_ldq.bits.refill_done := RegNext(refill_done && io.mem_grant.fire) 627 io.refill_to_ldq.bits.hasdata := hasData 628 io.refill_to_ldq.bits.data_raw := refill_data_raw.asUInt 629 io.refill_to_ldq.bits.id := io.id 630 631 // if the entry has a pending merge req, wait for it 632 // Note: now, only wait for store, because store may acquire T 633 io.mem_acquire.valid := !s_acquire && !(io.miss_req_pipe_reg.merge && miss_req_pipe_reg_bits.isFromStore) 634 val grow_param = req.req_coh.onAccess(req.cmd)._2 635 val acquireBlock = edge.AcquireBlock( 636 fromSource = io.id, 637 toAddress = req.addr, 638 lgSize = (log2Up(cfg.blockBytes)).U, 639 growPermissions = grow_param 640 )._2 641 val acquirePerm = edge.AcquirePerm( 642 fromSource = io.id, 643 toAddress = req.addr, 644 lgSize = (log2Up(cfg.blockBytes)).U, 645 growPermissions = grow_param 646 )._2 647 io.mem_acquire.bits := Mux(full_overwrite, acquirePerm, acquireBlock) 648 // resolve cache alias by L2 649 io.mem_acquire.bits.user.lift(AliasKey).foreach( _ := req.vaddr(13, 12)) 650 // pass vaddr to l2 651 io.mem_acquire.bits.user.lift(VaddrKey).foreach( _ := req.vaddr(VAddrBits-1, blockOffBits)) 652 // trigger prefetch 653 io.mem_acquire.bits.user.lift(PrefetchKey).foreach(_ := Mux(io.l2_pf_store_only, req.isFromStore, true.B)) 654 // req source 655 when(prefetch && !secondary_fired) { 656 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.L1DataPrefetch.id.U) 657 }.otherwise { 658 when(req.isFromStore) { 659 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUStoreData.id.U) 660 }.elsewhen(req.isFromLoad) { 661 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPULoadData.id.U) 662 }.elsewhen(req.isFromAMO) { 663 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUAtomicData.id.U) 664 }.otherwise { 665 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.L1DataPrefetch.id.U) 666 } 667 } 668 require(nSets <= 256) 669 670 // io.mem_grant.ready := !w_grantlast && s_acquire 671 io.mem_grant.ready := true.B 672 assert(!(io.mem_grant.valid && !(!w_grantlast && s_acquire)), "dcache should always be ready for mem_grant now") 673 674 val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire) 675 assert(RegNext(!io.mem_grant.fire || edge.isRequest(io.mem_grant.bits))) 676 io.mem_finish.valid := !s_grantack && w_grantfirst 677 io.mem_finish.bits := grantack 678 679 io.replace_pipe_req.valid := !s_replace_req 680 val replace = io.replace_pipe_req.bits 681 replace := DontCare 682 replace.miss := false.B 683 replace.miss_id := io.id 684 replace.miss_dirty := false.B 685 replace.probe := false.B 686 replace.probe_need_data := false.B 687 replace.source := LOAD_SOURCE.U 688 replace.vaddr := req.vaddr // only untag bits are needed 689 replace.addr := Cat(req.replace_tag, 0.U(pgUntagBits.W)) // only tag bits are needed 690 replace.store_mask := 0.U 691 replace.replace := true.B 692 replace.replace_way_en := req.way_en 693 replace.error := false.B 694 695 io.refill_pipe_req.valid := !s_refill && w_replace_resp && w_grantlast 696 val refill = io.refill_pipe_req.bits 697 refill.source := req.source 698 refill.vaddr := req.vaddr 699 refill.addr := req.addr 700 refill.way_en := req.way_en 701 refill.wmask := Mux( 702 hasData || req.isFromLoad, 703 ~0.U(DCacheBanks.W), 704 VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, req_store_mask).orR)).asUInt 705 ) 706 refill.data := refill_and_store_data.asTypeOf((new RefillPipeReq).data) 707 refill.miss_id := io.id 708 refill.id := req.id 709 def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = { 710 val c = categorize(cmd) 711 MuxLookup(Cat(c, param, dirty), Nothing, Seq( 712 //(effect param) -> (next) 713 Cat(rd, toB, false.B) -> Branch, 714 Cat(rd, toB, true.B) -> Branch, 715 Cat(rd, toT, false.B) -> Trunk, 716 Cat(rd, toT, true.B) -> Dirty, 717 Cat(wi, toT, false.B) -> Trunk, 718 Cat(wi, toT, true.B) -> Dirty, 719 Cat(wr, toT, false.B) -> Dirty, 720 Cat(wr, toT, true.B) -> Dirty)) 721 } 722 refill.meta.coh := ClientMetadata(missCohGen(req.cmd, grant_param, isDirty)) 723 refill.error := error 724 refill.prefetch := req.pf_source 725 refill.access := access 726 refill.alias := req.vaddr(13, 12) // TODO 727 assert(!io.refill_pipe_req.valid || (refill.meta.coh =/= ClientMetadata(Nothing)), "refill modifies meta to Nothing, should not happen") 728 729 io.sms_agt_evict_req.valid := io.refill_pipe_req.fire && should_replace && req_valid 730 io.sms_agt_evict_req.bits.vaddr := Cat(req.replace_tag(tagBits - 1, 2), req.vaddr(13, 12), 0.U((VAddrBits - tagBits).W)) 731 732 io.main_pipe_req.valid := !s_mainpipe_req && w_grantlast 733 io.main_pipe_req.bits := DontCare 734 io.main_pipe_req.bits.miss := true.B 735 io.main_pipe_req.bits.miss_id := io.id 736 io.main_pipe_req.bits.miss_param := grant_param 737 io.main_pipe_req.bits.miss_dirty := isDirty 738 io.main_pipe_req.bits.miss_way_en := req.way_en 739 io.main_pipe_req.bits.probe := false.B 740 io.main_pipe_req.bits.source := req.source 741 io.main_pipe_req.bits.cmd := req.cmd 742 io.main_pipe_req.bits.vaddr := req.vaddr 743 io.main_pipe_req.bits.addr := req.addr 744 io.main_pipe_req.bits.store_data := refill_and_store_data.asUInt 745 io.main_pipe_req.bits.store_mask := ~0.U(blockBytes.W) 746 io.main_pipe_req.bits.word_idx := req.word_idx 747 io.main_pipe_req.bits.amo_data := req.amo_data 748 io.main_pipe_req.bits.amo_mask := req.amo_mask 749 io.main_pipe_req.bits.error := error 750 io.main_pipe_req.bits.id := req.id 751 752 io.block_addr.valid := req_valid && w_grantlast && !w_refill_resp 753 io.block_addr.bits := req.addr 754 755 io.debug_early_replace.valid := BoolStopWatch(io.replace_pipe_resp, io.refill_pipe_req.fire) 756 io.debug_early_replace.bits.idx := addr_to_dcache_set(req.vaddr) 757 io.debug_early_replace.bits.tag := req.replace_tag 758 759 io.forwardInfo.apply(req_valid, req.addr, refill_and_store_data, w_grantfirst, w_grantlast) 760 761 io.matched := req_valid && (get_block(req.addr) === get_block(io.req.bits.addr)) && !prefetch 762 io.prefetch_info.late_prefetch := io.req.valid && !(io.req.bits.isFromPrefetch) && req_valid && (get_block(req.addr) === get_block(io.req.bits.addr)) && prefetch 763 764 when(io.prefetch_info.late_prefetch) { 765 prefetch := false.B 766 } 767 768 // refill latency monitor 769 val start_counting = RegNext(io.mem_acquire.fire) || (RegNextN(primary_fire, 2) && s_acquire) 770 io.latency_monitor.load_miss_refilling := req_valid && req_primary_fire.isFromLoad && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 771 io.latency_monitor.store_miss_refilling := req_valid && req_primary_fire.isFromStore && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 772 io.latency_monitor.amo_miss_refilling := req_valid && req_primary_fire.isFromAMO && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 773 io.latency_monitor.pf_miss_refilling := req_valid && req_primary_fire.isFromPrefetch && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 774 775 XSPerfAccumulate("miss_req_primary", primary_fire) 776 XSPerfAccumulate("miss_req_merged", secondary_fire) 777 XSPerfAccumulate("load_miss_penalty_to_use", 778 should_refill_data && 779 BoolStopWatch(primary_fire, io.refill_to_ldq.valid, true) 780 ) 781 XSPerfAccumulate("main_pipe_penalty", BoolStopWatch(io.main_pipe_req.fire, io.main_pipe_resp)) 782 XSPerfAccumulate("penalty_blocked_by_channel_A", io.mem_acquire.valid && !io.mem_acquire.ready) 783 XSPerfAccumulate("penalty_waiting_for_channel_D", s_acquire && !w_grantlast && !io.mem_grant.valid) 784 XSPerfAccumulate("penalty_waiting_for_channel_E", io.mem_finish.valid && !io.mem_finish.ready) 785 XSPerfAccumulate("penalty_from_grant_to_refill", !w_refill_resp && w_grantlast) 786 XSPerfAccumulate("prefetch_req_primary", primary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 787 XSPerfAccumulate("prefetch_req_merged", secondary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 788 XSPerfAccumulate("can_not_send_acquire_because_of_merging_store", !s_acquire && io.miss_req_pipe_reg.merge && miss_req_pipe_reg_bits.isFromStore) 789 790 val (mshr_penalty_sample, mshr_penalty) = TransactionLatencyCounter(RegNext(RegNext(primary_fire)), release_entry) 791 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 0, 20, 1, true, true) 792 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 20, 100, 10, true, false) 793 794 val load_miss_begin = primary_fire && io.req.bits.isFromLoad 795 val refill_finished = RegNext(!w_grantlast && refill_done) && should_refill_data 796 val (load_miss_penalty_sample, load_miss_penalty) = TransactionLatencyCounter(load_miss_begin, refill_finished) // not real refill finish time 797 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 0, 20, 1, true, true) 798 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 20, 100, 10, true, false) 799 800 val (a_to_d_penalty_sample, a_to_d_penalty) = TransactionLatencyCounter(start_counting, RegNext(io.mem_grant.fire && refill_done)) 801 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 0, 20, 1, true, true) 802 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 20, 100, 10, true, false) 803} 804 805class MissQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasPerfEvents { 806 val io = IO(new Bundle { 807 val hartId = Input(UInt(8.W)) 808 val req = Flipped(DecoupledIO(new MissReq)) 809 val resp = Output(new MissResp) 810 val refill_to_ldq = ValidIO(new Refill) 811 812 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 813 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 814 val mem_finish = DecoupledIO(new TLBundleE(edge.bundle)) 815 816 val refill_pipe_req = DecoupledIO(new RefillPipeReq) 817 val refill_pipe_req_dup = Vec(nDupStatus, DecoupledIO(new RefillPipeReqCtrl)) 818 val refill_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 819 820 val replace_pipe_req = DecoupledIO(new MainPipeReq) 821 val replace_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 822 823 val main_pipe_req = DecoupledIO(new MainPipeReq) 824 val main_pipe_resp = Flipped(ValidIO(new AtomicsResp)) 825 826 // block probe 827 val probe_addr = Input(UInt(PAddrBits.W)) 828 val probe_block = Output(Bool()) 829 830 val full = Output(Bool()) 831 832 // only for performance counter 833 // This is valid when an mshr has finished replacing a block (w_replace_resp), 834 // but hasn't received Grant from L2 (!w_grantlast) 835 val debug_early_replace = Vec(cfg.nMissEntries, ValidIO(new Bundle() { 836 // info about the block that has been replaced 837 val idx = UInt(idxBits.W) // vaddr 838 val tag = UInt(tagBits.W) // paddr 839 })) 840 841 val sms_agt_evict_req = DecoupledIO(new AGTEvictReq) 842 843 // forward missqueue 844 val forward = Vec(LoadPipelineWidth, new LduToMissqueueForwardIO) 845 val l2_pf_store_only = Input(Bool()) 846 847 val memSetPattenDetected = Output(Bool()) 848 val lqEmpty = Input(Bool()) 849 850 val prefetch_info = new Bundle { 851 val naive = new Bundle { 852 val late_miss_prefetch = Output(Bool()) 853 } 854 855 val fdp = new Bundle { 856 val late_miss_prefetch = Output(Bool()) 857 val prefetch_monitor_cnt = Output(Bool()) 858 val total_prefetch = Output(Bool()) 859 } 860 } 861 862 val bloom_filter_query = new Bundle { 863 val set = ValidIO(new BloomQueryBundle(BLOOM_FILTER_ENTRY_NUM)) 864 val clr = ValidIO(new BloomQueryBundle(BLOOM_FILTER_ENTRY_NUM)) 865 } 866 867 val mq_enq_cancel = Output(Bool()) 868 869 val debugTopDown = new DCacheTopDownIO 870 }) 871 872 // 128KBL1: FIXME: provide vaddr for l2 873 874 val entries = Seq.fill(cfg.nMissEntries)(Module(new MissEntry(edge))) 875 876 val miss_req_pipe_reg = RegInit(0.U.asTypeOf(new MissReqPipeRegBundle(edge))) 877 val acquire_from_pipereg = Wire(chiselTypeOf(io.mem_acquire)) 878 879 val primary_ready_vec = entries.map(_.io.primary_ready) 880 val secondary_ready_vec = entries.map(_.io.secondary_ready) 881 val secondary_reject_vec = entries.map(_.io.secondary_reject) 882 val probe_block_vec = entries.map { case e => e.io.block_addr.valid && e.io.block_addr.bits === io.probe_addr } 883 884 val merge = ParallelORR(Cat(secondary_ready_vec ++ Seq(miss_req_pipe_reg.merge_req(io.req.bits)))) 885 val reject = ParallelORR(Cat(secondary_reject_vec ++ Seq(miss_req_pipe_reg.reject_req(io.req.bits)))) 886 val alloc = !reject && !merge && ParallelORR(Cat(primary_ready_vec)) 887 val accept = alloc || merge 888 889 val req_mshr_handled_vec = entries.map(_.io.req_handled_by_this_entry) 890 // merged to pipeline reg 891 val req_pipeline_reg_handled = miss_req_pipe_reg.merge_req(io.req.bits) 892 assert(PopCount(Seq(req_pipeline_reg_handled, VecInit(req_mshr_handled_vec).asUInt.orR)) <= 1.U, "miss req will either go to mshr or pipeline reg") 893 assert(PopCount(req_mshr_handled_vec) <= 1.U, "Only one mshr can handle a req") 894 io.resp.id := Mux(!req_pipeline_reg_handled, OHToUInt(req_mshr_handled_vec), miss_req_pipe_reg.mshr_id) 895 io.resp.handled := Cat(req_mshr_handled_vec).orR || req_pipeline_reg_handled 896 io.resp.merged := merge 897 io.resp.repl_way_en := Mux(!req_pipeline_reg_handled, Mux1H(secondary_ready_vec, entries.map(_.io.repl_way_en)), miss_req_pipe_reg.req.way_en) 898 899 /* MissQueue enq logic is now splitted into 2 cycles 900 * 901 */ 902 miss_req_pipe_reg.req := io.req.bits 903 miss_req_pipe_reg.alloc := alloc && io.req.valid && !io.req.bits.cancel 904 miss_req_pipe_reg.merge := merge && io.req.valid && !io.req.bits.cancel 905 miss_req_pipe_reg.mshr_id := io.resp.id 906 907 assert(PopCount(Seq(alloc && io.req.valid, merge && io.req.valid)) <= 1.U, "allocate and merge a mshr in same cycle!") 908 909 val source_except_load_cnt = RegInit(0.U(10.W)) 910 when(VecInit(req_mshr_handled_vec).asUInt.orR || req_pipeline_reg_handled) { 911 when(io.req.bits.isFromLoad) { 912 source_except_load_cnt := 0.U 913 }.otherwise { 914 when(io.req.bits.isFromStore) { 915 source_except_load_cnt := source_except_load_cnt + 1.U 916 } 917 } 918 } 919 val Threshold = 8 920 val memSetPattenDetected = RegNext((source_except_load_cnt >= Threshold.U) && io.lqEmpty) 921 922 io.memSetPattenDetected := memSetPattenDetected 923 924 val forwardInfo_vec = VecInit(entries.map(_.io.forwardInfo)) 925 (0 until LoadPipelineWidth).map(i => { 926 val id = io.forward(i).mshrid 927 val req_valid = io.forward(i).valid 928 val paddr = io.forward(i).paddr 929 930 val (forward_mshr, forwardData) = forwardInfo_vec(id).forward(req_valid, paddr) 931 io.forward(i).forward_result_valid := forwardInfo_vec(id).check(req_valid, paddr) 932 io.forward(i).forward_mshr := forward_mshr 933 io.forward(i).forwardData := forwardData 934 }) 935 936 assert(RegNext(PopCount(secondary_ready_vec) <= 1.U)) 937// assert(RegNext(PopCount(secondary_reject_vec) <= 1.U)) 938 // It is possible that one mshr wants to merge a req, while another mshr wants to reject it. 939 // That is, a coming req has the same paddr as that of mshr_0 (merge), 940 // while it has the same set and the same way as mshr_1 (reject). 941 // In this situation, the coming req should be merged by mshr_0 942// assert(RegNext(PopCount(Seq(merge, reject)) <= 1.U)) 943 944 def select_valid_one[T <: Bundle]( 945 in: Seq[DecoupledIO[T]], 946 out: DecoupledIO[T], 947 name: Option[String] = None): Unit = { 948 949 if (name.nonEmpty) { out.suggestName(s"${name.get}_select") } 950 out.valid := Cat(in.map(_.valid)).orR 951 out.bits := ParallelMux(in.map(_.valid) zip in.map(_.bits)) 952 in.map(_.ready := out.ready) 953 assert(!RegNext(out.valid && PopCount(Cat(in.map(_.valid))) > 1.U)) 954 } 955 956 io.mem_grant.ready := false.B 957 958 val nMaxPrefetchEntry = WireInit(Constantin.createRecord("nMaxPrefetchEntry" + p(XSCoreParamsKey).HartId.toString, initValue = 14.U)) 959 entries.zipWithIndex.foreach { 960 case (e, i) => 961 val former_primary_ready = if(i == 0) 962 false.B 963 else 964 Cat((0 until i).map(j => entries(j).io.primary_ready)).orR 965 966 e.io.hartId := io.hartId 967 e.io.id := i.U 968 e.io.l2_pf_store_only := io.l2_pf_store_only 969 e.io.req.valid := io.req.valid 970 e.io.primary_valid := io.req.valid && 971 !merge && 972 !reject && 973 !former_primary_ready && 974 e.io.primary_ready 975 e.io.req.bits := io.req.bits.toMissReqWoStoreData() 976 977 e.io.mem_grant.valid := false.B 978 e.io.mem_grant.bits := DontCare 979 when (io.mem_grant.bits.source === i.U) { 980 e.io.mem_grant <> io.mem_grant 981 } 982 983 when(miss_req_pipe_reg.reg_valid() && miss_req_pipe_reg.mshr_id === i.U) { 984 e.io.miss_req_pipe_reg := miss_req_pipe_reg 985 }.otherwise { 986 e.io.miss_req_pipe_reg := DontCare 987 e.io.miss_req_pipe_reg.merge := false.B 988 e.io.miss_req_pipe_reg.alloc := false.B 989 } 990 991 e.io.acquire_fired_by_pipe_reg := acquire_from_pipereg.fire 992 993 e.io.refill_pipe_resp := io.refill_pipe_resp.valid && io.refill_pipe_resp.bits === i.U 994 e.io.replace_pipe_resp := io.replace_pipe_resp.valid && io.replace_pipe_resp.bits === i.U 995 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 996 997 e.io.memSetPattenDetected := memSetPattenDetected 998 e.io.nMaxPrefetchEntry := nMaxPrefetchEntry 999 1000 io.debug_early_replace(i) := e.io.debug_early_replace 1001 e.io.main_pipe_req.ready := io.main_pipe_req.ready 1002 } 1003 1004 io.req.ready := accept 1005 io.mq_enq_cancel := io.req.bits.cancel 1006 io.refill_to_ldq.valid := Cat(entries.map(_.io.refill_to_ldq.valid)).orR 1007 io.refill_to_ldq.bits := ParallelMux(entries.map(_.io.refill_to_ldq.valid) zip entries.map(_.io.refill_to_ldq.bits)) 1008 1009 acquire_from_pipereg.valid := miss_req_pipe_reg.can_send_acquire(io.req.valid, io.req.bits) 1010 acquire_from_pipereg.bits := miss_req_pipe_reg.get_acquire(io.l2_pf_store_only) 1011 1012 XSPerfAccumulate("acquire_fire_from_pipereg", acquire_from_pipereg.fire) 1013 XSPerfAccumulate("pipereg_valid", miss_req_pipe_reg.reg_valid()) 1014 1015 val acquire_sources = Seq(acquire_from_pipereg) ++ entries.map(_.io.mem_acquire) 1016 TLArbiter.lowest(edge, io.mem_acquire, acquire_sources:_*) 1017 TLArbiter.lowest(edge, io.mem_finish, entries.map(_.io.mem_finish):_*) 1018 1019 // arbiter_with_pipereg_N_dup(entries.map(_.io.refill_pipe_req), io.refill_pipe_req, 1020 // io.refill_pipe_req_dup, 1021 // Some("refill_pipe_req")) 1022 val out_refill_pipe_req = Wire(Decoupled(new RefillPipeReq)) 1023 val out_refill_pipe_req_ctrl = Wire(Decoupled(new RefillPipeReqCtrl)) 1024 out_refill_pipe_req_ctrl.valid := out_refill_pipe_req.valid 1025 out_refill_pipe_req_ctrl.bits := out_refill_pipe_req.bits.getCtrl 1026 out_refill_pipe_req.ready := out_refill_pipe_req_ctrl.ready 1027 arbiter(entries.map(_.io.refill_pipe_req), out_refill_pipe_req, Some("refill_pipe_req")) 1028 for (dup <- io.refill_pipe_req_dup) { 1029 AddPipelineReg(out_refill_pipe_req_ctrl, dup, false.B) 1030 } 1031 AddPipelineReg(out_refill_pipe_req, io.refill_pipe_req, false.B) 1032 1033 arbiter_with_pipereg(entries.map(_.io.replace_pipe_req), io.replace_pipe_req, Some("replace_pipe_req")) 1034 1035 // amo's main pipe req out 1036 val main_pipe_req_vec = entries.map(_.io.main_pipe_req) 1037 io.main_pipe_req.valid := VecInit(main_pipe_req_vec.map(_.valid)).asUInt.orR 1038 io.main_pipe_req.bits := Mux1H(main_pipe_req_vec.map(_.valid), main_pipe_req_vec.map(_.bits)) 1039 assert(PopCount(VecInit(main_pipe_req_vec.map(_.valid))) <= 1.U, "multi main pipe req") 1040 1041 // send evict hint to sms 1042 val sms_agt_evict_valid = Cat(entries.map(_.io.sms_agt_evict_req.valid)).orR 1043 val sms_agt_evict_valid_reg = RegInit(false.B) 1044 io.sms_agt_evict_req.valid := sms_agt_evict_valid_reg 1045 io.sms_agt_evict_req.bits := RegEnable(Mux1H(entries.map(_.io.sms_agt_evict_req.valid), entries.map(_.io.sms_agt_evict_req.bits)), sms_agt_evict_valid) 1046 when(sms_agt_evict_valid) { 1047 sms_agt_evict_valid_reg := true.B 1048 }.elsewhen(io.sms_agt_evict_req.fire) { 1049 sms_agt_evict_valid_reg := false.B 1050 } 1051 assert(PopCount(VecInit(entries.map(_.io.sms_agt_evict_req.valid))) <= 1.U, "multi sms_agt_evict req") 1052 1053 io.probe_block := Cat(probe_block_vec).orR 1054 1055 io.full := ~Cat(entries.map(_.io.primary_ready)).andR 1056 1057 // prefetch related 1058 io.prefetch_info.naive.late_miss_prefetch := io.req.valid && io.req.bits.isPrefetchRead && (miss_req_pipe_reg.matched(io.req.bits) || Cat(entries.map(_.io.matched)).orR) 1059 1060 io.prefetch_info.fdp.late_miss_prefetch := (miss_req_pipe_reg.prefetch_late_en(io.req.bits.toMissReqWoStoreData(), io.req.valid) || Cat(entries.map(_.io.prefetch_info.late_prefetch)).orR) 1061 io.prefetch_info.fdp.prefetch_monitor_cnt := io.refill_pipe_req.fire 1062 io.prefetch_info.fdp.total_prefetch := alloc && io.req.valid && !io.req.bits.cancel && isFromL1Prefetch(io.req.bits.pf_source) 1063 1064 io.bloom_filter_query.set.valid := alloc && io.req.valid && !io.req.bits.cancel && !isFromL1Prefetch(io.req.bits.replace_pf) && io.req.bits.replace_coh.isValid() && isFromL1Prefetch(io.req.bits.pf_source) 1065 io.bloom_filter_query.set.bits.addr := io.bloom_filter_query.set.bits.get_addr(Cat(io.req.bits.replace_tag, get_untag(io.req.bits.vaddr))) // the evict block address 1066 1067 io.bloom_filter_query.clr.valid := io.refill_pipe_req.fire && isFromL1Prefetch(io.refill_pipe_req.bits.prefetch) 1068 io.bloom_filter_query.clr.bits.addr := io.bloom_filter_query.clr.bits.get_addr(io.refill_pipe_req.bits.addr) 1069 1070 // L1MissTrace Chisel DB 1071 val debug_miss_trace = Wire(new L1MissTrace) 1072 debug_miss_trace.vaddr := io.req.bits.vaddr 1073 debug_miss_trace.paddr := io.req.bits.addr 1074 debug_miss_trace.source := io.req.bits.source 1075 debug_miss_trace.pc := io.req.bits.pc 1076 1077 val isWriteL1MissQMissTable = WireInit(Constantin.createRecord("isWriteL1MissQMissTable" + p(XSCoreParamsKey).HartId.toString)) 1078 val table = ChiselDB.createTable("L1MissQMissTrace_hart"+ p(XSCoreParamsKey).HartId.toString, new L1MissTrace) 1079 table.log(debug_miss_trace, isWriteL1MissQMissTable.orR && io.req.valid && !io.req.bits.cancel && alloc, "MissQueue", clock, reset) 1080 1081 // Difftest 1082 if (env.EnableDifftest) { 1083 val difftest = DifftestModule(new DiffRefillEvent, dontCare = true) 1084 difftest.coreid := io.hartId 1085 difftest.index := 1.U 1086 difftest.valid := io.refill_to_ldq.valid && io.refill_to_ldq.bits.hasdata && io.refill_to_ldq.bits.refill_done 1087 difftest.addr := io.refill_to_ldq.bits.addr 1088 difftest.data := io.refill_to_ldq.bits.data_raw.asTypeOf(difftest.data) 1089 difftest.idtfr := DontCare 1090 } 1091 1092 // Perf count 1093 XSPerfAccumulate("miss_req", io.req.fire && !io.req.bits.cancel) 1094 XSPerfAccumulate("miss_req_allocate", io.req.fire && !io.req.bits.cancel && alloc) 1095 XSPerfAccumulate("miss_req_load_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromLoad) 1096 XSPerfAccumulate("miss_req_store_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromStore) 1097 XSPerfAccumulate("miss_req_amo_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromAMO) 1098 XSPerfAccumulate("miss_req_merge_load", io.req.fire && !io.req.bits.cancel && merge && io.req.bits.isFromLoad) 1099 XSPerfAccumulate("miss_req_reject_load", io.req.valid && !io.req.bits.cancel && reject && io.req.bits.isFromLoad) 1100 XSPerfAccumulate("probe_blocked_by_miss", io.probe_block) 1101 XSPerfAccumulate("prefetch_primary_fire", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromPrefetch) 1102 XSPerfAccumulate("prefetch_secondary_fire", io.req.fire && !io.req.bits.cancel && merge && io.req.bits.isFromPrefetch) 1103 XSPerfAccumulate("memSetPattenDetected", memSetPattenDetected) 1104 val max_inflight = RegInit(0.U((log2Up(cfg.nMissEntries) + 1).W)) 1105 val num_valids = PopCount(~Cat(primary_ready_vec).asUInt) 1106 when (num_valids > max_inflight) { 1107 max_inflight := num_valids 1108 } 1109 // max inflight (average) = max_inflight_total / cycle cnt 1110 XSPerfAccumulate("max_inflight", max_inflight) 1111 QueuePerf(cfg.nMissEntries, num_valids, num_valids === cfg.nMissEntries.U) 1112 io.full := num_valids === cfg.nMissEntries.U 1113 XSPerfHistogram("num_valids", num_valids, true.B, 0, cfg.nMissEntries, 1) 1114 1115 XSPerfHistogram("L1DMLP_CPUData", PopCount(VecInit(entries.map(_.io.perf_pending_normal)).asUInt), true.B, 0, cfg.nMissEntries, 1) 1116 XSPerfHistogram("L1DMLP_Prefetch", PopCount(VecInit(entries.map(_.io.perf_pending_prefetch)).asUInt), true.B, 0, cfg.nMissEntries, 1) 1117 XSPerfHistogram("L1DMLP_Total", num_valids, true.B, 0, cfg.nMissEntries, 1) 1118 1119 XSPerfAccumulate("miss_load_refill_latency", PopCount(entries.map(_.io.latency_monitor.load_miss_refilling))) 1120 XSPerfAccumulate("miss_store_refill_latency", PopCount(entries.map(_.io.latency_monitor.store_miss_refilling))) 1121 XSPerfAccumulate("miss_amo_refill_latency", PopCount(entries.map(_.io.latency_monitor.amo_miss_refilling))) 1122 XSPerfAccumulate("miss_pf_refill_latency", PopCount(entries.map(_.io.latency_monitor.pf_miss_refilling))) 1123 1124 val rob_head_miss_in_dcache = VecInit(entries.map(_.io.rob_head_query.resp)).asUInt.orR 1125 1126 entries.foreach { 1127 case e => { 1128 e.io.rob_head_query.query_valid := io.debugTopDown.robHeadVaddr.valid 1129 e.io.rob_head_query.vaddr := io.debugTopDown.robHeadVaddr.bits 1130 } 1131 } 1132 1133 io.debugTopDown.robHeadMissInDCache := rob_head_miss_in_dcache 1134 1135 val perfValidCount = RegNext(PopCount(entries.map(entry => (!entry.io.primary_ready)))) 1136 val perfEvents = Seq( 1137 ("dcache_missq_req ", io.req.fire), 1138 ("dcache_missq_1_4_valid", (perfValidCount < (cfg.nMissEntries.U/4.U))), 1139 ("dcache_missq_2_4_valid", (perfValidCount > (cfg.nMissEntries.U/4.U)) & (perfValidCount <= (cfg.nMissEntries.U/2.U))), 1140 ("dcache_missq_3_4_valid", (perfValidCount > (cfg.nMissEntries.U/2.U)) & (perfValidCount <= (cfg.nMissEntries.U*3.U/4.U))), 1141 ("dcache_missq_4_4_valid", (perfValidCount > (cfg.nMissEntries.U*3.U/4.U))), 1142 ) 1143 generatePerfEvent() 1144} 1145