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