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 def debugEn(implicit p: Parameters): Boolean = p(DebugOptionsKey).AlwaysBasicDiff || p(DebugOptionsKey).EnableDifftest 42 43 val copyPdestInfo = mutable.HashMap[Int, (Int, Int)]() 44 45 def updateCopyPdestInfo: Unit = allExuParams.filter(_.copyWakeupOut).map(x => getExuIdx(x.name) -> (x.copyDistance, -1)).foreach { x => 46 copyPdestInfo.addOne(x) 47 } 48 def isCopyPdest(exuIdx: Int): Boolean = { 49 copyPdestInfo.contains(exuIdx) 50 } 51 def connectWakeup(exuIdx: Int): Unit = { 52 println(s"[Backend] copyPdestInfo ${copyPdestInfo}") 53 if (copyPdestInfo.contains(exuIdx)) { 54 println(s"[Backend] exuIdx ${exuIdx} be connected, old info ${copyPdestInfo(exuIdx)}") 55 val newInfo = exuIdx -> (copyPdestInfo(exuIdx)._1, copyPdestInfo(exuIdx)._2 + 1) 56 copyPdestInfo.remove(exuIdx) 57 copyPdestInfo += newInfo 58 println(s"[Backend] exuIdx ${exuIdx} be connected, new info ${copyPdestInfo(exuIdx)}") 59 } 60 } 61 def getCopyPdestIndex(exuIdx: Int): Int = { 62 copyPdestInfo(exuIdx)._2 / copyPdestInfo(exuIdx)._1 63 } 64 def intSchdParams = schdParams.get(IntScheduler()) 65 def fpSchdParams = schdParams.get(FpScheduler()) 66 def vfSchdParams = schdParams.get(VfScheduler()) 67 def memSchdParams = schdParams.get(MemScheduler()) 68 def allSchdParams: Seq[SchdBlockParams] = 69 (Seq(intSchdParams) :+ fpSchdParams :+ vfSchdParams :+ memSchdParams) 70 .filter(_.nonEmpty) 71 .map(_.get) 72 def allIssueParams: Seq[IssueBlockParams] = 73 allSchdParams.map(_.issueBlockParams).flatten 74 def allExuParams: Seq[ExeUnitParams] = 75 allIssueParams.map(_.exuBlockParams).flatten 76 77 // filter not fake exu unit 78 def allRealExuParams = 79 allExuParams.filterNot(_.fakeUnit) 80 81 def intPregParams: IntPregParams = pregParams.collectFirst { case x: IntPregParams => x }.get 82 def fpPregParams: FpPregParams = pregParams.collectFirst { case x: FpPregParams => x }.get 83 def vfPregParams: VfPregParams = pregParams.collectFirst { case x: VfPregParams => x }.get 84 def v0PregParams: V0PregParams = pregParams.collectFirst { case x: V0PregParams => x }.get 85 def vlPregParams: VlPregParams = pregParams.collectFirst { case x: VlPregParams => x }.get 86 def getPregParams: Map[DataConfig, PregParams] = { 87 pregParams.map(x => (x.dataCfg, x)).toMap 88 } 89 90 def pregIdxWidth = pregParams.map(_.addrWidth).max 91 92 def numSrc : Int = allSchdParams.map(_.issueBlockParams.map(_.numSrc).max).max 93 def numRegSrc : Int = allSchdParams.map(_.issueBlockParams.map(_.numRegSrc).max).max 94 def numVecRegSrc: Int = allSchdParams.map(_.issueBlockParams.map(_.numVecSrc).max).max 95 96 97 def AluCnt = allSchdParams.map(_.AluCnt).sum 98 def StaCnt = allSchdParams.map(_.StaCnt).sum 99 def StdCnt = allSchdParams.map(_.StdCnt).sum 100 def LduCnt = allSchdParams.map(_.LduCnt).sum 101 def HyuCnt = allSchdParams.map(_.HyuCnt).sum 102 def VlduCnt = allSchdParams.map(_.VlduCnt).sum 103 def VstuCnt = allSchdParams.map(_.VstuCnt).sum 104 def LsExuCnt = StaCnt + LduCnt + HyuCnt 105 val LdExuCnt = LduCnt + HyuCnt 106 val StaExuCnt = StaCnt + HyuCnt 107 def JmpCnt = allSchdParams.map(_.JmpCnt).sum 108 def BrhCnt = allSchdParams.map(_.BrhCnt).sum 109 def CsrCnt = allSchdParams.map(_.CsrCnt).sum 110 def IqCnt = allSchdParams.map(_.issueBlockParams.length).sum 111 112 def numPcReadPort = allSchdParams.map(_.numPcReadPort).sum 113 def numPcMemReadPort = allExuParams.filter(_.needPc).size 114 def numTargetReadPort = allRealExuParams.count(x => x.needTarget) 115 116 def numPregRd(dataCfg: DataConfig) = this.getRfReadSize(dataCfg) 117 def numPregWb(dataCfg: DataConfig) = this.getRfWriteSize(dataCfg) 118 119 def numNoDataWB = allSchdParams.map(_.numNoDataWB).sum 120 def numExu = allSchdParams.map(_.numExu).sum 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 genIntIQValidNumBundle(implicit p: Parameters) = { 131 this.intSchdParams.get.issueBlockParams.map(x => Vec(x.numDeq, UInt((x.numEntries).U.getWidth.W))) 132 } 133 134 def genFpIQValidNumBundle(implicit p: Parameters) = { 135 this.fpSchdParams.get.issueBlockParams.map(x => Vec(x.numDeq, UInt((x.numEntries).U.getWidth.W))) 136 } 137 138 def genIntWriteBackBundle(implicit p: Parameters) = { 139 Seq.fill(this.getIntRfWriteSize)(new RfWritePortWithConfig(IntData(), intPregParams.addrWidth)) 140 } 141 142 def genFpWriteBackBundle(implicit p: Parameters) = { 143 Seq.fill(this.getFpRfWriteSize)(new RfWritePortWithConfig(FpData(), fpPregParams.addrWidth)) 144 } 145 146 def genVfWriteBackBundle(implicit p: Parameters) = { 147 Seq.fill(this.getVfRfWriteSize)(new RfWritePortWithConfig(VecData(), vfPregParams.addrWidth)) 148 } 149 150 def genV0WriteBackBundle(implicit p: Parameters) = { 151 Seq.fill(this.getV0RfWriteSize)(new RfWritePortWithConfig(V0Data(), v0PregParams.addrWidth)) 152 } 153 154 def genVlWriteBackBundle(implicit p: Parameters) = { 155 Seq.fill(this.getVlRfWriteSize)(new RfWritePortWithConfig(VlData(), vlPregParams.addrWidth)) 156 } 157 158 def genWriteBackBundles(implicit p: Parameters): Seq[RfWritePortWithConfig] = { 159 genIntWriteBackBundle ++ genVfWriteBackBundle 160 } 161 162 def genWrite2CtrlBundles(implicit p: Parameters): MixedVec[ValidIO[ExuOutput]] = { 163 MixedVec(allSchdParams.map(_.genExuOutputValidBundle.flatten).flatten) 164 } 165 166 def getIntWbArbiterParams: WbArbiterParams = { 167 val intWbCfgs: Seq[IntWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(_.writeInt)).map(_.asInstanceOf[IntWB]) 168 datapath.WbArbiterParams(intWbCfgs, intPregParams, this) 169 } 170 171 def getVfWbArbiterParams: WbArbiterParams = { 172 val vfWbCfgs: Seq[VfWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(x => x.writeVec)).map(_.asInstanceOf[VfWB]) 173 datapath.WbArbiterParams(vfWbCfgs, vfPregParams, this) 174 } 175 176 def getFpWbArbiterParams: WbArbiterParams = { 177 val fpWbCfgs: Seq[FpWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(x => x.writeFp)).map(_.asInstanceOf[FpWB]) 178 datapath.WbArbiterParams(fpWbCfgs, vfPregParams, this) 179 } 180 181 def getV0WbArbiterParams: WbArbiterParams = { 182 val v0WbCfgs: Seq[V0WB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(x => x.writeV0)).map(_.asInstanceOf[V0WB]) 183 datapath.WbArbiterParams(v0WbCfgs, v0PregParams, this) 184 } 185 186 def getVlWbArbiterParams: WbArbiterParams = { 187 val vlWbCfgs: Seq[VlWB] = allSchdParams.flatMap(_.getWbCfgs.flatten.flatten.filter(x => x.writeVl)).map(_.asInstanceOf[VlWB]) 188 datapath.WbArbiterParams(vlWbCfgs, vlPregParams, this) 189 } 190 191 /** 192 * Get regfile read port params 193 * 194 * @param dataCfg [[IntData]] or [[VecData]] 195 * @return Seq[port->Seq[(exuIdx, priority)] 196 */ 197 def getRdPortParams(dataCfg: DataConfig) = { 198 // port -> Seq[exuIdx, priority] 199 val cfgs: Seq[(Int, Seq[(Int, Int)])] = allRealExuParams 200 .flatMap(x => x.rfrPortConfigs.flatten.map(xx => (xx, x.exuIdx))) 201 .filter { x => x._1.getDataConfig == dataCfg } 202 .map(x => (x._1.port, (x._2, x._1.priority))) 203 .groupBy(_._1) 204 .map(x => (x._1, x._2.map(_._2).sortBy({ case (priority, _) => priority }))) 205 .toSeq 206 .sortBy(_._1) 207 cfgs 208 } 209 210 /** 211 * Get regfile write back port params 212 * 213 * @param dataCfg [[IntData]] or [[VecData]] 214 * @return Seq[port->Seq[(exuIdx, priority)] 215 */ 216 def getWbPortParams(dataCfg: DataConfig) = { 217 val cfgs: Seq[(Int, Seq[(Int, Int)])] = allRealExuParams 218 .flatMap(x => x.wbPortConfigs.map(xx => (xx, x.exuIdx))) 219 .filter { x => x._1.dataCfg == dataCfg } 220 .map(x => (x._1.port, (x._2, x._1.priority))) 221 .groupBy(_._1) 222 .map(x => (x._1, x._2.map(_._2))) 223 .toSeq 224 .sortBy(_._1) 225 cfgs 226 } 227 228 def getRdPortIndices(dataCfg: DataConfig) = { 229 this.getRdPortParams(dataCfg).map(_._1) 230 } 231 232 def getWbPortIndices(dataCfg: DataConfig) = { 233 this.getWbPortParams(dataCfg).map(_._1) 234 } 235 236 def getRdCfgs[T <: RdConfig](implicit tag: ClassTag[T]): Seq[Seq[Seq[RdConfig]]] = { 237 val rdCfgs: Seq[Seq[Seq[RdConfig]]] = allIssueParams.map( 238 _.exuBlockParams.map( 239 _.rfrPortConfigs.map( 240 _.collectFirst{ case x: T => x } 241 .getOrElse(NoRD()) 242 ) 243 ) 244 ) 245 rdCfgs 246 } 247 248 def getAllWbCfgs: Seq[Seq[Set[PregWB]]] = { 249 allIssueParams.map(_.exuBlockParams.map(_.wbPortConfigs.toSet)) 250 } 251 252 def getWbCfgs[T <: PregWB](implicit tag: ClassTag[T]): Seq[Seq[PregWB]] = { 253 val wbCfgs: Seq[Seq[PregWB]] = allIssueParams.map(_.exuBlockParams.map(_.wbPortConfigs.collectFirst{ case x: T => x }.getOrElse(NoWB()))) 254 wbCfgs 255 } 256 257 /** 258 * Get size of read ports of int regfile 259 * 260 * @return if [[IntPregParams.numRead]] is [[None]], get size of ports in [[IntRD]] 261 */ 262 def getIntRfReadSize = { 263 this.intPregParams.numRead.getOrElse(this.getRdPortIndices(IntData()).size) 264 } 265 266 /** 267 * Get size of write ports of int regfile 268 * 269 * @return if [[IntPregParams.numWrite]] is [[None]], get size of ports in [[IntWB]] 270 */ 271 def getIntRfWriteSize = { 272 this.intPregParams.numWrite.getOrElse(this.getWbPortIndices(IntData()).size) 273 } 274 275 /** 276 * Get size of write ports of fp regfile 277 * 278 * @return if [[FpPregParams.numWrite]] is [[None]], get size of ports in [[FpWB]] 279 */ 280 def getFpRfWriteSize = { 281 this.fpPregParams.numWrite.getOrElse(this.getWbPortIndices(FpData()).size) 282 } 283 284 /** 285 * Get size of read ports of vec regfile 286 * 287 * @return if [[VfPregParams.numRead]] is [[None]], get size of ports in [[VfRD]] 288 */ 289 def getVfRfReadSize = { 290 this.vfPregParams.numRead.getOrElse(this.getRdPortIndices(VecData()).size) 291 } 292 293 /** 294 * Get size of write ports of vec regfile 295 * 296 * @return if [[VfPregParams.numWrite]] is [[None]], get size of ports in [[VfWB]] 297 */ 298 def getVfRfWriteSize = { 299 this.vfPregParams.numWrite.getOrElse(this.getWbPortIndices(VecData()).size) 300 } 301 302 def getV0RfWriteSize = { 303 this.v0PregParams.numWrite.getOrElse(this.getWbPortIndices(V0Data()).size) 304 } 305 306 def getVlRfWriteSize = { 307 this.vlPregParams.numWrite.getOrElse(this.getWbPortIndices(VlData()).size) 308 } 309 310 def getRfReadSize(dataCfg: DataConfig) = { 311 dataCfg match{ 312 case IntData() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) 313 case FpData() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) 314 case VecData() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) 315 case V0Data() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) 316 case VlData() => this.getPregParams(dataCfg).numRead.getOrElse(this.getRdPortIndices(dataCfg).size) 317 case _ => throw new IllegalArgumentException(s"DataConfig ${dataCfg} can not get RfReadSize") 318 } 319 } 320 321 def getRfWriteSize(dataCfg: DataConfig) = { 322 this.getPregParams(dataCfg).numWrite.getOrElse(this.getWbPortIndices(dataCfg).size) 323 } 324 325 def getExuIdx(name: String): Int = { 326 val exuParams = allRealExuParams 327 if (name != "WB") { 328 val foundExu = exuParams.find(_.name == name) 329 require(foundExu.nonEmpty, s"exu $name not find") 330 foundExu.get.exuIdx 331 } else 332 -1 333 } 334 335 def getExuName(idx: Int): String = { 336 val exuParams = allRealExuParams 337 exuParams(idx).name 338 } 339 340 def getExuParamByName(name: String): ExeUnitParams = { 341 val exuParams = allExuParams 342 exuParams.find(_.name == name).get 343 } 344 345 def getLdExuIdx(exu: ExeUnitParams): Int = { 346 val ldExuParams = allRealExuParams.filter(x => x.hasHyldaFu || x.hasLoadFu) 347 ldExuParams.indexOf(exu) 348 } 349 350 def getIntWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getIntWBPort.getOrElse(IntWB(port = -1)).port).filter(_._1 != -1) 351 def getFpWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getFpWBPort.getOrElse(FpWB(port = -1)).port).filter(_._1 != -1) 352 def getVfWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getVfWBPort.getOrElse(VfWB(port = -1)).port).filter(_._1 != -1) 353 def getV0WBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getV0WBPort.getOrElse(V0WB(port = -1)).port).filter(_._1 != -1) 354 def getVlWBExeGroup: Map[Int, Seq[ExeUnitParams]] = allRealExuParams.groupBy(x => x.getVlWBPort.getOrElse(VlWB(port = -1)).port).filter(_._1 != -1) 355 356 private def isContinuous(portIndices: Seq[Int]): Boolean = { 357 val portIndicesSet = portIndices.toSet 358 portIndicesSet.min == 0 && portIndicesSet.max == portIndicesSet.size - 1 359 } 360 361 def configChecks = { 362 checkReadPortContinuous 363 checkWritePortContinuous 364 configCheck 365 } 366 367 def checkReadPortContinuous = { 368 pregParams.filterNot(_.isFake).foreach { x => 369 if (x.numRead.isEmpty) { 370 val portIndices: Seq[Int] = getRdPortIndices(x.dataCfg) 371 require(isContinuous(portIndices), 372 s"The read ports of ${x.getClass.getSimpleName} should be continuous, " + 373 s"when numRead of ${x.getClass.getSimpleName} is None. The read port indices are $portIndices") 374 } 375 } 376 } 377 378 def checkWritePortContinuous = { 379 pregParams.filterNot(_.isFake).foreach { x => 380 if (x.numWrite.isEmpty) { 381 val portIndices: Seq[Int] = getWbPortIndices(x.dataCfg) 382 require( 383 isContinuous(portIndices), 384 s"The write ports of ${x.getClass.getSimpleName} should be continuous, " + 385 s"when numWrite of ${x.getClass.getSimpleName} is None. The write port indices are $portIndices" 386 ) 387 } 388 } 389 } 390 391 def configCheck = { 392 // check 0 393 val maxPortSource = 4 394 395 allRealExuParams.map { 396 case exuParam => exuParam.wbPortConfigs.collectFirst { case x: IntWB => x } 397 }.filter(_.isDefined).groupBy(_.get.port).foreach { 398 case (wbPort, priorities) => assert(priorities.size <= maxPortSource, "There has " + priorities.size + " exu's " + "Int WBport is " + wbPort + ", but the maximum is " + maxPortSource + ".") 399 } 400 allRealExuParams.map { 401 case exuParam => exuParam.wbPortConfigs.collectFirst { case x: VfWB => x } 402 }.filter(_.isDefined).groupBy(_.get.port).foreach { 403 case (wbPort, priorities) => assert(priorities.size <= maxPortSource, "There has " + priorities.size + " exu's " + "Vf WBport is " + wbPort + ", but the maximum is " + maxPortSource + ".") 404 } 405 406 // check 1 407 // if some exus share the same wb port and rd ports, 408 // the exu with high priority at wb must also have high priority at rd. 409 val wbTypes = Seq(IntWB(), FpWB(), VfWB()) 410 val rdTypes = Seq(IntRD(), FpRD(), VfRD()) 411 for(wbType <- wbTypes){ 412 for(rdType <- rdTypes){ 413 println(s"[BackendParams] wbType: ${wbType}, rdType: ${rdType}") 414 allRealExuParams.map { 415 case exuParam => 416 val wbPortConfigs = exuParam.wbPortConfigs 417 val wbConfigs = wbType match{ 418 case _: IntWB => wbPortConfigs.collectFirst { case x: IntWB => x } 419 case _: FpWB => wbPortConfigs.collectFirst { case x: FpWB => x } 420 case _: VfWB => wbPortConfigs.collectFirst { case x: VfWB => x } 421 case _ => None 422 } 423 val rfReadPortConfigs = exuParam.rfrPortConfigs 424 val rdConfigs = rdType match{ 425 case _: IntRD => rfReadPortConfigs.flatten.filter(_.isInstanceOf[IntRD]) 426 case _: FpRD => rfReadPortConfigs.flatten.filter(_.isInstanceOf[FpRD]) 427 case _: VfRD => rfReadPortConfigs.flatten.filter(_.isInstanceOf[VfRD]) 428 case _ => Seq() 429 } 430 (wbConfigs, rdConfigs) 431 }.filter(_._1.isDefined) 432 .sortBy(_._1.get.priority) 433 .groupBy(_._1.get.port).map { case (wbPort, intWbRdPairs) => 434 val rdCfgs = intWbRdPairs.map(_._2).flatten 435 println(s"[BackendParams] wb port ${wbPort} rdcfgs: ${rdCfgs}") 436 rdCfgs.groupBy(_.port).foreach { case (p, rdCfg) => 437 //println(s"[BackendParams] rdport: ${p}, cfgs: ${rdCfg}") 438 rdCfg.zip(rdCfg.drop(1)).foreach { case (cfg0, cfg1) => assert(cfg0.priority <= cfg1.priority, s"an exu has high priority at ${wbType} wb port ${wbPort}, but has low priority at ${rdType} rd port ${p}") } 439 } 440 } 441 } 442 } 443 } 444} 445