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 freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp, TransferSizes} 26import freechips.rocketchip.tilelink._ 27import freechips.rocketchip.util.BundleFieldBase 28import device.RAMHelper 29import huancun.{AliasField, AliasKey, PreferCacheField, PrefetchField, DirtyField} 30import scala.math.max 31 32// DCache specific parameters 33case class DCacheParameters 34( 35 nSets: Int = 256, 36 nWays: Int = 8, 37 rowBits: Int = 128, 38 tagECC: Option[String] = None, 39 dataECC: Option[String] = None, 40 replacer: Option[String] = Some("random"), 41 nMissEntries: Int = 1, 42 nProbeEntries: Int = 1, 43 nReleaseEntries: Int = 1, 44 nMMIOEntries: Int = 1, 45 nMMIOs: Int = 1, 46 blockBytes: Int = 64, 47 alwaysReleaseData: Boolean = true 48) extends L1CacheParameters { 49 // if sets * blockBytes > 4KB(page size), 50 // cache alias will happen, 51 // we need to avoid this by recoding additional bits in L2 cache 52 val setBytes = nSets * blockBytes 53 val aliasBitsOpt = if(setBytes > pageSize) Some(log2Ceil(setBytes / pageSize)) else None 54 val reqFields: Seq[BundleFieldBase] = Seq( 55 PrefetchField(), 56 PreferCacheField() 57 ) ++ aliasBitsOpt.map(AliasField) 58 val echoFields: Seq[BundleFieldBase] = Seq(DirtyField()) 59 60 def tagCode: Code = Code.fromString(tagECC) 61 62 def dataCode: Code = Code.fromString(dataECC) 63} 64 65// Physical Address 66// -------------------------------------- 67// | Physical Tag | PIndex | Offset | 68// -------------------------------------- 69// | 70// DCacheTagOffset 71// 72// Virtual Address 73// -------------------------------------- 74// | Above index | Set | Bank | Offset | 75// -------------------------------------- 76// | | | | 77// | | | 0 78// | | DCacheBankOffset 79// | DCacheSetOffset 80// DCacheAboveIndexOffset 81 82// Default DCache size = 64 sets * 8 ways * 8 banks * 8 Byte = 32K Byte 83 84trait HasDCacheParameters extends HasL1CacheParameters { 85 val cacheParams = dcacheParameters 86 val cfg = cacheParams 87 88 def encWordBits = cacheParams.dataCode.width(wordBits) 89 90 def encRowBits = encWordBits * rowWords // for DuplicatedDataArray only 91 def eccBits = encWordBits - wordBits 92 93 def encTagBits = cacheParams.tagCode.width(tagBits) 94 def eccTagBits = encTagBits - tagBits 95 96 def lrscCycles = LRSCCycles // ISA requires 16-insn LRSC sequences to succeed 97 def lrscBackoff = 3 // disallow LRSC reacquisition briefly 98 def blockProbeAfterGrantCycles = 8 // give the processor some time to issue a request after a grant 99 100 def nSourceType = 3 101 def sourceTypeWidth = log2Up(nSourceType) 102 def LOAD_SOURCE = 0 103 def STORE_SOURCE = 1 104 def AMO_SOURCE = 2 105 def SOFT_PREFETCH = 3 106 107 // each source use a id to distinguish its multiple reqs 108 def reqIdWidth = 64 109 110 require(isPow2(cfg.nMissEntries)) 111 require(isPow2(cfg.nReleaseEntries)) 112 val nEntries = max(cfg.nMissEntries, cfg.nReleaseEntries) << 1 113 val releaseIdBase = max(cfg.nMissEntries, cfg.nReleaseEntries) 114 115 // banked dcache support 116 val DCacheSets = cacheParams.nSets 117 val DCacheWays = cacheParams.nWays 118 val DCacheBanks = 8 119 val DCacheSRAMRowBits = 64 // hardcoded 120 val DCacheWordBits = 64 // hardcoded 121 val DCacheWordBytes = DCacheWordBits / 8 122 123 val DCacheSizeBits = DCacheSRAMRowBits * DCacheBanks * DCacheWays * DCacheSets 124 val DCacheSizeBytes = DCacheSizeBits / 8 125 val DCacheSizeWords = DCacheSizeBits / 64 // TODO 126 127 val DCacheSameVPAddrLength = 12 128 129 val DCacheSRAMRowBytes = DCacheSRAMRowBits / 8 130 val DCacheWordOffset = log2Up(DCacheWordBytes) 131 132 val DCacheBankOffset = log2Up(DCacheSRAMRowBytes) 133 val DCacheSetOffset = DCacheBankOffset + log2Up(DCacheBanks) 134 val DCacheAboveIndexOffset = DCacheSetOffset + log2Up(DCacheSets) 135 val DCacheTagOffset = DCacheAboveIndexOffset min DCacheSameVPAddrLength 136 val DCacheLineOffset = DCacheSetOffset 137 val DCacheIndexOffset = DCacheBankOffset 138 139 def addr_to_dcache_bank(addr: UInt) = { 140 require(addr.getWidth >= DCacheSetOffset) 141 addr(DCacheSetOffset-1, DCacheBankOffset) 142 } 143 144 def addr_to_dcache_set(addr: UInt) = { 145 require(addr.getWidth >= DCacheAboveIndexOffset) 146 addr(DCacheAboveIndexOffset-1, DCacheSetOffset) 147 } 148 149 def get_data_of_bank(bank: Int, data: UInt) = { 150 require(data.getWidth >= (bank+1)*DCacheSRAMRowBits) 151 data(DCacheSRAMRowBits * (bank + 1) - 1, DCacheSRAMRowBits * bank) 152 } 153 154 def get_mask_of_bank(bank: Int, data: UInt) = { 155 require(data.getWidth >= (bank+1)*DCacheSRAMRowBytes) 156 data(DCacheSRAMRowBytes * (bank + 1) - 1, DCacheSRAMRowBytes * bank) 157 } 158 159 val numReplaceRespPorts = 2 160 161 require(isPow2(nSets), s"nSets($nSets) must be pow2") 162 require(isPow2(nWays), s"nWays($nWays) must be pow2") 163 require(full_divide(rowBits, wordBits), s"rowBits($rowBits) must be multiple of wordBits($wordBits)") 164 require(full_divide(beatBits, rowBits), s"beatBits($beatBits) must be multiple of rowBits($rowBits)") 165} 166 167abstract class DCacheModule(implicit p: Parameters) extends L1CacheModule 168 with HasDCacheParameters 169 170abstract class DCacheBundle(implicit p: Parameters) extends L1CacheBundle 171 with HasDCacheParameters 172 173class ReplacementAccessBundle(implicit p: Parameters) extends DCacheBundle { 174 val set = UInt(log2Up(nSets).W) 175 val way = UInt(log2Up(nWays).W) 176} 177 178class ReplacementWayReqIO(implicit p: Parameters) extends DCacheBundle { 179 val set = ValidIO(UInt(log2Up(nSets).W)) 180 val way = Input(UInt(log2Up(nWays).W)) 181} 182 183// memory request in word granularity(load, mmio, lr/sc, atomics) 184class DCacheWordReq(implicit p: Parameters) extends DCacheBundle 185{ 186 val cmd = UInt(M_SZ.W) 187 val addr = UInt(PAddrBits.W) 188 val data = UInt(DataBits.W) 189 val mask = UInt((DataBits/8).W) 190 val id = UInt(reqIdWidth.W) 191 val instrtype = UInt(sourceTypeWidth.W) 192 def dump() = { 193 XSDebug("DCacheWordReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 194 cmd, addr, data, mask, id) 195 } 196} 197 198// memory request in word granularity(store) 199class DCacheLineReq(implicit p: Parameters) extends DCacheBundle 200{ 201 val cmd = UInt(M_SZ.W) 202 val vaddr = UInt(VAddrBits.W) 203 val addr = UInt(PAddrBits.W) 204 val data = UInt((cfg.blockBytes * 8).W) 205 val mask = UInt(cfg.blockBytes.W) 206 val id = UInt(reqIdWidth.W) 207 def dump() = { 208 XSDebug("DCacheLineReq: cmd: %x addr: %x data: %x mask: %x id: %d\n", 209 cmd, addr, data, mask, id) 210 } 211 def idx: UInt = get_idx(vaddr) 212} 213 214class DCacheWordReqWithVaddr(implicit p: Parameters) extends DCacheWordReq { 215 val vaddr = UInt(VAddrBits.W) 216 val wline = Bool() 217} 218 219class DCacheWordResp(implicit p: Parameters) extends DCacheBundle 220{ 221 val data = UInt(DataBits.W) 222 // cache req missed, send it to miss queue 223 val miss = Bool() 224 // cache req nacked, replay it later 225 val miss_enter = Bool() 226 // cache miss, and enter the missqueue successfully. just for softprefetch 227 val replay = Bool() 228 val id = UInt(reqIdWidth.W) 229 def dump() = { 230 XSDebug("DCacheWordResp: data: %x id: %d miss: %b replay: %b\n", 231 data, id, miss, replay) 232 } 233} 234 235class DCacheLineResp(implicit p: Parameters) extends DCacheBundle 236{ 237 val data = UInt((cfg.blockBytes * 8).W) 238 // cache req missed, send it to miss queue 239 val miss = Bool() 240 // cache req nacked, replay it later 241 val replay = Bool() 242 val id = UInt(reqIdWidth.W) 243 def dump() = { 244 XSDebug("DCacheLineResp: data: %x id: %d miss: %b replay: %b\n", 245 data, id, miss, replay) 246 } 247} 248 249class Refill(implicit p: Parameters) extends DCacheBundle 250{ 251 val addr = UInt(PAddrBits.W) 252 val data = UInt(l1BusDataWidth.W) 253 // for debug usage 254 val data_raw = UInt((cfg.blockBytes * 8).W) 255 val hasdata = Bool() 256 val refill_done = Bool() 257 def dump() = { 258 XSDebug("Refill: addr: %x data: %x\n", addr, data) 259 } 260} 261 262class Release(implicit p: Parameters) extends DCacheBundle 263{ 264 val paddr = UInt(PAddrBits.W) 265 def dump() = { 266 XSDebug("Release: paddr: %x\n", paddr(PAddrBits-1, DCacheTagOffset)) 267 } 268} 269 270class DCacheWordIO(implicit p: Parameters) extends DCacheBundle 271{ 272 val req = DecoupledIO(new DCacheWordReq) 273 val resp = Flipped(DecoupledIO(new DCacheWordResp)) 274} 275 276class DCacheWordIOWithVaddr(implicit p: Parameters) extends DCacheBundle 277{ 278 val req = DecoupledIO(new DCacheWordReqWithVaddr) 279 val resp = Flipped(DecoupledIO(new DCacheWordResp)) 280} 281 282// used by load unit 283class DCacheLoadIO(implicit p: Parameters) extends DCacheWordIO 284{ 285 // kill previous cycle's req 286 val s1_kill = Output(Bool()) 287 val s2_kill = Output(Bool()) 288 // cycle 0: virtual address: req.addr 289 // cycle 1: physical address: s1_paddr 290 val s1_paddr = Output(UInt(PAddrBits.W)) 291 val s1_hit_way = Input(UInt(nWays.W)) 292 val s1_disable_fast_wakeup = Input(Bool()) 293 val s1_bank_conflict = Input(Bool()) 294} 295 296class DCacheLineIO(implicit p: Parameters) extends DCacheBundle 297{ 298 val req = DecoupledIO(new DCacheLineReq) 299 val resp = Flipped(DecoupledIO(new DCacheLineResp)) 300} 301 302class DCacheToSbufferIO(implicit p: Parameters) extends DCacheBundle { 303 // sbuffer will directly send request to dcache main pipe 304 val req = Flipped(Decoupled(new DCacheLineReq)) 305 306 val main_pipe_hit_resp = ValidIO(new DCacheLineResp) 307 val refill_hit_resp = ValidIO(new DCacheLineResp) 308 309 val replay_resp = ValidIO(new DCacheLineResp) 310 311 def hit_resps: Seq[ValidIO[DCacheLineResp]] = Seq(main_pipe_hit_resp, refill_hit_resp) 312} 313 314class DCacheToLsuIO(implicit p: Parameters) extends DCacheBundle { 315 val load = Vec(LoadPipelineWidth, Flipped(new DCacheLoadIO)) // for speculative load 316 val lsq = ValidIO(new Refill) // refill to load queue, wake up load misses 317 val store = new DCacheToSbufferIO // for sbuffer 318 val atomics = Flipped(new DCacheWordIOWithVaddr) // atomics reqs 319 val release = ValidIO(new Release) // cacheline release hint for ld-ld violation check 320} 321 322class DCacheIO(implicit p: Parameters) extends DCacheBundle { 323 val lsu = new DCacheToLsuIO 324 val csr = new L1CacheToCsrIO 325 val error = new L1CacheErrorInfo 326 val mshrFull = Output(Bool()) 327} 328 329 330class DCache()(implicit p: Parameters) extends LazyModule with HasDCacheParameters { 331 332 val clientParameters = TLMasterPortParameters.v1( 333 Seq(TLMasterParameters.v1( 334 name = "dcache", 335 sourceId = IdRange(0, nEntries + 1), 336 supportsProbe = TransferSizes(cfg.blockBytes) 337 )), 338 requestFields = cacheParams.reqFields, 339 echoFields = cacheParams.echoFields 340 ) 341 342 val clientNode = TLClientNode(Seq(clientParameters)) 343 344 lazy val module = new DCacheImp(this) 345} 346 347 348class DCacheImp(outer: DCache) extends LazyModuleImp(outer) with HasDCacheParameters { 349 350 val io = IO(new DCacheIO) 351 352 val (bus, edge) = outer.clientNode.out.head 353 require(bus.d.bits.data.getWidth == l1BusDataWidth, "DCache: tilelink width does not match") 354 355 println("DCache:") 356 println(" DCacheSets: " + DCacheSets) 357 println(" DCacheWays: " + DCacheWays) 358 println(" DCacheBanks: " + DCacheBanks) 359 println(" DCacheSRAMRowBits: " + DCacheSRAMRowBits) 360 println(" DCacheWordOffset: " + DCacheWordOffset) 361 println(" DCacheBankOffset: " + DCacheBankOffset) 362 println(" DCacheSetOffset: " + DCacheSetOffset) 363 println(" DCacheTagOffset: " + DCacheTagOffset) 364 println(" DCacheAboveIndexOffset: " + DCacheAboveIndexOffset) 365 366 //---------------------------------------- 367 // core data structures 368 val bankedDataArray = Module(new BankedDataArray) 369 val metaArray = Module(new AsynchronousMetaArray(readPorts = 4, writePorts = 3)) 370 val tagArray = Module(new DuplicatedTagArray(readPorts = LoadPipelineWidth + 1)) 371 bankedDataArray.dump() 372 373 val errors = bankedDataArray.io.errors ++ metaArray.io.errors 374 io.error <> RegNext(Mux1H(errors.map(e => e.ecc_error.valid -> e))) 375 // assert(!io.error.ecc_error.valid) 376 377 //---------------------------------------- 378 // core modules 379 val ldu = Seq.tabulate(LoadPipelineWidth)({ i => Module(new LoadPipe(i))}) 380 val atomicsReplayUnit = Module(new AtomicsReplayEntry) 381 val mainPipe = Module(new MainPipe) 382 val refillPipe = Module(new RefillPipe) 383 val replacePipe = Module(new ReplacePipe) 384 val missQueue = Module(new MissQueue(edge)) 385 val probeQueue = Module(new ProbeQueue(edge)) 386 val wb = Module(new WritebackQueue(edge)) 387 388 //---------------------------------------- 389 // meta array 390 val meta_read_ports = ldu.map(_.io.meta_read) ++ 391 Seq(mainPipe.io.meta_read, 392 replacePipe.io.meta_read) 393 val meta_resp_ports = ldu.map(_.io.meta_resp) ++ 394 Seq(mainPipe.io.meta_resp, 395 replacePipe.io.meta_resp) 396 val meta_write_ports = Seq( 397 mainPipe.io.meta_write, 398 refillPipe.io.meta_write, 399 replacePipe.io.meta_write 400 ) 401 meta_read_ports.zip(metaArray.io.read).foreach { case (p, r) => r <> p } 402 meta_resp_ports.zip(metaArray.io.resp).foreach { case (p, r) => p := r } 403 meta_write_ports.zip(metaArray.io.write).foreach { case (p, w) => w <> p } 404 405 //---------------------------------------- 406 // tag array 407 require(tagArray.io.read.size == (ldu.size + 1)) 408 ldu.zipWithIndex.foreach { 409 case (ld, i) => 410 tagArray.io.read(i) <> ld.io.tag_read 411 ld.io.tag_resp := tagArray.io.resp(i) 412 } 413 tagArray.io.read.last <> mainPipe.io.tag_read 414 mainPipe.io.tag_resp := tagArray.io.resp.last 415 416 val tag_write_arb = Module(new Arbiter(new TagWriteReq, 2)) 417 tag_write_arb.io.in(0) <> refillPipe.io.tag_write 418 tag_write_arb.io.in(1) <> mainPipe.io.tag_write 419 tagArray.io.write <> tag_write_arb.io.out 420 421 //---------------------------------------- 422 // data array 423 424 val dataReadLineArb = Module(new Arbiter(new L1BankedDataReadLineReq, 2)) 425 dataReadLineArb.io.in(0) <> replacePipe.io.data_read 426 dataReadLineArb.io.in(1) <> mainPipe.io.data_read 427 428 val dataWriteArb = Module(new Arbiter(new L1BankedDataWriteReq, 2)) 429 dataWriteArb.io.in(0) <> refillPipe.io.data_write 430 dataWriteArb.io.in(1) <> mainPipe.io.data_write 431 432 bankedDataArray.io.write <> dataWriteArb.io.out 433 bankedDataArray.io.read(0) <> ldu(0).io.banked_data_read 434 bankedDataArray.io.read(1) <> ldu(1).io.banked_data_read 435 bankedDataArray.io.readline <> dataReadLineArb.io.out 436 437 ldu(0).io.banked_data_resp := bankedDataArray.io.resp 438 ldu(1).io.banked_data_resp := bankedDataArray.io.resp 439 mainPipe.io.data_resp := bankedDataArray.io.resp 440 replacePipe.io.data_resp := bankedDataArray.io.resp 441 442 ldu(0).io.bank_conflict_fast := bankedDataArray.io.bank_conflict_fast(0) 443 ldu(1).io.bank_conflict_fast := bankedDataArray.io.bank_conflict_fast(1) 444 ldu(0).io.bank_conflict_slow := bankedDataArray.io.bank_conflict_slow(0) 445 ldu(1).io.bank_conflict_slow := bankedDataArray.io.bank_conflict_slow(1) 446 447 //---------------------------------------- 448 // load pipe 449 // the s1 kill signal 450 // only lsu uses this, replay never kills 451 for (w <- 0 until LoadPipelineWidth) { 452 ldu(w).io.lsu <> io.lsu.load(w) 453 454 // replay and nack not needed anymore 455 // TODO: remove replay and nack 456 ldu(w).io.nack := false.B 457 458 ldu(w).io.disable_ld_fast_wakeup := 459 bankedDataArray.io.bank_conflict_fast(w) // load pipe fast wake up should be disabled when bank conflict 460 } 461 462 //---------------------------------------- 463 // atomics 464 // atomics not finished yet 465 io.lsu.atomics <> atomicsReplayUnit.io.lsu 466 atomicsReplayUnit.io.pipe_resp := mainPipe.io.atomic_resp 467 468 //---------------------------------------- 469 // miss queue 470 val MissReqPortCount = LoadPipelineWidth + 1 471 val MainPipeMissReqPort = 0 472 473 // Request 474 val missReqArb = Module(new RRArbiter(new MissReq, MissReqPortCount)) 475 476 missReqArb.io.in(MainPipeMissReqPort) <> mainPipe.io.miss 477 for (w <- 0 until LoadPipelineWidth) { missReqArb.io.in(w + 1) <> ldu(w).io.miss_req } 478 479 wb.io.miss_req.valid := missReqArb.io.out.valid 480 wb.io.miss_req.bits := missReqArb.io.out.bits.addr 481 482 block_decoupled(missReqArb.io.out, missQueue.io.req, wb.io.block_miss_req) 483 484 // refill to load queue 485 io.lsu.lsq <> missQueue.io.refill_to_ldq 486 487 // tilelink stuff 488 bus.a <> missQueue.io.mem_acquire 489 bus.e <> missQueue.io.mem_finish 490 missQueue.io.probe_addr := bus.b.bits.address 491 492 missQueue.io.main_pipe_resp := mainPipe.io.atomic_resp 493 494 //---------------------------------------- 495 // probe 496 // probeQueue.io.mem_probe <> bus.b 497 block_decoupled(bus.b, probeQueue.io.mem_probe, missQueue.io.probe_block) 498 probeQueue.io.lrsc_locked_block <> mainPipe.io.lrsc_locked_block 499 500 //---------------------------------------- 501 // mainPipe 502 // when a req enters main pipe, if it is set-conflict with replace pipe or refill pipe, 503 // block the req in main pipe 504 val refillPipeStatus, replacePipeStatusS0 = Wire(Valid(UInt(idxBits.W))) 505 refillPipeStatus.valid := refillPipe.io.req.valid 506 refillPipeStatus.bits := get_idx(refillPipe.io.req.bits.paddrWithVirtualAlias) 507 replacePipeStatusS0.valid := replacePipe.io.req.valid 508 replacePipeStatusS0.bits := get_idx(replacePipe.io.req.bits.vaddr) 509 val blockMainPipeReqs = Seq( 510 refillPipeStatus, 511 replacePipeStatusS0, 512 replacePipe.io.status.s1_set, 513 replacePipe.io.status.s2_set 514 ) 515 val storeShouldBeBlocked = Cat(blockMainPipeReqs.map(r => r.valid && r.bits === io.lsu.store.req.bits.idx)).orR 516 val probeShouldBeBlocked = Cat(blockMainPipeReqs.map(r => r.valid && r.bits === get_idx(probeQueue.io.pipe_req.bits.vaddr))).orR 517 518 block_decoupled(probeQueue.io.pipe_req, mainPipe.io.probe_req, probeShouldBeBlocked) 519 block_decoupled(io.lsu.store.req, mainPipe.io.store_req, storeShouldBeBlocked) 520 521 io.lsu.store.replay_resp := mainPipe.io.store_replay_resp 522 io.lsu.store.main_pipe_hit_resp := mainPipe.io.store_hit_resp 523 524 val mainPipeAtomicReqArb = Module(new Arbiter(new MainPipeReq, 2)) 525 mainPipeAtomicReqArb.io.in(0) <> missQueue.io.main_pipe_req 526 mainPipeAtomicReqArb.io.in(1) <> atomicsReplayUnit.io.pipe_req 527 mainPipe.io.atomic_req <> mainPipeAtomicReqArb.io.out 528 529 mainPipe.io.invalid_resv_set := wb.io.req.fire && wb.io.req.bits.addr === mainPipe.io.lrsc_locked_block.bits 530 531 //---------------------------------------- 532 // replace pipe 533 val mpStatus = mainPipe.io.status 534 val replaceSet = addr_to_dcache_set(missQueue.io.replace_pipe_req.bits.vaddr) 535 val replaceWayEn = missQueue.io.replace_pipe_req.bits.way_en 536 val replaceShouldBeBlocked = // mpStatus.s0_set.valid && replaceSet === mpStatus.s0_set.bits || 537 Cat(Seq(mpStatus.s1, mpStatus.s2, mpStatus.s3).map(s => 538 s.valid && s.bits.set === replaceSet && s.bits.way_en === replaceWayEn 539 )).orR() 540 block_decoupled(missQueue.io.replace_pipe_req, replacePipe.io.req, replaceShouldBeBlocked) 541 missQueue.io.replace_pipe_resp := replacePipe.io.resp 542 543 //---------------------------------------- 544 // refill pipe 545 val refillShouldBeBlocked = (mpStatus.s1.valid && mpStatus.s1.bits.set === missQueue.io.refill_pipe_req.bits.idx) || 546 Cat(Seq(mpStatus.s2, mpStatus.s3).map(s => 547 s.valid && 548 s.bits.set === missQueue.io.refill_pipe_req.bits.idx && 549 s.bits.way_en === missQueue.io.refill_pipe_req.bits.way_en 550 )).orR 551 block_decoupled(missQueue.io.refill_pipe_req, refillPipe.io.req, refillShouldBeBlocked) 552 io.lsu.store.refill_hit_resp := refillPipe.io.store_resp 553 554 //---------------------------------------- 555 // wb 556 // add a queue between MainPipe and WritebackUnit to reduce MainPipe stalls due to WritebackUnit busy 557 val wbArb = Module(new Arbiter(new WritebackReq, 2)) 558 wbArb.io.in.zip(Seq(mainPipe.io.wb, replacePipe.io.wb)).foreach { case (arb, pipe) => arb <> pipe } 559 wb.io.req <> wbArb.io.out 560 bus.c <> wb.io.mem_release 561 wb.io.release_wakeup := refillPipe.io.release_wakeup 562 wb.io.release_update := mainPipe.io.release_update 563 io.lsu.release.valid := bus.c.fire() 564 io.lsu.release.bits.paddr := bus.c.bits.address 565 566 // connect bus d 567 missQueue.io.mem_grant.valid := false.B 568 missQueue.io.mem_grant.bits := DontCare 569 570 wb.io.mem_grant.valid := false.B 571 wb.io.mem_grant.bits := DontCare 572 573 // in L1DCache, we ony expect Grant[Data] and ReleaseAck 574 bus.d.ready := false.B 575 when (bus.d.bits.opcode === TLMessages.Grant || bus.d.bits.opcode === TLMessages.GrantData) { 576 missQueue.io.mem_grant <> bus.d 577 } .elsewhen (bus.d.bits.opcode === TLMessages.ReleaseAck) { 578 wb.io.mem_grant <> bus.d 579 } .otherwise { 580 assert (!bus.d.fire()) 581 } 582 583 //---------------------------------------- 584 // replacement algorithm 585 val replacer = ReplacementPolicy.fromString(cacheParams.replacer, nWays, nSets) 586 587 val replWayReqs = ldu.map(_.io.replace_way) ++ Seq(mainPipe.io.replace_way) 588 replWayReqs.foreach{ 589 case req => 590 req.way := DontCare 591 when (req.set.valid) { req.way := replacer.way(req.set.bits) } 592 } 593 594 val replAccessReqs = ldu.map(_.io.replace_access) ++ Seq( 595 mainPipe.io.replace_access, 596 refillPipe.io.replace_access 597 ) 598 val touchWays = Seq.fill(replAccessReqs.size)(Wire(ValidIO(UInt(log2Up(nWays).W)))) 599 touchWays.zip(replAccessReqs).foreach { 600 case (w, req) => 601 w.valid := req.valid 602 w.bits := req.bits.way 603 } 604 val touchSets = replAccessReqs.map(_.bits.set) 605 replacer.access(touchSets, touchWays) 606 607 //---------------------------------------- 608 // assertions 609 // dcache should only deal with DRAM addresses 610 when (bus.a.fire()) { 611 assert(bus.a.bits.address >= 0x80000000L.U) 612 } 613 when (bus.b.fire()) { 614 assert(bus.b.bits.address >= 0x80000000L.U) 615 } 616 when (bus.c.fire()) { 617 assert(bus.c.bits.address >= 0x80000000L.U) 618 } 619 620 //---------------------------------------- 621 // utility functions 622 def block_decoupled[T <: Data](source: DecoupledIO[T], sink: DecoupledIO[T], block_signal: Bool) = { 623 sink.valid := source.valid && !block_signal 624 source.ready := sink.ready && !block_signal 625 sink.bits := source.bits 626 } 627 628 //---------------------------------------- 629 // Customized csr cache op support 630 val cacheOpDecoder = Module(new CSRCacheOpDecoder("dcache", CacheInstrucion.COP_ID_DCACHE)) 631 cacheOpDecoder.io.csr <> io.csr 632 bankedDataArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 633 metaArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 634 tagArray.io.cacheOp.req := cacheOpDecoder.io.cache.req 635 cacheOpDecoder.io.cache.resp.valid := bankedDataArray.io.cacheOp.resp.valid || 636 metaArray.io.cacheOp.resp.valid || 637 tagArray.io.cacheOp.resp.valid 638 cacheOpDecoder.io.cache.resp.bits := Mux1H(List( 639 bankedDataArray.io.cacheOp.resp.valid -> bankedDataArray.io.cacheOp.resp.bits, 640 metaArray.io.cacheOp.resp.valid -> metaArray.io.cacheOp.resp.bits, 641 tagArray.io.cacheOp.resp.valid -> tagArray.io.cacheOp.resp.bits, 642 )) 643 assert(!((bankedDataArray.io.cacheOp.resp.valid +& metaArray.io.cacheOp.resp.valid +& tagArray.io.cacheOp.resp.valid) > 1.U)) 644 645 //---------------------------------------- 646 // performance counters 647 val num_loads = PopCount(ldu.map(e => e.io.lsu.req.fire())) 648 XSPerfAccumulate("num_loads", num_loads) 649 650 io.mshrFull := missQueue.io.full 651 652 // performance counter 653 val ld_access = Wire(Vec(LoadPipelineWidth, missQueue.io.debug_early_replace.last.cloneType)) 654 val st_access = Wire(ld_access.last.cloneType) 655 ld_access.zip(ldu).foreach { 656 case (a, u) => 657 a.valid := RegNext(u.io.lsu.req.fire()) && !u.io.lsu.s1_kill 658 a.bits.idx := RegNext(get_idx(u.io.lsu.req.bits.addr)) 659 a.bits.tag := get_tag(u.io.lsu.s1_paddr) 660 } 661 st_access.valid := RegNext(mainPipe.io.store_req.fire()) 662 st_access.bits.idx := RegNext(get_idx(mainPipe.io.store_req.bits.vaddr)) 663 st_access.bits.tag := RegNext(get_tag(mainPipe.io.store_req.bits.addr)) 664 val access_info = ld_access.toSeq ++ Seq(st_access) 665 val early_replace = RegNext(missQueue.io.debug_early_replace) 666 val access_early_replace = access_info.map { 667 case acc => 668 Cat(early_replace.map { 669 case r => 670 acc.valid && r.valid && 671 acc.bits.tag === r.bits.tag && 672 acc.bits.idx === r.bits.idx 673 }) 674 } 675 XSPerfAccumulate("access_early_replace", PopCount(Cat(access_early_replace))) 676 677 val wb_perf = wb.perfEvents.map(_._1).zip(wb.perfinfo.perfEvents.perf_events) 678 val mainp_perf = mainPipe.perfEvents.map(_._1).zip(mainPipe.perfinfo.perfEvents.perf_events) 679 val missq_perf = missQueue.perfEvents.map(_._1).zip(missQueue.perfinfo.perfEvents.perf_events) 680 val probq_perf = probeQueue.perfEvents.map(_._1).zip(probeQueue.perfinfo.perfEvents.perf_events) 681 val ldu_0_perf = ldu(0).perfEvents.map(_._1).zip(ldu(0).perfinfo.perfEvents.perf_events) 682 val ldu_1_perf = ldu(1).perfEvents.map(_._1).zip(ldu(1).perfinfo.perfEvents.perf_events) 683 val perfEvents = wb_perf ++ mainp_perf ++ missq_perf ++ probq_perf ++ ldu_0_perf ++ ldu_1_perf 684 val perflist = wb.perfinfo.perfEvents.perf_events ++ mainPipe.perfinfo.perfEvents.perf_events ++ 685 missQueue.perfinfo.perfEvents.perf_events ++ probeQueue.perfinfo.perfEvents.perf_events ++ 686 ldu(0).perfinfo.perfEvents.perf_events ++ ldu(1).perfinfo.perfEvents.perf_events 687 val perf_length = perflist.length 688 val perfinfo = IO(new Bundle(){ 689 val perfEvents = Output(new PerfEventsBundle(perflist.length)) 690 }) 691 perfinfo.perfEvents.perf_events := perflist 692 693} 694 695class AMOHelper() extends ExtModule { 696 val clock = IO(Input(Clock())) 697 val enable = IO(Input(Bool())) 698 val cmd = IO(Input(UInt(5.W))) 699 val addr = IO(Input(UInt(64.W))) 700 val wdata = IO(Input(UInt(64.W))) 701 val mask = IO(Input(UInt(8.W))) 702 val rdata = IO(Output(UInt(64.W))) 703} 704 705class DCacheWrapper()(implicit p: Parameters) extends LazyModule with HasXSParameter { 706 707 val useDcache = coreParams.dcacheParametersOpt.nonEmpty 708 val clientNode = if (useDcache) TLIdentityNode() else null 709 val dcache = if (useDcache) LazyModule(new DCache()) else null 710 if (useDcache) { 711 clientNode := dcache.clientNode 712 } 713 714 lazy val module = new LazyModuleImp(this) { 715 val io = IO(new DCacheIO) 716 val perfinfo = IO(new Bundle(){ 717 val perfEvents = Output(new PerfEventsBundle(dcache.asInstanceOf[DCache].module.perf_length)) 718 }) 719 val perfEvents = dcache.asInstanceOf[DCache].module.perfEvents.map(_._1).zip(dcache.asInstanceOf[DCache].module.perfinfo.perfEvents.perf_events) 720 if (!useDcache) { 721 // a fake dcache which uses dpi-c to access memory, only for debug usage! 722 val fake_dcache = Module(new FakeDCache()) 723 io <> fake_dcache.io 724 } 725 else { 726 io <> dcache.module.io 727 perfinfo := dcache.asInstanceOf[DCache].module.perfinfo 728 } 729 } 730} 731