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