1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan.backend.roq.RoqPtr 6import xiangshan.backend.decode.{ImmUnion, XDecode} 7import xiangshan.mem.{LqPtr, SqPtr} 8import xiangshan.frontend.PreDecodeInfoForDebug 9import xiangshan.frontend.PreDecodeInfo 10import xiangshan.frontend.HasBPUParameter 11import xiangshan.frontend.PreDecodeInfo 12import xiangshan.frontend.HasTageParameter 13import xiangshan.frontend.HasSCParameter 14import xiangshan.frontend.HasIFUConst 15import xiangshan.frontend.GlobalHistory 16import xiangshan.frontend.RASEntry 17import xiangshan.frontend.BPUCtrl 18import utils._ 19 20import scala.math.max 21import Chisel.experimental.chiselName 22import chipsalliance.rocketchip.config.Parameters 23import xiangshan.backend.ftq.FtqPtr 24 25// Fetch FetchWidth x 32-bit insts from Icache 26class FetchPacket(implicit p: Parameters) extends XSBundle { 27 val instrs = Vec(PredictWidth, UInt(32.W)) 28 val mask = UInt(PredictWidth.W) 29 val pdmask = UInt(PredictWidth.W) 30 // val pc = UInt(VAddrBits.W) 31 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 32 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 33 val pd = Vec(PredictWidth, new PreDecodeInfo) 34 val ipf = Bool() 35 val acf = Bool() 36 val crossPageIPFFix = Bool() 37 val pred_taken = UInt(PredictWidth.W) 38 val ftqPtr = new FtqPtr 39} 40 41class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 42 val valid = Bool() 43 val bits = gen.cloneType.asInstanceOf[T] 44 45 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 46} 47 48object ValidUndirectioned { 49 def apply[T <: Data](gen: T) = { 50 new ValidUndirectioned[T](gen) 51 } 52} 53 54object RSFeedbackType { 55 val tlbMiss = 0.U(2.W) 56 val mshrFull = 1.U(2.W) 57 val dataInvalid = 2.U(2.W) 58 59 def apply() = UInt(2.W) 60} 61 62class SCMeta(val useSC: Boolean)(implicit p: Parameters) extends XSBundle with HasSCParameter { 63 val tageTaken = if (useSC) Bool() else UInt(0.W) 64 val scUsed = if (useSC) Bool() else UInt(0.W) 65 val scPred = if (useSC) Bool() else UInt(0.W) 66 // Suppose ctrbits of all tables are identical 67 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 68} 69 70class TageMeta(implicit p: Parameters) extends XSBundle with HasTageParameter { 71 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 72 val altDiffers = Bool() 73 val providerU = UInt(2.W) 74 val providerCtr = UInt(3.W) 75 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 76 val taken = Bool() 77 val scMeta = new SCMeta(EnableSC) 78} 79 80@chiselName 81class BranchPrediction(implicit p: Parameters) extends XSBundle with HasIFUConst { 82 // val redirect = Bool() 83 val takens = UInt(PredictWidth.W) 84 // val jmpIdx = UInt(log2Up(PredictWidth).W) 85 val brMask = UInt(PredictWidth.W) 86 val jalMask = UInt(PredictWidth.W) 87 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 88 89 // half RVI could only start at the end of a packet 90 val hasHalfRVI = Bool() 91 92 def brNotTakens = (~takens & brMask) 93 94 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 95 (if (i == 0) false.B else ParallelORR(brNotTakens(i - 1, 0))))) 96 97 // if not taken before the half RVI inst 98 def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth - 2, 0))) 99 100 // could get PredictWidth-1 when only the first bank is valid 101 def jmpIdx = ParallelPriorityEncoder(takens) 102 103 // only used when taken 104 def target = { 105 val generator = new PriorityMuxGenerator[UInt] 106 generator.register(takens.asBools, targets, List.fill(PredictWidth)(None)) 107 generator() 108 } 109 110 def taken = ParallelORR(takens) 111 112 def takenOnBr = taken && ParallelPriorityMux(takens, brMask.asBools) 113 114 def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(takens, sawNotTakenBr), ParallelORR(brNotTakens)) 115} 116 117class PredictorAnswer(implicit p: Parameters) extends XSBundle { 118 val hit = if (!env.FPGAPlatform) Bool() else UInt(0.W) 119 val taken = if (!env.FPGAPlatform) Bool() else UInt(0.W) 120 val target = if (!env.FPGAPlatform) UInt(VAddrBits.W) else UInt(0.W) 121} 122 123class BpuMeta(implicit p: Parameters) extends XSBundle with HasBPUParameter { 124 val btbWriteWay = UInt(log2Up(BtbWays).W) 125 val btbHit = Bool() 126 val bimCtr = UInt(2.W) 127 val tageMeta = new TageMeta 128 // for global history 129 130 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 131 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 132 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 133 134 val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor} 135 136 val ubtbHit = if (BPUDebug) UInt(1.W) else UInt(0.W) 137 138 val ubtbAns = new PredictorAnswer 139 val btbAns = new PredictorAnswer 140 val tageAns = new PredictorAnswer 141 val rasAns = new PredictorAnswer 142 val loopAns = new PredictorAnswer 143 144 // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 145 // this.histPtr := histPtr 146 // this.tageMeta := tageMeta 147 // this.rasSp := rasSp 148 // this.rasTopCtr := rasTopCtr 149 // this.asUInt 150 // } 151 def size = 0.U.asTypeOf(this).getWidth 152 153 def fromUInt(x: UInt) = x.asTypeOf(this) 154} 155 156class Predecode(implicit p: Parameters) extends XSBundle with HasIFUConst { 157 val hasLastHalfRVI = Bool() 158 val mask = UInt(PredictWidth.W) 159 val lastHalf = Bool() 160 val pd = Vec(PredictWidth, (new PreDecodeInfo)) 161} 162 163class CfiUpdateInfo(implicit p: Parameters) extends XSBundle with HasBPUParameter { 164 // from backend 165 val pc = UInt(VAddrBits.W) 166 // frontend -> backend -> frontend 167 val pd = new PreDecodeInfo 168 val rasSp = UInt(log2Up(RasSize).W) 169 val rasEntry = new RASEntry 170 val hist = new GlobalHistory 171 val predHist = new GlobalHistory 172 val specCnt = Vec(PredictWidth, UInt(10.W)) 173 // need pipeline update 174 val sawNotTakenBranch = Bool() 175 val predTaken = Bool() 176 val target = UInt(VAddrBits.W) 177 val taken = Bool() 178 val isMisPred = Bool() 179} 180 181// Dequeue DecodeWidth insts from Ibuffer 182class CtrlFlow(implicit p: Parameters) extends XSBundle { 183 val instr = UInt(32.W) 184 val pc = UInt(VAddrBits.W) 185 val foldpc = UInt(MemPredPCWidth.W) 186 val exceptionVec = ExceptionVec() 187 val intrVec = Vec(12, Bool()) 188 val pd = new PreDecodeInfo 189 val pred_taken = Bool() 190 val crossPageIPFFix = Bool() 191 val storeSetHit = Bool() // inst has been allocated an store set 192 val loadWaitBit = Bool() // load inst should not be executed until all former store addr calcuated 193 val ssid = UInt(SSIDWidth.W) 194 val ftqPtr = new FtqPtr 195 val ftqOffset = UInt(log2Up(PredictWidth).W) 196} 197 198class FtqEntry(implicit p: Parameters) extends XSBundle { 199 // fetch pc, pc of each inst could be generated by concatenation 200 val ftqPC = UInt(VAddrBits.W) 201 val lastPacketPC = ValidUndirectioned(UInt(VAddrBits.W)) 202 // prediction metas 203 val hist = new GlobalHistory 204 val predHist = new GlobalHistory 205 val rasSp = UInt(log2Ceil(RasSize).W) 206 val rasTop = new RASEntry() 207 val specCnt = Vec(PredictWidth, UInt(10.W)) 208 val metas = Vec(PredictWidth, new BpuMeta) 209 210 val cfiIsCall, cfiIsRet, cfiIsJalr, cfiIsRVC = Bool() 211 val rvc_mask = Vec(PredictWidth, Bool()) 212 val br_mask = Vec(PredictWidth, Bool()) 213 val cfiIndex = ValidUndirectioned(UInt(log2Up(PredictWidth).W)) 214 val valids = Vec(PredictWidth, Bool()) 215 216 // backend update 217 val mispred = Vec(PredictWidth, Bool()) 218 val target = UInt(VAddrBits.W) 219 220 // For perf counters 221 val pd = Vec(PredictWidth, new PreDecodeInfoForDebug(!env.FPGAPlatform)) 222 223 def takens = VecInit((0 until PredictWidth).map(i => cfiIndex.valid && cfiIndex.bits === i.U)) 224 def hasLastPrev = lastPacketPC.valid 225 226 override def toPrintable: Printable = { 227 p"ftqPC: ${Hexadecimal(ftqPC)} lastPacketPC: ${Hexadecimal(lastPacketPC.bits)} hasLastPrev:$hasLastPrev " + 228 p"rasSp:$rasSp specCnt:$specCnt brmask:${Binary(Cat(br_mask))} rvcmask:${Binary(Cat(rvc_mask))} " + 229 p"valids:${Binary(valids.asUInt())} cfi valid: ${cfiIndex.valid} " + 230 p"cfi index: ${cfiIndex.bits} isCall:$cfiIsCall isRet:$cfiIsRet isJalr:$cfiIsJalr, isRvc:$cfiIsRVC " + 231 p"mispred:${Binary(Cat(mispred))} target:${Hexadecimal(target)}\n" 232 } 233 234} 235 236 237class FPUCtrlSignals(implicit p: Parameters) extends XSBundle { 238 val isAddSub = Bool() // swap23 239 val typeTagIn = UInt(2.W) 240 val typeTagOut = UInt(2.W) 241 val fromInt = Bool() 242 val wflags = Bool() 243 val fpWen = Bool() 244 val fmaCmd = UInt(2.W) 245 val div = Bool() 246 val sqrt = Bool() 247 val fcvt = Bool() 248 val typ = UInt(2.W) 249 val fmt = UInt(2.W) 250 val ren3 = Bool() //TODO: remove SrcType.fp 251 val rm = UInt(3.W) 252} 253 254// Decode DecodeWidth insts at Decode Stage 255class CtrlSignals(implicit p: Parameters) extends XSBundle { 256 val srcType = Vec(3, SrcType()) 257 val lsrc = Vec(3, UInt(5.W)) 258 val ldest = UInt(5.W) 259 val fuType = FuType() 260 val fuOpType = FuOpType() 261 val rfWen = Bool() 262 val fpWen = Bool() 263 val isXSTrap = Bool() 264 val noSpecExec = Bool() // wait forward 265 val blockBackward = Bool() // block backward 266 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 267 val isRVF = Bool() 268 val selImm = SelImm() 269 val imm = UInt(ImmUnion.maxLen.W) 270 val commitType = CommitType() 271 val fpu = new FPUCtrlSignals 272 val isMove = Bool() 273 274 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 275 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 276 val signals = 277 Seq(srcType(0), srcType(1), srcType(2), fuType, fuOpType, rfWen, fpWen, 278 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 279 signals zip decoder map { case (s, d) => s := d } 280 commitType := DontCare 281 this 282 } 283} 284 285class CfCtrl(implicit p: Parameters) extends XSBundle { 286 val cf = new CtrlFlow 287 val ctrl = new CtrlSignals 288} 289 290class PerfDebugInfo(implicit p: Parameters) extends XSBundle { 291 val src1MoveElim = Bool() 292 val src2MoveElim = Bool() 293 // val fetchTime = UInt(64.W) 294 val renameTime = UInt(64.W) 295 val dispatchTime = UInt(64.W) 296 val issueTime = UInt(64.W) 297 val writebackTime = UInt(64.W) 298 // val commitTime = UInt(64.W) 299} 300 301// Separate LSQ 302class LSIdx(implicit p: Parameters) extends XSBundle { 303 val lqIdx = new LqPtr 304 val sqIdx = new SqPtr 305} 306 307// CfCtrl -> MicroOp at Rename Stage 308class MicroOp(implicit p: Parameters) extends CfCtrl { 309 val srcState = Vec(3, SrcState()) 310 val psrc = Vec(3, UInt(PhyRegIdxWidth.W)) 311 val pdest = UInt(PhyRegIdxWidth.W) 312 val old_pdest = UInt(PhyRegIdxWidth.W) 313 val roqIdx = new RoqPtr 314 val lqIdx = new LqPtr 315 val sqIdx = new SqPtr 316 val diffTestDebugLrScValid = Bool() 317 val debugInfo = new PerfDebugInfo 318 def needRfRPort(index: Int, rfType: Int, ignoreState: Boolean = true) : Bool = { 319 (index, rfType) match { 320 case (0, 0) => ctrl.srcType(0) === SrcType.reg && ctrl.lsrc(0) =/= 0.U && (srcState(0) === SrcState.rdy || ignoreState.B) 321 case (1, 0) => ctrl.srcType(1) === SrcType.reg && ctrl.lsrc(1) =/= 0.U && (srcState(1) === SrcState.rdy || ignoreState.B) 322 case (0, 1) => ctrl.srcType(0) === SrcType.fp && (srcState(0) === SrcState.rdy || ignoreState.B) 323 case (1, 1) => ctrl.srcType(1) === SrcType.fp && (srcState(1) === SrcState.rdy || ignoreState.B) 324 case (2, 1) => ctrl.srcType(2) === SrcType.fp && (srcState(2) === SrcState.rdy || ignoreState.B) 325 case _ => false.B 326 } 327 } 328 def srcIsReady: Vec[Bool] = { 329 VecInit(ctrl.srcType.zip(srcState).map{ case (t, s) => SrcType.isPcImm(t) || s === SrcState.rdy }) 330 } 331 def doWriteIntRf: Bool = ctrl.rfWen && ctrl.ldest =/= 0.U 332 def doWriteFpRf: Bool = ctrl.fpWen 333} 334 335class MicroOpRbExt(implicit p: Parameters) extends XSBundle { 336 val uop = new MicroOp 337 val flag = UInt(1.W) 338} 339 340class Redirect(implicit p: Parameters) extends XSBundle { 341 val roqIdx = new RoqPtr 342 val ftqIdx = new FtqPtr 343 val ftqOffset = UInt(log2Up(PredictWidth).W) 344 val level = RedirectLevel() 345 val interrupt = Bool() 346 val cfiUpdate = new CfiUpdateInfo 347 348 val stFtqIdx = new FtqPtr // for load violation predict 349 val stFtqOffset = UInt(log2Up(PredictWidth).W) 350 351 // def isUnconditional() = RedirectLevel.isUnconditional(level) 352 def flushItself() = RedirectLevel.flushItself(level) 353 // def isException() = RedirectLevel.isException(level) 354} 355 356class Dp1ToDp2IO(implicit p: Parameters) extends XSBundle { 357 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 358 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 359 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 360} 361 362class ReplayPregReq(implicit p: Parameters) extends XSBundle { 363 // NOTE: set isInt and isFp both to 'false' when invalid 364 val isInt = Bool() 365 val isFp = Bool() 366 val preg = UInt(PhyRegIdxWidth.W) 367} 368 369class DebugBundle(implicit p: Parameters) extends XSBundle { 370 val isMMIO = Bool() 371 val isPerfCnt = Bool() 372 val paddr = UInt(PAddrBits.W) 373} 374 375class ExuInput(implicit p: Parameters) extends XSBundle { 376 val uop = new MicroOp 377 val src = Vec(3, UInt((XLEN + 1).W)) 378} 379 380class ExuOutput(implicit p: Parameters) extends XSBundle { 381 val uop = new MicroOp 382 val data = UInt((XLEN + 1).W) 383 val fflags = UInt(5.W) 384 val redirectValid = Bool() 385 val redirect = new Redirect 386 val debug = new DebugBundle 387} 388 389class ExternalInterruptIO(implicit p: Parameters) extends XSBundle { 390 val mtip = Input(Bool()) 391 val msip = Input(Bool()) 392 val meip = Input(Bool()) 393} 394 395class CSRSpecialIO(implicit p: Parameters) extends XSBundle { 396 val exception = Flipped(ValidIO(new MicroOp)) 397 val isInterrupt = Input(Bool()) 398 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 399 val trapTarget = Output(UInt(VAddrBits.W)) 400 val externalInterrupt = new ExternalInterruptIO 401 val interrupt = Output(Bool()) 402} 403 404class ExceptionInfo(implicit p: Parameters) extends XSBundle { 405 val uop = new MicroOp 406 val isInterrupt = Bool() 407} 408 409class RoqCommitInfo(implicit p: Parameters) extends XSBundle { 410 val ldest = UInt(5.W) 411 val rfWen = Bool() 412 val fpWen = Bool() 413 val wflags = Bool() 414 val commitType = CommitType() 415 val pdest = UInt(PhyRegIdxWidth.W) 416 val old_pdest = UInt(PhyRegIdxWidth.W) 417 val ftqIdx = new FtqPtr 418 val ftqOffset = UInt(log2Up(PredictWidth).W) 419 420 // these should be optimized for synthesis verilog 421 val pc = UInt(VAddrBits.W) 422} 423 424class RoqCommitIO(implicit p: Parameters) extends XSBundle { 425 val isWalk = Output(Bool()) 426 val valid = Vec(CommitWidth, Output(Bool())) 427 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 428 429 def hasWalkInstr = isWalk && valid.asUInt.orR 430 431 def hasCommitInstr = !isWalk && valid.asUInt.orR 432} 433 434class RSFeedback(implicit p: Parameters) extends XSBundle { 435 val rsIdx = UInt(log2Up(IssQueSize).W) 436 val hit = Bool() 437 val flushState = Bool() 438 val sourceType = RSFeedbackType() 439} 440 441class FrontendToBackendIO(implicit p: Parameters) extends XSBundle { 442 // to backend end 443 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 444 val fetchInfo = DecoupledIO(new FtqEntry) 445 // from backend 446 val redirect_cfiUpdate = Flipped(ValidIO(new Redirect)) 447 val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry)) 448 val ftqEnqPtr = Input(new FtqPtr) 449 val ftqLeftOne = Input(Bool()) 450} 451 452class TlbCsrBundle(implicit p: Parameters) extends XSBundle { 453 val satp = new Bundle { 454 val mode = UInt(4.W) // TODO: may change number to parameter 455 val asid = UInt(16.W) 456 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 457 } 458 val priv = new Bundle { 459 val mxr = Bool() 460 val sum = Bool() 461 val imode = UInt(2.W) 462 val dmode = UInt(2.W) 463 } 464 465 override def toPrintable: Printable = { 466 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 467 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 468 } 469} 470 471class SfenceBundle(implicit p: Parameters) extends XSBundle { 472 val valid = Bool() 473 val bits = new Bundle { 474 val rs1 = Bool() 475 val rs2 = Bool() 476 val addr = UInt(VAddrBits.W) 477 } 478 479 override def toPrintable: Printable = { 480 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 481 } 482} 483 484// Bundle for load violation predictor updating 485class MemPredUpdateReq(implicit p: Parameters) extends XSBundle { 486 val valid = Bool() 487 488 // wait table update 489 val waddr = UInt(MemPredPCWidth.W) 490 val wdata = Bool() // true.B by default 491 492 // store set update 493 // by default, ldpc/stpc should be xor folded 494 val ldpc = UInt(MemPredPCWidth.W) 495 val stpc = UInt(MemPredPCWidth.W) 496} 497 498class PerfInfoIO extends Bundle { 499 val clean = Input(Bool()) 500 val dump = Input(Bool()) 501} 502 503class CustomCSRCtrlIO(implicit p: Parameters) extends XSBundle { 504 // Prefetcher 505 val l1plus_pf_enable = Output(Bool()) 506 val l2_pf_enable = Output(Bool()) 507 // Labeled XiangShan 508 val dsid = Output(UInt(8.W)) // TODO: DsidWidth as parameter 509 // Load violation predictor 510 val lvpred_disable = Output(Bool()) 511 val no_spec_load = Output(Bool()) 512 val waittable_timeout = Output(UInt(5.W)) 513 // Branch predictor 514 val bp_ctrl = Output(new BPUCtrl) 515 // Memory Block 516 val sbuffer_threshold = Output(UInt(4.W)) 517 // Rename 518 val move_elim_enable = Output(Bool()) 519} 520