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