1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan.backend.SelImm 6import xiangshan.backend.brq.BrqPtr 7import xiangshan.backend.rename.FreeListPtr 8import xiangshan.backend.roq.RoqPtr 9import xiangshan.backend.decode.{ImmUnion, XDecode} 10import xiangshan.mem.{LqPtr, SqPtr} 11import xiangshan.frontend.PreDecodeInfo 12import xiangshan.frontend.HasBPUParameter 13import xiangshan.frontend.HasTageParameter 14import xiangshan.frontend.HasIFUConst 15import xiangshan.frontend.GlobalHistory 16import utils._ 17 18import scala.math.max 19import Chisel.experimental.chiselName 20 21// Fetch FetchWidth x 32-bit insts from Icache 22class FetchPacket extends XSBundle { 23 val instrs = Vec(PredictWidth, UInt(32.W)) 24 val mask = UInt(PredictWidth.W) 25 val pdmask = UInt(PredictWidth.W) 26 // val pc = UInt(VAddrBits.W) 27 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 28 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 29 val bpuMeta = Vec(PredictWidth, new BpuMeta) 30 val pd = Vec(PredictWidth, new PreDecodeInfo) 31 val ipf = Bool() 32 val acf = Bool() 33 val crossPageIPFFix = Bool() 34 val predTaken = Bool() 35} 36 37class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 38 val valid = Bool() 39 val bits = gen.cloneType.asInstanceOf[T] 40 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 41} 42 43object ValidUndirectioned { 44 def apply[T <: Data](gen: T) = { 45 new ValidUndirectioned[T](gen) 46 } 47} 48 49class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter { 50 def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_) 51 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 52 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 53 val tageTaken = if (useSC) Bool() else UInt(0.W) 54 val scUsed = if (useSC) Bool() else UInt(0.W) 55 val scPred = if (useSC) Bool() else UInt(0.W) 56 // Suppose ctrbits of all tables are identical 57 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 58 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 59} 60 61class TageMeta extends XSBundle with HasTageParameter { 62 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 63 val altDiffers = Bool() 64 val providerU = UInt(2.W) 65 val providerCtr = UInt(3.W) 66 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 67 val taken = Bool() 68 val scMeta = new SCMeta(EnableSC) 69} 70 71@chiselName 72class BranchPrediction extends XSBundle with HasIFUConst { 73 // val redirect = Bool() 74 val takens = UInt(PredictWidth.W) 75 // val jmpIdx = UInt(log2Up(PredictWidth).W) 76 val brMask = UInt(PredictWidth.W) 77 val jalMask = UInt(PredictWidth.W) 78 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 79 80 // marks the last 2 bytes of this fetch packet 81 // val endsAtTheEndOfFirstBank = Bool() 82 // val endsAtTheEndOfLastBank = Bool() 83 84 // half RVI could only start at the end of a packet 85 val hasHalfRVI = Bool() 86 87 88 // assumes that only one of the two conditions could be true 89 def lastHalfRVIMask = Cat(hasHalfRVI.asUInt, 0.U((PredictWidth-1).W)) 90 91 def lastHalfRVIClearMask = ~lastHalfRVIMask 92 // is taken from half RVI 93 def lastHalfRVITaken = takens(PredictWidth-1) && hasHalfRVI 94 95 def lastHalfRVIIdx = (PredictWidth-1).U 96 // should not be used if not lastHalfRVITaken 97 def lastHalfRVITarget = targets(PredictWidth-1) 98 99 def realTakens = takens & lastHalfRVIClearMask 100 def realBrMask = brMask & lastHalfRVIClearMask 101 def realJalMask = jalMask & lastHalfRVIClearMask 102 103 def brNotTakens = (~takens & realBrMask) 104 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 105 (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0))))) 106 // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 107 def unmaskedJmpIdx = ParallelPriorityEncoder(takens) 108 // if not taken before the half RVI inst 109 def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0))) 110 // could get PredictWidth-1 when only the first bank is valid 111 def jmpIdx = ParallelPriorityEncoder(realTakens) 112 // only used when taken 113 def target = { 114 val generator = new PriorityMuxGenerator[UInt] 115 generator.register(realTakens.asBools, targets, List.fill(PredictWidth)(None)) 116 generator() 117 } 118 def taken = ParallelORR(realTakens) 119 def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools) 120 def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens)) 121} 122 123class BpuMeta extends XSBundle with HasBPUParameter { 124 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 125 val ubtbHits = Bool() 126 val btbWriteWay = UInt(log2Up(BtbWays).W) 127 val btbHitJal = Bool() 128 val bimCtr = UInt(2.W) 129 val tageMeta = new TageMeta 130 val rasSp = UInt(log2Up(RasSize).W) 131 val rasTopCtr = UInt(8.W) 132 val rasToqAddr = UInt(VAddrBits.W) 133 val fetchIdx = UInt(log2Up(PredictWidth).W) 134 val specCnt = UInt(10.W) 135 // for global history 136 val predTaken = Bool() 137 val hist = new GlobalHistory 138 val predHist = new GlobalHistory 139 val sawNotTakenBranch = Bool() 140 141 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 142 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 143 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 144 145 val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor} 146 147 // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 148 // this.histPtr := histPtr 149 // this.tageMeta := tageMeta 150 // this.rasSp := rasSp 151 // this.rasTopCtr := rasTopCtr 152 // this.asUInt 153 // } 154 def size = 0.U.asTypeOf(this).getWidth 155 def fromUInt(x: UInt) = x.asTypeOf(this) 156} 157 158class Predecode extends XSBundle with HasIFUConst { 159 val hasLastHalfRVI = Bool() 160 val mask = UInt(PredictWidth.W) 161 val lastHalf = Bool() 162 val pd = Vec(PredictWidth, (new PreDecodeInfo)) 163} 164 165class CfiUpdateInfo extends XSBundle with HasBPUParameter { 166 // from backend 167 val pc = UInt(VAddrBits.W) 168 val pnpc = UInt(VAddrBits.W) 169 val fetchIdx = UInt(log2Up(PredictWidth).W) 170 // frontend -> backend -> frontend 171 val pd = new PreDecodeInfo 172 val bpuMeta = new BpuMeta 173 174 // need pipeline update 175 val target = UInt(VAddrBits.W) 176 val brTarget = UInt(VAddrBits.W) 177 val taken = Bool() 178 val isMisPred = Bool() 179 val brTag = new BrqPtr 180 val isReplay = Bool() 181} 182 183// Dequeue DecodeWidth insts from Ibuffer 184class CtrlFlow extends XSBundle { 185 val instr = UInt(32.W) 186 val pc = UInt(VAddrBits.W) 187 val exceptionVec = ExceptionVec() 188 val intrVec = Vec(12, Bool()) 189 val brUpdate = new CfiUpdateInfo 190 val crossPageIPFFix = Bool() 191} 192 193class OfqEntry extends XSBundle { 194 // fetch pc, pc of each inst could be generated by concatenation 195 val pc = UInt(VAddrBits.W) 196 197 // prediction metas 198 val hist = new GlobalHistory 199 val predHist = new GlobalHistory 200 val rasSp = UInt(log2Ceil(RasSize).W) 201 val rasTop = new RASEntry() 202 val metas = Vec(PredictWidth, new BpuMeta) 203 204 val brMask = UInt(PredictWidth.W) 205 val jalMask = UInt(PredictWidth.W) 206 207 val mispred = UInt(PredictWidth.W) 208} 209 210 211 212class FPUCtrlSignals extends XSBundle { 213 val isAddSub = Bool() // swap23 214 val typeTagIn = UInt(2.W) 215 val typeTagOut = UInt(2.W) 216 val fromInt = Bool() 217 val wflags = Bool() 218 val fpWen = Bool() 219 val fmaCmd = UInt(2.W) 220 val div = Bool() 221 val sqrt = Bool() 222 val fcvt = Bool() 223 val typ = UInt(2.W) 224 val fmt = UInt(2.W) 225 val ren3 = Bool() //TODO: remove SrcType.fp 226} 227 228// Decode DecodeWidth insts at Decode Stage 229class CtrlSignals extends XSBundle { 230 val src1Type, src2Type, src3Type = SrcType() 231 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 232 val ldest = UInt(5.W) 233 val fuType = FuType() 234 val fuOpType = FuOpType() 235 val rfWen = Bool() 236 val fpWen = Bool() 237 val isXSTrap = Bool() 238 val noSpecExec = Bool() // wait forward 239 val blockBackward = Bool() // block backward 240 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 241 val isRVF = Bool() 242 val selImm = SelImm() 243 val imm = UInt(ImmUnion.maxLen.W) 244 val commitType = CommitType() 245 val fpu = new FPUCtrlSignals 246 247 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 248 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 249 val signals = 250 Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen, 251 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 252 signals zip decoder map { case(s, d) => s := d } 253 commitType := DontCare 254 this 255 } 256} 257 258class CfCtrl extends XSBundle { 259 val cf = new CtrlFlow 260 val ctrl = new CtrlSignals 261 val brTag = new BrqPtr 262} 263 264class PerfDebugInfo extends XSBundle { 265 // val fetchTime = UInt(64.W) 266 val renameTime = UInt(64.W) 267 val dispatchTime = UInt(64.W) 268 val issueTime = UInt(64.W) 269 val writebackTime = UInt(64.W) 270 // val commitTime = UInt(64.W) 271} 272 273// Separate LSQ 274class LSIdx extends XSBundle { 275 val lqIdx = new LqPtr 276 val sqIdx = new SqPtr 277} 278 279// CfCtrl -> MicroOp at Rename Stage 280class MicroOp extends CfCtrl { 281 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 282 val src1State, src2State, src3State = SrcState() 283 val roqIdx = new RoqPtr 284 val lqIdx = new LqPtr 285 val sqIdx = new SqPtr 286 val diffTestDebugLrScValid = Bool() 287 val debugInfo = new PerfDebugInfo 288} 289 290class Redirect extends XSBundle { 291 val roqIdx = new RoqPtr 292 val level = RedirectLevel() 293 val interrupt = Bool() 294 val pc = UInt(VAddrBits.W) 295 val target = UInt(VAddrBits.W) 296 val brTag = new BrqPtr 297 298 def isUnconditional() = RedirectLevel.isUnconditional(level) 299 def flushItself() = RedirectLevel.flushItself(level) 300 def isException() = RedirectLevel.isException(level) 301} 302 303class Dp1ToDp2IO extends XSBundle { 304 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 305 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 306 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 307} 308 309class ReplayPregReq extends XSBundle { 310 // NOTE: set isInt and isFp both to 'false' when invalid 311 val isInt = Bool() 312 val isFp = Bool() 313 val preg = UInt(PhyRegIdxWidth.W) 314} 315 316class DebugBundle extends XSBundle{ 317 val isMMIO = Bool() 318 val isPerfCnt = Bool() 319} 320 321class ExuInput extends XSBundle { 322 val uop = new MicroOp 323 val src1, src2, src3 = UInt((XLEN+1).W) 324} 325 326class ExuOutput extends XSBundle { 327 val uop = new MicroOp 328 val data = UInt((XLEN+1).W) 329 val fflags = UInt(5.W) 330 val redirectValid = Bool() 331 val redirect = new Redirect 332 val brUpdate = new CfiUpdateInfo 333 val debug = new DebugBundle 334} 335 336class ExternalInterruptIO extends XSBundle { 337 val mtip = Input(Bool()) 338 val msip = Input(Bool()) 339 val meip = Input(Bool()) 340} 341 342class CSRSpecialIO extends XSBundle { 343 val exception = Flipped(ValidIO(new MicroOp)) 344 val isInterrupt = Input(Bool()) 345 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 346 val trapTarget = Output(UInt(VAddrBits.W)) 347 val externalInterrupt = new ExternalInterruptIO 348 val interrupt = Output(Bool()) 349} 350 351class RoqCommitInfo extends XSBundle { 352 val ldest = UInt(5.W) 353 val rfWen = Bool() 354 val fpWen = Bool() 355 val wflags = Bool() 356 val commitType = CommitType() 357 val pdest = UInt(PhyRegIdxWidth.W) 358 val old_pdest = UInt(PhyRegIdxWidth.W) 359 val lqIdx = new LqPtr 360 val sqIdx = new SqPtr 361 362 // these should be optimized for synthesis verilog 363 val pc = UInt(VAddrBits.W) 364} 365 366class RoqCommitIO extends XSBundle { 367 val isWalk = Output(Bool()) 368 val valid = Vec(CommitWidth, Output(Bool())) 369 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 370 371 def hasWalkInstr = isWalk && valid.asUInt.orR 372 def hasCommitInstr = !isWalk && valid.asUInt.orR 373} 374 375class TlbFeedback extends XSBundle { 376 val roqIdx = new RoqPtr 377 val hit = Bool() 378} 379 380class FrontendToBackendIO extends XSBundle { 381 // to backend end 382 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 383 // from backend 384 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 385 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 386 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 387} 388 389class TlbCsrBundle extends XSBundle { 390 val satp = new Bundle { 391 val mode = UInt(4.W) // TODO: may change number to parameter 392 val asid = UInt(16.W) 393 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 394 } 395 val priv = new Bundle { 396 val mxr = Bool() 397 val sum = Bool() 398 val imode = UInt(2.W) 399 val dmode = UInt(2.W) 400 } 401 402 override def toPrintable: Printable = { 403 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 404 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 405 } 406} 407 408class SfenceBundle extends XSBundle { 409 val valid = Bool() 410 val bits = new Bundle { 411 val rs1 = Bool() 412 val rs2 = Bool() 413 val addr = UInt(VAddrBits.W) 414 } 415 416 override def toPrintable: Printable = { 417 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 418 } 419} 420