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