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