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