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