1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.cache 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.experimental.ExtModule 22import chisel3.util._ 23import xiangshan._ 24import utils._ 25import utility._ 26import freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp, TransferSizes} 27import freechips.rocketchip.tilelink._ 28import freechips.rocketchip.util.{BundleFieldBase, UIntToOH1} 29import device.RAMHelper 30import coupledL2.{AliasField, AliasKey, DirtyField, PrefetchField} 31import utility.ReqSourceField 32import utility.FastArbiter 33import mem.{AddPipelineReg} 34import xiangshan.cache.dcache.ReplayCarry 35 36import scala.math.max 37 38// DCache specific parameters 39case class DCacheParameters 40( 41 nSets: Int = 256, 42 nWays: Int = 8, 43 rowBits: Int = 64, 44 tagECC: Option[String] = None, 45 dataECC: Option[String] = None, 46 replacer: Option[String] = Some("setplru"), 47 updateReplaceOn2ndmiss: Boolean = true, 48 nMissEntries: Int = 1, 49 nProbeEntries: Int = 1, 50 nReleaseEntries: Int = 1, 51 nMMIOEntries: Int = 1, 52 nMMIOs: Int = 1, 53 blockBytes: Int = 64, 54 alwaysReleaseData: Boolean = false 55) extends L1CacheParameters { 56 // if sets * blockBytes > 4KB(page size), 57 // cache alias will happen, 58 // we need to avoid this by recoding additional bits in L2 cache 59 val setBytes = nSets * blockBytes 60 val aliasBitsOpt = if(setBytes > pageSize) Some(log2Ceil(setBytes / pageSize)) else None 61 val reqFields: Seq[BundleFieldBase] = Seq( 62 PrefetchField(), 63 ReqSourceField() 64 ) ++ aliasBitsOpt.map(AliasField) 65 val echoFields: Seq[BundleFieldBase] = Nil 66 67 def tagCode: Code = Code.fromString(tagECC) 68 69 def dataCode: Code = Code.fromString(dataECC) 70} 71 72// Physical Address 73// -------------------------------------- 74// | Physical Tag | PIndex | Offset | 75// -------------------------------------- 76// | 77// DCacheTagOffset 78// 79// Virtual Address 80// -------------------------------------- 81// | Above index | Set | Bank | Offset | 82// -------------------------------------- 83// | | | | 84// | | | 0 85// | | DCacheBankOffset 86// | DCacheSetOffset 87// DCacheAboveIndexOffset 88 89// Default DCache size = 64 sets * 8 ways * 8 banks * 8 Byte = 32K Byte 90 91trait HasDCacheParameters extends HasL1CacheParameters { 92 val cacheParams = dcacheParameters 93 val cfg = cacheParams 94 95 def encWordBits = cacheParams.dataCode.width(wordBits) 96 97 def encRowBits = encWordBits * rowWords // for DuplicatedDataArray only 98 def eccBits = encWordBits - wordBits 99 100 def encTagBits = cacheParams.tagCode.width(tagBits) 101 def eccTagBits = encTagBits - tagBits 102 103 def blockProbeAfterGrantCycles = 8 // give the processor some time to issue a request after a grant 104 105 def nSourceType = 10 106 def sourceTypeWidth = log2Up(nSourceType) 107 // non-prefetch source < 3 108 def LOAD_SOURCE = 0 109 def STORE_SOURCE = 1 110 def AMO_SOURCE = 2 111 // prefetch source >= 3 112 def DCACHE_PREFETCH_SOURCE = 3 113 def SOFT_PREFETCH = 4 114 def HW_PREFETCH_AGT = 5 115 def HW_PREFETCH_PHT_CUR = 6 116 def HW_PREFETCH_PHT_INC = 7 117 def HW_PREFETCH_PHT_DEC = 8 118 def HW_PREFETCH_BOP = 9 119 def HW_PREFETCH_STRIDE = 10 120 121 // each source use a id to distinguish its multiple reqs 122 def reqIdWidth = log2Up(nEntries) max log2Up(StoreBufferSize) 123 124 require(isPow2(cfg.nMissEntries)) // TODO 125 // require(isPow2(cfg.nReleaseEntries)) 126 require(cfg.nMissEntries < cfg.nReleaseEntries) 127 val nEntries = cfg.nMissEntries + cfg.nReleaseEntries 128 val releaseIdBase = cfg.nMissEntries 129 130 // banked dcache support 131 val DCacheSetDiv = 1 132 val DCacheSets = cacheParams.nSets 133 val DCacheWays = cacheParams.nWays 134 val DCacheBanks = 8 // hardcoded 135 val DCacheDupNum = 16 136 val DCacheSRAMRowBits = cacheParams.rowBits // hardcoded 137 val DCacheWordBits = 64 // hardcoded 138 val DCacheWordBytes = DCacheWordBits / 8 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 // uncache 161 val uncacheIdxBits = log2Up(StoreQueueSize + 1) max log2Up(VirtualLoadQueueSize + 1) 162 // hardware prefetch parameters 163 // high confidence hardware prefetch port 164 val HighConfHWPFLoadPort = LoadPipelineWidth - 1 // use the last load port by default 165 val IgnorePrefetchConfidence = false 166 167 // parameters about duplicating regs to solve fanout 168 // In Main Pipe: 169 // tag_write.ready -> data_write.valid * 8 banks 170 // tag_write.ready -> meta_write.valid 171 // tag_write.ready -> tag_write.valid 172 // tag_write.ready -> err_write.valid 173 // tag_write.ready -> wb.valid 174 val nDupTagWriteReady = DCacheBanks + 4 175 // In Main Pipe: 176 // data_write.ready -> data_write.valid * 8 banks 177 // data_write.ready -> meta_write.valid 178 // data_write.ready -> tag_write.valid 179 // data_write.ready -> err_write.valid 180 // data_write.ready -> wb.valid 181 val nDupDataWriteReady = DCacheBanks + 4 182 val nDupWbReady = DCacheBanks + 4 183 val nDupStatus = nDupTagWriteReady + nDupDataWriteReady 184 val dataWritePort = 0 185 val metaWritePort = DCacheBanks 186 val tagWritePort = metaWritePort + 1 187 val errWritePort = tagWritePort + 1 188 val wbPort = errWritePort + 1 189 190 def set_to_dcache_div(set: UInt) = { 191 require(set.getWidth >= DCacheSetBits) 192 if (DCacheSetDivBits == 0) 0.U else set(DCacheSetDivBits-1, 0) 193 } 194 195 def set_to_dcache_div_set(set: UInt) = { 196 require(set.getWidth >= DCacheSetBits) 197 set(DCacheSetBits - 1, DCacheSetDivBits) 198 } 199 200 def addr_to_dcache_bank(addr: UInt) = { 201 require(addr.getWidth >= DCacheSetOffset) 202 addr(DCacheSetOffset-1, DCacheBankOffset) 203 } 204 205 def addr_to_dcache_div(addr: UInt) = { 206 require(addr.getWidth >= DCacheAboveIndexOffset) 207 if(DCacheSetDivBits == 0) 0.U else addr(DCacheSetOffset + DCacheSetDivBits - 1, DCacheSetOffset) 208 } 209 210 def addr_to_dcache_div_set(addr: UInt) = { 211 require(addr.getWidth >= DCacheAboveIndexOffset) 212 addr(DCacheAboveIndexOffset - 1, DCacheSetOffset + DCacheSetDivBits) 213 } 214 215 def addr_to_dcache_set(addr: UInt) = { 216 require(addr.getWidth >= DCacheAboveIndexOffset) 217 addr(DCacheAboveIndexOffset-1, DCacheSetOffset) 218 } 219 220 def get_data_of_bank(bank: Int, data: UInt) = { 221 require(data.getWidth >= (bank+1)*DCacheSRAMRowBits) 222 data(DCacheSRAMRowBits * (bank + 1) - 1, DCacheSRAMRowBits * bank) 223 } 224 225 def get_mask_of_bank(bank: Int, data: UInt) = { 226 require(data.getWidth >= (bank+1)*DCacheSRAMRowBytes) 227 data(DCacheSRAMRowBytes * (bank + 1) - 1, DCacheSRAMRowBytes * bank) 228 } 229 230 def arbiter[T <: Bundle]( 231 in: Seq[DecoupledIO[T]], 232 out: DecoupledIO[T], 233 name: Option[String] = None): Unit = { 234 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 235 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 236 for ((a, req) <- arb.io.in.zip(in)) { 237 a <> req 238 } 239 out <> arb.io.out 240 } 241 242 def arbiter_with_pipereg[T <: Bundle]( 243 in: Seq[DecoupledIO[T]], 244 out: DecoupledIO[T], 245 name: Option[String] = None): Unit = { 246 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 247 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 248 for ((a, req) <- arb.io.in.zip(in)) { 249 a <> req 250 } 251 AddPipelineReg(arb.io.out, out, false.B) 252 } 253 254 def arbiter_with_pipereg_N_dup[T <: Bundle]( 255 in: Seq[DecoupledIO[T]], 256 out: DecoupledIO[T], 257 dups: Seq[DecoupledIO[T]], 258 name: Option[String] = None): Unit = { 259 val arb = Module(new Arbiter[T](chiselTypeOf(out.bits), in.size)) 260 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 261 for ((a, req) <- arb.io.in.zip(in)) { 262 a <> req 263 } 264 for (dup <- dups) { 265 AddPipelineReg(arb.io.out, dup, false.B) 266 } 267 AddPipelineReg(arb.io.out, out, false.B) 268 } 269 270 def rrArbiter[T <: Bundle]( 271 in: Seq[DecoupledIO[T]], 272 out: DecoupledIO[T], 273 name: Option[String] = None): Unit = { 274 val arb = Module(new RRArbiter[T](chiselTypeOf(out.bits), in.size)) 275 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 276 for ((a, req) <- arb.io.in.zip(in)) { 277 a <> req 278 } 279 out <> arb.io.out 280 } 281 282 def fastArbiter[T <: Bundle]( 283 in: Seq[DecoupledIO[T]], 284 out: DecoupledIO[T], 285 name: Option[String] = None): Unit = { 286 val arb = Module(new FastArbiter[T](chiselTypeOf(out.bits), in.size)) 287 if (name.nonEmpty) { arb.suggestName(s"${name.get}_arb") } 288 for ((a, req) <- arb.io.in.zip(in)) { 289 a <> req 290 } 291 out <> arb.io.out 292 } 293 294 val numReplaceRespPorts = 2 295 296 require(isPow2(nSets), s"nSets($nSets) must be pow2") 297 require(isPow2(nWays), s"nWays($nWays) must be pow2") 298 require(full_divide(rowBits, wordBits), s"rowBits($rowBits) must be multiple of wordBits($wordBits)") 299 require(full_divide(beatBits, rowBits), s"beatBits($beatBits) must be multiple of rowBits($rowBits)") 300} 301 302abstract class DCacheModule(implicit p: Parameters) extends L1CacheModule 303 with HasDCacheParameters 304 305abstract class DCacheBundle(implicit p: Parameters) extends L1CacheBundle 306 with HasDCacheParameters 307 308class ReplacementAccessBundle(implicit p: Parameters) extends DCacheBundle { 309 val set = UInt(log2Up(nSets).W) 310 val way = UInt(log2Up(nWays).W) 311} 312 313class ReplacementWayReqIO(implicit p: Parameters) extends DCacheBundle { 314 val set = ValidIO(UInt(log2Up(nSets).W)) 315 val way = Input(UInt(log2Up(nWays).W)) 316} 317 318class DCacheExtraMeta(implicit p: Parameters) extends DCacheBundle 319{ 320 val error = Bool() // cache line has been marked as corrupted by l2 / ecc error detected when store 321 val prefetch = Bool() // cache line is first required by prefetch 322 val access = Bool() // cache line has been accessed by load / store 323 324 // val debug_access_timestamp = UInt(64.W) // last time a load / store / refill access that cacheline 325} 326 327// memory request in word granularity(load, mmio, lr/sc, atomics) 328class DCacheWordReq(implicit p: Parameters) extends DCacheBundle 329{ 330 val cmd = UInt(M_SZ.W) 331 val vaddr = UInt(VAddrBits.W) 332 val data = UInt(VLEN.W) 333 val mask = UInt((VLEN/8).W) 334 val id = UInt(reqIdWidth.W) 335 val instrtype = UInt(sourceTypeWidth.W) 336 val isFirstIssue = Bool() 337 val replayCarry = new ReplayCarry 338 339 val debug_robIdx = UInt(log2Ceil(RobSize).W) 340 def dump() = { 341 XSDebug("DCacheWordReq: cmd: %x vaddr: %x data: %x mask: %x id: %d\n", 342 cmd, vaddr, data, mask, id) 343 } 344} 345 346// memory request in word granularity(store) 347class DCacheLineReq(implicit p: Parameters) extends DCacheBundle 348{ 349 val cmd = UInt(M_SZ.W) 350 val vaddr = UInt(VAddrBits.W) 351 val addr = UInt(PAddrBits.W) 352 val data = UInt((cfg.blockBytes * 8).W) 353 val mask = UInt(cfg.blockBytes.W) 354 val id = UInt(reqIdWidth.W) 355 def dump() = { 356 XSDebug("DCacheLineReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 357 cmd, addr, data, mask, id) 358 } 359 def idx: UInt = get_idx(vaddr) 360} 361 362class DCacheWordReqWithVaddr(implicit p: Parameters) extends DCacheWordReq { 363 val addr = UInt(PAddrBits.W) 364 val wline = Bool() 365} 366 367class BaseDCacheWordResp(implicit p: Parameters) extends DCacheBundle 368{ 369 // read in s2 370 val data = UInt(VLEN.W) 371 // select in s3 372 val data_delayed = UInt(VLEN.W) 373 val id = UInt(reqIdWidth.W) 374 // cache req missed, send it to miss queue 375 val miss = Bool() 376 // cache miss, and failed to enter the missqueue, replay from RS is needed 377 val replay = Bool() 378 val replayCarry = new ReplayCarry 379 // data has been corrupted 380 val tag_error = Bool() // tag error 381 val mshr_id = UInt(log2Up(cfg.nMissEntries).W) 382 383 val debug_robIdx = UInt(log2Ceil(RobSize).W) 384 def dump() = { 385 XSDebug("DCacheWordResp: data: %x id: %d miss: %b replay: %b\n", 386 data, id, miss, replay) 387 } 388} 389 390class DCacheWordResp(implicit p: Parameters) extends BaseDCacheWordResp 391{ 392 val meta_prefetch = Bool() 393 val meta_access = Bool() 394 // s2 395 val handled = Bool() 396 // s3: 1 cycle after data resp 397 val error_delayed = Bool() // all kinds of errors, include tag error 398 val replacementUpdated = Bool() 399} 400 401class BankedDCacheWordResp(implicit p: Parameters) extends DCacheWordResp 402{ 403 val bank_data = Vec(DCacheBanks, Bits(DCacheSRAMRowBits.W)) 404 val bank_oh = UInt(DCacheBanks.W) 405} 406 407class DCacheWordRespWithError(implicit p: Parameters) extends BaseDCacheWordResp 408{ 409 val error = Bool() // all kinds of errors, include tag error 410} 411 412class DCacheLineResp(implicit p: Parameters) extends DCacheBundle 413{ 414 val data = UInt((cfg.blockBytes * 8).W) 415 // cache req missed, send it to miss queue 416 val miss = Bool() 417 // cache req nacked, replay it later 418 val replay = Bool() 419 val id = UInt(reqIdWidth.W) 420 def dump() = { 421 XSDebug("DCacheLineResp: data: %x id: %d miss: %b replay: %b\n", 422 data, id, miss, replay) 423 } 424} 425 426class Refill(implicit p: Parameters) extends DCacheBundle 427{ 428 val addr = UInt(PAddrBits.W) 429 val data = UInt(l1BusDataWidth.W) 430 val error = Bool() // refilled data has been corrupted 431 // for debug usage 432 val data_raw = UInt((cfg.blockBytes * 8).W) 433 val hasdata = Bool() 434 val refill_done = Bool() 435 def dump() = { 436 XSDebug("Refill: addr: %x data: %x\n", addr, data) 437 } 438 val id = UInt(log2Up(cfg.nMissEntries).W) 439} 440 441class Release(implicit p: Parameters) extends DCacheBundle 442{ 443 val paddr = UInt(PAddrBits.W) 444 def dump() = { 445 XSDebug("Release: paddr: %x\n", paddr(PAddrBits-1, DCacheTagOffset)) 446 } 447} 448 449class DCacheWordIO(implicit p: Parameters) extends DCacheBundle 450{ 451 val req = DecoupledIO(new DCacheWordReq) 452 val resp = Flipped(DecoupledIO(new DCacheWordResp)) 453} 454 455 456class UncacheWordReq(implicit p: Parameters) extends DCacheBundle 457{ 458 val cmd = UInt(M_SZ.W) 459 val addr = UInt(PAddrBits.W) 460 val data = UInt(XLEN.W) 461 val mask = UInt((XLEN/8).W) 462 val id = UInt(uncacheIdxBits.W) 463 val instrtype = UInt(sourceTypeWidth.W) 464 val atomic = Bool() 465 val isFirstIssue = Bool() 466 val replayCarry = new ReplayCarry 467 468 def dump() = { 469 XSDebug("UncacheWordReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 470 cmd, addr, data, mask, id) 471 } 472} 473 474class UncacheWordResp(implicit p: Parameters) extends DCacheBundle 475{ 476 val data = UInt(XLEN.W) 477 val data_delayed = UInt(XLEN.W) 478 val id = UInt(uncacheIdxBits.W) 479 val miss = Bool() 480 val replay = Bool() 481 val tag_error = Bool() 482 val error = Bool() 483 val replayCarry = new ReplayCarry 484 val mshr_id = UInt(log2Up(cfg.nMissEntries).W) // FIXME: why uncacheWordResp is not merged to baseDcacheResp 485 486 val debug_robIdx = UInt(log2Ceil(RobSize).W) 487 def dump() = { 488 XSDebug("UncacheWordResp: data: %x id: %d miss: %b replay: %b, tag_error: %b, error: %b\n", 489 data, id, miss, replay, tag_error, error) 490 } 491} 492 493class UncacheWordIO(implicit p: Parameters) extends DCacheBundle 494{ 495 val req = DecoupledIO(new UncacheWordReq) 496 val resp = Flipped(DecoupledIO(new UncacheWordResp)) 497} 498 499class AtomicsResp(implicit p: Parameters) extends DCacheBundle { 500 val data = UInt(DataBits.W) 501 val miss = Bool() 502 val miss_id = UInt(log2Up(cfg.nMissEntries).W) 503 val replay = Bool() 504 val error = Bool() 505 506 val ack_miss_queue = Bool() 507 508 val id = UInt(reqIdWidth.W) 509} 510 511class AtomicWordIO(implicit p: Parameters) extends DCacheBundle 512{ 513 val req = DecoupledIO(new MainPipeReq) 514 val resp = Flipped(ValidIO(new AtomicsResp)) 515 val block_lr = Input(Bool()) 516} 517 518// used by load unit 519class DCacheLoadIO(implicit p: Parameters) extends DCacheWordIO 520{ 521 // kill previous cycle's req 522 val s1_kill = Output(Bool()) 523 val s2_kill = Output(Bool()) 524 val s2_pc = Output(UInt(VAddrBits.W)) 525 // cycle 0: load has updated replacement before 526 val replacementUpdated = Output(Bool()) 527 // cycle 0: virtual address: req.addr 528 // cycle 1: physical address: s1_paddr 529 val s1_paddr_dup_lsu = Output(UInt(PAddrBits.W)) // lsu side paddr 530 val s1_paddr_dup_dcache = Output(UInt(PAddrBits.W)) // dcache side paddr 531 val s1_disable_fast_wakeup = Input(Bool()) 532 // cycle 2: hit signal 533 val s2_hit = Input(Bool()) // hit signal for lsu, 534 val s2_first_hit = Input(Bool()) 535 val s2_bank_conflict = Input(Bool()) 536 val s2_wpu_pred_fail = Input(Bool()) 537 val s2_mq_nack = Input(Bool()) 538 539 // debug 540 val debug_s1_hit_way = Input(UInt(nWays.W)) 541} 542 543class DCacheLineIO(implicit p: Parameters) extends DCacheBundle 544{ 545 val req = DecoupledIO(new DCacheLineReq) 546 val resp = Flipped(DecoupledIO(new DCacheLineResp)) 547} 548 549class DCacheToSbufferIO(implicit p: Parameters) extends DCacheBundle { 550 // sbuffer will directly send request to dcache main pipe 551 val req = Flipped(Decoupled(new DCacheLineReq)) 552 553 val main_pipe_hit_resp = ValidIO(new DCacheLineResp) 554 val refill_hit_resp = ValidIO(new DCacheLineResp) 555 556 val replay_resp = ValidIO(new DCacheLineResp) 557 558 def hit_resps: Seq[ValidIO[DCacheLineResp]] = Seq(main_pipe_hit_resp, refill_hit_resp) 559} 560 561// forward tilelink channel D's data to ldu 562class DcacheToLduForwardIO(implicit p: Parameters) extends DCacheBundle { 563 val valid = Bool() 564 val data = UInt(l1BusDataWidth.W) 565 val mshrid = UInt(log2Up(cfg.nMissEntries).W) 566 val last = Bool() 567 568 def apply(req_valid : Bool, req_data : UInt, req_mshrid : UInt, req_last : Bool) = { 569 valid := req_valid 570 data := req_data 571 mshrid := req_mshrid 572 last := req_last 573 } 574 575 def dontCare() = { 576 valid := false.B 577 data := DontCare 578 mshrid := DontCare 579 last := DontCare 580 } 581 582 def forward(req_valid : Bool, req_mshr_id : UInt, req_paddr : UInt) = { 583 val all_match = req_valid && valid && 584 req_mshr_id === mshrid && 585 req_paddr(log2Up(refillBytes)) === last 586 587 val forward_D = RegInit(false.B) 588 val forwardData = RegInit(VecInit(List.fill(VLEN/8)(0.U(8.W)))) 589 590 val block_idx = req_paddr(log2Up(refillBytes) - 1, 3) 591 val block_data = Wire(Vec(l1BusDataWidth / 64, UInt(64.W))) 592 (0 until l1BusDataWidth / 64).map(i => { 593 block_data(i) := data(64 * i + 63, 64 * i) 594 }) 595 val selected_data = Wire(UInt(128.W)) 596 selected_data := Mux(req_paddr(3), Fill(2, block_data(block_idx)), Cat(block_data(block_idx + 1.U), block_data(block_idx))) 597 598 forward_D := all_match 599 for (i <- 0 until VLEN/8) { 600 forwardData(i) := selected_data(8 * i + 7, 8 * i) 601 } 602 603 (forward_D, forwardData) 604 } 605} 606 607class MissEntryForwardIO(implicit p: Parameters) extends DCacheBundle { 608 val inflight = Bool() 609 val paddr = UInt(PAddrBits.W) 610 val raw_data = Vec(blockBytes/beatBytes, UInt(beatBits.W)) 611 val firstbeat_valid = Bool() 612 val lastbeat_valid = Bool() 613 614 def apply(mshr_valid : Bool, mshr_paddr : UInt, mshr_rawdata : Vec[UInt], mshr_first_valid : Bool, mshr_last_valid : Bool) = { 615 inflight := mshr_valid 616 paddr := mshr_paddr 617 raw_data := mshr_rawdata 618 firstbeat_valid := mshr_first_valid 619 lastbeat_valid := mshr_last_valid 620 } 621 622 // check if we can forward from mshr or D channel 623 def check(req_valid : Bool, req_paddr : UInt) = { 624 RegNext(req_valid && inflight && req_paddr(PAddrBits - 1, blockOffBits) === paddr(PAddrBits - 1, blockOffBits)) 625 } 626 627 def forward(req_valid : Bool, req_paddr : UInt) = { 628 val all_match = (req_paddr(log2Up(refillBytes)) === 0.U && firstbeat_valid) || 629 (req_paddr(log2Up(refillBytes)) === 1.U && lastbeat_valid) 630 631 val forward_mshr = RegInit(false.B) 632 val forwardData = RegInit(VecInit(List.fill(VLEN/8)(0.U(8.W)))) 633 634 val beat_data = raw_data(req_paddr(log2Up(refillBytes))) 635 val block_idx = req_paddr(log2Up(refillBytes) - 1, 3) 636 val block_data = Wire(Vec(l1BusDataWidth / 64, UInt(64.W))) 637 (0 until l1BusDataWidth / 64).map(i => { 638 block_data(i) := beat_data(64 * i + 63, 64 * i) 639 }) 640 val selected_data = Wire(UInt(128.W)) 641 selected_data := Mux(req_paddr(3), Fill(2, block_data(block_idx)), Cat(block_data(block_idx + 1.U), block_data(block_idx))) 642 643 forward_mshr := all_match 644 for (i <- 0 until VLEN/8) { 645 forwardData(i) := selected_data(8 * i + 7, 8 * i) 646 } 647 648 (forward_mshr, forwardData) 649 } 650} 651 652// forward mshr's data to ldu 653class LduToMissqueueForwardIO(implicit p: Parameters) extends DCacheBundle { 654 // req 655 val valid = Input(Bool()) 656 val mshrid = Input(UInt(log2Up(cfg.nMissEntries).W)) 657 val paddr = Input(UInt(PAddrBits.W)) 658 // resp 659 val forward_mshr = Output(Bool()) 660 val forwardData = Output(Vec(VLEN/8, UInt(8.W))) 661 val forward_result_valid = Output(Bool()) 662 663 def connect(sink: LduToMissqueueForwardIO) = { 664 sink.valid := valid 665 sink.mshrid := mshrid 666 sink.paddr := paddr 667 forward_mshr := sink.forward_mshr 668 forwardData := sink.forwardData 669 forward_result_valid := sink.forward_result_valid 670 } 671 672 def forward() = { 673 (forward_result_valid, forward_mshr, forwardData) 674 } 675} 676 677class DCacheToLsuIO(implicit p: Parameters) extends DCacheBundle { 678 val load = Vec(LoadPipelineWidth, Flipped(new DCacheLoadIO)) // for speculative load 679 val lsq = ValidIO(new Refill) // refill to load queue, wake up load misses 680 val store = new DCacheToSbufferIO // for sbuffer 681 val atomics = Flipped(new AtomicWordIO) // atomics reqs 682 val release = ValidIO(new Release) // cacheline release hint for ld-ld violation check 683 val forward_D = Output(Vec(LoadPipelineWidth, new DcacheToLduForwardIO)) 684 val forward_mshr = Vec(LoadPipelineWidth, new LduToMissqueueForwardIO) 685} 686 687class DCacheIO(implicit p: Parameters) extends DCacheBundle { 688 val hartId = Input(UInt(8.W)) 689 val l2_pf_store_only = Input(Bool()) 690 val lsu = new DCacheToLsuIO 691 val csr = new L1CacheToCsrIO 692 val error = new L1CacheErrorInfo 693 val mshrFull = Output(Bool()) 694 val force_write = Input(Bool()) 695} 696 697 698class DCache()(implicit p: Parameters) extends LazyModule with HasDCacheParameters { 699 700 val clientParameters = TLMasterPortParameters.v1( 701 Seq(TLMasterParameters.v1( 702 name = "dcache", 703 sourceId = IdRange(0, nEntries + 1), 704 supportsProbe = TransferSizes(cfg.blockBytes) 705 )), 706 requestFields = cacheParams.reqFields, 707 echoFields = cacheParams.echoFields 708 ) 709 710 val clientNode = TLClientNode(Seq(clientParameters)) 711 712 lazy val module = new DCacheImp(this) 713} 714 715 716class DCacheImp(outer: DCache) extends LazyModuleImp(outer) with HasDCacheParameters with HasPerfEvents { 717 718 val io = IO(new DCacheIO) 719 720 val (bus, edge) = outer.clientNode.out.head 721 require(bus.d.bits.data.getWidth == l1BusDataWidth, "DCache: tilelink width does not match") 722 723 println("DCache:") 724 println(" DCacheSets: " + DCacheSets) 725 println(" DCacheSetDiv: " + DCacheSetDiv) 726 println(" DCacheWays: " + DCacheWays) 727 println(" DCacheBanks: " + DCacheBanks) 728 println(" DCacheSRAMRowBits: " + DCacheSRAMRowBits) 729 println(" DCacheWordOffset: " + DCacheWordOffset) 730 println(" DCacheBankOffset: " + DCacheBankOffset) 731 println(" DCacheSetOffset: " + DCacheSetOffset) 732 println(" DCacheTagOffset: " + DCacheTagOffset) 733 println(" DCacheAboveIndexOffset: " + DCacheAboveIndexOffset) 734 735 //---------------------------------------- 736 // core data structures 737 val bankedDataArray = if(EnableDCacheWPU) Module(new SramedDataArray) else Module(new BankedDataArray) 738 val metaArray = Module(new L1CohMetaArray(readPorts = LoadPipelineWidth + 1, writePorts = 2)) 739 val errorArray = Module(new L1FlagMetaArray(readPorts = LoadPipelineWidth + 1, writePorts = 2)) 740 val prefetchArray = Module(new L1FlagMetaArray(readPorts = LoadPipelineWidth + 1, writePorts = 2)) // prefetch flag array 741 val accessArray = Module(new L1FlagMetaArray(readPorts = LoadPipelineWidth + 1, writePorts = LoadPipelineWidth + 2)) 742 val tagArray = Module(new DuplicatedTagArray(readPorts = LoadPipelineWidth + 1)) 743 bankedDataArray.dump() 744 745 //---------------------------------------- 746 // core modules 747 val ldu = Seq.tabulate(LoadPipelineWidth)({ i => Module(new LoadPipe(i))}) 748 // val atomicsReplayUnit = Module(new AtomicsReplayEntry) 749 val mainPipe = Module(new MainPipe) 750 val refillPipe = Module(new RefillPipe) 751 val missQueue = Module(new MissQueue(edge)) 752 val probeQueue = Module(new ProbeQueue(edge)) 753 val wb = Module(new WritebackQueue(edge)) 754 755 missQueue.io.hartId := io.hartId 756 missQueue.io.l2_pf_store_only := RegNext(io.l2_pf_store_only, false.B) 757 758 val errors = ldu.map(_.io.error) ++ // load error 759 Seq(mainPipe.io.error) // store / misc error 760 io.error <> RegNext(Mux1H(errors.map(e => RegNext(e.valid) -> RegNext(e)))) 761 762 //---------------------------------------- 763 // meta array 764 765 // read / write coh meta 766 val meta_read_ports = ldu.map(_.io.meta_read) ++ 767 Seq(mainPipe.io.meta_read) 768 val meta_resp_ports = ldu.map(_.io.meta_resp) ++ 769 Seq(mainPipe.io.meta_resp) 770 val meta_write_ports = Seq( 771 mainPipe.io.meta_write, 772 refillPipe.io.meta_write 773 ) 774 meta_read_ports.zip(metaArray.io.read).foreach { case (p, r) => r <> p } 775 meta_resp_ports.zip(metaArray.io.resp).foreach { case (p, r) => p := r } 776 meta_write_ports.zip(metaArray.io.write).foreach { case (p, w) => w <> p } 777 778 // read extra meta 779 meta_read_ports.zip(errorArray.io.read).foreach { case (p, r) => r <> p } 780 meta_read_ports.zip(prefetchArray.io.read).foreach { case (p, r) => r <> p } 781 meta_read_ports.zip(accessArray.io.read).foreach { case (p, r) => r <> p } 782 val extra_meta_resp_ports = ldu.map(_.io.extra_meta_resp) ++ 783 Seq(mainPipe.io.extra_meta_resp) 784 extra_meta_resp_ports.zip(errorArray.io.resp).foreach { case (p, r) => { 785 (0 until nWays).map(i => { p(i).error := r(i) }) 786 }} 787 extra_meta_resp_ports.zip(prefetchArray.io.resp).foreach { case (p, r) => { 788 (0 until nWays).map(i => { p(i).prefetch := r(i) }) 789 }} 790 extra_meta_resp_ports.zip(accessArray.io.resp).foreach { case (p, r) => { 791 (0 until nWays).map(i => { p(i).access := r(i) }) 792 }} 793 794 // write extra meta 795 val error_flag_write_ports = Seq( 796 mainPipe.io.error_flag_write, // error flag generated by corrupted store 797 refillPipe.io.error_flag_write // corrupted signal from l2 798 ) 799 error_flag_write_ports.zip(errorArray.io.write).foreach { case (p, w) => w <> p } 800 801 val prefetch_flag_write_ports = Seq( 802 mainPipe.io.prefetch_flag_write, // set prefetch_flag to false if coh is set to Nothing 803 refillPipe.io.prefetch_flag_write // refill required by prefetch will set prefetch_flag 804 ) 805 prefetch_flag_write_ports.zip(prefetchArray.io.write).foreach { case (p, w) => w <> p } 806 807 val access_flag_write_ports = ldu.map(_.io.access_flag_write) ++ Seq( 808 mainPipe.io.access_flag_write, 809 refillPipe.io.access_flag_write 810 ) 811 access_flag_write_ports.zip(accessArray.io.write).foreach { case (p, w) => w <> p } 812 813 //---------------------------------------- 814 // tag array 815 require(tagArray.io.read.size == (ldu.size + 1)) 816 val tag_write_intend = missQueue.io.refill_pipe_req.valid || mainPipe.io.tag_write_intend 817 assert(!RegNext(!tag_write_intend && tagArray.io.write.valid)) 818 ldu.zipWithIndex.foreach { 819 case (ld, i) => 820 tagArray.io.read(i) <> ld.io.tag_read 821 ld.io.tag_resp := tagArray.io.resp(i) 822 ld.io.tag_read.ready := !tag_write_intend 823 } 824 tagArray.io.read.last <> mainPipe.io.tag_read 825 mainPipe.io.tag_resp := tagArray.io.resp.last 826 827 val fake_tag_read_conflict_this_cycle = PopCount(ldu.map(ld=> ld.io.tag_read.valid)) 828 XSPerfAccumulate("fake_tag_read_conflict", fake_tag_read_conflict_this_cycle) 829 830 val tag_write_arb = Module(new Arbiter(new TagWriteReq, 2)) 831 tag_write_arb.io.in(0) <> refillPipe.io.tag_write 832 tag_write_arb.io.in(1) <> mainPipe.io.tag_write 833 tagArray.io.write <> tag_write_arb.io.out 834 835 //---------------------------------------- 836 // data array 837 mainPipe.io.data_read.zip(ldu).map(x => x._1 := x._2.io.lsu.req.valid) 838 839 val dataWriteArb = Module(new Arbiter(new L1BankedDataWriteReq, 2)) 840 dataWriteArb.io.in(0) <> refillPipe.io.data_write 841 dataWriteArb.io.in(1) <> mainPipe.io.data_write 842 843 bankedDataArray.io.write <> dataWriteArb.io.out 844 845 for (bank <- 0 until DCacheBanks) { 846 val dataWriteArb_dup = Module(new Arbiter(new L1BankedDataWriteReqCtrl, 2)) 847 dataWriteArb_dup.io.in(0).valid := refillPipe.io.data_write_dup(bank).valid 848 dataWriteArb_dup.io.in(0).bits := refillPipe.io.data_write_dup(bank).bits 849 dataWriteArb_dup.io.in(1).valid := mainPipe.io.data_write_dup(bank).valid 850 dataWriteArb_dup.io.in(1).bits := mainPipe.io.data_write_dup(bank).bits 851 852 bankedDataArray.io.write_dup(bank) <> dataWriteArb_dup.io.out 853 } 854 855 bankedDataArray.io.readline <> mainPipe.io.data_readline 856 bankedDataArray.io.readline_intend := mainPipe.io.data_read_intend 857 mainPipe.io.readline_error_delayed := bankedDataArray.io.readline_error_delayed 858 mainPipe.io.data_resp := bankedDataArray.io.readline_resp 859 860 (0 until LoadPipelineWidth).map(i => { 861 bankedDataArray.io.read(i) <> ldu(i).io.banked_data_read 862 bankedDataArray.io.is128Req(i) <> ldu(i).io.is128Req 863 bankedDataArray.io.read_error_delayed(i) <> ldu(i).io.read_error_delayed 864 865 ldu(i).io.banked_data_resp := bankedDataArray.io.read_resp_delayed(i) 866 867 ldu(i).io.bank_conflict_slow := bankedDataArray.io.bank_conflict_slow(i) 868 }) 869 870 (0 until LoadPipelineWidth).map(i => { 871 val (_, _, done, _) = edge.count(bus.d) 872 when(bus.d.bits.opcode === TLMessages.GrantData) { 873 io.lsu.forward_D(i).apply(bus.d.valid, bus.d.bits.data, bus.d.bits.source, done) 874 }.otherwise { 875 io.lsu.forward_D(i).dontCare() 876 } 877 }) 878 mainPipe.io.force_write <> io.force_write 879 880 //---------------------------------------- 881 // load pipe 882 // the s1 kill signal 883 // only lsu uses this, replay never kills 884 for (w <- 0 until LoadPipelineWidth) { 885 ldu(w).io.lsu <> io.lsu.load(w) 886 887 // TODO:when have load128Req 888 ldu(w).io.load128Req := false.B 889 890 // replay and nack not needed anymore 891 // TODO: remove replay and nack 892 ldu(w).io.nack := false.B 893 894 ldu(w).io.disable_ld_fast_wakeup := 895 bankedDataArray.io.disable_ld_fast_wakeup(w) // load pipe fast wake up should be disabled when bank conflict 896 } 897 898 /** LoadMissDB: record load miss state */ 899 val isWriteLoadMissTable = WireInit(Constantin.createRecord("isWriteLoadMissTable" + p(XSCoreParamsKey).HartId.toString)) 900 val isFirstHitWrite = WireInit(Constantin.createRecord("isFirstHitWrite" + p(XSCoreParamsKey).HartId.toString)) 901 val tableName = "LoadMissDB" + p(XSCoreParamsKey).HartId.toString 902 val siteName = "DcacheWrapper" + p(XSCoreParamsKey).HartId.toString 903 val loadMissTable = ChiselDB.createTable(tableName, new LoadMissEntry) 904 for( i <- 0 until LoadPipelineWidth){ 905 val loadMissEntry = Wire(new LoadMissEntry) 906 val loadMissWriteEn = 907 (!ldu(i).io.lsu.resp.bits.replay && ldu(i).io.miss_req.fire) || 908 (ldu(i).io.lsu.s2_first_hit && ldu(i).io.lsu.resp.valid && isFirstHitWrite.orR) 909 loadMissEntry.timeCnt := GTimer() 910 loadMissEntry.robIdx := ldu(i).io.lsu.resp.bits.debug_robIdx 911 loadMissEntry.paddr := ldu(i).io.miss_req.bits.addr 912 loadMissEntry.vaddr := ldu(i).io.miss_req.bits.vaddr 913 loadMissEntry.missState := OHToUInt(Cat(Seq( 914 ldu(i).io.miss_req.fire & ldu(i).io.miss_resp.merged, 915 ldu(i).io.miss_req.fire & !ldu(i).io.miss_resp.merged, 916 ldu(i).io.lsu.s2_first_hit && ldu(i).io.lsu.resp.valid 917 ))) 918 loadMissTable.log( 919 data = loadMissEntry, 920 en = isWriteLoadMissTable.orR && loadMissWriteEn, 921 site = siteName, 922 clock = clock, 923 reset = reset 924 ) 925 } 926 927 //---------------------------------------- 928 // atomics 929 // atomics not finished yet 930 // io.lsu.atomics <> atomicsReplayUnit.io.lsu 931 io.lsu.atomics.resp := RegNext(mainPipe.io.atomic_resp) 932 io.lsu.atomics.block_lr := mainPipe.io.block_lr 933 // atomicsReplayUnit.io.pipe_resp := RegNext(mainPipe.io.atomic_resp) 934 // atomicsReplayUnit.io.block_lr <> mainPipe.io.block_lr 935 936 //---------------------------------------- 937 // miss queue 938 val MissReqPortCount = LoadPipelineWidth + 1 939 val MainPipeMissReqPort = 0 940 941 // Request 942 val missReqArb = Module(new ArbiterFilterByCacheLineAddr(new MissReq, MissReqPortCount, blockOffBits, PAddrBits)) 943 944 missReqArb.io.in(MainPipeMissReqPort) <> mainPipe.io.miss_req 945 for (w <- 0 until LoadPipelineWidth) { missReqArb.io.in(w + 1) <> ldu(w).io.miss_req } 946 947 for (w <- 0 until LoadPipelineWidth) { ldu(w).io.miss_resp := missQueue.io.resp } 948 mainPipe.io.miss_resp := missQueue.io.resp 949 950 wb.io.miss_req.valid := missReqArb.io.out.valid 951 wb.io.miss_req.bits := missReqArb.io.out.bits.addr 952 953 // block_decoupled(missReqArb.io.out, missQueue.io.req, wb.io.block_miss_req) 954 missReqArb.io.out <> missQueue.io.req 955 when(wb.io.block_miss_req) { 956 missQueue.io.req.bits.cancel := true.B 957 missReqArb.io.out.ready := false.B 958 } 959 960 XSPerfAccumulate("miss_queue_fire", PopCount(VecInit(missReqArb.io.in.map(_.fire))) >= 1.U) 961 XSPerfAccumulate("miss_queue_muti_fire", PopCount(VecInit(missReqArb.io.in.map(_.fire))) > 1.U) 962 963 XSPerfAccumulate("miss_queue_has_enq_req", PopCount(VecInit(missReqArb.io.in.map(_.valid))) >= 1.U) 964 XSPerfAccumulate("miss_queue_has_muti_enq_req", PopCount(VecInit(missReqArb.io.in.map(_.valid))) > 1.U) 965 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) 966 967 // forward missqueue 968 (0 until LoadPipelineWidth).map(i => io.lsu.forward_mshr(i).connect(missQueue.io.forward(i))) 969 970 // refill to load queue 971 io.lsu.lsq <> missQueue.io.refill_to_ldq 972 973 // tilelink stuff 974 bus.a <> missQueue.io.mem_acquire 975 bus.e <> missQueue.io.mem_finish 976 missQueue.io.probe_addr := bus.b.bits.address 977 978 missQueue.io.main_pipe_resp := RegNext(mainPipe.io.atomic_resp) 979 980 //---------------------------------------- 981 // probe 982 // probeQueue.io.mem_probe <> bus.b 983 block_decoupled(bus.b, probeQueue.io.mem_probe, missQueue.io.probe_block) 984 probeQueue.io.lrsc_locked_block <> mainPipe.io.lrsc_locked_block 985 probeQueue.io.update_resv_set <> mainPipe.io.update_resv_set 986 987 //---------------------------------------- 988 // mainPipe 989 // when a req enters main pipe, if it is set-conflict with replace pipe or refill pipe, 990 // block the req in main pipe 991 block_decoupled(probeQueue.io.pipe_req, mainPipe.io.probe_req, missQueue.io.refill_pipe_req.valid) 992 block_decoupled(io.lsu.store.req, mainPipe.io.store_req, refillPipe.io.req.valid) 993 994 io.lsu.store.replay_resp := RegNext(mainPipe.io.store_replay_resp) 995 io.lsu.store.main_pipe_hit_resp := mainPipe.io.store_hit_resp 996 997 arbiter_with_pipereg( 998 in = Seq(missQueue.io.main_pipe_req, io.lsu.atomics.req), 999 out = mainPipe.io.atomic_req, 1000 name = Some("main_pipe_atomic_req") 1001 ) 1002 1003 mainPipe.io.invalid_resv_set := RegNext(wb.io.req.fire && wb.io.req.bits.addr === mainPipe.io.lrsc_locked_block.bits) 1004 1005 //---------------------------------------- 1006 // replace (main pipe) 1007 val mpStatus = mainPipe.io.status 1008 mainPipe.io.replace_req <> missQueue.io.replace_pipe_req 1009 missQueue.io.replace_pipe_resp := mainPipe.io.replace_resp 1010 1011 //---------------------------------------- 1012 // refill pipe 1013 val refillShouldBeBlocked = (mpStatus.s1.valid && mpStatus.s1.bits.set === missQueue.io.refill_pipe_req.bits.idx) || 1014 Cat(Seq(mpStatus.s2, mpStatus.s3).map(s => 1015 s.valid && 1016 s.bits.set === missQueue.io.refill_pipe_req.bits.idx && 1017 s.bits.way_en === missQueue.io.refill_pipe_req.bits.way_en 1018 )).orR 1019 block_decoupled(missQueue.io.refill_pipe_req, refillPipe.io.req, refillShouldBeBlocked) 1020 1021 val mpStatus_dup = mainPipe.io.status_dup 1022 val mq_refill_dup = missQueue.io.refill_pipe_req_dup 1023 val refillShouldBeBlocked_dup = VecInit((0 until nDupStatus).map { case i => 1024 mpStatus_dup(i).s1.valid && mpStatus_dup(i).s1.bits.set === mq_refill_dup(i).bits.idx || 1025 Cat(Seq(mpStatus_dup(i).s2, mpStatus_dup(i).s3).map(s => 1026 s.valid && 1027 s.bits.set === mq_refill_dup(i).bits.idx && 1028 s.bits.way_en === mq_refill_dup(i).bits.way_en 1029 )).orR 1030 }) 1031 dontTouch(refillShouldBeBlocked_dup) 1032 1033 refillPipe.io.req_dup_for_data_w.zipWithIndex.foreach { case (r, i) => 1034 r.bits := (mq_refill_dup.drop(dataWritePort).take(DCacheBanks))(i).bits 1035 } 1036 refillPipe.io.req_dup_for_meta_w.bits := mq_refill_dup(metaWritePort).bits 1037 refillPipe.io.req_dup_for_tag_w.bits := mq_refill_dup(tagWritePort).bits 1038 refillPipe.io.req_dup_for_err_w.bits := mq_refill_dup(errWritePort).bits 1039 refillPipe.io.req_dup_for_data_w.zipWithIndex.foreach { case (r, i) => 1040 r.valid := (mq_refill_dup.drop(dataWritePort).take(DCacheBanks))(i).valid && 1041 !(refillShouldBeBlocked_dup.drop(dataWritePort).take(DCacheBanks))(i) 1042 } 1043 refillPipe.io.req_dup_for_meta_w.valid := mq_refill_dup(metaWritePort).valid && !refillShouldBeBlocked_dup(metaWritePort) 1044 refillPipe.io.req_dup_for_tag_w.valid := mq_refill_dup(tagWritePort).valid && !refillShouldBeBlocked_dup(tagWritePort) 1045 refillPipe.io.req_dup_for_err_w.valid := mq_refill_dup(errWritePort).valid && !refillShouldBeBlocked_dup(errWritePort) 1046 1047 val refillPipe_io_req_valid_dup = VecInit(mq_refill_dup.zip(refillShouldBeBlocked_dup).map( 1048 x => x._1.valid && !x._2 1049 )) 1050 val refillPipe_io_data_write_valid_dup = VecInit(refillPipe_io_req_valid_dup.slice(0, nDupDataWriteReady)) 1051 val refillPipe_io_tag_write_valid_dup = VecInit(refillPipe_io_req_valid_dup.slice(nDupDataWriteReady, nDupStatus)) 1052 dontTouch(refillPipe_io_req_valid_dup) 1053 dontTouch(refillPipe_io_data_write_valid_dup) 1054 dontTouch(refillPipe_io_tag_write_valid_dup) 1055 mainPipe.io.data_write_ready_dup := VecInit(refillPipe_io_data_write_valid_dup.map(v => !v)) 1056 mainPipe.io.tag_write_ready_dup := VecInit(refillPipe_io_tag_write_valid_dup.map(v => !v)) 1057 mainPipe.io.wb_ready_dup := wb.io.req_ready_dup 1058 1059 mq_refill_dup.zip(refillShouldBeBlocked_dup).foreach { case (r, block) => 1060 r.ready := refillPipe.io.req.ready && !block 1061 } 1062 1063 missQueue.io.refill_pipe_resp := refillPipe.io.resp 1064 io.lsu.store.refill_hit_resp := RegNext(refillPipe.io.store_resp) 1065 1066 //---------------------------------------- 1067 // wb 1068 // add a queue between MainPipe and WritebackUnit to reduce MainPipe stalls due to WritebackUnit busy 1069 1070 wb.io.req <> mainPipe.io.wb 1071 bus.c <> wb.io.mem_release 1072 wb.io.release_wakeup := refillPipe.io.release_wakeup 1073 wb.io.release_update := mainPipe.io.release_update 1074 wb.io.probe_ttob_check_req <> mainPipe.io.probe_ttob_check_req 1075 wb.io.probe_ttob_check_resp <> mainPipe.io.probe_ttob_check_resp 1076 1077 io.lsu.release.valid := RegNext(wb.io.req.fire()) 1078 io.lsu.release.bits.paddr := RegNext(wb.io.req.bits.addr) 1079 // Note: RegNext() is required by: 1080 // * load queue released flag update logic 1081 // * load / load violation check logic 1082 // * and timing requirements 1083 // CHANGE IT WITH CARE 1084 1085 // connect bus d 1086 missQueue.io.mem_grant.valid := false.B 1087 missQueue.io.mem_grant.bits := DontCare 1088 1089 wb.io.mem_grant.valid := false.B 1090 wb.io.mem_grant.bits := DontCare 1091 1092 // in L1DCache, we ony expect Grant[Data] and ReleaseAck 1093 bus.d.ready := false.B 1094 when (bus.d.bits.opcode === TLMessages.Grant || bus.d.bits.opcode === TLMessages.GrantData) { 1095 missQueue.io.mem_grant <> bus.d 1096 } .elsewhen (bus.d.bits.opcode === TLMessages.ReleaseAck) { 1097 wb.io.mem_grant <> bus.d 1098 } .otherwise { 1099 assert (!bus.d.fire()) 1100 } 1101 1102 //---------------------------------------- 1103 // replacement algorithm 1104 val replacer = ReplacementPolicy.fromString(cacheParams.replacer, nWays, nSets) 1105 1106 val replWayReqs = ldu.map(_.io.replace_way) ++ Seq(mainPipe.io.replace_way) 1107 replWayReqs.foreach{ 1108 case req => 1109 req.way := DontCare 1110 when (req.set.valid) { req.way := replacer.way(req.set.bits) } 1111 } 1112 1113 val replAccessReqs = ldu.map(_.io.replace_access) ++ Seq( 1114 mainPipe.io.replace_access 1115 ) 1116 val touchWays = Seq.fill(replAccessReqs.size)(Wire(ValidIO(UInt(log2Up(nWays).W)))) 1117 touchWays.zip(replAccessReqs).foreach { 1118 case (w, req) => 1119 w.valid := req.valid 1120 w.bits := req.bits.way 1121 } 1122 val touchSets = replAccessReqs.map(_.bits.set) 1123 replacer.access(touchSets, touchWays) 1124 1125 //---------------------------------------- 1126 // assertions 1127 // dcache should only deal with DRAM addresses 1128 when (bus.a.fire()) { 1129 assert(bus.a.bits.address >= 0x80000000L.U) 1130 } 1131 when (bus.b.fire()) { 1132 assert(bus.b.bits.address >= 0x80000000L.U) 1133 } 1134 when (bus.c.fire()) { 1135 assert(bus.c.bits.address >= 0x80000000L.U) 1136 } 1137 1138 //---------------------------------------- 1139 // utility functions 1140 def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = { 1141 sink.valid := source.valid && !block_signal 1142 source.ready := sink.ready && !block_signal 1143 sink.bits := source.bits 1144 } 1145 1146 //---------------------------------------- 1147 // Customized csr cache op support 1148 val cacheOpDecoder = Module(new CSRCacheOpDecoder("dcache", CacheInstrucion.COP_ID_DCACHE)) 1149 cacheOpDecoder.io.csr <> io.csr 1150 bankedDataArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 1151 // dup cacheOp_req_valid 1152 bankedDataArray.io.cacheOp_req_dup.zipWithIndex.map{ case(dup, i) => dup := cacheOpDecoder.io.cache_req_dup(i) } 1153 // dup cacheOp_req_bits_opCode 1154 bankedDataArray.io.cacheOp_req_bits_opCode_dup.zipWithIndex.map{ case (dup, i) => dup := cacheOpDecoder.io.cacheOp_req_bits_opCode_dup(i) } 1155 1156 tagArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 1157 // dup cacheOp_req_valid 1158 tagArray.io.cacheOp_req_dup.zipWithIndex.map{ case(dup, i) => dup := cacheOpDecoder.io.cache_req_dup(i) } 1159 // dup cacheOp_req_bits_opCode 1160 tagArray.io.cacheOp_req_bits_opCode_dup.zipWithIndex.map{ case (dup, i) => dup := cacheOpDecoder.io.cacheOp_req_bits_opCode_dup(i) } 1161 1162 cacheOpDecoder.io.cache.resp.valid := bankedDataArray.io.cacheOp.resp.valid || 1163 tagArray.io.cacheOp.resp.valid 1164 cacheOpDecoder.io.cache.resp.bits := Mux1H(List( 1165 bankedDataArray.io.cacheOp.resp.valid -> bankedDataArray.io.cacheOp.resp.bits, 1166 tagArray.io.cacheOp.resp.valid -> tagArray.io.cacheOp.resp.bits, 1167 )) 1168 cacheOpDecoder.io.error := io.error 1169 assert(!((bankedDataArray.io.cacheOp.resp.valid +& tagArray.io.cacheOp.resp.valid) > 1.U)) 1170 1171 //---------------------------------------- 1172 // performance counters 1173 val num_loads = PopCount(ldu.map(e => e.io.lsu.req.fire())) 1174 XSPerfAccumulate("num_loads", num_loads) 1175 1176 io.mshrFull := missQueue.io.full 1177 1178 // performance counter 1179 val ld_access = Wire(Vec(LoadPipelineWidth, missQueue.io.debug_early_replace.last.cloneType)) 1180 val st_access = Wire(ld_access.last.cloneType) 1181 ld_access.zip(ldu).foreach { 1182 case (a, u) => 1183 a.valid := RegNext(u.io.lsu.req.fire()) && !u.io.lsu.s1_kill 1184 a.bits.idx := RegNext(get_idx(u.io.lsu.req.bits.vaddr)) 1185 a.bits.tag := get_tag(u.io.lsu.s1_paddr_dup_dcache) 1186 } 1187 st_access.valid := RegNext(mainPipe.io.store_req.fire()) 1188 st_access.bits.idx := RegNext(get_idx(mainPipe.io.store_req.bits.vaddr)) 1189 st_access.bits.tag := RegNext(get_tag(mainPipe.io.store_req.bits.addr)) 1190 val access_info = ld_access.toSeq ++ Seq(st_access) 1191 val early_replace = RegNext(missQueue.io.debug_early_replace) 1192 val access_early_replace = access_info.map { 1193 case acc => 1194 Cat(early_replace.map { 1195 case r => 1196 acc.valid && r.valid && 1197 acc.bits.tag === r.bits.tag && 1198 acc.bits.idx === r.bits.idx 1199 }) 1200 } 1201 XSPerfAccumulate("access_early_replace", PopCount(Cat(access_early_replace))) 1202 1203 val perfEvents = (Seq(wb, mainPipe, missQueue, probeQueue) ++ ldu).flatMap(_.getPerfEvents) 1204 generatePerfEvent() 1205} 1206 1207class AMOHelper() extends ExtModule { 1208 val clock = IO(Input(Clock())) 1209 val enable = IO(Input(Bool())) 1210 val cmd = IO(Input(UInt(5.W))) 1211 val addr = IO(Input(UInt(64.W))) 1212 val wdata = IO(Input(UInt(64.W))) 1213 val mask = IO(Input(UInt(8.W))) 1214 val rdata = IO(Output(UInt(64.W))) 1215} 1216 1217class DCacheWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter { 1218 1219 val useDcache = coreParams.dcacheParametersOpt.nonEmpty 1220 val clientNode = if (useDcache) TLIdentityNode() else null 1221 val dcache = if (useDcache) LazyModule(new DCache()) else null 1222 if (useDcache) { 1223 clientNode := dcache.clientNode 1224 } 1225 1226 lazy val module = new LazyModuleImp(this) with HasPerfEvents { 1227 val io = IO(new DCacheIO) 1228 val perfEvents = if (!useDcache) { 1229 // a fake dcache which uses dpi-c to access memory, only for debug usage! 1230 val fake_dcache = Module(new FakeDCache()) 1231 io <> fake_dcache.io 1232 Seq() 1233 } 1234 else { 1235 io <> dcache.module.io 1236 dcache.module.getPerfEvents 1237 } 1238 generatePerfEvent() 1239 } 1240} 1241