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