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} 329 330class MicroOpRbExt(implicit p: Parameters) extends XSBundle { 331 val uop = new MicroOp 332 val flag = UInt(1.W) 333} 334 335class Redirect(implicit p: Parameters) extends XSBundle { 336 val roqIdx = new RoqPtr 337 val ftqIdx = new FtqPtr 338 val ftqOffset = UInt(log2Up(PredictWidth).W) 339 val level = RedirectLevel() 340 val interrupt = Bool() 341 val cfiUpdate = new CfiUpdateInfo 342 343 val stFtqIdx = new FtqPtr // for load violation predict 344 val stFtqOffset = UInt(log2Up(PredictWidth).W) 345 346 // def isUnconditional() = RedirectLevel.isUnconditional(level) 347 def flushItself() = RedirectLevel.flushItself(level) 348 // def isException() = RedirectLevel.isException(level) 349} 350 351class Dp1ToDp2IO(implicit p: Parameters) extends XSBundle { 352 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 353 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 354 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 355} 356 357class ReplayPregReq(implicit p: Parameters) extends XSBundle { 358 // NOTE: set isInt and isFp both to 'false' when invalid 359 val isInt = Bool() 360 val isFp = Bool() 361 val preg = UInt(PhyRegIdxWidth.W) 362} 363 364class DebugBundle(implicit p: Parameters) extends XSBundle { 365 val isMMIO = Bool() 366 val isPerfCnt = Bool() 367 val paddr = UInt(PAddrBits.W) 368} 369 370class ExuInput(implicit p: Parameters) extends XSBundle { 371 val uop = new MicroOp 372 val src = Vec(3, UInt((XLEN + 1).W)) 373} 374 375class ExuOutput(implicit p: Parameters) extends XSBundle { 376 val uop = new MicroOp 377 val data = UInt((XLEN + 1).W) 378 val fflags = UInt(5.W) 379 val redirectValid = Bool() 380 val redirect = new Redirect 381 val debug = new DebugBundle 382} 383 384class ExternalInterruptIO(implicit p: Parameters) extends XSBundle { 385 val mtip = Input(Bool()) 386 val msip = Input(Bool()) 387 val meip = Input(Bool()) 388} 389 390class CSRSpecialIO(implicit p: Parameters) extends XSBundle { 391 val exception = Flipped(ValidIO(new MicroOp)) 392 val isInterrupt = Input(Bool()) 393 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 394 val trapTarget = Output(UInt(VAddrBits.W)) 395 val externalInterrupt = new ExternalInterruptIO 396 val interrupt = Output(Bool()) 397} 398 399class ExceptionInfo(implicit p: Parameters) extends XSBundle { 400 val uop = new MicroOp 401 val isInterrupt = Bool() 402} 403 404class RoqCommitInfo(implicit p: Parameters) extends XSBundle { 405 val ldest = UInt(5.W) 406 val rfWen = Bool() 407 val fpWen = Bool() 408 val wflags = Bool() 409 val commitType = CommitType() 410 val pdest = UInt(PhyRegIdxWidth.W) 411 val old_pdest = UInt(PhyRegIdxWidth.W) 412 val ftqIdx = new FtqPtr 413 val ftqOffset = UInt(log2Up(PredictWidth).W) 414 415 // these should be optimized for synthesis verilog 416 val pc = UInt(VAddrBits.W) 417} 418 419class RoqCommitIO(implicit p: Parameters) extends XSBundle { 420 val isWalk = Output(Bool()) 421 val valid = Vec(CommitWidth, Output(Bool())) 422 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 423 424 def hasWalkInstr = isWalk && valid.asUInt.orR 425 426 def hasCommitInstr = !isWalk && valid.asUInt.orR 427} 428 429class RSFeedback(implicit p: Parameters) extends XSBundle { 430 val rsIdx = UInt(log2Up(IssQueSize).W) 431 val hit = Bool() 432 val flushState = Bool() 433 val sourceType = RSFeedbackType() 434} 435 436class FrontendToBackendIO(implicit p: Parameters) extends XSBundle { 437 // to backend end 438 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 439 val fetchInfo = DecoupledIO(new FtqEntry) 440 // from backend 441 val redirect_cfiUpdate = Flipped(ValidIO(new Redirect)) 442 val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry)) 443 val ftqEnqPtr = Input(new FtqPtr) 444 val ftqLeftOne = Input(Bool()) 445} 446 447class TlbCsrBundle(implicit p: Parameters) extends XSBundle { 448 val satp = new Bundle { 449 val mode = UInt(4.W) // TODO: may change number to parameter 450 val asid = UInt(16.W) 451 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 452 } 453 val priv = new Bundle { 454 val mxr = Bool() 455 val sum = Bool() 456 val imode = UInt(2.W) 457 val dmode = UInt(2.W) 458 } 459 460 override def toPrintable: Printable = { 461 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 462 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 463 } 464} 465 466class SfenceBundle(implicit p: Parameters) extends XSBundle { 467 val valid = Bool() 468 val bits = new Bundle { 469 val rs1 = Bool() 470 val rs2 = Bool() 471 val addr = UInt(VAddrBits.W) 472 } 473 474 override def toPrintable: Printable = { 475 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 476 } 477} 478 479// Bundle for load violation predictor updating 480class MemPredUpdateReq(implicit p: Parameters) extends XSBundle { 481 val valid = Bool() 482 483 // wait table update 484 val waddr = UInt(MemPredPCWidth.W) 485 val wdata = Bool() // true.B by default 486 487 // store set update 488 // by default, ldpc/stpc should be xor folded 489 val ldpc = UInt(MemPredPCWidth.W) 490 val stpc = UInt(MemPredPCWidth.W) 491} 492 493class PerfInfoIO extends Bundle { 494 val clean = Input(Bool()) 495 val dump = Input(Bool()) 496} 497 498class CustomCSRCtrlIO(implicit p: Parameters) extends XSBundle { 499 // Prefetcher 500 val l1plus_pf_enable = Output(Bool()) 501 val l2_pf_enable = Output(Bool()) 502 // Labeled XiangShan 503 val dsid = Output(UInt(8.W)) // TODO: DsidWidth as parameter 504 // Load violation predictor 505 val lvpred_disable = Output(Bool()) 506 val no_spec_load = Output(Bool()) 507 val waittable_timeout = Output(UInt(5.W)) 508 // Branch predictor 509 val bp_ctrl = Output(new BPUCtrl) 510 // Memory Block 511 val sbuffer_threshold = Output(UInt(4.W)) 512 // Rename 513 val move_elim_enable = Output(Bool()) 514} 515