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