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