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