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