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