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