xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/ICache.scala (revision 149e918c520847554be4cf7f6594881d6d3a32c8)
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 = 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 ICacheMetaBits      = tagBits // FIXME: unportable: maybe use somemethod to get width
85  def ICacheMetaCodeBits  = 1       // FIXME: unportable: maybe use cacheParams.tagCode.somemethod to get width
86  def ICacheMetaEntryBits = ICacheMetaBits + ICacheMetaCodeBits
87
88  def ICacheDataBits     = blockBits / ICacheDataBanks
89  def ICacheDataCodeSegs = math.ceil(ICacheDataBits / DataCodeUnit).toInt // split data to segments for ECC checking
90  def ICacheDataCodeBits =
91    ICacheDataCodeSegs * 1 // FIXME: unportable: maybe use cacheParams.dataCode.somemethod to get width
92  def ICacheDataEntryBits = ICacheDataBits + ICacheDataCodeBits
93  def ICacheBankVisitNum  = 32 * 8 / ICacheDataBits + 1
94  def highestIdxBit       = log2Ceil(nSets) - 1
95
96  require((ICacheDataBanks >= 2) && isPow2(ICacheDataBanks))
97  require(ICacheDataSRAMWidth >= ICacheDataEntryBits)
98  require(isPow2(ICacheSets), s"nSets($ICacheSets) must be pow2")
99  require(isPow2(ICacheWays), s"nWays($ICacheWays) must be pow2")
100
101  def getBits(num: Int) = log2Ceil(num).W
102
103  def generatePipeControl(lastFire: Bool, thisFire: Bool, thisFlush: Bool, lastFlush: Bool): Bool = {
104    val valid = RegInit(false.B)
105    when(thisFlush)(valid := false.B)
106      .elsewhen(lastFire && !lastFlush)(valid := true.B)
107      .elsewhen(thisFire)(valid := false.B)
108    valid
109  }
110
111  def ResultHoldBypass[T <: Data](data: T, valid: Bool): T =
112    Mux(valid, data, RegEnable(data, valid))
113
114  def ResultHoldBypass[T <: Data](data: T, init: T, valid: Bool): T =
115    Mux(valid, data, RegEnable(data, init, valid))
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  def encodeMetaECC(meta: UInt): UInt = {
136    require(meta.getWidth == ICacheMetaBits)
137    val code = cacheParams.tagCode.encode(meta) >> ICacheMetaBits
138    code.asTypeOf(UInt(ICacheMetaCodeBits.W))
139  }
140
141  def encodeDataECC(data: UInt): UInt = {
142    require(data.getWidth == ICacheDataBits)
143    val datas = data.asTypeOf(Vec(ICacheDataCodeSegs, UInt((ICacheDataBits / ICacheDataCodeSegs).W)))
144    val codes = VecInit(datas.map(cacheParams.dataCode.encode(_) >> (ICacheDataBits / ICacheDataCodeSegs)))
145    codes.asTypeOf(UInt(ICacheDataCodeBits.W))
146  }
147
148  def getBankSel(blkOffset: UInt, valid: Bool = true.B): Vec[UInt] = {
149    val bankIdxLow  = Cat(0.U(1.W), blkOffset) >> log2Ceil(blockBytes / ICacheDataBanks)
150    val bankIdxHigh = (Cat(0.U(1.W), blkOffset) + 32.U) >> log2Ceil(blockBytes / ICacheDataBanks)
151    val bankSel     = VecInit((0 until ICacheDataBanks * 2).map(i => (i.U >= bankIdxLow) && (i.U <= bankIdxHigh)))
152    assert(
153      !valid || PopCount(bankSel) === ICacheBankVisitNum.U,
154      "The number of bank visits must be %d, but bankSel=0x%x",
155      ICacheBankVisitNum.U,
156      bankSel.asUInt
157    )
158    bankSel.asTypeOf(UInt((ICacheDataBanks * 2).W)).asTypeOf(Vec(2, UInt(ICacheDataBanks.W)))
159  }
160
161  def getLineSel(blkOffset: UInt)(implicit p: Parameters): Vec[Bool] = {
162    val bankIdxLow = blkOffset >> log2Ceil(blockBytes / ICacheDataBanks)
163    val lineSel    = VecInit((0 until ICacheDataBanks).map(i => i.U < bankIdxLow))
164    lineSel
165  }
166
167  def getBlkAddr(addr:           UInt) = addr >> blockOffBits
168  def getPhyTagFromBlk(addr:     UInt): UInt = addr >> (pgUntagBits - blockOffBits)
169  def getIdxFromBlk(addr:        UInt) = addr(idxBits - 1, 0)
170  def get_paddr_from_ptag(vaddr: UInt, ptag: UInt) = Cat(ptag, vaddr(pgUntagBits - 1, 0))
171}
172
173abstract class ICacheBundle(implicit p: Parameters) extends XSBundle
174    with HasICacheParameters
175
176abstract class ICacheModule(implicit p: Parameters) extends XSModule
177    with HasICacheParameters
178
179abstract class ICacheArray(implicit p: Parameters) extends XSModule
180    with HasICacheParameters
181
182class ICacheMetadata(implicit p: Parameters) extends ICacheBundle {
183  val tag = UInt(tagBits.W)
184}
185
186object ICacheMetadata {
187  def apply(tag: Bits)(implicit p: Parameters) = {
188    val meta = Wire(new ICacheMetadata)
189    meta.tag := tag
190    meta
191  }
192}
193
194class ICacheMetaArray()(implicit p: Parameters) extends ICacheArray {
195  class ICacheMetaEntry(implicit p: Parameters) extends ICacheBundle {
196    val meta: ICacheMetadata = new ICacheMetadata
197    val code: UInt           = UInt(ICacheMetaCodeBits.W)
198  }
199
200  private object ICacheMetaEntry {
201    def apply(meta: ICacheMetadata)(implicit p: Parameters): ICacheMetaEntry = {
202      val entry = Wire(new ICacheMetaEntry)
203      entry.meta := meta
204      entry.code := encodeMetaECC(meta.asUInt)
205      entry
206    }
207  }
208
209  // sanity check
210  require(ICacheMetaEntryBits == (new ICacheMetaEntry).getWidth)
211
212  val io = IO(new Bundle {
213    val write    = Flipped(DecoupledIO(new ICacheMetaWriteBundle))
214    val read     = Flipped(DecoupledIO(new ICacheReadBundle))
215    val readResp = Output(new ICacheMetaRespBundle)
216    val fencei   = Input(Bool())
217  })
218
219  val port_0_read_0 = io.read.valid && !io.read.bits.vSetIdx(0)(0)
220  val port_0_read_1 = io.read.valid && io.read.bits.vSetIdx(0)(0)
221  val port_1_read_1 = io.read.valid && io.read.bits.vSetIdx(1)(0) && io.read.bits.isDoubleLine
222  val port_1_read_0 = io.read.valid && !io.read.bits.vSetIdx(1)(0) && io.read.bits.isDoubleLine
223
224  val port_0_read_0_reg = RegEnable(port_0_read_0, 0.U.asTypeOf(port_0_read_0), io.read.fire)
225  val port_0_read_1_reg = RegEnable(port_0_read_1, 0.U.asTypeOf(port_0_read_1), io.read.fire)
226  val port_1_read_1_reg = RegEnable(port_1_read_1, 0.U.asTypeOf(port_1_read_1), io.read.fire)
227  val port_1_read_0_reg = RegEnable(port_1_read_0, 0.U.asTypeOf(port_1_read_0), io.read.fire)
228
229  val bank_0_idx = Mux(port_0_read_0, io.read.bits.vSetIdx(0), io.read.bits.vSetIdx(1))
230  val bank_1_idx = Mux(port_0_read_1, io.read.bits.vSetIdx(0), io.read.bits.vSetIdx(1))
231  val bank_idx   = Seq(bank_0_idx, bank_1_idx)
232
233  val write_bank_0 = io.write.valid && !io.write.bits.bankIdx
234  val write_bank_1 = io.write.valid && io.write.bits.bankIdx
235
236  val write_meta_bits = ICacheMetaEntry(meta =
237    ICacheMetadata(
238      tag = io.write.bits.phyTag
239    )
240  )
241
242  val tagArrays = (0 until 2) map { bank =>
243    val tagArray = Module(new SRAMTemplate(
244      new ICacheMetaEntry(),
245      set = nSets / 2,
246      way = nWays,
247      shouldReset = true,
248      holdRead = true,
249      singlePort = true
250    ))
251
252    // meta connection
253    if (bank == 0) {
254      tagArray.io.r.req.valid := port_0_read_0 || port_1_read_0
255      tagArray.io.r.req.bits.apply(setIdx = bank_0_idx(highestIdxBit, 1))
256      tagArray.io.w.req.valid := write_bank_0
257      tagArray.io.w.req.bits.apply(
258        data = write_meta_bits,
259        setIdx = io.write.bits.virIdx(highestIdxBit, 1),
260        waymask = io.write.bits.waymask
261      )
262    } else {
263      tagArray.io.r.req.valid := port_0_read_1 || port_1_read_1
264      tagArray.io.r.req.bits.apply(setIdx = bank_1_idx(highestIdxBit, 1))
265      tagArray.io.w.req.valid := write_bank_1
266      tagArray.io.w.req.bits.apply(
267        data = write_meta_bits,
268        setIdx = io.write.bits.virIdx(highestIdxBit, 1),
269        waymask = io.write.bits.waymask
270      )
271    }
272
273    tagArray
274  }
275
276  val read_set_idx_next = RegEnable(io.read.bits.vSetIdx, 0.U.asTypeOf(io.read.bits.vSetIdx), io.read.fire)
277  val valid_array       = RegInit(VecInit(Seq.fill(nWays)(0.U(nSets.W))))
278  val valid_metas       = Wire(Vec(PortNumber, Vec(nWays, Bool())))
279  // valid read
280  (0 until PortNumber).foreach(i =>
281    (0 until nWays).foreach(way =>
282      valid_metas(i)(way) := valid_array(way)(read_set_idx_next(i))
283    )
284  )
285  io.readResp.entryValid := valid_metas
286
287  io.read.ready := !io.write.valid && !io.fencei && tagArrays.map(_.io.r.req.ready).reduce(_ && _)
288
289  // valid write
290  val way_num = OHToUInt(io.write.bits.waymask)
291  when(io.write.valid) {
292    valid_array(way_num) := valid_array(way_num).bitSet(io.write.bits.virIdx, true.B)
293  }
294
295  XSPerfAccumulate("meta_refill_num", io.write.valid)
296
297  io.readResp.metas <> DontCare
298  io.readResp.codes <> DontCare
299  val readMetaEntries = tagArrays.map(port => port.io.r.resp.asTypeOf(Vec(nWays, new ICacheMetaEntry())))
300  val readMetas       = readMetaEntries.map(_.map(_.meta))
301  val readCodes       = readMetaEntries.map(_.map(_.code))
302
303  // TEST: force ECC to fail by setting readCodes to 0
304  if (ICacheForceMetaECCError) {
305    readCodes.foreach(_.foreach(_ := 0.U))
306  }
307
308  when(port_0_read_0_reg) {
309    io.readResp.metas(0) := readMetas(0)
310    io.readResp.codes(0) := readCodes(0)
311  }.elsewhen(port_0_read_1_reg) {
312    io.readResp.metas(0) := readMetas(1)
313    io.readResp.codes(0) := readCodes(1)
314  }
315
316  when(port_1_read_0_reg) {
317    io.readResp.metas(1) := readMetas(0)
318    io.readResp.codes(1) := readCodes(0)
319  }.elsewhen(port_1_read_1_reg) {
320    io.readResp.metas(1) := readMetas(1)
321    io.readResp.codes(1) := readCodes(1)
322  }
323
324  io.write.ready := true.B // TODO : has bug ? should be !io.cacheOp.req.valid
325
326  // fencei logic : reset valid_array
327  when(io.fencei) {
328    (0 until nWays).foreach(way =>
329      valid_array(way) := 0.U
330    )
331  }
332}
333
334class ICacheDataArray(implicit p: Parameters) extends ICacheArray {
335  class ICacheDataEntry(implicit p: Parameters) extends ICacheBundle {
336    val data = UInt(ICacheDataBits.W)
337    val code = UInt(ICacheDataCodeBits.W)
338  }
339
340  object ICacheDataEntry {
341    def apply(data: UInt)(implicit p: Parameters) = {
342      val entry = Wire(new ICacheDataEntry)
343      entry.data := data
344      entry.code := encodeDataECC(data)
345      entry
346    }
347  }
348
349  val io = IO {
350    new Bundle {
351      val write = Flipped(DecoupledIO(new ICacheDataWriteBundle))
352      // TODO: fix hard code
353      val read     = Flipped(Vec(4, DecoupledIO(new ICacheReadBundle)))
354      val readResp = Output(new ICacheDataRespBundle)
355    }
356  }
357
358  /**
359    ******************************************************************************
360    * data array
361    ******************************************************************************
362    */
363  val writeDatas   = io.write.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt(ICacheDataBits.W)))
364  val writeEntries = writeDatas.map(ICacheDataEntry(_).asUInt)
365
366  val bankSel  = getBankSel(io.read(0).bits.blkOffset, io.read(0).valid)
367  val lineSel  = getLineSel(io.read(0).bits.blkOffset)
368  val waymasks = io.read(0).bits.wayMask
369  val masks    = Wire(Vec(nWays, Vec(ICacheDataBanks, Bool())))
370  (0 until nWays).foreach { way =>
371    (0 until ICacheDataBanks).foreach { bank =>
372      masks(way)(bank) := Mux(
373        lineSel(bank),
374        waymasks(1)(way) && bankSel(1)(bank).asBool,
375        waymasks(0)(way) && bankSel(0)(bank).asBool
376      )
377    }
378  }
379
380  val dataArrays = (0 until nWays).map { way =>
381    (0 until ICacheDataBanks).map { bank =>
382      val sramBank = Module(new SRAMTemplateWithFixedWidth(
383        UInt(ICacheDataEntryBits.W),
384        set = nSets,
385        width = ICacheDataSRAMWidth,
386        shouldReset = true,
387        holdRead = true,
388        singlePort = true
389      ))
390
391      // read
392      sramBank.io.r.req.valid := io.read(bank % 4).valid && masks(way)(bank)
393      sramBank.io.r.req.bits.apply(setIdx =
394        Mux(lineSel(bank), io.read(bank % 4).bits.vSetIdx(1), io.read(bank % 4).bits.vSetIdx(0))
395      )
396      // write
397      sramBank.io.w.req.valid := io.write.valid && io.write.bits.waymask(way).asBool
398      sramBank.io.w.req.bits.apply(
399        data = writeEntries(bank),
400        setIdx = io.write.bits.virIdx,
401        // waymask is invalid when way of SRAMTemplate <= 1
402        waymask = 0.U
403      )
404      sramBank
405    }
406  }
407
408  /**
409    ******************************************************************************
410    * read logic
411    ******************************************************************************
412    */
413  val masksReg = RegEnable(masks, 0.U.asTypeOf(masks), io.read(0).valid)
414  val readDataWithCode = (0 until ICacheDataBanks).map(bank =>
415    Mux1H(VecInit(masksReg.map(_(bank))).asTypeOf(UInt(nWays.W)), dataArrays.map(_(bank).io.r.resp.asUInt))
416  )
417  val readEntries = readDataWithCode.map(_.asTypeOf(new ICacheDataEntry()))
418  val readDatas   = VecInit(readEntries.map(_.data))
419  val readCodes   = VecInit(readEntries.map(_.code))
420
421  // TEST: force ECC to fail by setting readCodes to 0
422  if (ICacheForceDataECCError) {
423    readCodes.foreach(_ := 0.U)
424  }
425
426  /**
427    ******************************************************************************
428    * IO
429    ******************************************************************************
430    */
431  io.readResp.datas := readDatas
432  io.readResp.codes := readCodes
433  io.write.ready    := true.B
434  io.read.foreach(_.ready := !io.write.valid)
435}
436
437class ICacheReplacer(implicit p: Parameters) extends ICacheModule {
438  val io = IO(new Bundle {
439    val touch  = Vec(PortNumber, Flipped(ValidIO(new ReplacerTouch)))
440    val victim = Flipped(new ReplacerVictim)
441  })
442
443  val replacers = Seq.fill(PortNumber)(ReplacementPolicy.fromString(cacheParams.replacer, nWays, nSets / PortNumber))
444
445  // touch
446  val touch_sets = Seq.fill(PortNumber)(Wire(Vec(2, UInt(log2Ceil(nSets / 2).W))))
447  val touch_ways = Seq.fill(PortNumber)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W)))))
448  (0 until PortNumber).foreach { i =>
449    touch_sets(i)(0) := Mux(
450      io.touch(i).bits.vSetIdx(0),
451      io.touch(1).bits.vSetIdx(highestIdxBit, 1),
452      io.touch(0).bits.vSetIdx(highestIdxBit, 1)
453    )
454    touch_ways(i)(0).bits  := Mux(io.touch(i).bits.vSetIdx(0), io.touch(1).bits.way, io.touch(0).bits.way)
455    touch_ways(i)(0).valid := Mux(io.touch(i).bits.vSetIdx(0), io.touch(1).valid, io.touch(0).valid)
456  }
457
458  // victim
459  io.victim.way := Mux(
460    io.victim.vSetIdx.bits(0),
461    replacers(1).way(io.victim.vSetIdx.bits(highestIdxBit, 1)),
462    replacers(0).way(io.victim.vSetIdx.bits(highestIdxBit, 1))
463  )
464
465  // touch the victim in next cycle
466  val victim_vSetIdx_reg =
467    RegEnable(io.victim.vSetIdx.bits, 0.U.asTypeOf(io.victim.vSetIdx.bits), io.victim.vSetIdx.valid)
468  val victim_way_reg = RegEnable(io.victim.way, 0.U.asTypeOf(io.victim.way), io.victim.vSetIdx.valid)
469  (0 until PortNumber).foreach { i =>
470    touch_sets(i)(1)       := victim_vSetIdx_reg(highestIdxBit, 1)
471    touch_ways(i)(1).bits  := victim_way_reg
472    touch_ways(i)(1).valid := RegNext(io.victim.vSetIdx.valid) && (victim_vSetIdx_reg(0) === i.U)
473  }
474
475  ((replacers zip touch_sets) zip touch_ways).map { case ((r, s), w) => r.access(s, w) }
476}
477
478class ICacheIO(implicit p: Parameters) extends ICacheBundle {
479  val hartId       = Input(UInt(hartIdLen.W))
480  val ftqPrefetch  = Flipped(new FtqToPrefetchIO)
481  val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle)))
482  val stop         = Input(Bool())
483  val fetch        = new ICacheMainPipeBundle
484  val toIFU        = Output(Bool())
485  val pmp          = Vec(2 * PortNumber, new ICachePMPBundle)
486  val itlb         = Vec(PortNumber, new TlbRequestIO)
487  val perfInfo     = Output(new ICachePerfInfo)
488  val error        = ValidIO(new L1CacheErrorInfo)
489  /* CSR control signal */
490  val csr_pf_enable     = Input(Bool())
491  val csr_parity_enable = Input(Bool())
492  val fencei            = Input(Bool())
493  val flush             = Input(Bool())
494}
495
496class ICache()(implicit p: Parameters) extends LazyModule with HasICacheParameters {
497  override def shouldBeInlined: Boolean = false
498
499  val clientParameters = TLMasterPortParameters.v1(
500    Seq(TLMasterParameters.v1(
501      name = "icache",
502      sourceId = IdRange(0, cacheParams.nFetchMshr + cacheParams.nPrefetchMshr + 1)
503    )),
504    requestFields = cacheParams.reqFields,
505    echoFields = cacheParams.echoFields
506  )
507
508  val clientNode = TLClientNode(Seq(clientParameters))
509
510  lazy val module = new ICacheImp(this)
511}
512
513class ICacheImp(outer: ICache) extends LazyModuleImp(outer) with HasICacheParameters with HasPerfEvents {
514  val io = IO(new ICacheIO)
515
516  println("ICache:")
517  println("  TagECC: " + cacheParams.tagECC)
518  println("  DataECC: " + cacheParams.dataECC)
519  println("  ICacheSets: " + cacheParams.nSets)
520  println("  ICacheWays: " + cacheParams.nWays)
521  println("  PortNumber: " + cacheParams.PortNumber)
522  println("  nFetchMshr: " + cacheParams.nFetchMshr)
523  println("  nPrefetchMshr: " + cacheParams.nPrefetchMshr)
524  println("  nWayLookupSize: " + cacheParams.nWayLookupSize)
525  println("  DataCodeUnit: " + cacheParams.DataCodeUnit)
526  println("  ICacheDataBanks: " + cacheParams.ICacheDataBanks)
527  println("  ICacheDataSRAMWidth: " + cacheParams.ICacheDataSRAMWidth)
528
529  val (bus, edge) = outer.clientNode.out.head
530
531  val metaArray  = Module(new ICacheMetaArray)
532  val dataArray  = Module(new ICacheDataArray)
533  val mainPipe   = Module(new ICacheMainPipe)
534  val missUnit   = Module(new ICacheMissUnit(edge))
535  val replacer   = Module(new ICacheReplacer)
536  val prefetcher = Module(new IPrefetchPipe)
537  val wayLookup  = Module(new WayLookup)
538
539  dataArray.io.write <> missUnit.io.data_write
540  dataArray.io.read <> mainPipe.io.dataArray.toIData
541  dataArray.io.readResp <> mainPipe.io.dataArray.fromIData
542
543  metaArray.io.fencei := io.fencei
544  metaArray.io.write <> missUnit.io.meta_write
545  metaArray.io.read <> prefetcher.io.metaRead.toIMeta
546  metaArray.io.readResp <> prefetcher.io.metaRead.fromIMeta
547
548  prefetcher.io.flush             := io.flush
549  prefetcher.io.csr_pf_enable     := io.csr_pf_enable
550  prefetcher.io.csr_parity_enable := io.csr_parity_enable
551  prefetcher.io.MSHRResp          := missUnit.io.fetch_resp
552  prefetcher.io.flushFromBpu      := io.ftqPrefetch.flushFromBpu
553  // cache softPrefetch
554  private val softPrefetchValid = RegInit(false.B)
555  private val softPrefetch      = RegInit(0.U.asTypeOf(new IPrefetchReq))
556  /* FIXME:
557   * If there is already a pending softPrefetch request, it will be overwritten.
558   * Also, if there are multiple softPrefetch requests in the same cycle, only the first one will be accepted.
559   * We should implement a softPrefetchQueue (like ibuffer, multi-in, single-out) to solve this.
560   * However, the impact on performance still needs to be assessed.
561   * Considering that the frequency of prefetch.i may not be high, let's start with a temporary dummy solution.
562   */
563  when(io.softPrefetch.map(_.valid).reduce(_ || _)) {
564    softPrefetchValid := true.B
565    softPrefetch.fromSoftPrefetch(MuxCase(
566      0.U.asTypeOf(new SoftIfetchPrefetchBundle),
567      io.softPrefetch.map(req => req.valid -> req.bits)
568    ))
569  }.elsewhen(prefetcher.io.req.fire) {
570    softPrefetchValid := false.B
571  }
572  // pass ftqPrefetch
573  private val ftqPrefetch = WireInit(0.U.asTypeOf(new IPrefetchReq))
574  ftqPrefetch.fromFtqICacheInfo(io.ftqPrefetch.req.bits)
575  // software prefetch has higher priority
576  prefetcher.io.req.valid                 := softPrefetchValid || io.ftqPrefetch.req.valid
577  prefetcher.io.req.bits                  := Mux(softPrefetchValid, softPrefetch, ftqPrefetch)
578  prefetcher.io.req.bits.backendException := io.ftqPrefetch.backendException
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