1package xiangshan.backend 2 3import chipsalliance.rocketchip.config.Parameters 4import chisel3._ 5import chisel3.util.BitPat.bitPatToUInt 6import chisel3.util._ 7import xiangshan._ 8import xiangshan.backend.datapath.DataConfig._ 9import xiangshan.backend.datapath.WbConfig.WbConfig 10import xiangshan.backend.decode.{ImmUnion, XDecode} 11import xiangshan.backend.exu.ExeUnitParams 12import xiangshan.backend.fu.FuType 13import xiangshan.backend.fu.fpu.Bundles.Frm 14import xiangshan.backend.fu.vector.Bundles.{Category, Nf, VConfig, VLmul, VSew, VType, Vl, Vxrm} 15import xiangshan.backend.issue.{IssueBlockParams, IssueQueueJumpBundle, SchedulerType, StatusArrayDeqRespBundle, IssueQueueDeqRespBundle} 16import xiangshan.backend.regfile.{RfReadPortWithConfig, RfWritePortWithConfig} 17import xiangshan.backend.rob.RobPtr 18import xiangshan.frontend._ 19import xiangshan.mem.{LqPtr, SqPtr} 20 21object Bundles { 22 23 // frontend -> backend 24 class StaticInst(implicit p: Parameters) extends XSBundle { 25 val instr = UInt(32.W) 26 val pc = UInt(VAddrBits.W) 27 val foldpc = UInt(MemPredPCWidth.W) 28 val exceptionVec = ExceptionVec() 29 val trigger = new TriggerCf 30 val preDecodeInfo = new PreDecodeInfo 31 val pred_taken = Bool() 32 val crossPageIPFFix = Bool() 33 val ftqPtr = new FtqPtr 34 val ftqOffset = UInt(log2Up(PredictWidth).W) 35 36 def connectCtrlFlow(source: CtrlFlow): Unit = { 37 this.instr := source.instr 38 this.pc := source.pc 39 this.foldpc := source.foldpc 40 this.exceptionVec := source.exceptionVec 41 this.trigger := source.trigger 42 this.preDecodeInfo := source.pd 43 this.pred_taken := source.pred_taken 44 this.crossPageIPFFix := source.crossPageIPFFix 45 this.ftqPtr := source.ftqPtr 46 this.ftqOffset := source.ftqOffset 47 } 48 } 49 50 // StaticInst --[Decode]--> DecodedInst 51 class DecodedInst(implicit p: Parameters) extends XSBundle { 52 def numSrc = backendParams.numSrc 53 // passed from StaticInst 54 val instr = UInt(32.W) 55 val pc = UInt(VAddrBits.W) 56 val foldpc = UInt(MemPredPCWidth.W) 57 val exceptionVec = ExceptionVec() 58 val trigger = new TriggerCf 59 val preDecodeInfo = new PreDecodeInfo 60 val pred_taken = Bool() 61 val crossPageIPFFix = Bool() 62 val ftqPtr = new FtqPtr 63 val ftqOffset = UInt(log2Up(PredictWidth).W) 64 // decoded 65 val srcType = Vec(numSrc, SrcType()) 66 val lsrc = Vec(numSrc, UInt(6.W)) 67 val ldest = UInt(6.W) 68 val fuType = FuType() 69 val fuOpType = FuOpType() 70 val rfWen = Bool() 71 val fpWen = Bool() 72 val vecWen = Bool() 73 val isXSTrap = Bool() 74 val waitForward = Bool() // no speculate execution 75 val blockBackward = Bool() 76 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 77 val selImm = SelImm() 78 val imm = UInt(ImmUnion.maxLen.W) 79 val fpu = new FPUCtrlSignals 80 val vpu = new VPUCtrlSignals 81 val isMove = Bool() 82 val uopIdx = UInt(5.W) 83 val uopSplitType = UopSplitType() 84 val isVset = Bool() 85 val firstUop = Bool() 86 val lastUop = Bool() 87 val numUops = UInt(log2Up(MaxUopSize).W) // rob need this 88 val commitType = CommitType() // Todo: remove it 89 90 private def allSignals = srcType.take(3) ++ Seq(fuType, fuOpType, rfWen, fpWen, vecWen, 91 isXSTrap, waitForward, blockBackward, flushPipe, uopSplitType, selImm) 92 93 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]): DecodedInst = { 94 val decoder: Seq[UInt] = ListLookup( 95 inst, XDecode.decodeDefault.map(bitPatToUInt), 96 table.map{ case (pat, pats) => (pat, pats.map(bitPatToUInt)) }.toArray 97 ) 98 allSignals zip decoder foreach { case (s, d) => s := d } 99 this 100 } 101 102 def isSoftPrefetch: Bool = { 103 fuType === FuType.alu.U && fuOpType === ALUOpType.or && selImm === SelImm.IMM_I && ldest === 0.U 104 } 105 106 def connectStaticInst(source: StaticInst): Unit = { 107 for ((name, data) <- this.elements) { 108 if (source.elements.contains(name)) { 109 data := source.elements(name) 110 } 111 } 112 } 113 } 114 115 // DecodedInst --[Rename]--> DynInst 116 class DynInst(implicit p: Parameters) extends XSBundle { 117 def numSrc = backendParams.numSrc 118 // passed from StaticInst 119 val instr = UInt(32.W) 120 val pc = UInt(VAddrBits.W) 121 val foldpc = UInt(MemPredPCWidth.W) 122 val exceptionVec = ExceptionVec() 123 val trigger = new TriggerCf 124 val preDecodeInfo = new PreDecodeInfo 125 val pred_taken = Bool() 126 val crossPageIPFFix = Bool() 127 val ftqPtr = new FtqPtr 128 val ftqOffset = UInt(log2Up(PredictWidth).W) 129 // passed from DecodedInst 130 val srcType = Vec(numSrc, SrcType()) 131 val lsrc = Vec(numSrc, UInt(6.W)) 132 val ldest = UInt(6.W) 133 val fuType = FuType() 134 val fuOpType = FuOpType() 135 val rfWen = Bool() 136 val fpWen = Bool() 137 val vecWen = Bool() 138 val isXSTrap = Bool() 139 val waitForward = Bool() // no speculate execution 140 val blockBackward = Bool() 141 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 142 val selImm = SelImm() 143 val imm = UInt(XLEN.W) // Todo: check if it need minimized 144 val fpu = new FPUCtrlSignals 145 val vpu = new VPUCtrlSignals 146 val isMove = Bool() 147 val uopIdx = UInt(5.W) 148 val isVset = Bool() 149 val firstUop = Bool() 150 val lastUop = Bool() 151 val numUops = UInt(log2Up(MaxUopSize).W) // rob need this 152 val commitType = CommitType() 153 // rename 154 val srcState = Vec(numSrc, SrcState()) 155 val psrc = Vec(numSrc, UInt(PhyRegIdxWidth.W)) 156 val pdest = UInt(PhyRegIdxWidth.W) 157 val oldPdest = UInt(PhyRegIdxWidth.W) 158 val robIdx = new RobPtr 159 160 val eliminatedMove = Bool() 161 val debugInfo = new PerfDebugInfo 162 val storeSetHit = Bool() // inst has been allocated an store set 163 val waitForRobIdx = new RobPtr // store set predicted previous store robIdx 164 // Load wait is needed 165 // load inst will not be executed until former store (predicted by mdp) addr calcuated 166 val loadWaitBit = Bool() 167 // If (loadWaitBit && loadWaitStrict), strict load wait is needed 168 // load inst will not be executed until ALL former store addr calcuated 169 val loadWaitStrict = Bool() 170 val ssid = UInt(SSIDWidth.W) 171 // Todo 172 val lqIdx = new LqPtr 173 val sqIdx = new SqPtr 174 // debug module 175 val singleStep = Bool() 176 // schedule 177 val replayInst = Bool() 178 179 def isLUI: Bool = this.fuType === FuType.alu.U && this.selImm === SelImm.IMM_U 180 def isWFI: Bool = this.fuType === FuType.csr.U && fuOpType === CSROpType.wfi 181 182 def isSvinvalBegin(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && !flush 183 def isSvinval(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.sfence && !flush 184 def isSvinvalEnd(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && flush 185 186 def srcIsReady: Vec[Bool] = { 187 VecInit(this.srcType.zip(this.srcState).map { 188 case (t, s) => SrcType.isNotReg(t) || SrcState.isReady(s) 189 }) 190 } 191 192 def clearExceptions( 193 exceptionBits: Seq[Int] = Seq(), 194 flushPipe : Boolean = false, 195 replayInst : Boolean = false 196 ): DynInst = { 197 this.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B) 198 if (!flushPipe) { this.flushPipe := false.B } 199 if (!replayInst) { this.replayInst := false.B } 200 this 201 } 202 203 def asWakeUpBundle: IssueQueueWakeUpBundle = { 204 val wakeup = Output(new IssueQueueWakeUpBundle(pdest.getWidth)) 205 wakeup.rfWen := this.rfWen 206 wakeup.fpWen := this.fpWen 207 wakeup.vecWen := this.vecWen 208 wakeup.pdest := this.pdest 209 wakeup 210 } 211 212 def needWriteRf: Bool = (rfWen && ldest =/= 0.U) || fpWen || vecWen 213 } 214 215 trait BundleSource { 216 var source = "not exist" 217 } 218 219 class IssueQueueWakeUpBundle(PregIdxWidth: Int) extends Bundle with BundleSource { 220 val rfWen = Bool() 221 val fpWen = Bool() 222 val vecWen = Bool() 223 val pdest = UInt(PregIdxWidth.W) 224 225 /** 226 * @param successor Seq[(psrc, srcType)] 227 * @return Seq[if wakeup psrc] 228 */ 229 def wakeUp(successor: Seq[(UInt, UInt)], valid: Bool): Seq[Bool]= { 230 successor.map { case (thatPsrc, srcType) => 231 val pdestMatch = pdest === thatPsrc 232 pdestMatch && ( 233 SrcType.isFp(srcType) && this.fpWen || 234 SrcType.isXp(srcType) && this.rfWen || 235 SrcType.isVp(srcType) && this.vecWen 236 ) && valid 237 } 238 } 239 } 240 241 class VPUCtrlSignals(implicit p: Parameters) extends XSBundle { 242 // vtype 243 val vill = Bool() 244 val vma = Bool() // 1: agnostic, 0: undisturbed 245 val vta = Bool() // 1: agnostic, 0: undisturbed 246 val vsew = VSew() 247 val vlmul = VLmul() // 1/8~8 --> -3~3 248 249 val vm = Bool() // 0: need v0.t 250 val vstart = Vl() 251 252 // float rounding mode 253 val frm = Frm() 254 // vector fix int rounding mode 255 val vxrm = Vxrm() 256 // vector uop index, exclude other non-vector uop 257 val vuopIdx = UopIdx() 258 // maybe used if data dependancy 259 val vmask = UInt(MaskSrcData().dataWidth.W) 260 val vl = Vl() 261 262 // vector load/store 263 val nf = Nf() 264 265 val needScalaSrc = Bool() 266 267 val isReverse = Bool() // vrsub, vrdiv 268 val isExt = Bool() 269 val isNarrow = Bool() 270 val isDstMask = Bool() // vvm, vvvm, mmm 271 val isMove = Bool() // vmv.s.x, vmv.v.v, vmv.v.x, vmv.v.i 272 273 def vtype: VType = { 274 val res = Wire(VType()) 275 res.illegal := this.vill 276 res.vma := this.vma 277 res.vta := this.vta 278 res.vsew := this.vsew 279 res.vlmul := this.vlmul 280 res 281 } 282 283 def vconfig: VConfig = { 284 val res = Wire(VConfig()) 285 res.vtype := this.vtype 286 res.vl := this.vl 287 res 288 } 289 } 290 291 // DynInst --[IssueQueue]--> DataPath 292 class IssueQueueIssueBundle( 293 iqParams: IssueBlockParams, 294 exuParams: ExeUnitParams, 295 addrWidth: Int, 296 vaddrBits: Int 297 )(implicit 298 p: Parameters 299 ) extends Bundle { 300 private val rfReadDataCfgSet: Seq[Set[DataConfig]] = exuParams.getRfReadDataCfgSet 301 302 val rf: MixedVec[MixedVec[RfReadPortWithConfig]] = Flipped(MixedVec( 303 rfReadDataCfgSet.map((set: Set[DataConfig]) => 304 MixedVec(set.map((x: DataConfig) => new RfReadPortWithConfig(x, addrWidth)).toSeq) 305 ) 306 )) 307 val srcType = Vec(exuParams.numRegSrc, SrcType()) // used to select imm or reg data 308 val immType = SelImm() // used to select imm extractor 309 val common = new ExuInput(exuParams) 310 val jmp = if (exuParams.needPc) Some(Flipped(new IssueQueueJumpBundle)) else None 311 val addrOH = UInt(iqParams.numEntries.W) 312 313 def getSource: SchedulerType = exuParams.getWBSource 314 def getIntRfReadBundle: Seq[RfReadPortWithConfig] = rf.flatten.filter(_.readInt) 315 def getVfRfReadBundle: Seq[RfReadPortWithConfig] = rf.flatten.filter(_.readVf) 316 } 317 318 class OGRespBundle(implicit p:Parameters, params: IssueBlockParams) extends XSBundle { 319 val og0resp = Valid(new StatusArrayDeqRespBundle) 320 val og1resp = Valid(new StatusArrayDeqRespBundle) 321 } 322 323 class fuBusyRespBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle { 324 val respType = RSFeedbackType() // update credit if needs replay 325 val rfWen = Bool() // TODO: use params to identify IntWB/VfWB 326 val fuType = FuType() 327 } 328 329 class FuBusyTableWriteBundle(implicit p: Parameters, params: IssueBlockParams) extends XSBundle { 330 val deqResp = Valid(new fuBusyRespBundle) 331 val og0Resp = Valid(new fuBusyRespBundle) 332 val og1Resp = Valid(new fuBusyRespBundle) 333 } 334 335 // DataPath --[ExuInput]--> Exu 336 class ExuInput(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle { 337 val fuType = FuType() 338 val fuOpType = FuOpType() 339 val src = Vec(params.numRegSrc, UInt(params.dataBitsMax.W)) 340 val imm = UInt(XLEN.W) 341 val robIdx = new RobPtr 342 val iqIdx = UInt(log2Up(MemIQSizeMax).W)// Only used by store yet 343 val isFirstIssue = Bool() // Only used by store yet 344 val pdest = UInt(params.wbPregIdxWidth.W) 345 val rfWen = if (params.writeIntRf) Some(Bool()) else None 346 val fpWen = if (params.writeFpRf) Some(Bool()) else None 347 val vecWen = if (params.writeVecRf) Some(Bool()) else None 348 val fpu = if (params.needFPUCtrl) Some(new FPUCtrlSignals) else None 349 val vpu = if (params.needVPUCtrl) Some(new VPUCtrlSignals) else None 350 val flushPipe = if (params.flushPipe) Some(Bool()) else None 351 val pc = if (params.needPc) Some(UInt(VAddrData().dataWidth.W)) else None 352 val jalrTarget = if (params.hasJmpFu) Some(UInt(VAddrData().dataWidth.W)) else None 353 val preDecode = if (params.hasPredecode) Some(new PreDecodeInfo) else None 354 val ftqIdx = if (params.needPc || params.replayInst) 355 Some(new FtqPtr) else None 356 val ftqOffset = if (params.needPc || params.replayInst) 357 Some(UInt(log2Up(PredictWidth).W)) else None 358 val predictInfo = if (params.hasPredecode) Some(new Bundle { 359 val target = UInt(VAddrData().dataWidth.W) 360 val taken = Bool() 361 }) else None 362 val sqIdx = if (params.hasMemAddrFu || params.hasStdFu) Some(new SqPtr) else None 363 val lqIdx = if (params.hasMemAddrFu) Some(new LqPtr) else None 364 365 def fromIssueBundle(source: IssueQueueIssueBundle): Unit = { 366 // src is assigned to rfReadData 367 this.fuType := source.common.fuType 368 this.fuOpType := source.common.fuOpType 369 this.imm := source.common.imm 370 this.robIdx := source.common.robIdx 371 this.pdest := source.common.pdest 372 this.isFirstIssue := source.common.isFirstIssue // Only used by mem debug log 373 this.iqIdx := source.common.iqIdx // Only used by mem feedback 374 this.rfWen .foreach(_ := source.common.rfWen.get) 375 this.fpWen .foreach(_ := source.common.fpWen.get) 376 this.vecWen .foreach(_ := source.common.vecWen.get) 377 this.fpu .foreach(_ := source.common.fpu.get) 378 this.vpu .foreach(_ := source.common.vpu.get) 379 this.flushPipe .foreach(_ := source.common.flushPipe.get) 380 this.pc .foreach(_ := source.jmp.get.pc) 381 this.jalrTarget .foreach(_ := source.jmp.get.target) 382 this.preDecode .foreach(_ := source.common.preDecode.get) 383 this.ftqIdx .foreach(_ := source.common.ftqIdx.get) 384 this.ftqOffset .foreach(_ := source.common.ftqOffset.get) 385 this.predictInfo .foreach(_ := source.common.predictInfo.get) 386 this.lqIdx .foreach(_ := source.common.lqIdx.get) 387 this.sqIdx .foreach(_ := source.common.sqIdx.get) 388 } 389 } 390 391 // ExuInput --[FuncUnit]--> ExuOutput 392 class ExuOutput( 393 val params: ExeUnitParams, 394 )(implicit 395 val p: Parameters 396 ) extends Bundle with BundleSource with HasXSParameter { 397 val data = UInt(params.dataBitsMax.W) 398 val pdest = UInt(params.wbPregIdxWidth.W) 399 val robIdx = new RobPtr 400 val intWen = if (params.writeIntRf) Some(Bool()) else None 401 val fpWen = if (params.writeFpRf) Some(Bool()) else None 402 val vecWen = if (params.writeVecRf) Some(Bool()) else None 403 val redirect = if (params.hasRedirect) Some(ValidIO(new Redirect)) else None 404 val fflags = if (params.writeFflags) Some(UInt(5.W)) else None 405 val vxsat = if (params.writeVxsat) Some(Bool()) else None 406 val exceptionVec = if (params.exceptionOut.nonEmpty) Some(ExceptionVec()) else None 407 val flushPipe = if (params.flushPipe) Some(Bool()) else None 408 val replay = if (params.replayInst) Some(Bool()) else None 409 val lqIdx = if (params.hasLoadFu) Some(new LqPtr()) else None 410 val sqIdx = if (params.hasStoreAddrFu || params.hasStdFu) 411 Some(new SqPtr()) else None 412 val ftqIdx = if (params.needPc || params.replayInst) 413 Some(new FtqPtr) else None 414 val ftqOffset = if (params.needPc || params.replayInst) 415 Some(UInt(log2Up(PredictWidth).W)) else None 416 // uop info 417 val predecodeInfo = if(params.hasPredecode) Some(new PreDecodeInfo) else None 418 val debug = new DebugBundle 419 val debugInfo = new PerfDebugInfo 420 } 421 422 // ExuOutput + DynInst --> WriteBackBundle 423 class WriteBackBundle(val params: WbConfig)(implicit p: Parameters) extends Bundle with BundleSource { 424 val rfWen = Bool() 425 val fpWen = Bool() 426 val vecWen = Bool() 427 val pdest = UInt(params.pregIdxWidth.W) 428 val data = UInt(params.dataWidth.W) 429 val robIdx = new RobPtr()(p) 430 val flushPipe = Bool() 431 val replayInst = Bool() 432 val redirect = ValidIO(new Redirect) 433 val fflags = UInt(5.W) 434 val vxsat = Bool() 435 val exceptionVec = ExceptionVec() 436 val debug = new DebugBundle 437 val debugInfo = new PerfDebugInfo 438 439 def fromExuOutput(source: ExuOutput) = { 440 this.rfWen := source.intWen.getOrElse(false.B) 441 this.fpWen := source.fpWen.getOrElse(false.B) 442 this.vecWen := source.vecWen.getOrElse(false.B) 443 this.pdest := source.pdest 444 this.data := source.data 445 this.robIdx := source.robIdx 446 this.flushPipe := source.flushPipe.getOrElse(false.B) 447 this.replayInst := source.replay.getOrElse(false.B) 448 this.redirect := source.redirect.getOrElse(0.U.asTypeOf(this.redirect)) 449 this.fflags := source.fflags.getOrElse(0.U.asTypeOf(this.fflags)) 450 this.vxsat := source.vxsat.getOrElse(0.U.asTypeOf(this.vxsat)) 451 this.exceptionVec := source.exceptionVec.getOrElse(0.U.asTypeOf(this.exceptionVec)) 452 this.debug := source.debug 453 this.debugInfo := source.debugInfo 454 } 455 456 def asWakeUpBundle: IssueQueueWakeUpBundle = { 457 val wakeup = Output(new IssueQueueWakeUpBundle(params.pregIdxWidth)) 458 wakeup.rfWen := this.rfWen 459 wakeup.fpWen := this.fpWen 460 wakeup.vecWen := this.vecWen 461 wakeup.pdest := this.pdest 462 wakeup.source = this.source 463 wakeup 464 } 465 466 def asIntRfWriteBundle(fire: Bool): RfWritePortWithConfig = { 467 val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, this.params.pregIdxWidth))) 468 rfWrite.wen := this.rfWen && fire 469 rfWrite.addr := this.pdest 470 rfWrite.data := this.data 471 rfWrite.intWen := this.rfWen 472 rfWrite.fpWen := false.B 473 rfWrite.vecWen := false.B 474 rfWrite 475 } 476 477 def asVfRfWriteBundle(fire: Bool): RfWritePortWithConfig = { 478 val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, this.params.pregIdxWidth))) 479 rfWrite.wen := (this.fpWen || this.vecWen) && fire 480 rfWrite.addr := this.pdest 481 rfWrite.data := this.data 482 rfWrite.intWen := false.B 483 rfWrite.fpWen := this.fpWen 484 rfWrite.vecWen := this.vecWen 485 rfWrite 486 } 487 } 488 489 class ExceptionInfo extends Bundle { 490 val pc = UInt(VAddrData().dataWidth.W) 491 val instr = UInt(32.W) 492 val commitType = CommitType() 493 val exceptionVec = ExceptionVec() 494 val singleStep = Bool() 495 val crossPageIPFFix = Bool() 496 val isInterrupt = Bool() 497 } 498 499 object UopIdx { 500 def apply()(implicit p: Parameters): UInt = UInt(log2Up(p(XSCoreParamsKey).MaxUopSize + 1).W) 501 } 502 503 class MemExuInput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle { 504 val uop = new DynInst 505 val src = if (isVector) Vec(5, UInt(VLEN.W)) else Vec(3, UInt(XLEN.W)) 506 val iqIdx = UInt(log2Up(MemIQSizeMax).W) 507 val isFirstIssue = Bool() 508 } 509 510 class MemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle { 511 val uop = new DynInst 512 val data = if (isVector) UInt(VLEN.W) else UInt(XLEN.W) 513 val debug = new DebugBundle 514 } 515 516 class MemMicroOpRbExt(implicit p: Parameters) extends XSBundle { 517 val uop = new DynInst 518 val flag = UInt(1.W) 519 } 520} 521