1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17 18package xiangshan.frontend.icache 19 20import chisel3._ 21import chisel3.util._ 22import freechips.rocketchip.diplomacy.IdRange 23import freechips.rocketchip.diplomacy.LazyModule 24import freechips.rocketchip.diplomacy.LazyModuleImp 25import freechips.rocketchip.tilelink._ 26import freechips.rocketchip.util.BundleFieldBase 27import huancun.AliasField 28import huancun.PrefetchField 29import org.chipsalliance.cde.config.Parameters 30import utility._ 31import utils._ 32import xiangshan._ 33import xiangshan.cache._ 34import xiangshan.cache.mmu.TlbRequestIO 35import xiangshan.frontend._ 36 37case class ICacheParameters( 38 nSets: Int = 256, 39 nWays: Int = 4, 40 rowBits: Int = 64, 41 nTLBEntries: Int = 32, 42 tagECC: Option[String] = None, 43 dataECC: Option[String] = None, 44 replacer: Option[String] = Some("random"), 45 PortNumber: Int = 2, 46 nFetchMshr: Int = 4, 47 nPrefetchMshr: Int = 10, 48 nWayLookupSize: Int = 32, 49 DataCodeUnit: Int = 64, 50 ICacheDataBanks: Int = 8, 51 ICacheDataSRAMWidth: Int = 66, 52 // TODO: hard code, need delete 53 partWayNum: Int = 4, 54 nMMIOs: Int = 1, 55 blockBytes: Int = 64 56) extends L1CacheParameters { 57 58 val setBytes = nSets * blockBytes 59 val aliasBitsOpt = 60 DCacheParameters().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 def tagCode: Code = Code.fromString(tagECC) 67 def dataCode: Code = Code.fromString(dataECC) 68 def replacement = ReplacementPolicy.fromString(replacer, nWays, nSets) 69} 70 71trait HasICacheParameters extends HasL1CacheParameters with HasInstrMMIOConst with HasIFUConst { 72 val cacheParams = icacheParameters 73 74 def ICacheSets = cacheParams.nSets 75 def ICacheWays = cacheParams.nWays 76 def PortNumber = cacheParams.PortNumber 77 def nFetchMshr = cacheParams.nFetchMshr 78 def nPrefetchMshr = cacheParams.nPrefetchMshr 79 def nWayLookupSize = cacheParams.nWayLookupSize 80 def DataCodeUnit = cacheParams.DataCodeUnit 81 def ICacheDataBanks = cacheParams.ICacheDataBanks 82 def ICacheDataSRAMWidth = cacheParams.ICacheDataSRAMWidth 83 def partWayNum = cacheParams.partWayNum 84 85 def ICacheMetaBits = tagBits // FIXME: unportable: maybe use somemethod to get width 86 def ICacheMetaCodeBits = 1 // FIXME: unportable: maybe use cacheParams.tagCode.somemethod to get width 87 def ICacheMetaEntryBits = ICacheMetaBits + ICacheMetaCodeBits 88 89 def ICacheDataBits = blockBits / ICacheDataBanks 90 def ICacheDataCodeSegs = math.ceil(ICacheDataBits / DataCodeUnit).toInt // split data to segments for ECC checking 91 def ICacheDataCodeBits = 92 ICacheDataCodeSegs * 1 // FIXME: unportable: maybe use cacheParams.dataCode.somemethod to get width 93 def ICacheDataEntryBits = ICacheDataBits + ICacheDataCodeBits 94 def ICacheBankVisitNum = 32 * 8 / ICacheDataBits + 1 95 def highestIdxBit = log2Ceil(nSets) - 1 96 97 require((ICacheDataBanks >= 2) && isPow2(ICacheDataBanks)) 98 require(ICacheDataSRAMWidth >= ICacheDataEntryBits) 99 require(isPow2(ICacheSets), s"nSets($ICacheSets) must be pow2") 100 require(isPow2(ICacheWays), s"nWays($ICacheWays) must be pow2") 101 102 def getBits(num: Int) = log2Ceil(num).W 103 104 def generatePipeControl(lastFire: Bool, thisFire: Bool, thisFlush: Bool, lastFlush: Bool): Bool = { 105 val valid = RegInit(false.B) 106 when(thisFlush)(valid := false.B) 107 .elsewhen(lastFire && !lastFlush)(valid := true.B) 108 .elsewhen(thisFire)(valid := false.B) 109 valid 110 } 111 112 def ResultHoldBypass[T <: Data](data: T, valid: Bool): T = 113 Mux(valid, data, RegEnable(data, valid)) 114 115 def ResultHoldBypass[T <: Data](data: T, init: T, valid: Bool): T = 116 Mux(valid, data, RegEnable(data, init, valid)) 117 118 def holdReleaseLatch(valid: Bool, release: Bool, flush: Bool): Bool = { 119 val bit = RegInit(false.B) 120 when(flush)(bit := false.B) 121 .elsewhen(valid && !release)(bit := true.B) 122 .elsewhen(release)(bit := false.B) 123 bit || valid 124 } 125 126 def blockCounter(block: Bool, flush: Bool, threshold: Int): Bool = { 127 val counter = RegInit(0.U(log2Up(threshold + 1).W)) 128 when(block)(counter := counter + 1.U) 129 when(flush)(counter := 0.U) 130 counter > threshold.U 131 } 132 133 def InitQueue[T <: Data](entry: T, size: Int): Vec[T] = 134 return RegInit(VecInit(Seq.fill(size)(0.U.asTypeOf(entry.cloneType)))) 135 136 def encodeMetaECC(meta: UInt): UInt = { 137 require(meta.getWidth == ICacheMetaBits) 138 val code = cacheParams.tagCode.encode(meta) >> ICacheMetaBits 139 code.asTypeOf(UInt(ICacheMetaCodeBits.W)) 140 } 141 142 def encodeDataECC(data: UInt): UInt = { 143 require(data.getWidth == ICacheDataBits) 144 val datas = data.asTypeOf(Vec(ICacheDataCodeSegs, UInt((ICacheDataBits / ICacheDataCodeSegs).W))) 145 val codes = VecInit(datas.map(cacheParams.dataCode.encode(_) >> (ICacheDataBits / ICacheDataCodeSegs))) 146 codes.asTypeOf(UInt(ICacheDataCodeBits.W)) 147 } 148 149 def getBankSel(blkOffset: UInt, valid: Bool = true.B): Vec[UInt] = { 150 val bankIdxLow = Cat(0.U(1.W), blkOffset) >> log2Ceil(blockBytes / ICacheDataBanks) 151 val bankIdxHigh = (Cat(0.U(1.W), blkOffset) + 32.U) >> log2Ceil(blockBytes / ICacheDataBanks) 152 val bankSel = VecInit((0 until ICacheDataBanks * 2).map(i => (i.U >= bankIdxLow) && (i.U <= bankIdxHigh))) 153 assert( 154 !valid || PopCount(bankSel) === ICacheBankVisitNum.U, 155 "The number of bank visits must be %d, but bankSel=0x%x", 156 ICacheBankVisitNum.U, 157 bankSel.asUInt 158 ) 159 bankSel.asTypeOf(UInt((ICacheDataBanks * 2).W)).asTypeOf(Vec(2, UInt(ICacheDataBanks.W))) 160 } 161 162 def getLineSel(blkOffset: UInt)(implicit p: Parameters): Vec[Bool] = { 163 val bankIdxLow = blkOffset >> log2Ceil(blockBytes / ICacheDataBanks) 164 val lineSel = VecInit((0 until ICacheDataBanks).map(i => i.U < bankIdxLow)) 165 lineSel 166 } 167 168 def getBlkAddr(addr: UInt) = addr >> blockOffBits 169 def getPhyTagFromBlk(addr: UInt): UInt = addr >> (pgUntagBits - blockOffBits) 170 def getIdxFromBlk(addr: UInt) = addr(idxBits - 1, 0) 171 def get_paddr_from_ptag(vaddr: UInt, ptag: UInt) = Cat(ptag, vaddr(pgUntagBits - 1, 0)) 172} 173 174abstract class ICacheBundle(implicit p: Parameters) extends XSBundle 175 with HasICacheParameters 176 177abstract class ICacheModule(implicit p: Parameters) extends XSModule 178 with HasICacheParameters 179 180abstract class ICacheArray(implicit p: Parameters) extends XSModule 181 with HasICacheParameters 182 183class ICacheMetadata(implicit p: Parameters) extends ICacheBundle { 184 val tag = UInt(tagBits.W) 185} 186 187object ICacheMetadata { 188 def apply(tag: Bits)(implicit p: Parameters) = { 189 val meta = Wire(new ICacheMetadata) 190 meta.tag := tag 191 meta 192 } 193} 194 195class ICacheMetaArray()(implicit p: Parameters) extends ICacheArray { 196 class ICacheMetaEntry(implicit p: Parameters) extends ICacheBundle { 197 val meta: ICacheMetadata = new ICacheMetadata 198 val code: UInt = UInt(ICacheMetaCodeBits.W) 199 } 200 201 private object ICacheMetaEntry { 202 def apply(meta: ICacheMetadata)(implicit p: Parameters): ICacheMetaEntry = { 203 val entry = Wire(new ICacheMetaEntry) 204 entry.meta := meta 205 entry.code := encodeMetaECC(meta.asUInt) 206 entry 207 } 208 } 209 210 // sanity check 211 require(ICacheMetaEntryBits == (new ICacheMetaEntry).getWidth) 212 213 val io = IO(new Bundle { 214 val write = Flipped(DecoupledIO(new ICacheMetaWriteBundle)) 215 val read = Flipped(DecoupledIO(new ICacheReadBundle)) 216 val readResp = Output(new ICacheMetaRespBundle) 217 val fencei = Input(Bool()) 218 }) 219 220 val port_0_read_0 = io.read.valid && !io.read.bits.vSetIdx(0)(0) 221 val port_0_read_1 = io.read.valid && io.read.bits.vSetIdx(0)(0) 222 val port_1_read_1 = io.read.valid && io.read.bits.vSetIdx(1)(0) && io.read.bits.isDoubleLine 223 val port_1_read_0 = io.read.valid && !io.read.bits.vSetIdx(1)(0) && io.read.bits.isDoubleLine 224 225 val port_0_read_0_reg = RegEnable(port_0_read_0, 0.U.asTypeOf(port_0_read_0), io.read.fire) 226 val port_0_read_1_reg = RegEnable(port_0_read_1, 0.U.asTypeOf(port_0_read_1), io.read.fire) 227 val port_1_read_1_reg = RegEnable(port_1_read_1, 0.U.asTypeOf(port_1_read_1), io.read.fire) 228 val port_1_read_0_reg = RegEnable(port_1_read_0, 0.U.asTypeOf(port_1_read_0), io.read.fire) 229 230 val bank_0_idx = Mux(port_0_read_0, io.read.bits.vSetIdx(0), io.read.bits.vSetIdx(1)) 231 val bank_1_idx = Mux(port_0_read_1, io.read.bits.vSetIdx(0), io.read.bits.vSetIdx(1)) 232 val bank_idx = Seq(bank_0_idx, bank_1_idx) 233 234 val write_bank_0 = io.write.valid && !io.write.bits.bankIdx 235 val write_bank_1 = io.write.valid && io.write.bits.bankIdx 236 237 val write_meta_bits = ICacheMetaEntry(meta = 238 ICacheMetadata( 239 tag = io.write.bits.phyTag 240 ) 241 ) 242 243 val tagArrays = (0 until 2) map { bank => 244 val tagArray = Module(new SRAMTemplate( 245 new ICacheMetaEntry(), 246 set = nSets / 2, 247 way = nWays, 248 shouldReset = true, 249 holdRead = true, 250 singlePort = true 251 )) 252 253 // meta connection 254 if (bank == 0) { 255 tagArray.io.r.req.valid := port_0_read_0 || port_1_read_0 256 tagArray.io.r.req.bits.apply(setIdx = bank_0_idx(highestIdxBit, 1)) 257 tagArray.io.w.req.valid := write_bank_0 258 tagArray.io.w.req.bits.apply( 259 data = write_meta_bits, 260 setIdx = io.write.bits.virIdx(highestIdxBit, 1), 261 waymask = io.write.bits.waymask 262 ) 263 } else { 264 tagArray.io.r.req.valid := port_0_read_1 || port_1_read_1 265 tagArray.io.r.req.bits.apply(setIdx = bank_1_idx(highestIdxBit, 1)) 266 tagArray.io.w.req.valid := write_bank_1 267 tagArray.io.w.req.bits.apply( 268 data = write_meta_bits, 269 setIdx = io.write.bits.virIdx(highestIdxBit, 1), 270 waymask = io.write.bits.waymask 271 ) 272 } 273 274 tagArray 275 } 276 277 val read_set_idx_next = RegEnable(io.read.bits.vSetIdx, 0.U.asTypeOf(io.read.bits.vSetIdx), io.read.fire) 278 val valid_array = RegInit(VecInit(Seq.fill(nWays)(0.U(nSets.W)))) 279 val valid_metas = Wire(Vec(PortNumber, Vec(nWays, Bool()))) 280 // valid read 281 (0 until PortNumber).foreach(i => 282 (0 until nWays).foreach(way => 283 valid_metas(i)(way) := valid_array(way)(read_set_idx_next(i)) 284 ) 285 ) 286 io.readResp.entryValid := valid_metas 287 288 io.read.ready := !io.write.valid && !io.fencei && tagArrays.map(_.io.r.req.ready).reduce(_ && _) 289 290 // valid write 291 val way_num = OHToUInt(io.write.bits.waymask) 292 when(io.write.valid) { 293 valid_array(way_num) := valid_array(way_num).bitSet(io.write.bits.virIdx, true.B) 294 } 295 296 XSPerfAccumulate("meta_refill_num", io.write.valid) 297 298 io.readResp.metas <> DontCare 299 io.readResp.codes <> DontCare 300 val readMetaEntries = tagArrays.map(port => port.io.r.resp.asTypeOf(Vec(nWays, new ICacheMetaEntry()))) 301 val readMetas = readMetaEntries.map(_.map(_.meta)) 302 val readCodes = readMetaEntries.map(_.map(_.code)) 303 304 // TEST: force ECC to fail by setting readCodes to 0 305 if (ICacheForceMetaECCError) { 306 readCodes.foreach(_.foreach(_ := 0.U)) 307 } 308 309 when(port_0_read_0_reg) { 310 io.readResp.metas(0) := readMetas(0) 311 io.readResp.codes(0) := readCodes(0) 312 }.elsewhen(port_0_read_1_reg) { 313 io.readResp.metas(0) := readMetas(1) 314 io.readResp.codes(0) := readCodes(1) 315 } 316 317 when(port_1_read_0_reg) { 318 io.readResp.metas(1) := readMetas(0) 319 io.readResp.codes(1) := readCodes(0) 320 }.elsewhen(port_1_read_1_reg) { 321 io.readResp.metas(1) := readMetas(1) 322 io.readResp.codes(1) := readCodes(1) 323 } 324 325 io.write.ready := true.B // TODO : has bug ? should be !io.cacheOp.req.valid 326 327 // fencei logic : reset valid_array 328 when(io.fencei) { 329 (0 until nWays).foreach(way => 330 valid_array(way) := 0.U 331 ) 332 } 333} 334 335class ICacheDataArray(implicit p: Parameters) extends ICacheArray { 336 class ICacheDataEntry(implicit p: Parameters) extends ICacheBundle { 337 val data = UInt(ICacheDataBits.W) 338 val code = UInt(ICacheDataCodeBits.W) 339 } 340 341 object ICacheDataEntry { 342 def apply(data: UInt)(implicit p: Parameters) = { 343 val entry = Wire(new ICacheDataEntry) 344 entry.data := data 345 entry.code := encodeDataECC(data) 346 entry 347 } 348 } 349 350 val io = IO { 351 new Bundle { 352 val write = Flipped(DecoupledIO(new ICacheDataWriteBundle)) 353 // TODO: fix hard code 354 val read = Flipped(Vec(4, DecoupledIO(new ICacheReadBundle))) 355 val readResp = Output(new ICacheDataRespBundle) 356 } 357 } 358 359 /** 360 ****************************************************************************** 361 * data array 362 ****************************************************************************** 363 */ 364 val writeDatas = io.write.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt(ICacheDataBits.W))) 365 val writeEntries = writeDatas.map(ICacheDataEntry(_).asUInt) 366 367 val bankSel = getBankSel(io.read(0).bits.blkOffset, io.read(0).valid) 368 val lineSel = getLineSel(io.read(0).bits.blkOffset) 369 val waymasks = io.read(0).bits.wayMask 370 val masks = Wire(Vec(nWays, Vec(ICacheDataBanks, Bool()))) 371 (0 until nWays).foreach { way => 372 (0 until ICacheDataBanks).foreach { bank => 373 masks(way)(bank) := Mux( 374 lineSel(bank), 375 waymasks(1)(way) && bankSel(1)(bank).asBool, 376 waymasks(0)(way) && bankSel(0)(bank).asBool 377 ) 378 } 379 } 380 381 val dataArrays = (0 until nWays).map { way => 382 (0 until ICacheDataBanks).map { bank => 383 val sramBank = Module(new SRAMTemplateWithFixedWidth( 384 UInt(ICacheDataEntryBits.W), 385 set = nSets, 386 width = ICacheDataSRAMWidth, 387 shouldReset = true, 388 holdRead = true, 389 singlePort = true 390 )) 391 392 // read 393 sramBank.io.r.req.valid := io.read(bank % 4).valid && masks(way)(bank) 394 sramBank.io.r.req.bits.apply(setIdx = 395 Mux(lineSel(bank), io.read(bank % 4).bits.vSetIdx(1), io.read(bank % 4).bits.vSetIdx(0)) 396 ) 397 // write 398 sramBank.io.w.req.valid := io.write.valid && io.write.bits.waymask(way).asBool 399 sramBank.io.w.req.bits.apply( 400 data = writeEntries(bank), 401 setIdx = io.write.bits.virIdx, 402 // waymask is invalid when way of SRAMTemplate <= 1 403 waymask = 0.U 404 ) 405 sramBank 406 } 407 } 408 409 /** 410 ****************************************************************************** 411 * read logic 412 ****************************************************************************** 413 */ 414 val masksReg = RegEnable(masks, 0.U.asTypeOf(masks), io.read(0).valid) 415 val readDataWithCode = (0 until ICacheDataBanks).map(bank => 416 Mux1H(VecInit(masksReg.map(_(bank))).asTypeOf(UInt(nWays.W)), dataArrays.map(_(bank).io.r.resp.asUInt)) 417 ) 418 val readEntries = readDataWithCode.map(_.asTypeOf(new ICacheDataEntry())) 419 val readDatas = VecInit(readEntries.map(_.data)) 420 val readCodes = VecInit(readEntries.map(_.code)) 421 422 // TEST: force ECC to fail by setting readCodes to 0 423 if (ICacheForceDataECCError) { 424 readCodes.foreach(_ := 0.U) 425 } 426 427 /** 428 ****************************************************************************** 429 * IO 430 ****************************************************************************** 431 */ 432 io.readResp.datas := readDatas 433 io.readResp.codes := readCodes 434 io.write.ready := true.B 435 io.read.foreach(_.ready := !io.write.valid) 436} 437 438class ICacheReplacer(implicit p: Parameters) extends ICacheModule { 439 val io = IO(new Bundle { 440 val touch = Vec(PortNumber, Flipped(ValidIO(new ReplacerTouch))) 441 val victim = Flipped(new ReplacerVictim) 442 }) 443 444 val replacers = Seq.fill(PortNumber)(ReplacementPolicy.fromString(cacheParams.replacer, nWays, nSets / PortNumber)) 445 446 // touch 447 val touch_sets = Seq.fill(PortNumber)(Wire(Vec(2, UInt(log2Ceil(nSets / 2).W)))) 448 val touch_ways = Seq.fill(PortNumber)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W))))) 449 (0 until PortNumber).foreach { i => 450 touch_sets(i)(0) := Mux( 451 io.touch(i).bits.vSetIdx(0), 452 io.touch(1).bits.vSetIdx(highestIdxBit, 1), 453 io.touch(0).bits.vSetIdx(highestIdxBit, 1) 454 ) 455 touch_ways(i)(0).bits := Mux(io.touch(i).bits.vSetIdx(0), io.touch(1).bits.way, io.touch(0).bits.way) 456 touch_ways(i)(0).valid := Mux(io.touch(i).bits.vSetIdx(0), io.touch(1).valid, io.touch(0).valid) 457 } 458 459 // victim 460 io.victim.way := Mux( 461 io.victim.vSetIdx.bits(0), 462 replacers(1).way(io.victim.vSetIdx.bits(highestIdxBit, 1)), 463 replacers(0).way(io.victim.vSetIdx.bits(highestIdxBit, 1)) 464 ) 465 466 // touch the victim in next cycle 467 val victim_vSetIdx_reg = 468 RegEnable(io.victim.vSetIdx.bits, 0.U.asTypeOf(io.victim.vSetIdx.bits), io.victim.vSetIdx.valid) 469 val victim_way_reg = RegEnable(io.victim.way, 0.U.asTypeOf(io.victim.way), io.victim.vSetIdx.valid) 470 (0 until PortNumber).foreach { i => 471 touch_sets(i)(1) := victim_vSetIdx_reg(highestIdxBit, 1) 472 touch_ways(i)(1).bits := victim_way_reg 473 touch_ways(i)(1).valid := RegNext(io.victim.vSetIdx.valid) && (victim_vSetIdx_reg(0) === i.U) 474 } 475 476 ((replacers zip touch_sets) zip touch_ways).map { case ((r, s), w) => r.access(s, w) } 477} 478 479class ICacheIO(implicit p: Parameters) extends ICacheBundle { 480 val hartId = Input(UInt(hartIdLen.W)) 481 val ftqPrefetch = Flipped(new FtqToPrefetchIO) 482 val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle))) 483 val stop = Input(Bool()) 484 val fetch = new ICacheMainPipeBundle 485 val toIFU = Output(Bool()) 486 val pmp = Vec(2 * PortNumber, new ICachePMPBundle) 487 val itlb = Vec(PortNumber, new TlbRequestIO) 488 val perfInfo = Output(new ICachePerfInfo) 489 val error = ValidIO(new L1CacheErrorInfo) 490 /* CSR control signal */ 491 val csr_pf_enable = Input(Bool()) 492 val csr_parity_enable = Input(Bool()) 493 val fencei = Input(Bool()) 494 val flush = Input(Bool()) 495} 496 497class ICache()(implicit p: Parameters) extends LazyModule with HasICacheParameters { 498 override def shouldBeInlined: Boolean = false 499 500 val clientParameters = TLMasterPortParameters.v1( 501 Seq(TLMasterParameters.v1( 502 name = "icache", 503 sourceId = IdRange(0, cacheParams.nFetchMshr + cacheParams.nPrefetchMshr + 1) 504 )), 505 requestFields = cacheParams.reqFields, 506 echoFields = cacheParams.echoFields 507 ) 508 509 val clientNode = TLClientNode(Seq(clientParameters)) 510 511 lazy val module = new ICacheImp(this) 512} 513 514class ICacheImp(outer: ICache) extends LazyModuleImp(outer) with HasICacheParameters with HasPerfEvents { 515 val io = IO(new ICacheIO) 516 517 println("ICache:") 518 println(" TagECC: " + cacheParams.tagECC) 519 println(" DataECC: " + cacheParams.dataECC) 520 println(" ICacheSets: " + cacheParams.nSets) 521 println(" ICacheWays: " + cacheParams.nWays) 522 println(" PortNumber: " + cacheParams.PortNumber) 523 println(" nFetchMshr: " + cacheParams.nFetchMshr) 524 println(" nPrefetchMshr: " + cacheParams.nPrefetchMshr) 525 println(" nWayLookupSize: " + cacheParams.nWayLookupSize) 526 println(" DataCodeUnit: " + cacheParams.DataCodeUnit) 527 println(" ICacheDataBanks: " + cacheParams.ICacheDataBanks) 528 println(" ICacheDataSRAMWidth: " + cacheParams.ICacheDataSRAMWidth) 529 530 val (bus, edge) = outer.clientNode.out.head 531 532 val metaArray = Module(new ICacheMetaArray) 533 val dataArray = Module(new ICacheDataArray) 534 val mainPipe = Module(new ICacheMainPipe) 535 val missUnit = Module(new ICacheMissUnit(edge)) 536 val replacer = Module(new ICacheReplacer) 537 val prefetcher = Module(new IPrefetchPipe) 538 val wayLookup = Module(new WayLookup) 539 540 dataArray.io.write <> missUnit.io.data_write 541 dataArray.io.read <> mainPipe.io.dataArray.toIData 542 dataArray.io.readResp <> mainPipe.io.dataArray.fromIData 543 544 metaArray.io.fencei := io.fencei 545 metaArray.io.write <> missUnit.io.meta_write 546 metaArray.io.read <> prefetcher.io.metaRead.toIMeta 547 metaArray.io.readResp <> prefetcher.io.metaRead.fromIMeta 548 549 prefetcher.io.flush := io.flush 550 prefetcher.io.csr_pf_enable := io.csr_pf_enable 551 prefetcher.io.csr_parity_enable := io.csr_parity_enable 552 prefetcher.io.MSHRResp := missUnit.io.fetch_resp 553 prefetcher.io.flushFromBpu := io.ftqPrefetch.flushFromBpu 554 // cache softPrefetch 555 private val softPrefetchValid = RegInit(false.B) 556 private val softPrefetch = RegInit(0.U.asTypeOf(new IPrefetchReq)) 557 /* FIXME: 558 * If there is already a pending softPrefetch request, it will be overwritten. 559 * Also, if there are multiple softPrefetch requests in the same cycle, only the first one will be accepted. 560 * We should implement a softPrefetchQueue (like ibuffer, multi-in, single-out) to solve this. 561 * However, the impact on performance still needs to be assessed. 562 * Considering that the frequency of prefetch.i may not be high, let's start with a temporary dummy solution. 563 */ 564 when(io.softPrefetch.map(_.valid).reduce(_ || _)) { 565 softPrefetchValid := true.B 566 softPrefetch.fromSoftPrefetch(MuxCase( 567 0.U.asTypeOf(new SoftIfetchPrefetchBundle), 568 io.softPrefetch.map(req => req.valid -> req.bits) 569 )) 570 }.elsewhen(prefetcher.io.req.fire) { 571 softPrefetchValid := false.B 572 } 573 // pass ftqPrefetch 574 private val ftqPrefetch = WireInit(0.U.asTypeOf(new IPrefetchReq)) 575 ftqPrefetch.fromFtqICacheInfo(io.ftqPrefetch.req.bits) 576 // software prefetch has higher priority 577 prefetcher.io.req.valid := softPrefetchValid || io.ftqPrefetch.req.valid 578 prefetcher.io.req.bits := Mux(softPrefetchValid, softPrefetch, ftqPrefetch) 579 io.ftqPrefetch.req.ready := prefetcher.io.req.ready && !softPrefetchValid 580 581 missUnit.io.hartId := io.hartId 582 missUnit.io.fencei := io.fencei 583 missUnit.io.flush := io.flush 584 missUnit.io.fetch_req <> mainPipe.io.mshr.req 585 missUnit.io.prefetch_req <> prefetcher.io.MSHRReq 586 missUnit.io.mem_grant.valid := false.B 587 missUnit.io.mem_grant.bits := DontCare 588 missUnit.io.mem_grant <> bus.d 589 590 mainPipe.io.flush := io.flush 591 mainPipe.io.respStall := io.stop 592 mainPipe.io.csr_parity_enable := io.csr_parity_enable 593 mainPipe.io.hartId := io.hartId 594 mainPipe.io.mshr.resp := missUnit.io.fetch_resp 595 mainPipe.io.fetch.req <> io.fetch.req 596 mainPipe.io.wayLookupRead <> wayLookup.io.read 597 598 wayLookup.io.flush := io.flush 599 wayLookup.io.write <> prefetcher.io.wayLookupWrite 600 wayLookup.io.update := missUnit.io.fetch_resp 601 602 replacer.io.touch <> mainPipe.io.touch 603 replacer.io.victim <> missUnit.io.victim 604 605 io.pmp(0) <> mainPipe.io.pmp(0) 606 io.pmp(1) <> mainPipe.io.pmp(1) 607 io.pmp(2) <> prefetcher.io.pmp(0) 608 io.pmp(3) <> prefetcher.io.pmp(1) 609 610 io.itlb(0) <> prefetcher.io.itlb(0) 611 io.itlb(1) <> prefetcher.io.itlb(1) 612 613 // notify IFU that Icache pipeline is available 614 io.toIFU := mainPipe.io.fetch.req.ready 615 io.perfInfo := mainPipe.io.perfInfo 616 617 io.fetch.resp <> mainPipe.io.fetch.resp 618 io.fetch.topdownIcacheMiss := mainPipe.io.fetch.topdownIcacheMiss 619 io.fetch.topdownItlbMiss := mainPipe.io.fetch.topdownItlbMiss 620 621 bus.b.ready := false.B 622 bus.c.valid := false.B 623 bus.c.bits := DontCare 624 bus.e.valid := false.B 625 bus.e.bits := DontCare 626 627 bus.a <> missUnit.io.mem_acquire 628 629 // Parity error port 630 val errors = mainPipe.io.errors 631 val errors_valid = errors.map(e => e.valid).reduce(_ | _) 632 io.error.bits <> RegEnable( 633 PriorityMux(errors.map(e => e.valid -> e.bits)), 634 0.U.asTypeOf(errors(0).bits), 635 errors_valid 636 ) 637 io.error.valid := RegNext(errors_valid, false.B) 638 639 XSPerfAccumulate( 640 "softPrefetch_drop_not_ready", 641 io.softPrefetch.map(_.valid).reduce(_ || _) && softPrefetchValid && !prefetcher.io.req.fire 642 ) 643 XSPerfAccumulate("softPrefetch_drop_multi_req", PopCount(io.softPrefetch.map(_.valid)) > 1.U) 644 XSPerfAccumulate("softPrefetch_block_ftq", softPrefetchValid && io.ftqPrefetch.req.valid) 645 646 val perfEvents = Seq( 647 ("icache_miss_cnt ", false.B), 648 ("icache_miss_penalty", BoolStopWatch(start = false.B, stop = false.B || false.B, startHighPriority = true)) 649 ) 650 generatePerfEvent() 651} 652 653class ICachePartWayReadBundle[T <: Data](gen: T, pWay: Int)(implicit p: Parameters) 654 extends ICacheBundle { 655 val req = Flipped(Vec( 656 PortNumber, 657 Decoupled(new Bundle { 658 val ridx = UInt((log2Ceil(nSets) - 1).W) 659 }) 660 )) 661 val resp = Output(new Bundle { 662 val rdata = Vec(PortNumber, Vec(pWay, gen)) 663 }) 664} 665 666class ICacheWriteBundle[T <: Data](gen: T, pWay: Int)(implicit p: Parameters) 667 extends ICacheBundle { 668 val wdata = gen 669 val widx = UInt((log2Ceil(nSets) - 1).W) 670 val wbankidx = Bool() 671 val wmask = Vec(pWay, Bool()) 672} 673 674class ICachePartWayArray[T <: Data](gen: T, pWay: Int)(implicit p: Parameters) extends ICacheArray { 675 676 // including part way data 677 val io = IO { 678 new Bundle { 679 val read = new ICachePartWayReadBundle(gen, pWay) 680 val write = Flipped(ValidIO(new ICacheWriteBundle(gen, pWay))) 681 } 682 } 683 684 io.read.req.map(_.ready := !io.write.valid) 685 686 val srams = (0 until PortNumber) map { bank => 687 val sramBank = Module(new SRAMTemplate( 688 gen, 689 set = nSets / 2, 690 way = pWay, 691 shouldReset = true, 692 holdRead = true, 693 singlePort = true 694 )) 695 696 sramBank.io.r.req.valid := io.read.req(bank).valid 697 sramBank.io.r.req.bits.apply(setIdx = io.read.req(bank).bits.ridx) 698 699 if (bank == 0) sramBank.io.w.req.valid := io.write.valid && !io.write.bits.wbankidx 700 else sramBank.io.w.req.valid := io.write.valid && io.write.bits.wbankidx 701 sramBank.io.w.req.bits.apply( 702 data = io.write.bits.wdata, 703 setIdx = io.write.bits.widx, 704 waymask = io.write.bits.wmask.asUInt 705 ) 706 707 sramBank 708 } 709 710 io.read.req.map(_.ready := !io.write.valid && srams.map(_.io.r.req.ready).reduce(_ && _)) 711 712 io.read.resp.rdata := VecInit(srams.map(bank => bank.io.r.resp.asTypeOf(Vec(pWay, gen)))) 713 714} 715 716// Automatically partition the SRAM based on the width of the data and the desired width. 717// final SRAM width = width * way 718class SRAMTemplateWithFixedWidth[T <: Data]( 719 gen: T, 720 set: Int, 721 width: Int, 722 way: Int = 1, 723 shouldReset: Boolean = false, 724 holdRead: Boolean = false, 725 singlePort: Boolean = false, 726 bypassWrite: Boolean = false 727) extends Module { 728 729 val dataBits = gen.getWidth 730 val bankNum = math.ceil(dataBits.toDouble / width.toDouble).toInt 731 val totalBits = bankNum * width 732 733 val io = IO(new Bundle { 734 val r = Flipped(new SRAMReadBus(gen, set, way)) 735 val w = Flipped(new SRAMWriteBus(gen, set, way)) 736 }) 737 738 val wordType = UInt(width.W) 739 val writeDatas = (0 until bankNum).map(bank => 740 VecInit((0 until way).map(i => 741 io.w.req.bits.data(i).asTypeOf(UInt(totalBits.W)).asTypeOf(Vec(bankNum, wordType))(bank) 742 )) 743 ) 744 745 val srams = (0 until bankNum) map { bank => 746 val sramBank = Module(new SRAMTemplate( 747 wordType, 748 set = set, 749 way = way, 750 shouldReset = shouldReset, 751 holdRead = holdRead, 752 singlePort = singlePort, 753 bypassWrite = bypassWrite 754 )) 755 // read req 756 sramBank.io.r.req.valid := io.r.req.valid 757 sramBank.io.r.req.bits.setIdx := io.r.req.bits.setIdx 758 759 // write req 760 sramBank.io.w.req.valid := io.w.req.valid 761 sramBank.io.w.req.bits.setIdx := io.w.req.bits.setIdx 762 sramBank.io.w.req.bits.data := writeDatas(bank) 763 sramBank.io.w.req.bits.waymask.map(_ := io.w.req.bits.waymask.get) 764 765 sramBank 766 } 767 768 io.r.req.ready := !io.w.req.valid 769 (0 until way).foreach { i => 770 io.r.resp.data(i) := VecInit((0 until bankNum).map(bank => 771 srams(bank).io.r.resp.data(i) 772 )).asTypeOf(UInt(totalBits.W))(dataBits - 1, 0).asTypeOf(gen.cloneType) 773 } 774 775 io.r.req.ready := srams.head.io.r.req.ready 776 io.w.req.ready := srams.head.io.w.req.ready 777} 778