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