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