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