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