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