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