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