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