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