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 672 val grantack = RegEnable(edge.GrantAck(io.mem_grant.bits), io.mem_grant.fire) 673 assert(RegNext(!io.mem_grant.fire || edge.isRequest(io.mem_grant.bits))) 674 io.mem_finish.valid := !s_grantack && w_grantfirst 675 io.mem_finish.bits := grantack 676 677 io.replace_pipe_req.valid := !s_replace_req 678 val replace = io.replace_pipe_req.bits 679 replace := DontCare 680 replace.miss := false.B 681 replace.miss_id := io.id 682 replace.miss_dirty := false.B 683 replace.probe := false.B 684 replace.probe_need_data := false.B 685 replace.source := LOAD_SOURCE.U 686 replace.vaddr := req.vaddr // only untag bits are needed 687 replace.addr := Cat(req.replace_tag, 0.U(pgUntagBits.W)) // only tag bits are needed 688 replace.store_mask := 0.U 689 replace.replace := true.B 690 replace.replace_way_en := req.way_en 691 replace.error := false.B 692 693 io.refill_pipe_req.valid := !s_refill && w_replace_resp && w_grantlast 694 val refill = io.refill_pipe_req.bits 695 refill.source := req.source 696 refill.vaddr := req.vaddr 697 refill.addr := req.addr 698 refill.way_en := req.way_en 699 refill.wmask := Mux( 700 hasData || req.isFromLoad, 701 ~0.U(DCacheBanks.W), 702 VecInit((0 until DCacheBanks).map(i => get_mask_of_bank(i, req_store_mask).orR)).asUInt 703 ) 704 refill.data := refill_and_store_data.asTypeOf((new RefillPipeReq).data) 705 refill.miss_id := io.id 706 refill.id := req.id 707 def missCohGen(cmd: UInt, param: UInt, dirty: Bool) = { 708 val c = categorize(cmd) 709 MuxLookup(Cat(c, param, dirty), Nothing, Seq( 710 //(effect param) -> (next) 711 Cat(rd, toB, false.B) -> Branch, 712 Cat(rd, toB, true.B) -> Branch, 713 Cat(rd, toT, false.B) -> Trunk, 714 Cat(rd, toT, true.B) -> Dirty, 715 Cat(wi, toT, false.B) -> Trunk, 716 Cat(wi, toT, true.B) -> Dirty, 717 Cat(wr, toT, false.B) -> Dirty, 718 Cat(wr, toT, true.B) -> Dirty)) 719 } 720 refill.meta.coh := ClientMetadata(missCohGen(req.cmd, grant_param, isDirty)) 721 refill.error := error 722 refill.prefetch := req.pf_source 723 refill.access := access 724 refill.alias := req.vaddr(13, 12) // TODO 725 assert(!io.refill_pipe_req.valid || (refill.meta.coh =/= ClientMetadata(Nothing)), "refill modifies meta to Nothing, should not happen") 726 727 io.sms_agt_evict_req.valid := io.refill_pipe_req.fire && should_replace && req_valid 728 io.sms_agt_evict_req.bits.vaddr := Cat(req.replace_tag(tagBits - 1, 2), req.vaddr(13, 12), 0.U((VAddrBits - tagBits).W)) 729 730 io.main_pipe_req.valid := !s_mainpipe_req && w_grantlast 731 io.main_pipe_req.bits := DontCare 732 io.main_pipe_req.bits.miss := true.B 733 io.main_pipe_req.bits.miss_id := io.id 734 io.main_pipe_req.bits.miss_param := grant_param 735 io.main_pipe_req.bits.miss_dirty := isDirty 736 io.main_pipe_req.bits.miss_way_en := req.way_en 737 io.main_pipe_req.bits.probe := false.B 738 io.main_pipe_req.bits.source := req.source 739 io.main_pipe_req.bits.cmd := req.cmd 740 io.main_pipe_req.bits.vaddr := req.vaddr 741 io.main_pipe_req.bits.addr := req.addr 742 io.main_pipe_req.bits.store_data := refill_and_store_data.asUInt 743 io.main_pipe_req.bits.store_mask := ~0.U(blockBytes.W) 744 io.main_pipe_req.bits.word_idx := req.word_idx 745 io.main_pipe_req.bits.amo_data := req.amo_data 746 io.main_pipe_req.bits.amo_mask := req.amo_mask 747 io.main_pipe_req.bits.error := error 748 io.main_pipe_req.bits.id := req.id 749 750 io.block_addr.valid := req_valid && w_grantlast && !w_refill_resp 751 io.block_addr.bits := req.addr 752 753 io.debug_early_replace.valid := BoolStopWatch(io.replace_pipe_resp, io.refill_pipe_req.fire) 754 io.debug_early_replace.bits.idx := addr_to_dcache_set(req.vaddr) 755 io.debug_early_replace.bits.tag := req.replace_tag 756 757 io.forwardInfo.apply(req_valid, req.addr, refill_and_store_data, w_grantfirst, w_grantlast) 758 759 io.matched := req_valid && (get_block(req.addr) === get_block(io.req.bits.addr)) && !prefetch 760 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 761 762 when(io.prefetch_info.late_prefetch) { 763 prefetch := false.B 764 } 765 766 // refill latency monitor 767 val start_counting = RegNext(io.mem_acquire.fire) || (RegNextN(primary_fire, 2) && s_acquire) 768 io.latency_monitor.load_miss_refilling := req_valid && req_primary_fire.isFromLoad && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 769 io.latency_monitor.store_miss_refilling := req_valid && req_primary_fire.isFromStore && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 770 io.latency_monitor.amo_miss_refilling := req_valid && req_primary_fire.isFromAMO && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 771 io.latency_monitor.pf_miss_refilling := req_valid && req_primary_fire.isFromPrefetch && BoolStopWatch(start_counting, io.mem_grant.fire && !refill_done, true, true) 772 773 XSPerfAccumulate("miss_req_primary", primary_fire) 774 XSPerfAccumulate("miss_req_merged", secondary_fire) 775 XSPerfAccumulate("load_miss_penalty_to_use", 776 should_refill_data && 777 BoolStopWatch(primary_fire, io.refill_to_ldq.valid, true) 778 ) 779 XSPerfAccumulate("main_pipe_penalty", BoolStopWatch(io.main_pipe_req.fire, io.main_pipe_resp)) 780 XSPerfAccumulate("penalty_blocked_by_channel_A", io.mem_acquire.valid && !io.mem_acquire.ready) 781 XSPerfAccumulate("penalty_waiting_for_channel_D", s_acquire && !w_grantlast && !io.mem_grant.valid) 782 XSPerfAccumulate("penalty_waiting_for_channel_E", io.mem_finish.valid && !io.mem_finish.ready) 783 XSPerfAccumulate("penalty_from_grant_to_refill", !w_refill_resp && w_grantlast) 784 XSPerfAccumulate("prefetch_req_primary", primary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 785 XSPerfAccumulate("prefetch_req_merged", secondary_fire && io.req.bits.source === DCACHE_PREFETCH_SOURCE.U) 786 XSPerfAccumulate("can_not_send_acquire_because_of_merging_store", !s_acquire && io.miss_req_pipe_reg.merge && miss_req_pipe_reg_bits.isFromStore) 787 788 val (mshr_penalty_sample, mshr_penalty) = TransactionLatencyCounter(RegNext(RegNext(primary_fire)), release_entry) 789 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 0, 20, 1, true, true) 790 XSPerfHistogram("miss_penalty", mshr_penalty, mshr_penalty_sample, 20, 100, 10, true, false) 791 792 val load_miss_begin = primary_fire && io.req.bits.isFromLoad 793 val refill_finished = RegNext(!w_grantlast && refill_done) && should_refill_data 794 val (load_miss_penalty_sample, load_miss_penalty) = TransactionLatencyCounter(load_miss_begin, refill_finished) // not real refill finish time 795 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 0, 20, 1, true, true) 796 XSPerfHistogram("load_miss_penalty_to_use", load_miss_penalty, load_miss_penalty_sample, 20, 100, 10, true, false) 797 798 val (a_to_d_penalty_sample, a_to_d_penalty) = TransactionLatencyCounter(start_counting, RegNext(io.mem_grant.fire && refill_done)) 799 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 0, 20, 1, true, true) 800 XSPerfHistogram("a_to_d_penalty", a_to_d_penalty, a_to_d_penalty_sample, 20, 100, 10, true, false) 801} 802 803class MissQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasPerfEvents { 804 val io = IO(new Bundle { 805 val hartId = Input(UInt(8.W)) 806 val req = Flipped(DecoupledIO(new MissReq)) 807 val resp = Output(new MissResp) 808 val refill_to_ldq = ValidIO(new Refill) 809 810 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 811 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 812 val mem_finish = DecoupledIO(new TLBundleE(edge.bundle)) 813 814 val refill_pipe_req = DecoupledIO(new RefillPipeReq) 815 val refill_pipe_req_dup = Vec(nDupStatus, DecoupledIO(new RefillPipeReqCtrl)) 816 val refill_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 817 818 val replace_pipe_req = DecoupledIO(new MainPipeReq) 819 val replace_pipe_resp = Flipped(ValidIO(UInt(log2Up(cfg.nMissEntries).W))) 820 821 val main_pipe_req = DecoupledIO(new MainPipeReq) 822 val main_pipe_resp = Flipped(ValidIO(new AtomicsResp)) 823 824 // block probe 825 val probe_addr = Input(UInt(PAddrBits.W)) 826 val probe_block = Output(Bool()) 827 828 val full = Output(Bool()) 829 830 // only for performance counter 831 // This is valid when an mshr has finished replacing a block (w_replace_resp), 832 // but hasn't received Grant from L2 (!w_grantlast) 833 val debug_early_replace = Vec(cfg.nMissEntries, ValidIO(new Bundle() { 834 // info about the block that has been replaced 835 val idx = UInt(idxBits.W) // vaddr 836 val tag = UInt(tagBits.W) // paddr 837 })) 838 839 val sms_agt_evict_req = DecoupledIO(new AGTEvictReq) 840 841 // forward missqueue 842 val forward = Vec(LoadPipelineWidth, new LduToMissqueueForwardIO) 843 val l2_pf_store_only = Input(Bool()) 844 845 val memSetPattenDetected = Output(Bool()) 846 val lqEmpty = Input(Bool()) 847 848 val prefetch_info = new Bundle { 849 val naive = new Bundle { 850 val late_miss_prefetch = Output(Bool()) 851 } 852 853 val fdp = new Bundle { 854 val late_miss_prefetch = Output(Bool()) 855 val prefetch_monitor_cnt = Output(Bool()) 856 val total_prefetch = Output(Bool()) 857 } 858 } 859 860 val bloom_filter_query = new Bundle { 861 val set = ValidIO(new BloomQueryBundle(BLOOM_FILTER_ENTRY_NUM)) 862 val clr = ValidIO(new BloomQueryBundle(BLOOM_FILTER_ENTRY_NUM)) 863 } 864 865 val mq_enq_cancel = Output(Bool()) 866 867 val debugTopDown = new DCacheTopDownIO 868 }) 869 870 // 128KBL1: FIXME: provide vaddr for l2 871 872 val entries = Seq.fill(cfg.nMissEntries)(Module(new MissEntry(edge))) 873 874 val miss_req_pipe_reg = RegInit(0.U.asTypeOf(new MissReqPipeRegBundle(edge))) 875 val acquire_from_pipereg = Wire(chiselTypeOf(io.mem_acquire)) 876 877 val primary_ready_vec = entries.map(_.io.primary_ready) 878 val secondary_ready_vec = entries.map(_.io.secondary_ready) 879 val secondary_reject_vec = entries.map(_.io.secondary_reject) 880 val probe_block_vec = entries.map { case e => e.io.block_addr.valid && e.io.block_addr.bits === io.probe_addr } 881 882 val merge = Cat(secondary_ready_vec ++ Seq(miss_req_pipe_reg.merge_req(io.req.bits))).orR 883 val reject = Cat(secondary_reject_vec ++ Seq(miss_req_pipe_reg.reject_req(io.req.bits))).orR 884 val alloc = !reject && !merge && Cat(primary_ready_vec).orR 885 val accept = alloc || merge 886 887 val req_mshr_handled_vec = entries.map(_.io.req_handled_by_this_entry) 888 // merged to pipeline reg 889 val req_pipeline_reg_handled = miss_req_pipe_reg.merge_req(io.req.bits) 890 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") 891 assert(PopCount(req_mshr_handled_vec) <= 1.U, "Only one mshr can handle a req") 892 io.resp.id := Mux(!req_pipeline_reg_handled, OHToUInt(req_mshr_handled_vec), miss_req_pipe_reg.mshr_id) 893 io.resp.handled := Cat(req_mshr_handled_vec).orR || req_pipeline_reg_handled 894 io.resp.merged := merge 895 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) 896 897 /* MissQueue enq logic is now splitted into 2 cycles 898 * 899 */ 900 miss_req_pipe_reg.req := io.req.bits 901 miss_req_pipe_reg.alloc := alloc && io.req.valid && !io.req.bits.cancel 902 miss_req_pipe_reg.merge := merge && io.req.valid && !io.req.bits.cancel 903 miss_req_pipe_reg.mshr_id := io.resp.id 904 905 assert(PopCount(Seq(alloc && io.req.valid, merge && io.req.valid)) <= 1.U, "allocate and merge a mshr in same cycle!") 906 907 val source_except_load_cnt = RegInit(0.U(10.W)) 908 when(VecInit(req_mshr_handled_vec).asUInt.orR || req_pipeline_reg_handled) { 909 when(io.req.bits.isFromLoad) { 910 source_except_load_cnt := 0.U 911 }.otherwise { 912 when(io.req.bits.isFromStore) { 913 source_except_load_cnt := source_except_load_cnt + 1.U 914 } 915 } 916 } 917 val Threshold = 8 918 val memSetPattenDetected = RegNext((source_except_load_cnt >= Threshold.U) && io.lqEmpty) 919 920 io.memSetPattenDetected := memSetPattenDetected 921 922 val forwardInfo_vec = VecInit(entries.map(_.io.forwardInfo)) 923 (0 until LoadPipelineWidth).map(i => { 924 val id = io.forward(i).mshrid 925 val req_valid = io.forward(i).valid 926 val paddr = io.forward(i).paddr 927 928 val (forward_mshr, forwardData) = forwardInfo_vec(id).forward(req_valid, paddr) 929 io.forward(i).forward_result_valid := forwardInfo_vec(id).check(req_valid, paddr) 930 io.forward(i).forward_mshr := forward_mshr 931 io.forward(i).forwardData := forwardData 932 }) 933 934 assert(RegNext(PopCount(secondary_ready_vec) <= 1.U)) 935// assert(RegNext(PopCount(secondary_reject_vec) <= 1.U)) 936 // It is possible that one mshr wants to merge a req, while another mshr wants to reject it. 937 // That is, a coming req has the same paddr as that of mshr_0 (merge), 938 // while it has the same set and the same way as mshr_1 (reject). 939 // In this situation, the coming req should be merged by mshr_0 940// assert(RegNext(PopCount(Seq(merge, reject)) <= 1.U)) 941 942 def select_valid_one[T <: Bundle]( 943 in: Seq[DecoupledIO[T]], 944 out: DecoupledIO[T], 945 name: Option[String] = None): Unit = { 946 947 if (name.nonEmpty) { out.suggestName(s"${name.get}_select") } 948 out.valid := Cat(in.map(_.valid)).orR 949 out.bits := ParallelMux(in.map(_.valid) zip in.map(_.bits)) 950 in.map(_.ready := out.ready) 951 assert(!RegNext(out.valid && PopCount(Cat(in.map(_.valid))) > 1.U)) 952 } 953 954 io.mem_grant.ready := false.B 955 956 val nMaxPrefetchEntry = WireInit(Constantin.createRecord("nMaxPrefetchEntry" + p(XSCoreParamsKey).HartId.toString, initValue = 14.U)) 957 entries.zipWithIndex.foreach { 958 case (e, i) => 959 val former_primary_ready = if(i == 0) 960 false.B 961 else 962 Cat((0 until i).map(j => entries(j).io.primary_ready)).orR 963 964 e.io.hartId := io.hartId 965 e.io.id := i.U 966 e.io.l2_pf_store_only := io.l2_pf_store_only 967 e.io.req.valid := io.req.valid 968 e.io.primary_valid := io.req.valid && 969 !merge && 970 !reject && 971 !former_primary_ready && 972 e.io.primary_ready 973 e.io.req.bits := io.req.bits.toMissReqWoStoreData() 974 975 e.io.mem_grant.valid := false.B 976 e.io.mem_grant.bits := DontCare 977 when (io.mem_grant.bits.source === i.U) { 978 e.io.mem_grant <> io.mem_grant 979 } 980 981 when(miss_req_pipe_reg.reg_valid() && miss_req_pipe_reg.mshr_id === i.U) { 982 e.io.miss_req_pipe_reg := miss_req_pipe_reg 983 }.otherwise { 984 e.io.miss_req_pipe_reg := DontCare 985 e.io.miss_req_pipe_reg.merge := false.B 986 e.io.miss_req_pipe_reg.alloc := false.B 987 } 988 989 e.io.acquire_fired_by_pipe_reg := acquire_from_pipereg.fire 990 991 e.io.refill_pipe_resp := io.refill_pipe_resp.valid && io.refill_pipe_resp.bits === i.U 992 e.io.replace_pipe_resp := io.replace_pipe_resp.valid && io.replace_pipe_resp.bits === i.U 993 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 994 995 e.io.memSetPattenDetected := memSetPattenDetected 996 e.io.nMaxPrefetchEntry := nMaxPrefetchEntry 997 998 io.debug_early_replace(i) := e.io.debug_early_replace 999 e.io.main_pipe_req.ready := io.main_pipe_req.ready 1000 } 1001 1002 io.req.ready := accept 1003 io.mq_enq_cancel := io.req.bits.cancel 1004 io.refill_to_ldq.valid := Cat(entries.map(_.io.refill_to_ldq.valid)).orR 1005 io.refill_to_ldq.bits := ParallelMux(entries.map(_.io.refill_to_ldq.valid) zip entries.map(_.io.refill_to_ldq.bits)) 1006 1007 acquire_from_pipereg.valid := miss_req_pipe_reg.can_send_acquire(io.req.valid, io.req.bits) 1008 acquire_from_pipereg.bits := miss_req_pipe_reg.get_acquire(io.l2_pf_store_only) 1009 1010 XSPerfAccumulate("acquire_fire_from_pipereg", acquire_from_pipereg.fire) 1011 XSPerfAccumulate("pipereg_valid", miss_req_pipe_reg.reg_valid()) 1012 1013 val acquire_sources = Seq(acquire_from_pipereg) ++ entries.map(_.io.mem_acquire) 1014 TLArbiter.lowest(edge, io.mem_acquire, acquire_sources:_*) 1015 TLArbiter.lowest(edge, io.mem_finish, entries.map(_.io.mem_finish):_*) 1016 1017 // arbiter_with_pipereg_N_dup(entries.map(_.io.refill_pipe_req), io.refill_pipe_req, 1018 // io.refill_pipe_req_dup, 1019 // Some("refill_pipe_req")) 1020 val out_refill_pipe_req = Wire(Decoupled(new RefillPipeReq)) 1021 val out_refill_pipe_req_ctrl = Wire(Decoupled(new RefillPipeReqCtrl)) 1022 out_refill_pipe_req_ctrl.valid := out_refill_pipe_req.valid 1023 out_refill_pipe_req_ctrl.bits := out_refill_pipe_req.bits.getCtrl 1024 out_refill_pipe_req.ready := out_refill_pipe_req_ctrl.ready 1025 arbiter(entries.map(_.io.refill_pipe_req), out_refill_pipe_req, Some("refill_pipe_req")) 1026 for (dup <- io.refill_pipe_req_dup) { 1027 AddPipelineReg(out_refill_pipe_req_ctrl, dup, false.B) 1028 } 1029 AddPipelineReg(out_refill_pipe_req, io.refill_pipe_req, false.B) 1030 1031 arbiter_with_pipereg(entries.map(_.io.replace_pipe_req), io.replace_pipe_req, Some("replace_pipe_req")) 1032 1033 // amo's main pipe req out 1034 val main_pipe_req_vec = entries.map(_.io.main_pipe_req) 1035 io.main_pipe_req.valid := VecInit(main_pipe_req_vec.map(_.valid)).asUInt.orR 1036 io.main_pipe_req.bits := Mux1H(main_pipe_req_vec.map(_.valid), main_pipe_req_vec.map(_.bits)) 1037 assert(PopCount(VecInit(main_pipe_req_vec.map(_.valid))) <= 1.U, "multi main pipe req") 1038 1039 // send evict hint to sms 1040 val sms_agt_evict_valid = Cat(entries.map(_.io.sms_agt_evict_req.valid)).orR 1041 val sms_agt_evict_valid_reg = RegInit(false.B) 1042 io.sms_agt_evict_req.valid := sms_agt_evict_valid_reg 1043 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) 1044 when(sms_agt_evict_valid) { 1045 sms_agt_evict_valid_reg := true.B 1046 }.elsewhen(io.sms_agt_evict_req.fire) { 1047 sms_agt_evict_valid_reg := false.B 1048 } 1049 assert(PopCount(VecInit(entries.map(_.io.sms_agt_evict_req.valid))) <= 1.U, "multi sms_agt_evict req") 1050 1051 io.probe_block := Cat(probe_block_vec).orR 1052 1053 io.full := ~Cat(entries.map(_.io.primary_ready)).andR 1054 1055 // prefetch related 1056 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) 1057 1058 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) 1059 io.prefetch_info.fdp.prefetch_monitor_cnt := io.refill_pipe_req.fire 1060 io.prefetch_info.fdp.total_prefetch := alloc && io.req.valid && !io.req.bits.cancel && isFromL1Prefetch(io.req.bits.pf_source) 1061 1062 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) 1063 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 1064 1065 io.bloom_filter_query.clr.valid := io.refill_pipe_req.fire && isFromL1Prefetch(io.refill_pipe_req.bits.prefetch) 1066 io.bloom_filter_query.clr.bits.addr := io.bloom_filter_query.clr.bits.get_addr(io.refill_pipe_req.bits.addr) 1067 1068 // L1MissTrace Chisel DB 1069 val debug_miss_trace = Wire(new L1MissTrace) 1070 debug_miss_trace.vaddr := io.req.bits.vaddr 1071 debug_miss_trace.paddr := io.req.bits.addr 1072 debug_miss_trace.source := io.req.bits.source 1073 debug_miss_trace.pc := io.req.bits.pc 1074 1075 val isWriteL1MissQMissTable = WireInit(Constantin.createRecord("isWriteL1MissQMissTable" + p(XSCoreParamsKey).HartId.toString)) 1076 val table = ChiselDB.createTable("L1MissQMissTrace_hart"+ p(XSCoreParamsKey).HartId.toString, new L1MissTrace) 1077 table.log(debug_miss_trace, isWriteL1MissQMissTable.orR && io.req.valid && !io.req.bits.cancel && alloc, "MissQueue", clock, reset) 1078 1079 // Difftest 1080 if (env.EnableDifftest) { 1081 val difftest = DifftestModule(new DiffRefillEvent, dontCare = true) 1082 difftest.coreid := io.hartId 1083 difftest.index := 1.U 1084 difftest.valid := io.refill_to_ldq.valid && io.refill_to_ldq.bits.hasdata && io.refill_to_ldq.bits.refill_done 1085 difftest.addr := io.refill_to_ldq.bits.addr 1086 difftest.data := io.refill_to_ldq.bits.data_raw.asTypeOf(difftest.data) 1087 difftest.idtfr := DontCare 1088 } 1089 1090 // Perf count 1091 XSPerfAccumulate("miss_req", io.req.fire && !io.req.bits.cancel) 1092 XSPerfAccumulate("miss_req_allocate", io.req.fire && !io.req.bits.cancel && alloc) 1093 XSPerfAccumulate("miss_req_load_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromLoad) 1094 XSPerfAccumulate("miss_req_store_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromStore) 1095 XSPerfAccumulate("miss_req_amo_allocate", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromAMO) 1096 XSPerfAccumulate("miss_req_merge_load", io.req.fire && !io.req.bits.cancel && merge && io.req.bits.isFromLoad) 1097 XSPerfAccumulate("miss_req_reject_load", io.req.valid && !io.req.bits.cancel && reject && io.req.bits.isFromLoad) 1098 XSPerfAccumulate("probe_blocked_by_miss", io.probe_block) 1099 XSPerfAccumulate("prefetch_primary_fire", io.req.fire && !io.req.bits.cancel && alloc && io.req.bits.isFromPrefetch) 1100 XSPerfAccumulate("prefetch_secondary_fire", io.req.fire && !io.req.bits.cancel && merge && io.req.bits.isFromPrefetch) 1101 XSPerfAccumulate("memSetPattenDetected", memSetPattenDetected) 1102 val max_inflight = RegInit(0.U((log2Up(cfg.nMissEntries) + 1).W)) 1103 val num_valids = PopCount(~Cat(primary_ready_vec).asUInt) 1104 when (num_valids > max_inflight) { 1105 max_inflight := num_valids 1106 } 1107 // max inflight (average) = max_inflight_total / cycle cnt 1108 XSPerfAccumulate("max_inflight", max_inflight) 1109 QueuePerf(cfg.nMissEntries, num_valids, num_valids === cfg.nMissEntries.U) 1110 io.full := num_valids === cfg.nMissEntries.U 1111 XSPerfHistogram("num_valids", num_valids, true.B, 0, cfg.nMissEntries, 1) 1112 1113 XSPerfHistogram("L1DMLP_CPUData", PopCount(VecInit(entries.map(_.io.perf_pending_normal)).asUInt), true.B, 0, cfg.nMissEntries, 1) 1114 XSPerfHistogram("L1DMLP_Prefetch", PopCount(VecInit(entries.map(_.io.perf_pending_prefetch)).asUInt), true.B, 0, cfg.nMissEntries, 1) 1115 XSPerfHistogram("L1DMLP_Total", num_valids, true.B, 0, cfg.nMissEntries, 1) 1116 1117 XSPerfAccumulate("miss_load_refill_latency", PopCount(entries.map(_.io.latency_monitor.load_miss_refilling))) 1118 XSPerfAccumulate("miss_store_refill_latency", PopCount(entries.map(_.io.latency_monitor.store_miss_refilling))) 1119 XSPerfAccumulate("miss_amo_refill_latency", PopCount(entries.map(_.io.latency_monitor.amo_miss_refilling))) 1120 XSPerfAccumulate("miss_pf_refill_latency", PopCount(entries.map(_.io.latency_monitor.pf_miss_refilling))) 1121 1122 val rob_head_miss_in_dcache = VecInit(entries.map(_.io.rob_head_query.resp)).asUInt.orR 1123 1124 entries.foreach { 1125 case e => { 1126 e.io.rob_head_query.query_valid := io.debugTopDown.robHeadVaddr.valid 1127 e.io.rob_head_query.vaddr := io.debugTopDown.robHeadVaddr.bits 1128 } 1129 } 1130 1131 io.debugTopDown.robHeadMissInDCache := rob_head_miss_in_dcache 1132 1133 val perfValidCount = RegNext(PopCount(entries.map(entry => (!entry.io.primary_ready)))) 1134 val perfEvents = Seq( 1135 ("dcache_missq_req ", io.req.fire), 1136 ("dcache_missq_1_4_valid", (perfValidCount < (cfg.nMissEntries.U/4.U))), 1137 ("dcache_missq_2_4_valid", (perfValidCount > (cfg.nMissEntries.U/4.U)) & (perfValidCount <= (cfg.nMissEntries.U/2.U))), 1138 ("dcache_missq_3_4_valid", (perfValidCount > (cfg.nMissEntries.U/2.U)) & (perfValidCount <= (cfg.nMissEntries.U*3.U/4.U))), 1139 ("dcache_missq_4_4_valid", (perfValidCount > (cfg.nMissEntries.U*3.U/4.U))), 1140 ) 1141 generatePerfEvent() 1142} 1143