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