xref: /XiangShan/src/main/scala/xiangshan/backend/BackendParams.scala (revision c1e19666c3197f387cecdaec1f822b40f972b205)
1/***************************************************************************************
2  * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3  * Copyright (c) 2020-2021 Peng Cheng Laboratory
4  *
5  * XiangShan is licensed under Mulan PSL v2.
6  * You can use this software according to the terms and conditions of the Mulan PSL v2.
7  * You may obtain a copy of Mulan PSL v2 at:
8  *          http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  *
14  * See the Mulan PSL v2 for more details.
15  ***************************************************************************************/
16
17package xiangshan.backend
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan.backend.Bundles._
23import xiangshan.backend.datapath.DataConfig._
24import xiangshan.backend.datapath.RdConfig._
25import xiangshan.backend.datapath.WbConfig._
26import xiangshan.backend.datapath.{WakeUpConfig, WbArbiterParams}
27import xiangshan.backend.exu.ExeUnitParams
28import xiangshan.backend.issue._
29import xiangshan.backend.regfile._
30import xiangshan.{DebugOptionsKey, XSCoreParamsKey}
31
32import scala.collection.mutable
33import scala.reflect.{ClassTag, classTag}
34
35case class BackendParams(
36  schdParams : Map[SchedulerType, SchdBlockParams],
37  pregParams : Seq[PregParams],
38  iqWakeUpParams : Seq[WakeUpConfig],
39) {
40
41  configChecks
42
43  def debugEn(implicit p: Parameters): Boolean = p(DebugOptionsKey).AlwaysBasicDiff || p(DebugOptionsKey).EnableDifftest
44
45  val copyPdestInfo = mutable.HashMap[Int, (Int, Int)]()
46
47  def updateCopyPdestInfo: Unit = allExuParams.filter(_.copyWakeupOut).map(x => getExuIdx(x.name) -> (x.copyDistance, -1)).foreach { x =>
48    copyPdestInfo.addOne(x)
49  }
50  def isCopyPdest(exuIdx: Int): Boolean = {
51    copyPdestInfo.contains(exuIdx)
52  }
53  def connectWakeup(exuIdx: Int): Unit = {
54    println(s"[Backend] copyPdestInfo ${copyPdestInfo}")
55    if (copyPdestInfo.contains(exuIdx)) {
56      println(s"[Backend] exuIdx ${exuIdx} be connected, old info ${copyPdestInfo(exuIdx)}")
57      val newInfo = exuIdx -> (copyPdestInfo(exuIdx)._1, copyPdestInfo(exuIdx)._2 + 1)
58      copyPdestInfo.remove(exuIdx)
59      copyPdestInfo += newInfo
60      println(s"[Backend] exuIdx ${exuIdx} be connected, new info ${copyPdestInfo(exuIdx)}")
61    }
62  }
63  def getCopyPdestIndex(exuIdx: Int): Int = {
64    copyPdestInfo(exuIdx)._2 / copyPdestInfo(exuIdx)._1
65  }
66  def intSchdParams = schdParams.get(IntScheduler())
67  def vfSchdParams = schdParams.get(VfScheduler())
68  def memSchdParams = schdParams.get(MemScheduler())
69  def allSchdParams: Seq[SchdBlockParams] =
70    (Seq(intSchdParams) :+ vfSchdParams :+ memSchdParams)
71    .filter(_.nonEmpty)
72    .map(_.get)
73  def allIssueParams: Seq[IssueBlockParams] =
74    allSchdParams.map(_.issueBlockParams).flatten
75  def allExuParams: Seq[ExeUnitParams] =
76    allIssueParams.map(_.exuBlockParams).flatten
77
78  // filter not fake exu unit
79  def allRealExuParams =
80    allExuParams.filterNot(_.fakeUnit)
81
82  def intPregParams: IntPregParams = pregParams.collectFirst { case x: IntPregParams => x }.get
83  def vfPregParams: VfPregParams = pregParams.collectFirst { case x: VfPregParams => x }.get
84  def getPregParams: Map[DataConfig, PregParams] = {
85    pregParams.map(x => (x.dataCfg, x)).toMap
86  }
87
88  def pregIdxWidth = pregParams.map(_.addrWidth).max
89
90  def numSrc      : Int = allSchdParams.map(_.issueBlockParams.map(_.numSrc).max).max
91  def numRegSrc   : Int = allSchdParams.map(_.issueBlockParams.map(_.numRegSrc).max).max
92  def numVecRegSrc: Int = allSchdParams.map(_.issueBlockParams.map(_.numVecSrc).max).max
93
94
95  def AluCnt = allSchdParams.map(_.AluCnt).sum
96  def StaCnt = allSchdParams.map(_.StaCnt).sum
97  def StdCnt = allSchdParams.map(_.StdCnt).sum
98  def LduCnt = allSchdParams.map(_.LduCnt).sum
99  def HyuCnt = allSchdParams.map(_.HyuCnt).sum
100  def VlduCnt = allSchdParams.map(_.VlduCnt).sum
101  def VstuCnt = allSchdParams.map(_.VstuCnt).sum
102  def LsExuCnt = StaCnt + LduCnt + HyuCnt
103  val LdExuCnt = LduCnt + HyuCnt
104  val StaExuCnt = StaCnt + HyuCnt
105  def JmpCnt = allSchdParams.map(_.JmpCnt).sum
106  def BrhCnt = allSchdParams.map(_.BrhCnt).sum
107  def CsrCnt = allSchdParams.map(_.CsrCnt).sum
108  def IqCnt = allSchdParams.map(_.issueBlockParams.length).sum
109
110  def numPcReadPort = allSchdParams.map(_.numPcReadPort).sum
111  def numPcMemReadPort = allExuParams.filter(_.needPc).size
112  def numTargetReadPort = allRealExuParams.count(x => x.needTarget)
113
114  def numPregRd(dataCfg: DataConfig) = this.getRfReadSize(dataCfg)
115  def numPregWb(dataCfg: DataConfig) = this.getRfWriteSize(dataCfg)
116
117  def numNoDataWB = allSchdParams.map(_.numNoDataWB).sum
118  def numExu = allSchdParams.map(_.numExu).sum
119  def vconfigPort = 13 // Todo: remove it
120  def vldPort = 14
121
122  def numException = allRealExuParams.count(_.exceptionOut.nonEmpty)
123
124  def numRedirect = allSchdParams.map(_.numRedirect).sum
125
126  def numLoadDp = memSchdParams.get.issueBlockParams.filter(x => x.isLdAddrIQ || x.isHyAddrIQ).map(_.numEnq).sum
127
128  def numStoreDp = memSchdParams.get.issueBlockParams.filter(x => x.isStAddrIQ || x.isHyAddrIQ).map(_.numEnq).sum
129
130  def genIQValidNumBundle(implicit p: Parameters) = {
131    this.intSchdParams.get.issueBlockParams.map(x => Vec(x.numDeq, UInt((x.numEntries+x.numEnq).U.getWidth.W)))
132  }
133
134  def genIntWriteBackBundle(implicit p: Parameters) = {
135    Seq.fill(this.getIntRfWriteSize)(new RfWritePortWithConfig(IntData(), intPregParams.addrWidth))
136  }
137
138  def genVfWriteBackBundle(implicit p: Parameters) = {
139    Seq.fill(this.getVfRfWriteSize)(new RfWritePortWithConfig(VecData(), vfPregParams.addrWidth))
140  }
141
142  def genWriteBackBundles(implicit p: Parameters): Seq[RfWritePortWithConfig] = {
143    genIntWriteBackBundle ++ genVfWriteBackBundle
144  }
145
146  def genWrite2CtrlBundles(implicit p: Parameters): MixedVec[ValidIO[ExuOutput]] = {
147    MixedVec(allSchdParams.map(_.genExuOutputValidBundle.flatten).flatten)
148  }
149
150  def getIntWbArbiterParams: WbArbiterParams = {
151    val intWbCfgs: Seq[IntWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(_.writeInt)).map(_.asInstanceOf[IntWB])
152    datapath.WbArbiterParams(intWbCfgs, intPregParams, this)
153  }
154
155  def getVfWbArbiterParams: WbArbiterParams = {
156    val vfWbCfgs: Seq[VfWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(x => x.writeVec || x.writeFp)).map(_.asInstanceOf[VfWB])
157    datapath.WbArbiterParams(vfWbCfgs, vfPregParams, this)
158  }
159
160  /**
161    * Get regfile read port params
162    *
163    * @param dataCfg [[IntData]] or [[VecData]]
164    * @return Seq[port->Seq[(exuIdx, priority)]
165    */
166  def getRdPortParams(dataCfg: DataConfig) = {
167    // port -> Seq[exuIdx, priority]
168    val cfgs: Seq[(Int, Seq[(Int, Int)])] = allRealExuParams
169      .flatMap(x => x.rfrPortConfigs.flatten.map(xx => (xx, x.exuIdx)))
170      .filter { x => x._1.getDataConfig == dataCfg }
171      .map(x => (x._1.port, (x._2, x._1.priority)))
172      .groupBy(_._1)
173      .map(x => (x._1, x._2.map(_._2).sortBy({ case (priority, _) => priority })))
174      .toSeq
175      .sortBy(_._1)
176    cfgs
177  }
178
179  /**
180    * Get regfile write back port params
181    *
182    * @param dataCfg [[IntData]] or [[VecData]]
183    * @return Seq[port->Seq[(exuIdx, priority)]
184    */
185  def getWbPortParams(dataCfg: DataConfig) = {
186    val cfgs: Seq[(Int, Seq[(Int, Int)])] = allRealExuParams
187      .flatMap(x => x.wbPortConfigs.map(xx => (xx, x.exuIdx)))
188      .filter { x => x._1.dataCfg == dataCfg }
189      .map(x => (x._1.port, (x._2, x._1.priority)))
190      .groupBy(_._1)
191      .map(x => (x._1, x._2.map(_._2)))
192      .toSeq
193      .sortBy(_._1)
194    cfgs
195  }
196
197  def getRdPortIndices(dataCfg: DataConfig) = {
198    this.getRdPortParams(dataCfg).map(_._1)
199  }
200
201  def getWbPortIndices(dataCfg: DataConfig) = {
202    this.getWbPortParams(dataCfg).map(_._1)
203  }
204
205  def getRdCfgs[T <: RdConfig](implicit tag: ClassTag[T]): Seq[Seq[Seq[RdConfig]]] = {
206    val rdCfgs: Seq[Seq[Seq[RdConfig]]] = allIssueParams.map(
207      _.exuBlockParams.map(
208        _.rfrPortConfigs.map(
209          _.collectFirst{ case x: T => x }
210            .getOrElse(NoRD())
211        )
212      )
213    )
214    rdCfgs
215  }
216
217  def getAllWbCfgs: Seq[Seq[Set[PregWB]]] = {
218    allIssueParams.map(_.exuBlockParams.map(_.wbPortConfigs.toSet))
219  }
220
221  def getWbCfgs[T <: PregWB](implicit tag: ClassTag[T]): Seq[Seq[PregWB]] = {
222    val wbCfgs: Seq[Seq[PregWB]] = allIssueParams.map(_.exuBlockParams.map(_.wbPortConfigs.collectFirst{ case x: T => x }.getOrElse(NoWB())))
223    wbCfgs
224  }
225
226  /**
227    * Get size of read ports of int regfile
228    *
229    * @return if [[IntPregParams.numRead]] is [[None]], get size of ports in [[IntRD]]
230    */
231  def getIntRfReadSize = {
232    this.intPregParams.numRead.getOrElse(this.getRdPortIndices(IntData()).size)
233  }
234
235  /**
236    * Get size of write ports of vf regfile
237    *
238    * @return if [[IntPregParams.numWrite]] is [[None]], get size of ports in [[IntWB]]
239    */
240  def getIntRfWriteSize = {
241    this.intPregParams.numWrite.getOrElse(this.getWbPortIndices(IntData()).size)
242  }
243
244  /**
245    * Get size of read ports of int regfile
246    *
247    * @return if [[VfPregParams.numRead]] is [[None]], get size of ports in [[VfRD]]
248    */
249  def getVfRfReadSize = {
250    this.vfPregParams.numRead.getOrElse(this.getRdPortIndices(VecData()).size)
251  }
252
253  /**
254    * Get size of write ports of vf regfile
255    *
256    * @return if [[VfPregParams.numWrite]] is [[None]], get size of ports in [[VfWB]]
257    */
258  def getVfRfWriteSize = {
259    this.vfPregParams.numWrite.getOrElse(this.getWbPortIndices(VecData()).size)
260  }
261
262  def getRfReadSize(dataCfg: DataConfig) = {
263    dataCfg match{
264      case IntData() =>  this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size)
265      case VecData() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) + 2
266    }
267  }
268
269  def getRfWriteSize(dataCfg: DataConfig) = {
270    this.getPregParams(dataCfg).numWrite.getOrElse(this.getWbPortIndices(dataCfg).size)
271  }
272
273  def getExuIdx(name: String): Int = {
274    val exuParams = allRealExuParams
275    if (name != "WB") {
276      val foundExu = exuParams.find(_.name == name)
277      require(foundExu.nonEmpty, s"exu $name not find")
278      foundExu.get.exuIdx
279    } else
280      -1
281  }
282
283  def getExuName(idx: Int): String = {
284    val exuParams = allRealExuParams
285    exuParams(idx).name
286  }
287
288  def getExuParamByName(name: String): ExeUnitParams = {
289    val exuParams = allExuParams
290    exuParams.find(_.name == name).get
291  }
292
293  def getLdExuIdx(exu: ExeUnitParams): Int = {
294    val ldExuParams = allRealExuParams.filter(x => x.hasHyldaFu || x.hasLoadFu)
295    ldExuParams.indexOf(exu)
296  }
297
298  def getIntWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getIntWBPort.getOrElse(IntWB(port = -1)).port).filter(_._1 != -1)
299  def getVfWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getVfWBPort.getOrElse(VfWB(port = -1)).port).filter(_._1 != -1)
300
301  private def isContinuous(portIndices: Seq[Int]): Boolean = {
302    val portIndicesSet = portIndices.toSet
303    portIndicesSet.min == 0 && portIndicesSet.max == portIndicesSet.size - 1
304  }
305
306  def configChecks = {
307    checkReadPortContinuous
308    checkWritePortContinuous
309    configCheck
310  }
311
312  def checkReadPortContinuous = {
313    pregParams.foreach { x =>
314      if (x.numRead.isEmpty) {
315        val portIndices: Seq[Int] = getRdPortIndices(x.dataCfg)
316        require(isContinuous(portIndices),
317          s"The read ports of ${x.getClass.getSimpleName} should be continuous, " +
318            s"when numRead of ${x.getClass.getSimpleName} is None. The read port indices are $portIndices")
319      }
320    }
321  }
322
323  def checkWritePortContinuous = {
324    pregParams.foreach { x =>
325      if (x.numWrite.isEmpty) {
326        val portIndices: Seq[Int] = getWbPortIndices(x.dataCfg)
327        require(
328          isContinuous(portIndices),
329          s"The write ports of ${x.getClass.getSimpleName} should be continuous, " +
330            s"when numWrite of ${x.getClass.getSimpleName} is None. The write port indices are $portIndices"
331        )
332      }
333    }
334  }
335
336  def configCheck = {
337    // check 0
338    val maxPortSource = 4
339
340    allRealExuParams.map {
341      case exuParam => exuParam.wbPortConfigs.collectFirst { case x: IntWB => x }
342    }.filter(_.isDefined).groupBy(_.get.port).foreach {
343      case (wbPort, priorities) => assert(priorities.size <= maxPortSource, "There has " + priorities.size + " exu's " + "Int WBport is " + wbPort + ", but the maximum is " + maxPortSource + ".")
344    }
345    allRealExuParams.map {
346      case exuParam => exuParam.wbPortConfigs.collectFirst { case x: VfWB => x }
347    }.filter(_.isDefined).groupBy(_.get.port).foreach {
348      case (wbPort, priorities) => assert(priorities.size <= maxPortSource, "There has " + priorities.size + " exu's " + "Vf  WBport is " + wbPort + ", but the maximum is " + maxPortSource + ".")
349    }
350
351    // check 1
352    val wbTypes = Seq(IntWB(), VfWB())
353    val rdTypes = Seq(IntRD(), VfRD())
354    for(wbType <- wbTypes){
355      for(rdType <- rdTypes){
356        allRealExuParams.map {
357          case exuParam =>
358            val wbPortConfigs = exuParam.wbPortConfigs
359            val wbConfigs = wbType match{
360              case _: IntWB => wbPortConfigs.collectFirst { case x: IntWB => x }
361              case _: VfWB  => wbPortConfigs.collectFirst { case x: VfWB => x }
362              case _        => None
363            }
364            val rfReadPortConfigs = exuParam.rfrPortConfigs
365            val rdConfigs = rdType match{
366              case _: IntRD => rfReadPortConfigs.flatten.filter(_.isInstanceOf[IntRD])
367              case _: VfRD  => rfReadPortConfigs.flatten.filter(_.isInstanceOf[VfRD])
368              case _        => Seq()
369            }
370            (wbConfigs, rdConfigs)
371        }.filter(_._1.isDefined)
372          .sortBy(_._1.get.priority)
373          .groupBy(_._1.get.port).map {
374            case (_, intWbRdPairs) =>
375              intWbRdPairs.map(_._2).flatten
376        }.map(rdCfgs => rdCfgs.groupBy(_.port).foreach {
377          case (_, rdCfgs) =>
378            rdCfgs.zip(rdCfgs.drop(1)).foreach { case (cfg0, cfg1) => assert(cfg0.priority <= cfg1.priority) }
379        })
380      }
381    }
382  }
383}
384