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