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