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