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} 153 154// Dequeue DecodeWidth insts from Ibuffer 155class CtrlFlow(implicit p: Parameters) extends XSBundle { 156 val instr = UInt(32.W) 157 val pc = UInt(VAddrBits.W) 158 val foldpc = UInt(MemPredPCWidth.W) 159 val exceptionVec = ExceptionVec() 160 val intrVec = Vec(12, Bool()) 161 val pd = new PreDecodeInfo 162 val pred_taken = Bool() 163 val crossPageIPFFix = Bool() 164 val storeSetHit = Bool() // inst has been allocated an store set 165 val loadWaitBit = Bool() // load inst should not be executed until all former store addr calcuated 166 val ssid = UInt(SSIDWidth.W) 167 val ftqPtr = new FtqPtr 168 val ftqOffset = UInt(log2Up(PredictWidth).W) 169} 170 171class FPUCtrlSignals(implicit p: Parameters) extends XSBundle { 172 val isAddSub = Bool() // swap23 173 val typeTagIn = UInt(2.W) 174 val typeTagOut = UInt(2.W) 175 val fromInt = Bool() 176 val wflags = Bool() 177 val fpWen = Bool() 178 val fmaCmd = UInt(2.W) 179 val div = Bool() 180 val sqrt = Bool() 181 val fcvt = Bool() 182 val typ = UInt(2.W) 183 val fmt = UInt(2.W) 184 val ren3 = Bool() //TODO: remove SrcType.fp 185 val rm = UInt(3.W) 186} 187 188// Decode DecodeWidth insts at Decode Stage 189class CtrlSignals(implicit p: Parameters) extends XSBundle { 190 val srcType = Vec(3, SrcType()) 191 val lsrc = Vec(3, UInt(5.W)) 192 val ldest = UInt(5.W) 193 val fuType = FuType() 194 val fuOpType = FuOpType() 195 val rfWen = Bool() 196 val fpWen = Bool() 197 val isXSTrap = Bool() 198 val noSpecExec = Bool() // wait forward 199 val blockBackward = Bool() // block backward 200 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 201 val isRVF = Bool() 202 val selImm = SelImm() 203 val imm = UInt(ImmUnion.maxLen.W) 204 val commitType = CommitType() 205 val fpu = new FPUCtrlSignals 206 val isMove = Bool() 207 208 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 209 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 210 val signals = 211 Seq(srcType(0), srcType(1), srcType(2), fuType, fuOpType, rfWen, fpWen, 212 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 213 signals zip decoder map { case (s, d) => s := d } 214 commitType := DontCare 215 this 216 } 217} 218 219class CfCtrl(implicit p: Parameters) extends XSBundle { 220 val cf = new CtrlFlow 221 val ctrl = new CtrlSignals 222} 223 224class PerfDebugInfo(implicit p: Parameters) extends XSBundle { 225 val src1MoveElim = Bool() 226 val src2MoveElim = Bool() 227 // val fetchTime = UInt(64.W) 228 val renameTime = UInt(64.W) 229 val dispatchTime = UInt(64.W) 230 val issueTime = UInt(64.W) 231 val writebackTime = UInt(64.W) 232 // val commitTime = UInt(64.W) 233} 234 235// Separate LSQ 236class LSIdx(implicit p: Parameters) extends XSBundle { 237 val lqIdx = new LqPtr 238 val sqIdx = new SqPtr 239} 240 241// CfCtrl -> MicroOp at Rename Stage 242class MicroOp(implicit p: Parameters) extends CfCtrl { 243 val srcState = Vec(3, SrcState()) 244 val psrc = Vec(3, UInt(PhyRegIdxWidth.W)) 245 val pdest = UInt(PhyRegIdxWidth.W) 246 val old_pdest = UInt(PhyRegIdxWidth.W) 247 val roqIdx = new RoqPtr 248 val lqIdx = new LqPtr 249 val sqIdx = new SqPtr 250 val diffTestDebugLrScValid = Bool() 251 val debugInfo = new PerfDebugInfo 252 def needRfRPort(index: Int, rfType: Int, ignoreState: Boolean = true) : Bool = { 253 (index, rfType) match { 254 case (0, 0) => ctrl.srcType(0) === SrcType.reg && ctrl.lsrc(0) =/= 0.U && (srcState(0) === SrcState.rdy || ignoreState.B) 255 case (1, 0) => ctrl.srcType(1) === SrcType.reg && ctrl.lsrc(1) =/= 0.U && (srcState(1) === SrcState.rdy || ignoreState.B) 256 case (0, 1) => ctrl.srcType(0) === SrcType.fp && (srcState(0) === SrcState.rdy || ignoreState.B) 257 case (1, 1) => ctrl.srcType(1) === SrcType.fp && (srcState(1) === SrcState.rdy || ignoreState.B) 258 case (2, 1) => ctrl.srcType(2) === SrcType.fp && (srcState(2) === SrcState.rdy || ignoreState.B) 259 case _ => false.B 260 } 261 } 262 def srcIsReady: Vec[Bool] = { 263 VecInit(ctrl.srcType.zip(srcState).map{ case (t, s) => SrcType.isPcImm(t) || s === SrcState.rdy }) 264 } 265 def doWriteIntRf: Bool = ctrl.rfWen && ctrl.ldest =/= 0.U 266 def doWriteFpRf: Bool = ctrl.fpWen 267} 268 269class MicroOpRbExt(implicit p: Parameters) extends XSBundle { 270 val uop = new MicroOp 271 val flag = UInt(1.W) 272} 273 274class Redirect(implicit p: Parameters) extends XSBundle { 275 val roqIdx = new RoqPtr 276 val ftqIdx = new FtqPtr 277 val ftqOffset = UInt(log2Up(PredictWidth).W) 278 val level = RedirectLevel() 279 val interrupt = Bool() 280 val cfiUpdate = new CfiUpdateInfo 281 282 val stFtqIdx = new FtqPtr // for load violation predict 283 val stFtqOffset = UInt(log2Up(PredictWidth).W) 284 285 // def isUnconditional() = RedirectLevel.isUnconditional(level) 286 def flushItself() = RedirectLevel.flushItself(level) 287 // def isException() = RedirectLevel.isException(level) 288} 289 290class Dp1ToDp2IO(implicit p: Parameters) extends XSBundle { 291 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 292 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 293 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 294} 295 296class ReplayPregReq(implicit p: Parameters) extends XSBundle { 297 // NOTE: set isInt and isFp both to 'false' when invalid 298 val isInt = Bool() 299 val isFp = Bool() 300 val preg = UInt(PhyRegIdxWidth.W) 301} 302 303class DebugBundle(implicit p: Parameters) extends XSBundle { 304 val isMMIO = Bool() 305 val isPerfCnt = Bool() 306 val paddr = UInt(PAddrBits.W) 307} 308 309class ExuInput(implicit p: Parameters) extends XSBundle { 310 val uop = new MicroOp 311 val src = Vec(3, UInt((XLEN + 1).W)) 312} 313 314class ExuOutput(implicit p: Parameters) extends XSBundle { 315 val uop = new MicroOp 316 val data = UInt((XLEN + 1).W) 317 val fflags = UInt(5.W) 318 val redirectValid = Bool() 319 val redirect = new Redirect 320 val debug = new DebugBundle 321} 322 323class ExternalInterruptIO(implicit p: Parameters) extends XSBundle { 324 val mtip = Input(Bool()) 325 val msip = Input(Bool()) 326 val meip = Input(Bool()) 327} 328 329class CSRSpecialIO(implicit p: Parameters) extends XSBundle { 330 val exception = Flipped(ValidIO(new MicroOp)) 331 val isInterrupt = Input(Bool()) 332 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 333 val trapTarget = Output(UInt(VAddrBits.W)) 334 val externalInterrupt = new ExternalInterruptIO 335 val interrupt = Output(Bool()) 336} 337 338class ExceptionInfo(implicit p: Parameters) extends XSBundle { 339 val uop = new MicroOp 340 val isInterrupt = Bool() 341} 342 343class RoqCommitInfo(implicit p: Parameters) extends XSBundle { 344 val ldest = UInt(5.W) 345 val rfWen = Bool() 346 val fpWen = Bool() 347 val wflags = Bool() 348 val commitType = CommitType() 349 val pdest = UInt(PhyRegIdxWidth.W) 350 val old_pdest = UInt(PhyRegIdxWidth.W) 351 val ftqIdx = new FtqPtr 352 val ftqOffset = UInt(log2Up(PredictWidth).W) 353 354 // these should be optimized for synthesis verilog 355 val pc = UInt(VAddrBits.W) 356} 357 358class RoqCommitIO(implicit p: Parameters) extends XSBundle { 359 val isWalk = Output(Bool()) 360 val valid = Vec(CommitWidth, Output(Bool())) 361 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 362 363 def hasWalkInstr = isWalk && valid.asUInt.orR 364 365 def hasCommitInstr = !isWalk && valid.asUInt.orR 366} 367 368class RSFeedback(implicit p: Parameters) extends XSBundle { 369 val rsIdx = UInt(log2Up(IssQueSize).W) 370 val hit = Bool() 371 val flushState = Bool() 372 val sourceType = RSFeedbackType() 373} 374 375class FrontendToCtrlIO(implicit p: Parameters) extends XSBundle { 376 // to backend end 377 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 378 val fromFtq = new FtqToCtrlIO 379 // from backend 380 val redirect_cfiUpdate = Flipped(ValidIO(new Redirect)) 381 val toFtq = Flipped(new CtrlToFtqIO) 382} 383 384class TlbCsrBundle(implicit p: Parameters) extends XSBundle { 385 val satp = new Bundle { 386 val mode = UInt(4.W) // TODO: may change number to parameter 387 val asid = UInt(16.W) 388 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 389 } 390 val priv = new Bundle { 391 val mxr = Bool() 392 val sum = Bool() 393 val imode = UInt(2.W) 394 val dmode = UInt(2.W) 395 } 396 397 override def toPrintable: Printable = { 398 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 399 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 400 } 401} 402 403class SfenceBundle(implicit p: Parameters) extends XSBundle { 404 val valid = Bool() 405 val bits = new Bundle { 406 val rs1 = Bool() 407 val rs2 = Bool() 408 val addr = UInt(VAddrBits.W) 409 } 410 411 override def toPrintable: Printable = { 412 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 413 } 414} 415 416// Bundle for load violation predictor updating 417class MemPredUpdateReq(implicit p: Parameters) extends XSBundle { 418 val valid = Bool() 419 420 // wait table update 421 val waddr = UInt(MemPredPCWidth.W) 422 val wdata = Bool() // true.B by default 423 424 // store set update 425 // by default, ldpc/stpc should be xor folded 426 val ldpc = UInt(MemPredPCWidth.W) 427 val stpc = UInt(MemPredPCWidth.W) 428} 429 430class PerfInfoIO extends Bundle { 431 val clean = Input(Bool()) 432 val dump = Input(Bool()) 433} 434 435class CustomCSRCtrlIO(implicit p: Parameters) extends XSBundle { 436 // Prefetcher 437 val l1plus_pf_enable = Output(Bool()) 438 val l2_pf_enable = Output(Bool()) 439 // Labeled XiangShan 440 val dsid = Output(UInt(8.W)) // TODO: DsidWidth as parameter 441 // Load violation predictor 442 val lvpred_disable = Output(Bool()) 443 val no_spec_load = Output(Bool()) 444 val waittable_timeout = Output(UInt(5.W)) 445 // Branch predictor 446 val bp_ctrl = Output(new BPUCtrl) 447 // Memory Block 448 val sbuffer_threshold = Output(UInt(4.W)) 449 // Rename 450 val move_elim_enable = Output(Bool()) 451} 452