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 chisel3._ 20import chisel3.experimental.ExtModule 21import chisel3.util._ 22import coupledL2.VaddrField 23import freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp, TransferSizes} 24import freechips.rocketchip.tilelink._ 25import freechips.rocketchip.util.BundleFieldBase 26import huancun.{AliasField, PrefetchField} 27import org.chipsalliance.cde.config.Parameters 28import utility._ 29import utils._ 30import xiangshan._ 31import xiangshan.backend.rob.RobDebugRollingIO 32import xiangshan.cache.wpu._ 33import xiangshan.mem.{AddPipelineReg, HasL1PrefetchSourceParameter} 34import xiangshan.mem.prefetch._ 35 36// DCache specific parameters 37case class DCacheParameters 38( 39 nSets: Int = 256, 40 nWays: Int = 8, 41 rowBits: Int = 64, 42 tagECC: Option[String] = None, 43 dataECC: Option[String] = None, 44 replacer: Option[String] = Some("setplru"), 45 updateReplaceOn2ndmiss: Boolean = true, 46 nMissEntries: Int = 1, 47 nProbeEntries: Int = 1, 48 nReleaseEntries: Int = 1, 49 nMMIOEntries: Int = 1, 50 nMMIOs: Int = 1, 51 blockBytes: Int = 64, 52 nMaxPrefetchEntry: Int = 1, 53 alwaysReleaseData: Boolean = false 54) extends L1CacheParameters { 55 // if sets * blockBytes > 4KB(page size), 56 // cache alias will happen, 57 // we need to avoid this by recoding additional bits in L2 cache 58 val setBytes = nSets * blockBytes 59 val aliasBitsOpt = if(setBytes > pageSize) Some(log2Ceil(setBytes / pageSize)) else None 60 61 def tagCode: Code = Code.fromString(tagECC) 62 63 def dataCode: Code = Code.fromString(dataECC) 64} 65 66// Physical Address 67// -------------------------------------- 68// | Physical Tag | PIndex | Offset | 69// -------------------------------------- 70// | 71// DCacheTagOffset 72// 73// Virtual Address 74// -------------------------------------- 75// | Above index | Set | Bank | Offset | 76// -------------------------------------- 77// | | | | 78// | | | 0 79// | | DCacheBankOffset 80// | DCacheSetOffset 81// DCacheAboveIndexOffset 82 83// Default DCache size = 64 sets * 8 ways * 8 banks * 8 Byte = 32K Byte 84 85trait HasDCacheParameters extends HasL1CacheParameters with HasL1PrefetchSourceParameter{ 86 val cacheParams = dcacheParameters 87 val cfg = cacheParams 88 89 def encWordBits = cacheParams.dataCode.width(wordBits) 90 91 def encRowBits = encWordBits * rowWords // for DuplicatedDataArray only 92 def eccBits = encWordBits - wordBits 93 94 def encTagBits = cacheParams.tagCode.width(tagBits) 95 def eccTagBits = encTagBits - tagBits 96 97 def blockProbeAfterGrantCycles = 8 // give the processor some time to issue a request after a grant 98 99 def nSourceType = 10 100 def sourceTypeWidth = log2Up(nSourceType) 101 // non-prefetch source < 3 102 def LOAD_SOURCE = 0 103 def STORE_SOURCE = 1 104 def AMO_SOURCE = 2 105 // prefetch source >= 3 106 def DCACHE_PREFETCH_SOURCE = 3 107 def SOFT_PREFETCH = 4 108 // the following sources are only used inside SMS 109 def HW_PREFETCH_AGT = 5 110 def HW_PREFETCH_PHT_CUR = 6 111 def HW_PREFETCH_PHT_INC = 7 112 def HW_PREFETCH_PHT_DEC = 8 113 def HW_PREFETCH_BOP = 9 114 def HW_PREFETCH_STRIDE = 10 115 116 def BLOOM_FILTER_ENTRY_NUM = 4096 117 118 // each source use a id to distinguish its multiple reqs 119 def reqIdWidth = log2Up(nEntries) max log2Up(StoreBufferSize) 120 121 require(isPow2(cfg.nMissEntries)) // TODO 122 // require(isPow2(cfg.nReleaseEntries)) 123 require(cfg.nMissEntries < cfg.nReleaseEntries) 124 val nEntries = cfg.nMissEntries + cfg.nReleaseEntries 125 val releaseIdBase = cfg.nMissEntries 126 127 // banked dcache support 128 val DCacheSetDiv = 1 129 val DCacheSets = cacheParams.nSets 130 val DCacheWays = cacheParams.nWays 131 val DCacheBanks = 8 // hardcoded 132 val DCacheDupNum = 16 133 val DCacheSRAMRowBits = cacheParams.rowBits // hardcoded 134 val DCacheWordBits = 64 // hardcoded 135 val DCacheWordBytes = DCacheWordBits / 8 136 val MaxPrefetchEntry = cacheParams.nMaxPrefetchEntry 137 val DCacheVWordBytes = VLEN / 8 138 require(DCacheSRAMRowBits == 64) 139 140 val DCacheSetDivBits = log2Ceil(DCacheSetDiv) 141 val DCacheSetBits = log2Ceil(DCacheSets) 142 val DCacheSizeBits = DCacheSRAMRowBits * DCacheBanks * DCacheWays * DCacheSets 143 val DCacheSizeBytes = DCacheSizeBits / 8 144 val DCacheSizeWords = DCacheSizeBits / 64 // TODO 145 146 val DCacheSameVPAddrLength = 12 147 148 val DCacheSRAMRowBytes = DCacheSRAMRowBits / 8 149 val DCacheWordOffset = log2Up(DCacheWordBytes) 150 val DCacheVWordOffset = log2Up(DCacheVWordBytes) 151 152 val DCacheBankOffset = log2Up(DCacheSRAMRowBytes) 153 val DCacheSetOffset = DCacheBankOffset + log2Up(DCacheBanks) 154 val DCacheAboveIndexOffset = DCacheSetOffset + log2Up(DCacheSets) 155 val DCacheTagOffset = DCacheAboveIndexOffset min DCacheSameVPAddrLength 156 val DCacheLineOffset = DCacheSetOffset 157 158 // uncache 159 val uncacheIdxBits = log2Up(StoreQueueSize + 1) max log2Up(VirtualLoadQueueSize + 1) 160 // hardware prefetch parameters 161 // high confidence hardware prefetch port 162 val HighConfHWPFLoadPort = LoadPipelineWidth - 1 // use the last load port by default 163 val IgnorePrefetchConfidence = false 164 165 // parameters about duplicating regs to solve fanout 166 // In Main Pipe: 167 // tag_write.ready -> data_write.valid * 8 banks 168 // tag_write.ready -> meta_write.valid 169 // tag_write.ready -> tag_write.valid 170 // tag_write.ready -> err_write.valid 171 // tag_write.ready -> wb.valid 172 val nDupTagWriteReady = DCacheBanks + 4 173 // In Main Pipe: 174 // data_write.ready -> data_write.valid * 8 banks 175 // data_write.ready -> meta_write.valid 176 // data_write.ready -> tag_write.valid 177 // data_write.ready -> err_write.valid 178 // data_write.ready -> wb.valid 179 val nDupDataWriteReady = DCacheBanks + 4 180 val nDupWbReady = DCacheBanks + 4 181 val nDupStatus = nDupTagWriteReady + nDupDataWriteReady 182 val dataWritePort = 0 183 val metaWritePort = DCacheBanks 184 val tagWritePort = metaWritePort + 1 185 val errWritePort = tagWritePort + 1 186 val wbPort = errWritePort + 1 187 188 def set_to_dcache_div(set: UInt) = { 189 require(set.getWidth >= DCacheSetBits) 190 if (DCacheSetDivBits == 0) 0.U else set(DCacheSetDivBits-1, 0) 191 } 192 193 def set_to_dcache_div_set(set: UInt) = { 194 require(set.getWidth >= DCacheSetBits) 195 set(DCacheSetBits - 1, DCacheSetDivBits) 196 } 197 198 def addr_to_dcache_bank(addr: UInt) = { 199 require(addr.getWidth >= DCacheSetOffset) 200 addr(DCacheSetOffset-1, DCacheBankOffset) 201 } 202 203 def addr_to_dcache_div(addr: UInt) = { 204 require(addr.getWidth >= DCacheAboveIndexOffset) 205 if(DCacheSetDivBits == 0) 0.U else addr(DCacheSetOffset + DCacheSetDivBits - 1, DCacheSetOffset) 206 } 207 208 def addr_to_dcache_div_set(addr: UInt) = { 209 require(addr.getWidth >= DCacheAboveIndexOffset) 210 addr(DCacheAboveIndexOffset - 1, DCacheSetOffset + DCacheSetDivBits) 211 } 212 213 def addr_to_dcache_set(addr: UInt) = { 214 require(addr.getWidth >= DCacheAboveIndexOffset) 215 addr(DCacheAboveIndexOffset-1, DCacheSetOffset) 216 } 217 218 def get_data_of_bank(bank: Int, data: UInt) = { 219 require(data.getWidth >= (bank+1)*DCacheSRAMRowBits) 220 data(DCacheSRAMRowBits * (bank + 1) - 1, DCacheSRAMRowBits * bank) 221 } 222 223 def get_mask_of_bank(bank: Int, data: UInt) = { 224 require(data.getWidth >= (bank+1)*DCacheSRAMRowBytes) 225 data(DCacheSRAMRowBytes * (bank + 1) - 1, DCacheSRAMRowBytes * bank) 226 } 227 228 def get_alias(vaddr: UInt): UInt ={ 229 require(blockOffBits + idxBits > pgIdxBits) 230 if(blockOffBits + idxBits > pgIdxBits){ 231 vaddr(blockOffBits + idxBits - 1, pgIdxBits) 232 }else{ 233 0.U 234 } 235 } 236 237 def is_alias_match(vaddr0: UInt, vaddr1: UInt): Bool = { 238 require(vaddr0.getWidth == VAddrBits && vaddr1.getWidth == VAddrBits) 239 if(blockOffBits + idxBits > pgIdxBits) { 240 vaddr0(blockOffBits + idxBits - 1, pgIdxBits) === vaddr1(blockOffBits + idxBits - 1, pgIdxBits) 241 }else { 242 // no alias problem 243 true.B 244 } 245 } 246 247 def get_direct_map_way(addr:UInt): UInt = { 248 addr(DCacheAboveIndexOffset + log2Up(DCacheWays) - 1, DCacheAboveIndexOffset) 249 } 250 251 def arbiter[T <: Bundle]( 252 in: Seq[DecoupledIO[T]], 253 out: DecoupledIO[T], 254 name: Option[String] = None): Unit = { 255 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 256 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 257 for ((a, req) <- arb.io.in.zip(in)) { 258 a <> req 259 } 260 out <> arb.io.out 261 } 262 263 def arbiter_with_pipereg[T <: Bundle]( 264 in: Seq[DecoupledIO[T]], 265 out: DecoupledIO[T], 266 name: Option[String] = None): Unit = { 267 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 268 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 269 for ((a, req) <- arb.io.in.zip(in)) { 270 a <> req 271 } 272 AddPipelineReg(arb.io.out, out, false.B) 273 } 274 275 def arbiter_with_pipereg_N_dup[T <: Bundle]( 276 in: Seq[DecoupledIO[T]], 277 out: DecoupledIO[T], 278 dups: Seq[DecoupledIO[T]], 279 name: Option[String] = None): Unit = { 280 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 281 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 282 for ((a, req) <- arb.io.in.zip(in)) { 283 a <> req 284 } 285 for (dup <- dups) { 286 AddPipelineReg(arb.io.out, dup, false.B) 287 } 288 AddPipelineReg(arb.io.out, out, false.B) 289 } 290 291 def rrArbiter[T <: Bundle]( 292 in: Seq[DecoupledIO[T]], 293 out: DecoupledIO[T], 294 name: Option[String] = None): Unit = { 295 val arb = Module(new RRArbiter[T](chiselTypeOf(out.bits), in.size)) 296 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 297 for ((a, req) <- arb.io.in.zip(in)) { 298 a <> req 299 } 300 out <> arb.io.out 301 } 302 303 def fastArbiter[T <: Bundle]( 304 in: Seq[DecoupledIO[T]], 305 out: DecoupledIO[T], 306 name: Option[String] = None): Unit = { 307 val arb = Module(new FastArbiter[T](chiselTypeOf(out.bits), in.size)) 308 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 309 for ((a, req) <- arb.io.in.zip(in)) { 310 a <> req 311 } 312 out <> arb.io.out 313 } 314 315 val numReplaceRespPorts = 2 316 317 require(isPow2(nSets), s"nSets($nSets) must be pow2") 318 require(isPow2(nWays), s"nWays($nWays) must be pow2") 319 require(full_divide(rowBits, wordBits), s"rowBits($rowBits) must be multiple of wordBits($wordBits)") 320 require(full_divide(beatBits, rowBits), s"beatBits($beatBits) must be multiple of rowBits($rowBits)") 321} 322 323abstract class DCacheModule(implicit p: Parameters) extends L1CacheModule 324 with HasDCacheParameters 325 326abstract class DCacheBundle(implicit p: Parameters) extends L1CacheBundle 327 with HasDCacheParameters 328 329class ReplacementAccessBundle(implicit p: Parameters) extends DCacheBundle { 330 val set = UInt(log2Up(nSets).W) 331 val way = UInt(log2Up(nWays).W) 332} 333 334class ReplacementWayReqIO(implicit p: Parameters) extends DCacheBundle { 335 val set = ValidIO(UInt(log2Up(nSets).W)) 336 val dmWay = Output(UInt(log2Up(nWays).W)) 337 val way = Input(UInt(log2Up(nWays).W)) 338} 339 340class DCacheExtraMeta(implicit p: Parameters) extends DCacheBundle 341{ 342 val error = Bool() // cache line has been marked as corrupted by l2 / ecc error detected when store 343 val prefetch = UInt(L1PfSourceBits.W) // cache line is first required by prefetch 344 val access = Bool() // cache line has been accessed by load / store 345 346 // val debug_access_timestamp = UInt(64.W) // last time a load / store / refill access that cacheline 347} 348 349// memory request in word granularity(load, mmio, lr/sc, atomics) 350class DCacheWordReq(implicit p: Parameters) extends DCacheBundle 351{ 352 val cmd = UInt(M_SZ.W) 353 val vaddr = UInt(VAddrBits.W) 354 val data = UInt(VLEN.W) 355 val mask = UInt((VLEN/8).W) 356 val id = UInt(reqIdWidth.W) 357 val instrtype = UInt(sourceTypeWidth.W) 358 val isFirstIssue = Bool() 359 val replayCarry = new ReplayCarry(nWays) 360 361 val debug_robIdx = UInt(log2Ceil(RobSize).W) 362 def dump() = { 363 XSDebug("DCacheWordReq: cmd: %x vaddr: %x data: %x mask: %x id: %d\n", 364 cmd, vaddr, data, mask, id) 365 } 366} 367 368// memory request in word granularity(store) 369class DCacheLineReq(implicit p: Parameters) extends DCacheBundle 370{ 371 val cmd = UInt(M_SZ.W) 372 val vaddr = UInt(VAddrBits.W) 373 val addr = UInt(PAddrBits.W) 374 val data = UInt((cfg.blockBytes * 8).W) 375 val mask = UInt(cfg.blockBytes.W) 376 val id = UInt(reqIdWidth.W) 377 def dump() = { 378 XSDebug("DCacheLineReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 379 cmd, addr, data, mask, id) 380 } 381 def idx: UInt = get_idx(vaddr) 382} 383 384class DCacheWordReqWithVaddr(implicit p: Parameters) extends DCacheWordReq { 385 val addr = UInt(PAddrBits.W) 386 val wline = Bool() 387} 388 389class DCacheWordReqWithVaddrAndPfFlag(implicit p: Parameters) extends DCacheWordReqWithVaddr { 390 val prefetch = Bool() 391 392 def toDCacheWordReqWithVaddr() = { 393 val res = Wire(new DCacheWordReqWithVaddr) 394 res.vaddr := vaddr 395 res.wline := wline 396 res.cmd := cmd 397 res.addr := addr 398 res.data := data 399 res.mask := mask 400 res.id := id 401 res.instrtype := instrtype 402 res.replayCarry := replayCarry 403 res.isFirstIssue := isFirstIssue 404 res.debug_robIdx := debug_robIdx 405 406 res 407 } 408} 409 410class BaseDCacheWordResp(implicit p: Parameters) extends DCacheBundle 411{ 412 // read in s2 413 val data = UInt(VLEN.W) 414 // select in s3 415 val data_delayed = UInt(VLEN.W) 416 val id = UInt(reqIdWidth.W) 417 // cache req missed, send it to miss queue 418 val miss = Bool() 419 // cache miss, and failed to enter the missqueue, replay from RS is needed 420 val replay = Bool() 421 val replayCarry = new ReplayCarry(nWays) 422 // data has been corrupted 423 val tag_error = Bool() // tag error 424 val mshr_id = UInt(log2Up(cfg.nMissEntries).W) 425 426 val debug_robIdx = UInt(log2Ceil(RobSize).W) 427 def dump() = { 428 XSDebug("DCacheWordResp: data: %x id: %d miss: %b replay: %b\n", 429 data, id, miss, replay) 430 } 431} 432 433class DCacheWordResp(implicit p: Parameters) extends BaseDCacheWordResp 434{ 435 val meta_prefetch = UInt(L1PfSourceBits.W) 436 val meta_access = Bool() 437 // s2 438 val handled = Bool() 439 val real_miss = Bool() 440 // s3: 1 cycle after data resp 441 val error_delayed = Bool() // all kinds of errors, include tag error 442 val replacementUpdated = Bool() 443} 444 445class BankedDCacheWordResp(implicit p: Parameters) extends DCacheWordResp 446{ 447 val bank_data = Vec(DCacheBanks, Bits(DCacheSRAMRowBits.W)) 448 val bank_oh = UInt(DCacheBanks.W) 449} 450 451class DCacheWordRespWithError(implicit p: Parameters) extends BaseDCacheWordResp 452{ 453 val error = Bool() // all kinds of errors, include tag error 454} 455 456class DCacheLineResp(implicit p: Parameters) extends DCacheBundle 457{ 458 val data = UInt((cfg.blockBytes * 8).W) 459 // cache req missed, send it to miss queue 460 val miss = Bool() 461 // cache req nacked, replay it later 462 val replay = Bool() 463 val id = UInt(reqIdWidth.W) 464 def dump() = { 465 XSDebug("DCacheLineResp: data: %x id: %d miss: %b replay: %b\n", 466 data, id, miss, replay) 467 } 468} 469 470class Refill(implicit p: Parameters) extends DCacheBundle 471{ 472 val addr = UInt(PAddrBits.W) 473 val data = UInt(l1BusDataWidth.W) 474 val error = Bool() // refilled data has been corrupted 475 // for debug usage 476 val data_raw = UInt((cfg.blockBytes * 8).W) 477 val hasdata = Bool() 478 val refill_done = Bool() 479 def dump() = { 480 XSDebug("Refill: addr: %x data: %x\n", addr, data) 481 } 482 val id = UInt(log2Up(cfg.nMissEntries).W) 483} 484 485class Release(implicit p: Parameters) extends DCacheBundle 486{ 487 val paddr = UInt(PAddrBits.W) 488 def dump() = { 489 XSDebug("Release: paddr: %x\n", paddr(PAddrBits-1, DCacheTagOffset)) 490 } 491} 492 493class DCacheWordIO(implicit p: Parameters) extends DCacheBundle 494{ 495 val req = DecoupledIO(new DCacheWordReq) 496 val resp = Flipped(DecoupledIO(new DCacheWordResp)) 497} 498 499 500class UncacheWordReq(implicit p: Parameters) extends DCacheBundle 501{ 502 val cmd = UInt(M_SZ.W) 503 val addr = UInt(PAddrBits.W) 504 val data = UInt(XLEN.W) 505 val mask = UInt((XLEN/8).W) 506 val id = UInt(uncacheIdxBits.W) 507 val instrtype = UInt(sourceTypeWidth.W) 508 val atomic = Bool() 509 val isFirstIssue = Bool() 510 val replayCarry = new ReplayCarry(nWays) 511 512 def dump() = { 513 XSDebug("UncacheWordReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 514 cmd, addr, data, mask, id) 515 } 516} 517 518class UncacheWordResp(implicit p: Parameters) extends DCacheBundle 519{ 520 val data = UInt(XLEN.W) 521 val data_delayed = UInt(XLEN.W) 522 val id = UInt(uncacheIdxBits.W) 523 val miss = Bool() 524 val replay = Bool() 525 val tag_error = Bool() 526 val error = Bool() 527 val replayCarry = new ReplayCarry(nWays) 528 val mshr_id = UInt(log2Up(cfg.nMissEntries).W) // FIXME: why uncacheWordResp is not merged to baseDcacheResp 529 530 val debug_robIdx = UInt(log2Ceil(RobSize).W) 531 def dump() = { 532 XSDebug("UncacheWordResp: data: %x id: %d miss: %b replay: %b, tag_error: %b, error: %b\n", 533 data, id, miss, replay, tag_error, error) 534 } 535} 536 537class UncacheWordIO(implicit p: Parameters) extends DCacheBundle 538{ 539 val req = DecoupledIO(new UncacheWordReq) 540 val resp = Flipped(DecoupledIO(new UncacheWordResp)) 541} 542 543class AtomicsResp(implicit p: Parameters) extends DCacheBundle { 544 val data = UInt(DataBits.W) 545 val miss = Bool() 546 val miss_id = UInt(log2Up(cfg.nMissEntries).W) 547 val replay = Bool() 548 val error = Bool() 549 550 val ack_miss_queue = Bool() 551 552 val id = UInt(reqIdWidth.W) 553} 554 555class AtomicWordIO(implicit p: Parameters) extends DCacheBundle 556{ 557 val req = DecoupledIO(new MainPipeReq) 558 val resp = Flipped(ValidIO(new AtomicsResp)) 559 val block_lr = Input(Bool()) 560} 561 562// used by load unit 563class DCacheLoadIO(implicit p: Parameters) extends DCacheWordIO 564{ 565 // kill previous cycle's req 566 val s1_kill = Output(Bool()) 567 val s2_kill = Output(Bool()) 568 val s0_pc = Output(UInt(VAddrBits.W)) 569 val s1_pc = Output(UInt(VAddrBits.W)) 570 val s2_pc = Output(UInt(VAddrBits.W)) 571 // cycle 0: load has updated replacement before 572 val replacementUpdated = Output(Bool()) 573 val is128Req = Bool() 574 // cycle 0: prefetch source bits 575 val pf_source = Output(UInt(L1PfSourceBits.W)) 576 // cycle 0: virtual address: req.addr 577 // cycle 1: physical address: s1_paddr 578 val s1_paddr_dup_lsu = Output(UInt(PAddrBits.W)) // lsu side paddr 579 val s1_paddr_dup_dcache = Output(UInt(PAddrBits.W)) // dcache side paddr 580 val s1_disable_fast_wakeup = Input(Bool()) 581 // cycle 2: hit signal 582 val s2_hit = Input(Bool()) // hit signal for lsu, 583 val s2_first_hit = Input(Bool()) 584 val s2_bank_conflict = Input(Bool()) 585 val s2_wpu_pred_fail = Input(Bool()) 586 val s2_mq_nack = Input(Bool()) 587 588 // debug 589 val debug_s1_hit_way = Input(UInt(nWays.W)) 590 val debug_s2_pred_way_num = Input(UInt(XLEN.W)) 591 val debug_s2_dm_way_num = Input(UInt(XLEN.W)) 592 val debug_s2_real_way_num = Input(UInt(XLEN.W)) 593} 594 595class DCacheLineIO(implicit p: Parameters) extends DCacheBundle 596{ 597 val req = DecoupledIO(new DCacheLineReq) 598 val resp = Flipped(DecoupledIO(new DCacheLineResp)) 599} 600 601class DCacheToSbufferIO(implicit p: Parameters) extends DCacheBundle { 602 // sbuffer will directly send request to dcache main pipe 603 val req = Flipped(Decoupled(new DCacheLineReq)) 604 605 val main_pipe_hit_resp = ValidIO(new DCacheLineResp) 606 val refill_hit_resp = ValidIO(new DCacheLineResp) 607 608 val replay_resp = ValidIO(new DCacheLineResp) 609 610 def hit_resps: Seq[ValidIO[DCacheLineResp]] = Seq(main_pipe_hit_resp, refill_hit_resp) 611} 612 613// forward tilelink channel D's data to ldu 614class DcacheToLduForwardIO(implicit p: Parameters) extends DCacheBundle { 615 val valid = Bool() 616 val data = UInt(l1BusDataWidth.W) 617 val mshrid = UInt(log2Up(cfg.nMissEntries).W) 618 val last = Bool() 619 620 def apply(req_valid : Bool, req_data : UInt, req_mshrid : UInt, req_last : Bool) = { 621 valid := req_valid 622 data := req_data 623 mshrid := req_mshrid 624 last := req_last 625 } 626 627 def dontCare() = { 628 valid := false.B 629 data := DontCare 630 mshrid := DontCare 631 last := DontCare 632 } 633 634 def forward(req_valid : Bool, req_mshr_id : UInt, req_paddr : UInt) = { 635 val all_match = req_valid && valid && 636 req_mshr_id === mshrid && 637 req_paddr(log2Up(refillBytes)) === last 638 639 val forward_D = RegInit(false.B) 640 val forwardData = RegInit(VecInit(List.fill(VLEN/8)(0.U(8.W)))) 641 642 val block_idx = req_paddr(log2Up(refillBytes) - 1, 3) 643 val block_data = Wire(Vec(l1BusDataWidth / 64, UInt(64.W))) 644 (0 until l1BusDataWidth / 64).map(i => { 645 block_data(i) := data(64 * i + 63, 64 * i) 646 }) 647 val selected_data = Wire(UInt(128.W)) 648 selected_data := Mux(req_paddr(3), Fill(2, block_data(block_idx)), Cat(block_data(block_idx + 1.U), block_data(block_idx))) 649 650 forward_D := all_match 651 for (i <- 0 until VLEN/8) { 652 forwardData(i) := selected_data(8 * i + 7, 8 * i) 653 } 654 655 (forward_D, forwardData) 656 } 657} 658 659class MissEntryForwardIO(implicit p: Parameters) extends DCacheBundle { 660 val inflight = Bool() 661 val paddr = UInt(PAddrBits.W) 662 val raw_data = Vec(blockRows, UInt(rowBits.W)) 663 val firstbeat_valid = Bool() 664 val lastbeat_valid = Bool() 665 666 def apply(mshr_valid : Bool, mshr_paddr : UInt, mshr_rawdata : Vec[UInt], mshr_first_valid : Bool, mshr_last_valid : Bool) = { 667 inflight := mshr_valid 668 paddr := mshr_paddr 669 raw_data := mshr_rawdata 670 firstbeat_valid := mshr_first_valid 671 lastbeat_valid := mshr_last_valid 672 } 673 674 // check if we can forward from mshr or D channel 675 def check(req_valid : Bool, req_paddr : UInt) = { 676 RegNext(req_valid && inflight && req_paddr(PAddrBits - 1, blockOffBits) === paddr(PAddrBits - 1, blockOffBits)) 677 } 678 679 def forward(req_valid : Bool, req_paddr : UInt) = { 680 val all_match = (req_paddr(log2Up(refillBytes)) === 0.U && firstbeat_valid) || 681 (req_paddr(log2Up(refillBytes)) === 1.U && lastbeat_valid) 682 683 val forward_mshr = RegInit(false.B) 684 val forwardData = RegInit(VecInit(List.fill(VLEN/8)(0.U(8.W)))) 685 686 val block_idx = req_paddr(log2Up(refillBytes), 3) 687 val block_data = raw_data 688 689 val selected_data = Wire(UInt(128.W)) 690 selected_data := Mux(req_paddr(3), Fill(2, block_data(block_idx)), Cat(block_data(block_idx + 1.U), block_data(block_idx))) 691 692 forward_mshr := all_match 693 for (i <- 0 until VLEN/8) { 694 forwardData(i) := selected_data(8 * i + 7, 8 * i) 695 } 696 697 (forward_mshr, forwardData) 698 } 699} 700 701// forward mshr's data to ldu 702class LduToMissqueueForwardIO(implicit p: Parameters) extends DCacheBundle { 703 // req 704 val valid = Input(Bool()) 705 val mshrid = Input(UInt(log2Up(cfg.nMissEntries).W)) 706 val paddr = Input(UInt(PAddrBits.W)) 707 // resp 708 val forward_mshr = Output(Bool()) 709 val forwardData = Output(Vec(VLEN/8, UInt(8.W))) 710 val forward_result_valid = Output(Bool()) 711 712 def connect(sink: LduToMissqueueForwardIO) = { 713 sink.valid := valid 714 sink.mshrid := mshrid 715 sink.paddr := paddr 716 forward_mshr := sink.forward_mshr 717 forwardData := sink.forwardData 718 forward_result_valid := sink.forward_result_valid 719 } 720 721 def forward() = { 722 (forward_result_valid, forward_mshr, forwardData) 723 } 724} 725 726class StorePrefetchReq(implicit p: Parameters) extends DCacheBundle { 727 val paddr = UInt(PAddrBits.W) 728 val vaddr = UInt(VAddrBits.W) 729} 730 731class DCacheToLsuIO(implicit p: Parameters) extends DCacheBundle { 732 val load = Vec(LoadPipelineWidth, Flipped(new DCacheLoadIO)) // for speculative load 733 val sta = Vec(StorePipelineWidth, Flipped(new DCacheStoreIO)) // for non-blocking store 734 val lsq = ValidIO(new Refill) // refill to load queue, wake up load misses 735 val tl_d_channel = Output(new DcacheToLduForwardIO) 736 val store = new DCacheToSbufferIO // for sbuffer 737 val atomics = Flipped(new AtomicWordIO) // atomics reqs 738 val release = ValidIO(new Release) // cacheline release hint for ld-ld violation check 739 val forward_D = Output(Vec(LoadPipelineWidth, new DcacheToLduForwardIO)) 740 val forward_mshr = Vec(LoadPipelineWidth, new LduToMissqueueForwardIO) 741} 742 743class DCacheTopDownIO(implicit p: Parameters) extends DCacheBundle { 744 val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W))) 745 val robHeadMissInDCache = Output(Bool()) 746 val robHeadOtherReplay = Input(Bool()) 747} 748 749class DCacheIO(implicit p: Parameters) extends DCacheBundle { 750 val hartId = Input(UInt(8.W)) 751 val l2_pf_store_only = Input(Bool()) 752 val lsu = new DCacheToLsuIO 753 val csr = new L1CacheToCsrIO 754 val error = new L1CacheErrorInfo 755 val mshrFull = Output(Bool()) 756 val memSetPattenDetected = Output(Bool()) 757 val lqEmpty = Input(Bool()) 758 val pf_ctrl = Output(new PrefetchControlBundle) 759 val force_write = Input(Bool()) 760 val sms_agt_evict_req = DecoupledIO(new AGTEvictReq) 761 val debugTopDown = new DCacheTopDownIO 762 val debugRolling = Flipped(new RobDebugRollingIO) 763} 764 765class DCache()(implicit p: Parameters) extends LazyModule with HasDCacheParameters { 766 override def shouldBeInlined: Boolean = false 767 768 val reqFields: Seq[BundleFieldBase] = Seq( 769 PrefetchField(), 770 ReqSourceField(), 771 VaddrField(VAddrBits - blockOffBits), 772 ) ++ cacheParams.aliasBitsOpt.map(AliasField) 773 val echoFields: Seq[BundleFieldBase] = Nil 774 775 val clientParameters = TLMasterPortParameters.v1( 776 Seq(TLMasterParameters.v1( 777 name = "dcache", 778 sourceId = IdRange(0, nEntries + 1), 779 supportsProbe = TransferSizes(cfg.blockBytes) 780 )), 781 requestFields = reqFields, 782 echoFields = echoFields 783 ) 784 785 val clientNode = TLClientNode(Seq(clientParameters)) 786 787 lazy val module = new DCacheImp(this) 788} 789 790 791class DCacheImp(outer: DCache) extends LazyModuleImp(outer) with HasDCacheParameters with HasPerfEvents with HasL1PrefetchSourceParameter { 792 793 val io = IO(new DCacheIO) 794 795 val (bus, edge) = outer.clientNode.out.head 796 require(bus.d.bits.data.getWidth == l1BusDataWidth, "DCache: tilelink width does not match") 797 798 println("DCache:") 799 println(" DCacheSets: " + DCacheSets) 800 println(" DCacheSetDiv: " + DCacheSetDiv) 801 println(" DCacheWays: " + DCacheWays) 802 println(" DCacheBanks: " + DCacheBanks) 803 println(" DCacheSRAMRowBits: " + DCacheSRAMRowBits) 804 println(" DCacheWordOffset: " + DCacheWordOffset) 805 println(" DCacheBankOffset: " + DCacheBankOffset) 806 println(" DCacheSetOffset: " + DCacheSetOffset) 807 println(" DCacheTagOffset: " + DCacheTagOffset) 808 println(" DCacheAboveIndexOffset: " + DCacheAboveIndexOffset) 809 println(" DcacheMaxPrefetchEntry: " + MaxPrefetchEntry) 810 println(" WPUEnable: " + dwpuParam.enWPU) 811 println(" WPUEnableCfPred: " + dwpuParam.enCfPred) 812 println(" WPUAlgorithm: " + dwpuParam.algoName) 813 814 // Enable L1 Store prefetch 815 val StorePrefetchL1Enabled = EnableStorePrefetchAtCommit || EnableStorePrefetchAtIssue || EnableStorePrefetchSPB 816 val MetaReadPort = 817 if (StorePrefetchL1Enabled) 818 1 + backendParams.LduCnt + backendParams.StaCnt + backendParams.HyuCnt 819 else 820 1 + backendParams.LduCnt + backendParams.HyuCnt 821 val TagReadPort = 822 if (StorePrefetchL1Enabled) 823 1 + backendParams.LduCnt + backendParams.StaCnt + backendParams.HyuCnt 824 else 825 1 + backendParams.LduCnt + backendParams.HyuCnt 826 827 // Enable L1 Load prefetch 828 val LoadPrefetchL1Enabled = true 829 val AccessArrayReadPort = if(LoadPrefetchL1Enabled) LoadPipelineWidth + 1 + 1 else LoadPipelineWidth + 1 830 val PrefetchArrayReadPort = if(LoadPrefetchL1Enabled) LoadPipelineWidth + 1 + 1 else LoadPipelineWidth + 1 831 832 //---------------------------------------- 833 // core data structures 834 val bankedDataArray = if(dwpuParam.enWPU) Module(new SramedDataArray) else Module(new BankedDataArray) 835 val metaArray = Module(new L1CohMetaArray(readPorts = MetaReadPort, writePorts = 2)) 836 val errorArray = Module(new L1FlagMetaArray(readPorts = LoadPipelineWidth + 1, writePorts = 2)) 837 val prefetchArray = Module(new L1PrefetchSourceArray(readPorts = PrefetchArrayReadPort, writePorts = 2 + LoadPipelineWidth)) // prefetch flag array 838 val accessArray = Module(new L1FlagMetaArray(readPorts = AccessArrayReadPort, writePorts = LoadPipelineWidth + 2)) 839 val tagArray = Module(new DuplicatedTagArray(readPorts = TagReadPort)) 840 val prefetcherMonitor = Module(new PrefetcherMonitor) 841 val fdpMonitor = Module(new FDPrefetcherMonitor) 842 val bloomFilter = Module(new BloomFilter(BLOOM_FILTER_ENTRY_NUM, true)) 843 val counterFilter = Module(new CounterFilter) 844 bankedDataArray.dump() 845 846 //---------------------------------------- 847 // core modules 848 val ldu = Seq.tabulate(LoadPipelineWidth)({ i => Module(new LoadPipe(i))}) 849 val stu = Seq.tabulate(StorePipelineWidth)({ i => Module(new StorePipe(i))}) 850 val mainPipe = Module(new MainPipe) 851 val refillPipe = Module(new RefillPipe) 852 val missQueue = Module(new MissQueue(edge)) 853 val probeQueue = Module(new ProbeQueue(edge)) 854 val wb = Module(new WritebackQueue(edge)) 855 856 missQueue.io.lqEmpty := io.lqEmpty 857 missQueue.io.hartId := io.hartId 858 missQueue.io.l2_pf_store_only := RegNext(io.l2_pf_store_only, false.B) 859 missQueue.io.debugTopDown <> io.debugTopDown 860 missQueue.io.sms_agt_evict_req <> io.sms_agt_evict_req 861 io.memSetPattenDetected := missQueue.io.memSetPattenDetected 862 863 val errors = ldu.map(_.io.error) ++ // load error 864 Seq(mainPipe.io.error) // store / misc error 865 io.error <> RegNext(Mux1H(errors.map(e => RegNext(e.valid) -> RegNext(e)))) 866 867 //---------------------------------------- 868 // meta array 869 val HybridLoadReadBase = LoadPipelineWidth - backendParams.HyuCnt 870 val HybridStoreReadBase = StorePipelineWidth - backendParams.HyuCnt 871 872 val hybrid_meta_read_ports = Wire(Vec(backendParams.HyuCnt, DecoupledIO(new MetaReadReq))) 873 val hybrid_meta_resp_ports = Wire(Vec(backendParams.HyuCnt, ldu(0).io.meta_resp.cloneType)) 874 for (i <- 0 until backendParams.HyuCnt) { 875 val HybridLoadMetaReadPort = HybridLoadReadBase + i 876 val HybridStoreMetaReadPort = HybridStoreReadBase + i 877 878 hybrid_meta_read_ports(i).valid := ldu(HybridLoadMetaReadPort).io.meta_read.valid || 879 (stu(HybridStoreMetaReadPort).io.meta_read.valid && StorePrefetchL1Enabled.B) 880 hybrid_meta_read_ports(i).bits := Mux(ldu(HybridLoadMetaReadPort).io.meta_read.valid, ldu(HybridLoadMetaReadPort).io.meta_read.bits, 881 stu(HybridStoreMetaReadPort).io.meta_read.bits) 882 883 ldu(HybridLoadMetaReadPort).io.meta_read.ready := hybrid_meta_read_ports(i).ready 884 stu(HybridStoreMetaReadPort).io.meta_read.ready := hybrid_meta_read_ports(i).ready && StorePrefetchL1Enabled.B 885 886 ldu(HybridLoadMetaReadPort).io.meta_resp := hybrid_meta_resp_ports(i) 887 stu(HybridStoreMetaReadPort).io.meta_resp := hybrid_meta_resp_ports(i) 888 } 889 890 // read / write coh meta 891 val meta_read_ports = ldu.map(_.io.meta_read).take(HybridLoadReadBase) ++ 892 Seq(mainPipe.io.meta_read) ++ 893 stu.map(_.io.meta_read).take(HybridStoreReadBase) ++ hybrid_meta_read_ports 894 895 val meta_resp_ports = ldu.map(_.io.meta_resp).take(HybridLoadReadBase) ++ 896 Seq(mainPipe.io.meta_resp) ++ 897 stu.map(_.io.meta_resp).take(HybridStoreReadBase) ++ hybrid_meta_resp_ports 898 899 val meta_write_ports = Seq( 900 mainPipe.io.meta_write, 901 refillPipe.io.meta_write 902 ) 903 if(StorePrefetchL1Enabled) { 904 meta_read_ports.zip(metaArray.io.read).foreach { case (p, r) => r <> p } 905 meta_resp_ports.zip(metaArray.io.resp).foreach { case (p, r) => p := r } 906 } else { 907 (meta_read_ports.take(HybridLoadReadBase + 1) ++ 908 meta_read_ports.takeRight(backendParams.HyuCnt)).zip(metaArray.io.read).foreach { case (p, r) => r <> p } 909 (meta_resp_ports.take(HybridLoadReadBase + 1) ++ 910 meta_resp_ports.takeRight(backendParams.HyuCnt)).zip(metaArray.io.resp).foreach { case (p, r) => p := r } 911 912 meta_read_ports.drop(HybridLoadReadBase + 1).take(HybridStoreReadBase).foreach { case p => p.ready := false.B } 913 meta_resp_ports.drop(HybridLoadReadBase + 1).take(HybridStoreReadBase).foreach { case p => p := 0.U.asTypeOf(p) } 914 } 915 meta_write_ports.zip(metaArray.io.write).foreach { case (p, w) => w <> p } 916 917 // read extra meta (exclude stu) 918 (meta_read_ports.take(HybridLoadReadBase + 1) ++ 919 meta_read_ports.takeRight(backendParams.HyuCnt)).zip(errorArray.io.read).foreach { case (p, r) => r <> p } 920 (meta_read_ports.take(HybridLoadReadBase + 1) ++ 921 meta_read_ports.takeRight(backendParams.HyuCnt)).zip(prefetchArray.io.read).foreach { case (p, r) => r <> p } 922 (meta_read_ports.take(HybridLoadReadBase + 1) ++ 923 meta_read_ports.takeRight(backendParams.HyuCnt)).zip(accessArray.io.read).foreach { case (p, r) => r <> p } 924 val extra_meta_resp_ports = ldu.map(_.io.extra_meta_resp).take(HybridLoadReadBase) ++ 925 Seq(mainPipe.io.extra_meta_resp) ++ 926 ldu.map(_.io.extra_meta_resp).takeRight(backendParams.HyuCnt) 927 extra_meta_resp_ports.zip(errorArray.io.resp).foreach { case (p, r) => { 928 (0 until nWays).map(i => { p(i).error := r(i) }) 929 }} 930 extra_meta_resp_ports.zip(prefetchArray.io.resp).foreach { case (p, r) => { 931 (0 until nWays).map(i => { p(i).prefetch := r(i) }) 932 }} 933 extra_meta_resp_ports.zip(accessArray.io.resp).foreach { case (p, r) => { 934 (0 until nWays).map(i => { p(i).access := r(i) }) 935 }} 936 937 if(LoadPrefetchL1Enabled) { 938 // use last port to read prefetch and access flag 939 prefetchArray.io.read.last.valid := refillPipe.io.prefetch_flag_write.valid 940 prefetchArray.io.read.last.bits.idx := refillPipe.io.prefetch_flag_write.bits.idx 941 prefetchArray.io.read.last.bits.way_en := refillPipe.io.prefetch_flag_write.bits.way_en 942 943 accessArray.io.read.last.valid := refillPipe.io.prefetch_flag_write.valid 944 accessArray.io.read.last.bits.idx := refillPipe.io.prefetch_flag_write.bits.idx 945 accessArray.io.read.last.bits.way_en := refillPipe.io.prefetch_flag_write.bits.way_en 946 947 val extra_flag_valid = RegNext(refillPipe.io.prefetch_flag_write.valid) 948 val extra_flag_way_en = RegEnable(refillPipe.io.prefetch_flag_write.bits.way_en, refillPipe.io.prefetch_flag_write.valid) 949 val extra_flag_prefetch = Mux1H(extra_flag_way_en, prefetchArray.io.resp.last) 950 val extra_flag_access = Mux1H(extra_flag_way_en, accessArray.io.resp.last) 951 952 prefetcherMonitor.io.validity.good_prefetch := extra_flag_valid && isFromL1Prefetch(extra_flag_prefetch) && extra_flag_access 953 prefetcherMonitor.io.validity.bad_prefetch := extra_flag_valid && isFromL1Prefetch(extra_flag_prefetch) && !extra_flag_access 954 } 955 956 // write extra meta 957 val error_flag_write_ports = Seq( 958 mainPipe.io.error_flag_write, // error flag generated by corrupted store 959 refillPipe.io.error_flag_write // corrupted signal from l2 960 ) 961 error_flag_write_ports.zip(errorArray.io.write).foreach { case (p, w) => w <> p } 962 963 val prefetch_flag_write_ports = ldu.map(_.io.prefetch_flag_write) ++ Seq( 964 mainPipe.io.prefetch_flag_write, // set prefetch_flag to false if coh is set to Nothing 965 refillPipe.io.prefetch_flag_write // refill required by prefetch will set prefetch_flag 966 ) 967 prefetch_flag_write_ports.zip(prefetchArray.io.write).foreach { case (p, w) => w <> p } 968 969 // FIXME: add hybrid unit? 970 val same_cycle_update_pf_flag = ldu(0).io.prefetch_flag_write.valid && ldu(1).io.prefetch_flag_write.valid && (ldu(0).io.prefetch_flag_write.bits.idx === ldu(1).io.prefetch_flag_write.bits.idx) && (ldu(0).io.prefetch_flag_write.bits.way_en === ldu(1).io.prefetch_flag_write.bits.way_en) 971 XSPerfAccumulate("same_cycle_update_pf_flag", same_cycle_update_pf_flag) 972 973 val access_flag_write_ports = ldu.map(_.io.access_flag_write) ++ Seq( 974 mainPipe.io.access_flag_write, 975 refillPipe.io.access_flag_write 976 ) 977 access_flag_write_ports.zip(accessArray.io.write).foreach { case (p, w) => w <> p } 978 979 //---------------------------------------- 980 // tag array 981 if(StorePrefetchL1Enabled) { 982 require(tagArray.io.read.size == (LoadPipelineWidth + StorePipelineWidth - backendParams.HyuCnt + 1)) 983 }else { 984 require(tagArray.io.read.size == (LoadPipelineWidth + 1)) 985 } 986 val tag_write_intend = missQueue.io.refill_pipe_req.valid || mainPipe.io.tag_write_intend 987 assert(!RegNext(!tag_write_intend && tagArray.io.write.valid)) 988 ldu.take(HybridLoadReadBase).zipWithIndex.foreach { 989 case (ld, i) => 990 tagArray.io.read(i) <> ld.io.tag_read 991 ld.io.tag_resp := tagArray.io.resp(i) 992 ld.io.tag_read.ready := !tag_write_intend 993 } 994 if(StorePrefetchL1Enabled) { 995 stu.take(HybridStoreReadBase).zipWithIndex.foreach { 996 case (st, i) => 997 tagArray.io.read(HybridLoadReadBase + i) <> st.io.tag_read 998 st.io.tag_resp := tagArray.io.resp(HybridLoadReadBase + i) 999 st.io.tag_read.ready := !tag_write_intend 1000 } 1001 }else { 1002 stu.foreach { 1003 case st => 1004 st.io.tag_read.ready := false.B 1005 st.io.tag_resp := 0.U.asTypeOf(st.io.tag_resp) 1006 } 1007 } 1008 for (i <- 0 until backendParams.HyuCnt) { 1009 val HybridLoadTagReadPort = HybridLoadReadBase + i 1010 val HybridStoreTagReadPort = HybridStoreReadBase + i 1011 val TagReadPort = 1012 if (EnableStorePrefetchSPB) 1013 HybridLoadReadBase + HybridStoreReadBase + i 1014 else 1015 HybridLoadReadBase + i 1016 1017 // read tag 1018 ldu(HybridLoadTagReadPort).io.tag_read.ready := false.B 1019 stu(HybridStoreTagReadPort).io.tag_read.ready := false.B 1020 1021 if (StorePrefetchL1Enabled) { 1022 when (ldu(HybridLoadTagReadPort).io.tag_read.valid) { 1023 tagArray.io.read(TagReadPort) <> ldu(HybridLoadTagReadPort).io.tag_read 1024 ldu(HybridLoadTagReadPort).io.tag_read.ready := !tag_write_intend 1025 } .otherwise { 1026 tagArray.io.read(TagReadPort) <> stu(HybridStoreTagReadPort).io.tag_read 1027 stu(HybridStoreTagReadPort).io.tag_read.ready := !tag_write_intend 1028 } 1029 } else { 1030 tagArray.io.read(TagReadPort) <> ldu(HybridLoadTagReadPort).io.tag_read 1031 ldu(HybridLoadTagReadPort).io.tag_read.ready := !tag_write_intend 1032 } 1033 1034 // tag resp 1035 ldu(HybridLoadTagReadPort).io.tag_resp := tagArray.io.resp(TagReadPort) 1036 stu(HybridStoreTagReadPort).io.tag_resp := tagArray.io.resp(TagReadPort) 1037 } 1038 tagArray.io.read.last <> mainPipe.io.tag_read 1039 mainPipe.io.tag_resp := tagArray.io.resp.last 1040 1041 val fake_tag_read_conflict_this_cycle = PopCount(ldu.map(ld=> ld.io.tag_read.valid)) 1042 XSPerfAccumulate("fake_tag_read_conflict", fake_tag_read_conflict_this_cycle) 1043 1044 val tag_write_arb = Module(new Arbiter(new TagWriteReq, 2)) 1045 tag_write_arb.io.in(0) <> refillPipe.io.tag_write 1046 tag_write_arb.io.in(1) <> mainPipe.io.tag_write 1047 tagArray.io.write <> tag_write_arb.io.out 1048 1049 ldu.map(m => { 1050 m.io.vtag_update.valid := tagArray.io.write.valid 1051 m.io.vtag_update.bits := tagArray.io.write.bits 1052 }) 1053 1054 //---------------------------------------- 1055 // data array 1056 mainPipe.io.data_read.zip(ldu).map(x => x._1 := x._2.io.lsu.req.valid) 1057 1058 val dataWriteArb = Module(new Arbiter(new L1BankedDataWriteReq, 2)) 1059 dataWriteArb.io.in(0) <> refillPipe.io.data_write 1060 dataWriteArb.io.in(1) <> mainPipe.io.data_write 1061 1062 bankedDataArray.io.write <> dataWriteArb.io.out 1063 1064 for (bank <- 0 until DCacheBanks) { 1065 val dataWriteArb_dup = Module(new Arbiter(new L1BankedDataWriteReqCtrl, 2)) 1066 dataWriteArb_dup.io.in(0).valid := refillPipe.io.data_write_dup(bank).valid 1067 dataWriteArb_dup.io.in(0).bits := refillPipe.io.data_write_dup(bank).bits 1068 dataWriteArb_dup.io.in(1).valid := mainPipe.io.data_write_dup(bank).valid 1069 dataWriteArb_dup.io.in(1).bits := mainPipe.io.data_write_dup(bank).bits 1070 1071 bankedDataArray.io.write_dup(bank) <> dataWriteArb_dup.io.out 1072 } 1073 1074 bankedDataArray.io.readline <> mainPipe.io.data_readline 1075 bankedDataArray.io.readline_intend := mainPipe.io.data_read_intend 1076 mainPipe.io.readline_error_delayed := bankedDataArray.io.readline_error_delayed 1077 mainPipe.io.data_resp := bankedDataArray.io.readline_resp 1078 1079 (0 until LoadPipelineWidth).map(i => { 1080 bankedDataArray.io.read(i) <> ldu(i).io.banked_data_read 1081 bankedDataArray.io.is128Req(i) <> ldu(i).io.is128Req 1082 bankedDataArray.io.read_error_delayed(i) <> ldu(i).io.read_error_delayed 1083 1084 ldu(i).io.banked_data_resp := bankedDataArray.io.read_resp_delayed(i) 1085 1086 ldu(i).io.bank_conflict_slow := bankedDataArray.io.bank_conflict_slow(i) 1087 }) 1088 1089 (0 until LoadPipelineWidth).map(i => { 1090 val (_, _, done, _) = edge.count(bus.d) 1091 when(bus.d.bits.opcode === TLMessages.GrantData) { 1092 io.lsu.forward_D(i).apply(bus.d.valid, bus.d.bits.data, bus.d.bits.source, done) 1093 }.otherwise { 1094 io.lsu.forward_D(i).dontCare() 1095 } 1096 }) 1097 // tl D channel wakeup 1098 val (_, _, done, _) = edge.count(bus.d) 1099 when (bus.d.bits.opcode === TLMessages.GrantData || bus.d.bits.opcode === TLMessages.Grant) { 1100 io.lsu.tl_d_channel.apply(bus.d.valid, bus.d.bits.data, bus.d.bits.source, done) 1101 } .otherwise { 1102 io.lsu.tl_d_channel.dontCare() 1103 } 1104 mainPipe.io.force_write <> io.force_write 1105 1106 /** dwpu */ 1107 val dwpu = Module(new DCacheWpuWrapper(LoadPipelineWidth)) 1108 for(i <- 0 until LoadPipelineWidth){ 1109 dwpu.io.req(i) <> ldu(i).io.dwpu.req(0) 1110 dwpu.io.resp(i) <> ldu(i).io.dwpu.resp(0) 1111 dwpu.io.lookup_upd(i) <> ldu(i).io.dwpu.lookup_upd(0) 1112 dwpu.io.cfpred(i) <> ldu(i).io.dwpu.cfpred(0) 1113 } 1114 dwpu.io.tagwrite_upd.valid := tagArray.io.write.valid 1115 dwpu.io.tagwrite_upd.bits.vaddr := tagArray.io.write.bits.vaddr 1116 dwpu.io.tagwrite_upd.bits.s1_real_way_en := tagArray.io.write.bits.way_en 1117 1118 //---------------------------------------- 1119 // load pipe 1120 // the s1 kill signal 1121 // only lsu uses this, replay never kills 1122 for (w <- 0 until LoadPipelineWidth) { 1123 ldu(w).io.lsu <> io.lsu.load(w) 1124 1125 // TODO:when have load128Req 1126 ldu(w).io.load128Req := io.lsu.load(w).is128Req 1127 1128 // replay and nack not needed anymore 1129 // TODO: remove replay and nack 1130 ldu(w).io.nack := false.B 1131 1132 ldu(w).io.disable_ld_fast_wakeup := 1133 bankedDataArray.io.disable_ld_fast_wakeup(w) // load pipe fast wake up should be disabled when bank conflict 1134 } 1135 1136 prefetcherMonitor.io.timely.total_prefetch := ldu.map(_.io.prefetch_info.naive.total_prefetch).reduce(_ || _) 1137 prefetcherMonitor.io.timely.late_hit_prefetch := ldu.map(_.io.prefetch_info.naive.late_hit_prefetch).reduce(_ || _) 1138 prefetcherMonitor.io.timely.late_miss_prefetch := missQueue.io.prefetch_info.naive.late_miss_prefetch 1139 prefetcherMonitor.io.timely.prefetch_hit := PopCount(ldu.map(_.io.prefetch_info.naive.prefetch_hit)) 1140 io.pf_ctrl <> prefetcherMonitor.io.pf_ctrl 1141 XSPerfAccumulate("useless_prefetch", ldu.map(_.io.prefetch_info.naive.total_prefetch).reduce(_ || _) && !(ldu.map(_.io.prefetch_info.naive.useful_prefetch).reduce(_ || _))) 1142 XSPerfAccumulate("useful_prefetch", ldu.map(_.io.prefetch_info.naive.useful_prefetch).reduce(_ || _)) 1143 XSPerfAccumulate("late_prefetch_hit", ldu.map(_.io.prefetch_info.naive.late_prefetch_hit).reduce(_ || _)) 1144 XSPerfAccumulate("late_load_hit", ldu.map(_.io.prefetch_info.naive.late_load_hit).reduce(_ || _)) 1145 1146 /** LoadMissDB: record load miss state */ 1147 val isWriteLoadMissTable = WireInit(Constantin.createRecord("isWriteLoadMissTable" + p(XSCoreParamsKey).HartId.toString)) 1148 val isFirstHitWrite = WireInit(Constantin.createRecord("isFirstHitWrite" + p(XSCoreParamsKey).HartId.toString)) 1149 val tableName = "LoadMissDB" + p(XSCoreParamsKey).HartId.toString 1150 val siteName = "DcacheWrapper" + p(XSCoreParamsKey).HartId.toString 1151 val loadMissTable = ChiselDB.createTable(tableName, new LoadMissEntry) 1152 for( i <- 0 until LoadPipelineWidth){ 1153 val loadMissEntry = Wire(new LoadMissEntry) 1154 val loadMissWriteEn = 1155 (!ldu(i).io.lsu.resp.bits.replay && ldu(i).io.miss_req.fire) || 1156 (ldu(i).io.lsu.s2_first_hit && ldu(i).io.lsu.resp.valid && isFirstHitWrite.orR) 1157 loadMissEntry.timeCnt := GTimer() 1158 loadMissEntry.robIdx := ldu(i).io.lsu.resp.bits.debug_robIdx 1159 loadMissEntry.paddr := ldu(i).io.miss_req.bits.addr 1160 loadMissEntry.vaddr := ldu(i).io.miss_req.bits.vaddr 1161 loadMissEntry.missState := OHToUInt(Cat(Seq( 1162 ldu(i).io.miss_req.fire & ldu(i).io.miss_resp.merged, 1163 ldu(i).io.miss_req.fire & !ldu(i).io.miss_resp.merged, 1164 ldu(i).io.lsu.s2_first_hit && ldu(i).io.lsu.resp.valid 1165 ))) 1166 loadMissTable.log( 1167 data = loadMissEntry, 1168 en = isWriteLoadMissTable.orR && loadMissWriteEn, 1169 site = siteName, 1170 clock = clock, 1171 reset = reset 1172 ) 1173 } 1174 1175 val isWriteLoadAccessTable = WireInit(Constantin.createRecord("isWriteLoadAccessTable" + p(XSCoreParamsKey).HartId.toString)) 1176 val loadAccessTable = ChiselDB.createTable("LoadAccessDB" + p(XSCoreParamsKey).HartId.toString, new LoadAccessEntry) 1177 for (i <- 0 until LoadPipelineWidth) { 1178 val loadAccessEntry = Wire(new LoadAccessEntry) 1179 loadAccessEntry.timeCnt := GTimer() 1180 loadAccessEntry.robIdx := ldu(i).io.lsu.resp.bits.debug_robIdx 1181 loadAccessEntry.paddr := ldu(i).io.miss_req.bits.addr 1182 loadAccessEntry.vaddr := ldu(i).io.miss_req.bits.vaddr 1183 loadAccessEntry.missState := OHToUInt(Cat(Seq( 1184 ldu(i).io.miss_req.fire & ldu(i).io.miss_resp.merged, 1185 ldu(i).io.miss_req.fire & !ldu(i).io.miss_resp.merged, 1186 ldu(i).io.lsu.s2_first_hit && ldu(i).io.lsu.resp.valid 1187 ))) 1188 loadAccessEntry.pred_way_num := ldu(i).io.lsu.debug_s2_pred_way_num 1189 loadAccessEntry.real_way_num := ldu(i).io.lsu.debug_s2_real_way_num 1190 loadAccessEntry.dm_way_num := ldu(i).io.lsu.debug_s2_dm_way_num 1191 loadAccessTable.log( 1192 data = loadAccessEntry, 1193 en = isWriteLoadAccessTable.orR && ldu(i).io.lsu.resp.valid, 1194 site = siteName + "_loadpipe" + i.toString, 1195 clock = clock, 1196 reset = reset 1197 ) 1198 } 1199 1200 //---------------------------------------- 1201 // Sta pipe 1202 for (w <- 0 until StorePipelineWidth) { 1203 stu(w).io.lsu <> io.lsu.sta(w) 1204 } 1205 1206 //---------------------------------------- 1207 // atomics 1208 // atomics not finished yet 1209 // io.lsu.atomics <> atomicsReplayUnit.io.lsu 1210 io.lsu.atomics.resp := RegNext(mainPipe.io.atomic_resp) 1211 io.lsu.atomics.block_lr := mainPipe.io.block_lr 1212 // atomicsReplayUnit.io.pipe_resp := RegNext(mainPipe.io.atomic_resp) 1213 // atomicsReplayUnit.io.block_lr <> mainPipe.io.block_lr 1214 1215 //---------------------------------------- 1216 // miss queue 1217 // missReqArb port: 1218 // enableStorePrefetch: main pipe * 1 + load pipe * 2 + store pipe * 1 + 1219 // hybrid * 1; disable: main pipe * 1 + load pipe * 2 + hybrid * 1 1220 // higher priority is given to lower indices 1221 val MissReqPortCount = if(StorePrefetchL1Enabled) 1 + backendParams.LduCnt + backendParams.StaCnt + backendParams.HyuCnt else 1 + backendParams.LduCnt + backendParams.HyuCnt 1222 val MainPipeMissReqPort = 0 1223 val HybridMissReqBase = MissReqPortCount - backendParams.HyuCnt 1224 1225 // Request 1226 val missReqArb = Module(new ArbiterFilterByCacheLineAddr(new MissReq, MissReqPortCount, blockOffBits, PAddrBits)) 1227 1228 missReqArb.io.in(MainPipeMissReqPort) <> mainPipe.io.miss_req 1229 for (w <- 0 until backendParams.LduCnt) { missReqArb.io.in(w + 1) <> ldu(w).io.miss_req } 1230 1231 for (w <- 0 until LoadPipelineWidth) { ldu(w).io.miss_resp := missQueue.io.resp } 1232 mainPipe.io.miss_resp := missQueue.io.resp 1233 1234 if(StorePrefetchL1Enabled) { 1235 for (w <- 0 until backendParams.StaCnt) { missReqArb.io.in(1 + backendParams.LduCnt + w) <> stu(w).io.miss_req } 1236 }else { 1237 for (w <- 0 until backendParams.StaCnt) { stu(w).io.miss_req.ready := false.B } 1238 } 1239 1240 for (i <- 0 until backendParams.HyuCnt) { 1241 val HybridLoadReqPort = HybridLoadReadBase + i 1242 val HybridStoreReqPort = HybridStoreReadBase + i 1243 val HybridMissReqPort = HybridMissReqBase + i 1244 1245 ldu(HybridLoadReqPort).io.miss_req.ready := false.B 1246 stu(HybridStoreReqPort).io.miss_req.ready := false.B 1247 1248 if (StorePrefetchL1Enabled) { 1249 when (ldu(HybridLoadReqPort).io.miss_req.valid) { 1250 missReqArb.io.in(HybridMissReqPort) <> ldu(HybridLoadReqPort).io.miss_req 1251 } .otherwise { 1252 missReqArb.io.in(HybridMissReqPort) <> stu(HybridStoreReqPort).io.miss_req 1253 } 1254 } else { 1255 missReqArb.io.in(HybridMissReqPort) <> ldu(HybridLoadReqPort).io.miss_req 1256 } 1257 } 1258 1259 1260 wb.io.miss_req.valid := missReqArb.io.out.valid 1261 wb.io.miss_req.bits := missReqArb.io.out.bits.addr 1262 1263 // block_decoupled(missReqArb.io.out, missQueue.io.req, wb.io.block_miss_req) 1264 missReqArb.io.out <> missQueue.io.req 1265 when(wb.io.block_miss_req) { 1266 missQueue.io.req.bits.cancel := true.B 1267 missReqArb.io.out.ready := false.B 1268 } 1269 1270 for (w <- 0 until LoadPipelineWidth) { ldu(w).io.mq_enq_cancel := missQueue.io.mq_enq_cancel } 1271 1272 XSPerfAccumulate("miss_queue_fire", PopCount(VecInit(missReqArb.io.in.map(_.fire))) >= 1.U) 1273 XSPerfAccumulate("miss_queue_muti_fire", PopCount(VecInit(missReqArb.io.in.map(_.fire))) > 1.U) 1274 1275 XSPerfAccumulate("miss_queue_has_enq_req", PopCount(VecInit(missReqArb.io.in.map(_.valid))) >= 1.U) 1276 XSPerfAccumulate("miss_queue_has_muti_enq_req", PopCount(VecInit(missReqArb.io.in.map(_.valid))) > 1.U) 1277 XSPerfAccumulate("miss_queue_has_muti_enq_but_not_fire", PopCount(VecInit(missReqArb.io.in.map(_.valid))) > 1.U && PopCount(VecInit(missReqArb.io.in.map(_.fire))) === 0.U) 1278 1279 // forward missqueue 1280 (0 until LoadPipelineWidth).map(i => io.lsu.forward_mshr(i).connect(missQueue.io.forward(i))) 1281 1282 // refill to load queue 1283 io.lsu.lsq <> missQueue.io.refill_to_ldq 1284 1285 // tilelink stuff 1286 bus.a <> missQueue.io.mem_acquire 1287 bus.e <> missQueue.io.mem_finish 1288 missQueue.io.probe_addr := bus.b.bits.address 1289 1290 missQueue.io.main_pipe_resp := RegNext(mainPipe.io.atomic_resp) 1291 1292 //---------------------------------------- 1293 // probe 1294 // probeQueue.io.mem_probe <> bus.b 1295 block_decoupled(bus.b, probeQueue.io.mem_probe, missQueue.io.probe_block) 1296 probeQueue.io.lrsc_locked_block <> mainPipe.io.lrsc_locked_block 1297 probeQueue.io.update_resv_set <> mainPipe.io.update_resv_set 1298 1299 //---------------------------------------- 1300 // mainPipe 1301 // when a req enters main pipe, if it is set-conflict with replace pipe or refill pipe, 1302 // block the req in main pipe 1303 block_decoupled(probeQueue.io.pipe_req, mainPipe.io.probe_req, missQueue.io.refill_pipe_req.valid) 1304 block_decoupled(io.lsu.store.req, mainPipe.io.store_req, refillPipe.io.req.valid) 1305 1306 io.lsu.store.replay_resp := RegNext(mainPipe.io.store_replay_resp) 1307 io.lsu.store.main_pipe_hit_resp := mainPipe.io.store_hit_resp 1308 1309 arbiter_with_pipereg( 1310 in = Seq(missQueue.io.main_pipe_req, io.lsu.atomics.req), 1311 out = mainPipe.io.atomic_req, 1312 name = Some("main_pipe_atomic_req") 1313 ) 1314 1315 mainPipe.io.invalid_resv_set := RegNext(wb.io.req.fire && wb.io.req.bits.addr === mainPipe.io.lrsc_locked_block.bits) 1316 1317 //---------------------------------------- 1318 // replace (main pipe) 1319 val mpStatus = mainPipe.io.status 1320 mainPipe.io.replace_req <> missQueue.io.replace_pipe_req 1321 missQueue.io.replace_pipe_resp := mainPipe.io.replace_resp 1322 1323 //---------------------------------------- 1324 // refill pipe 1325 val refillShouldBeBlocked = (mpStatus.s1.valid && mpStatus.s1.bits.set === missQueue.io.refill_pipe_req.bits.idx) || 1326 Cat(Seq(mpStatus.s2, mpStatus.s3).map(s => 1327 s.valid && 1328 s.bits.set === missQueue.io.refill_pipe_req.bits.idx && 1329 s.bits.way_en === missQueue.io.refill_pipe_req.bits.way_en 1330 )).orR 1331 block_decoupled(missQueue.io.refill_pipe_req, refillPipe.io.req, refillShouldBeBlocked) 1332 1333 val mpStatus_dup = mainPipe.io.status_dup 1334 val mq_refill_dup = missQueue.io.refill_pipe_req_dup 1335 val refillShouldBeBlocked_dup = VecInit((0 until nDupStatus).map { case i => 1336 mpStatus_dup(i).s1.valid && mpStatus_dup(i).s1.bits.set === mq_refill_dup(i).bits.idx || 1337 Cat(Seq(mpStatus_dup(i).s2, mpStatus_dup(i).s3).map(s => 1338 s.valid && 1339 s.bits.set === mq_refill_dup(i).bits.idx && 1340 s.bits.way_en === mq_refill_dup(i).bits.way_en 1341 )).orR 1342 }) 1343 dontTouch(refillShouldBeBlocked_dup) 1344 1345 refillPipe.io.req_dup_for_data_w.zipWithIndex.foreach { case (r, i) => 1346 r.bits := (mq_refill_dup.drop(dataWritePort).take(DCacheBanks))(i).bits 1347 } 1348 refillPipe.io.req_dup_for_meta_w.bits := mq_refill_dup(metaWritePort).bits 1349 refillPipe.io.req_dup_for_tag_w.bits := mq_refill_dup(tagWritePort).bits 1350 refillPipe.io.req_dup_for_err_w.bits := mq_refill_dup(errWritePort).bits 1351 refillPipe.io.req_dup_for_data_w.zipWithIndex.foreach { case (r, i) => 1352 r.valid := (mq_refill_dup.drop(dataWritePort).take(DCacheBanks))(i).valid && 1353 !(refillShouldBeBlocked_dup.drop(dataWritePort).take(DCacheBanks))(i) 1354 } 1355 refillPipe.io.req_dup_for_meta_w.valid := mq_refill_dup(metaWritePort).valid && !refillShouldBeBlocked_dup(metaWritePort) 1356 refillPipe.io.req_dup_for_tag_w.valid := mq_refill_dup(tagWritePort).valid && !refillShouldBeBlocked_dup(tagWritePort) 1357 refillPipe.io.req_dup_for_err_w.valid := mq_refill_dup(errWritePort).valid && !refillShouldBeBlocked_dup(errWritePort) 1358 1359 val refillPipe_io_req_valid_dup = VecInit(mq_refill_dup.zip(refillShouldBeBlocked_dup).map( 1360 x => x._1.valid && !x._2 1361 )) 1362 val refillPipe_io_data_write_valid_dup = VecInit(refillPipe_io_req_valid_dup.slice(0, nDupDataWriteReady)) 1363 val refillPipe_io_tag_write_valid_dup = VecInit(refillPipe_io_req_valid_dup.slice(nDupDataWriteReady, nDupStatus)) 1364 dontTouch(refillPipe_io_req_valid_dup) 1365 dontTouch(refillPipe_io_data_write_valid_dup) 1366 dontTouch(refillPipe_io_tag_write_valid_dup) 1367 mainPipe.io.data_write_ready_dup := VecInit(refillPipe_io_data_write_valid_dup.map(v => !v)) 1368 mainPipe.io.tag_write_ready_dup := VecInit(refillPipe_io_tag_write_valid_dup.map(v => !v)) 1369 mainPipe.io.wb_ready_dup := wb.io.req_ready_dup 1370 1371 mq_refill_dup.zip(refillShouldBeBlocked_dup).foreach { case (r, block) => 1372 r.ready := refillPipe.io.req.ready && !block 1373 } 1374 1375 missQueue.io.refill_pipe_resp := refillPipe.io.resp 1376 io.lsu.store.refill_hit_resp := RegNext(refillPipe.io.store_resp) 1377 1378 //---------------------------------------- 1379 // wb 1380 // add a queue between MainPipe and WritebackUnit to reduce MainPipe stalls due to WritebackUnit busy 1381 1382 wb.io.req <> mainPipe.io.wb 1383 bus.c <> wb.io.mem_release 1384 wb.io.release_wakeup := refillPipe.io.release_wakeup 1385 wb.io.release_update := mainPipe.io.release_update 1386 wb.io.probe_ttob_check_req <> mainPipe.io.probe_ttob_check_req 1387 wb.io.probe_ttob_check_resp <> mainPipe.io.probe_ttob_check_resp 1388 1389 io.lsu.release.valid := RegNext(wb.io.req.fire) 1390 io.lsu.release.bits.paddr := RegNext(wb.io.req.bits.addr) 1391 // Note: RegNext() is required by: 1392 // * load queue released flag update logic 1393 // * load / load violation check logic 1394 // * and timing requirements 1395 // CHANGE IT WITH CARE 1396 1397 // connect bus d 1398 missQueue.io.mem_grant.valid := false.B 1399 missQueue.io.mem_grant.bits := DontCare 1400 1401 wb.io.mem_grant.valid := false.B 1402 wb.io.mem_grant.bits := DontCare 1403 1404 // in L1DCache, we ony expect Grant[Data] and ReleaseAck 1405 bus.d.ready := false.B 1406 when (bus.d.bits.opcode === TLMessages.Grant || bus.d.bits.opcode === TLMessages.GrantData) { 1407 missQueue.io.mem_grant <> bus.d 1408 } .elsewhen (bus.d.bits.opcode === TLMessages.ReleaseAck) { 1409 wb.io.mem_grant <> bus.d 1410 } .otherwise { 1411 assert (!bus.d.fire) 1412 } 1413 1414 //---------------------------------------- 1415 // Feedback Direct Prefetch Monitor 1416 fdpMonitor.io.refill := missQueue.io.prefetch_info.fdp.prefetch_monitor_cnt 1417 fdpMonitor.io.timely.late_prefetch := missQueue.io.prefetch_info.fdp.late_miss_prefetch 1418 fdpMonitor.io.accuracy.total_prefetch := missQueue.io.prefetch_info.fdp.total_prefetch 1419 for (w <- 0 until LoadPipelineWidth) { 1420 if(w == 0) { 1421 fdpMonitor.io.accuracy.useful_prefetch(w) := ldu(w).io.prefetch_info.fdp.useful_prefetch 1422 }else { 1423 fdpMonitor.io.accuracy.useful_prefetch(w) := Mux(same_cycle_update_pf_flag, false.B, ldu(w).io.prefetch_info.fdp.useful_prefetch) 1424 } 1425 } 1426 for (w <- 0 until LoadPipelineWidth) { fdpMonitor.io.pollution.cache_pollution(w) := ldu(w).io.prefetch_info.fdp.pollution } 1427 for (w <- 0 until LoadPipelineWidth) { fdpMonitor.io.pollution.demand_miss(w) := ldu(w).io.prefetch_info.fdp.demand_miss } 1428 fdpMonitor.io.debugRolling := io.debugRolling 1429 1430 //---------------------------------------- 1431 // Bloom Filter 1432 bloomFilter.io.set <> missQueue.io.bloom_filter_query.set 1433 bloomFilter.io.clr <> missQueue.io.bloom_filter_query.clr 1434 1435 for (w <- 0 until LoadPipelineWidth) { bloomFilter.io.query(w) <> ldu(w).io.bloom_filter_query.query } 1436 for (w <- 0 until LoadPipelineWidth) { bloomFilter.io.resp(w) <> ldu(w).io.bloom_filter_query.resp } 1437 1438 for (w <- 0 until LoadPipelineWidth) { counterFilter.io.ld_in(w) <> ldu(w).io.counter_filter_enq } 1439 for (w <- 0 until LoadPipelineWidth) { counterFilter.io.query(w) <> ldu(w).io.counter_filter_query } 1440 1441 //---------------------------------------- 1442 // replacement algorithm 1443 val replacer = ReplacementPolicy.fromString(cacheParams.replacer, nWays, nSets) 1444 val replWayReqs = ldu.map(_.io.replace_way) ++ Seq(mainPipe.io.replace_way) ++ stu.map(_.io.replace_way) 1445 1446 val victimList = VictimList(nSets) 1447 if (dwpuParam.enCfPred) { 1448 when(missQueue.io.replace_pipe_req.valid) { 1449 victimList.replace(get_idx(missQueue.io.replace_pipe_req.bits.vaddr)) 1450 } 1451 replWayReqs.foreach { 1452 case req => 1453 req.way := DontCare 1454 when(req.set.valid) { 1455 when(victimList.whether_sa(req.set.bits)) { 1456 req.way := replacer.way(req.set.bits) 1457 }.otherwise { 1458 req.way := req.dmWay 1459 } 1460 } 1461 } 1462 } else { 1463 replWayReqs.foreach { 1464 case req => 1465 req.way := DontCare 1466 when(req.set.valid) { 1467 req.way := replacer.way(req.set.bits) 1468 } 1469 } 1470 } 1471 1472 val replAccessReqs = ldu.map(_.io.replace_access) ++ Seq( 1473 mainPipe.io.replace_access 1474 ) ++ stu.map(_.io.replace_access) 1475 val touchWays = Seq.fill(replAccessReqs.size)(Wire(ValidIO(UInt(log2Up(nWays).W)))) 1476 touchWays.zip(replAccessReqs).foreach { 1477 case (w, req) => 1478 w.valid := req.valid 1479 w.bits := req.bits.way 1480 } 1481 val touchSets = replAccessReqs.map(_.bits.set) 1482 replacer.access(touchSets, touchWays) 1483 1484 //---------------------------------------- 1485 // assertions 1486 // dcache should only deal with DRAM addresses 1487 when (bus.a.fire) { 1488 assert(bus.a.bits.address >= 0x80000000L.U) 1489 } 1490 when (bus.b.fire) { 1491 assert(bus.b.bits.address >= 0x80000000L.U) 1492 } 1493 when (bus.c.fire) { 1494 assert(bus.c.bits.address >= 0x80000000L.U) 1495 } 1496 1497 //---------------------------------------- 1498 // utility functions 1499 def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = { 1500 sink.valid := source.valid && !block_signal 1501 source.ready := sink.ready && !block_signal 1502 sink.bits := source.bits 1503 } 1504 1505 //---------------------------------------- 1506 // Customized csr cache op support 1507 val cacheOpDecoder = Module(new CSRCacheOpDecoder("dcache", CacheInstrucion.COP_ID_DCACHE)) 1508 cacheOpDecoder.io.csr <> io.csr 1509 bankedDataArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 1510 // dup cacheOp_req_valid 1511 bankedDataArray.io.cacheOp_req_dup.zipWithIndex.map{ case(dup, i) => dup := cacheOpDecoder.io.cache_req_dup(i) } 1512 // dup cacheOp_req_bits_opCode 1513 bankedDataArray.io.cacheOp_req_bits_opCode_dup.zipWithIndex.map{ case (dup, i) => dup := cacheOpDecoder.io.cacheOp_req_bits_opCode_dup(i) } 1514 1515 tagArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 1516 // dup cacheOp_req_valid 1517 tagArray.io.cacheOp_req_dup.zipWithIndex.map{ case(dup, i) => dup := cacheOpDecoder.io.cache_req_dup(i) } 1518 // dup cacheOp_req_bits_opCode 1519 tagArray.io.cacheOp_req_bits_opCode_dup.zipWithIndex.map{ case (dup, i) => dup := cacheOpDecoder.io.cacheOp_req_bits_opCode_dup(i) } 1520 1521 cacheOpDecoder.io.cache.resp.valid := bankedDataArray.io.cacheOp.resp.valid || 1522 tagArray.io.cacheOp.resp.valid 1523 cacheOpDecoder.io.cache.resp.bits := Mux1H(List( 1524 bankedDataArray.io.cacheOp.resp.valid -> bankedDataArray.io.cacheOp.resp.bits, 1525 tagArray.io.cacheOp.resp.valid -> tagArray.io.cacheOp.resp.bits, 1526 )) 1527 cacheOpDecoder.io.error := io.error 1528 assert(!((bankedDataArray.io.cacheOp.resp.valid +& tagArray.io.cacheOp.resp.valid) > 1.U)) 1529 1530 //---------------------------------------- 1531 // performance counters 1532 val num_loads = PopCount(ldu.map(e => e.io.lsu.req.fire)) 1533 XSPerfAccumulate("num_loads", num_loads) 1534 1535 io.mshrFull := missQueue.io.full 1536 1537 // performance counter 1538 val ld_access = Wire(Vec(LoadPipelineWidth, missQueue.io.debug_early_replace.last.cloneType)) 1539 val st_access = Wire(ld_access.last.cloneType) 1540 ld_access.zip(ldu).foreach { 1541 case (a, u) => 1542 a.valid := RegNext(u.io.lsu.req.fire) && !u.io.lsu.s1_kill 1543 a.bits.idx := RegNext(get_idx(u.io.lsu.req.bits.vaddr)) 1544 a.bits.tag := get_tag(u.io.lsu.s1_paddr_dup_dcache) 1545 } 1546 st_access.valid := RegNext(mainPipe.io.store_req.fire) 1547 st_access.bits.idx := RegNext(get_idx(mainPipe.io.store_req.bits.vaddr)) 1548 st_access.bits.tag := RegNext(get_tag(mainPipe.io.store_req.bits.addr)) 1549 val access_info = ld_access.toSeq ++ Seq(st_access) 1550 val early_replace = RegNext(missQueue.io.debug_early_replace) 1551 val access_early_replace = access_info.map { 1552 case acc => 1553 Cat(early_replace.map { 1554 case r => 1555 acc.valid && r.valid && 1556 acc.bits.tag === r.bits.tag && 1557 acc.bits.idx === r.bits.idx 1558 }) 1559 } 1560 XSPerfAccumulate("access_early_replace", PopCount(Cat(access_early_replace))) 1561 1562 val perfEvents = (Seq(wb, mainPipe, missQueue, probeQueue) ++ ldu).flatMap(_.getPerfEvents) 1563 generatePerfEvent() 1564} 1565 1566class AMOHelper() extends ExtModule { 1567 val clock = IO(Input(Clock())) 1568 val enable = IO(Input(Bool())) 1569 val cmd = IO(Input(UInt(5.W))) 1570 val addr = IO(Input(UInt(64.W))) 1571 val wdata = IO(Input(UInt(64.W))) 1572 val mask = IO(Input(UInt(8.W))) 1573 val rdata = IO(Output(UInt(64.W))) 1574} 1575 1576class DCacheWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter { 1577 override def shouldBeInlined: Boolean = false 1578 1579 val useDcache = coreParams.dcacheParametersOpt.nonEmpty 1580 val clientNode = if (useDcache) TLIdentityNode() else null 1581 val dcache = if (useDcache) LazyModule(new DCache()) else null 1582 if (useDcache) { 1583 clientNode := dcache.clientNode 1584 } 1585 1586 class DCacheWrapperImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) with HasPerfEvents { 1587 val io = IO(new DCacheIO) 1588 val perfEvents = if (!useDcache) { 1589 // a fake dcache which uses dpi-c to access memory, only for debug usage! 1590 val fake_dcache = Module(new FakeDCache()) 1591 io <> fake_dcache.io 1592 Seq() 1593 } 1594 else { 1595 io <> dcache.module.io 1596 dcache.module.getPerfEvents 1597 } 1598 generatePerfEvent() 1599 } 1600 1601 lazy val module = new DCacheWrapperImp(this) 1602} 1603