xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision e2695e90ecd97aafaf904296b2890a457b6a3f01)
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.{AllFoldedHistories, CircularGlobalHistory, GlobalHistory, ShiftingGlobalHistory}
28import xiangshan.frontend.RASEntry
29import xiangshan.frontend.BPUCtrl
30import xiangshan.frontend.FtqPtr
31import xiangshan.frontend.CGHPtr
32import xiangshan.frontend.FtqRead
33import xiangshan.frontend.FtqToCtrlIO
34import utils._
35import utility._
36
37import scala.math.max
38import Chisel.experimental.chiselName
39import chipsalliance.rocketchip.config.Parameters
40import chisel3.util.BitPat.bitPatToUInt
41import chisel3.util.experimental.decode.EspressoMinimizer
42import xiangshan.backend.exu.ExuConfig
43import xiangshan.backend.fu.PMPEntry
44import xiangshan.frontend.Ftq_Redirect_SRAMEntry
45import xiangshan.frontend.AllFoldedHistories
46import xiangshan.frontend.AllAheadFoldedHistoryOldestBits
47
48class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
49  val valid = Bool()
50  val bits = gen.cloneType.asInstanceOf[T]
51
52}
53
54object ValidUndirectioned {
55  def apply[T <: Data](gen: T) = {
56    new ValidUndirectioned[T](gen)
57  }
58}
59
60object RSFeedbackType {
61  val tlbMiss = 0.U(3.W)
62  val mshrFull = 1.U(3.W)
63  val dataInvalid = 2.U(3.W)
64  val bankConflict = 3.U(3.W)
65  val ldVioCheckRedo = 4.U(3.W)
66
67  val feedbackInvalid = 7.U(3.W)
68
69  def apply() = UInt(3.W)
70}
71
72class PredictorAnswer(implicit p: Parameters) extends XSBundle {
73  val hit    = if (!env.FPGAPlatform) Bool() else UInt(0.W)
74  val taken  = if (!env.FPGAPlatform) Bool() else UInt(0.W)
75  val target = if (!env.FPGAPlatform) UInt(VAddrBits.W) else UInt(0.W)
76}
77
78class CfiUpdateInfo(implicit p: Parameters) extends XSBundle with HasBPUParameter {
79  // from backend
80  val pc = UInt(VAddrBits.W)
81  // frontend -> backend -> frontend
82  val pd = new PreDecodeInfo
83  val rasSp = UInt(log2Up(RasSize).W)
84  val rasEntry = new RASEntry
85  // val hist = new ShiftingGlobalHistory
86  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
87  val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)
88  val lastBrNumOH = UInt((numBr+1).W)
89  val ghr = UInt(UbtbGHRLength.W)
90  val histPtr = new CGHPtr
91  val specCnt = Vec(numBr, UInt(10.W))
92  // need pipeline update
93  val br_hit = Bool()
94  val predTaken = Bool()
95  val target = UInt(VAddrBits.W)
96  val taken = Bool()
97  val isMisPred = Bool()
98  val shift = UInt((log2Ceil(numBr)+1).W)
99  val addIntoHist = Bool()
100
101  def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = {
102    // this.hist := entry.ghist
103    this.folded_hist := entry.folded_hist
104    this.lastBrNumOH := entry.lastBrNumOH
105    this.afhob := entry.afhob
106    this.histPtr := entry.histPtr
107    this.rasSp := entry.rasSp
108    this.rasEntry := entry.rasTop
109    this
110  }
111}
112
113// Dequeue DecodeWidth insts from Ibuffer
114class CtrlFlow(implicit p: Parameters) extends XSBundle {
115  val instr = UInt(32.W)
116  val pc = UInt(VAddrBits.W)
117  val foldpc = UInt(MemPredPCWidth.W)
118  val exceptionVec = ExceptionVec()
119  val trigger = new TriggerCf
120  val pd = new PreDecodeInfo
121  val pred_taken = Bool()
122  val crossPageIPFFix = Bool()
123  val storeSetHit = Bool() // inst has been allocated an store set
124  val waitForRobIdx = new RobPtr // store set predicted previous store robIdx
125  // Load wait is needed
126  // load inst will not be executed until former store (predicted by mdp) addr calcuated
127  val loadWaitBit = Bool()
128  // If (loadWaitBit && loadWaitStrict), strict load wait is needed
129  // load inst will not be executed until ALL former store addr calcuated
130  val loadWaitStrict = Bool()
131  val ssid = UInt(SSIDWidth.W)
132  val ftqPtr = new FtqPtr
133  val ftqOffset = UInt(log2Up(PredictWidth).W)
134}
135
136
137class FPUCtrlSignals(implicit p: Parameters) extends XSBundle {
138  val isAddSub = Bool() // swap23
139  val typeTagIn = UInt(1.W)
140  val typeTagOut = UInt(1.W)
141  val fromInt = Bool()
142  val wflags = Bool()
143  val fpWen = Bool()
144  val fmaCmd = UInt(2.W)
145  val div = Bool()
146  val sqrt = Bool()
147  val fcvt = Bool()
148  val typ = UInt(2.W)
149  val fmt = UInt(2.W)
150  val ren3 = Bool() //TODO: remove SrcType.fp
151  val rm = UInt(3.W)
152}
153
154class VType(implicit p: Parameters) extends XSBundle {
155  val vma   = Bool()
156  val vta   = Bool()
157  val vsew = UInt(3.W)
158  val vlmul = UInt(3.W)
159}
160
161class VConfig(implicit p: Parameters) extends XSBundle {
162  val vl    = UInt(8.W)
163  val vtype = new VType
164}
165
166// Decode DecodeWidth insts at Decode Stage
167class CtrlSignals(implicit p: Parameters) extends XSBundle {
168  val debug_globalID = UInt(XLEN.W)
169  val srcType = Vec(4, SrcType())
170  val lsrc = Vec(4, UInt(6.W))
171  val ldest = UInt(6.W)
172  val fuType = FuType()
173  val fuOpType = FuOpType()
174  val rfWen = Bool()
175  val fpWen = Bool()
176  val vecWen = Bool()
177  def fpVecWen = fpWen || vecWen
178  val isXSTrap = Bool()
179  val noSpecExec = Bool() // wait forward
180  val blockBackward = Bool() // block backward
181  val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit
182  val uopSplitType = UopSplitType()
183  val selImm = SelImm()
184  val imm = UInt(ImmUnion.maxLen.W)
185  val commitType = CommitType()
186  val fpu = new FPUCtrlSignals
187  val uopIdx = UInt(log2Up(MaxUopSize).W)
188  val firstUop = Bool()
189  val lastUop = Bool()
190  val vconfig = new VConfig
191  val isMove = Bool()
192  val vm = Bool()
193  val singleStep = Bool()
194  // This inst will flush all the pipe when it is the oldest inst in ROB,
195  // then replay from this inst itself
196  val replayInst = Bool()
197
198  private def allSignals = srcType.take(3) ++ Seq(fuType, fuOpType, rfWen, fpWen, vecWen,
199    isXSTrap, noSpecExec, blockBackward, flushPipe, uopSplitType, selImm)
200
201  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]): CtrlSignals = {
202    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table, EspressoMinimizer)
203    allSignals zip decoder foreach { case (s, d) => s := d }
204    commitType := DontCare
205    this
206  }
207
208  def decode(bit: List[BitPat]): CtrlSignals = {
209    allSignals.zip(bit.map(bitPatToUInt(_))).foreach{ case (s, d) => s := d }
210    this
211  }
212
213  def isWFI: Bool = fuType === FuType.csr && fuOpType === CSROpType.wfi
214  def isSoftPrefetch: Bool = {
215    fuType === FuType.alu && fuOpType === ALUOpType.or && selImm === SelImm.IMM_I && ldest === 0.U
216  }
217  def needWriteRf: Bool = (rfWen && ldest =/= 0.U) || fpWen || vecWen
218}
219
220class CfCtrl(implicit p: Parameters) extends XSBundle {
221  val cf = new CtrlFlow
222  val ctrl = new CtrlSignals
223}
224
225class PerfDebugInfo(implicit p: Parameters) extends XSBundle {
226  val eliminatedMove = Bool()
227  // val fetchTime = UInt(XLEN.W)
228  val renameTime = UInt(XLEN.W)
229  val dispatchTime = UInt(XLEN.W)
230  val enqRsTime = UInt(XLEN.W)
231  val selectTime = UInt(XLEN.W)
232  val issueTime = UInt(XLEN.W)
233  val writebackTime = UInt(XLEN.W)
234  // val commitTime = UInt(XLEN.W)
235  val runahead_checkpoint_id = UInt(XLEN.W)
236  val tlbFirstReqTime = UInt(XLEN.W)
237  val tlbRespTime = UInt(XLEN.W) // when getting hit result (including delay in L2TLB hit)
238}
239
240// Separate LSQ
241class LSIdx(implicit p: Parameters) extends XSBundle {
242  val lqIdx = new LqPtr
243  val sqIdx = new SqPtr
244}
245
246// CfCtrl -> MicroOp at Rename Stage
247class MicroOp(implicit p: Parameters) extends CfCtrl {
248  val srcState = Vec(4, SrcState())
249  val psrc = Vec(4, UInt(PhyRegIdxWidth.W))
250  val pdest = UInt(PhyRegIdxWidth.W)
251  val old_pdest = UInt(PhyRegIdxWidth.W)
252  val robIdx = new RobPtr
253  val lqIdx = new LqPtr
254  val sqIdx = new SqPtr
255  val eliminatedMove = Bool()
256  val debugInfo = new PerfDebugInfo
257  def needRfRPort(index: Int, isFp: Boolean, ignoreState: Boolean = true) : Bool = {
258    val stateReady = srcState(index) === SrcState.rdy || ignoreState.B
259    val readReg = if (isFp) {
260      ctrl.srcType(index) === SrcType.fp
261    } else {
262      ctrl.srcType(index) === SrcType.reg && ctrl.lsrc(index) =/= 0.U
263    }
264    readReg && stateReady
265  }
266  def srcIsReady: Vec[Bool] = {
267    VecInit(ctrl.srcType.zip(srcState).map{ case (t, s) => SrcType.isPcOrImm(t) || s === SrcState.rdy })
268  }
269  def clearExceptions(
270    exceptionBits: Seq[Int] = Seq(),
271    flushPipe: Boolean = false,
272    replayInst: Boolean = false
273  ): MicroOp = {
274    cf.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B)
275    if (!flushPipe) { ctrl.flushPipe := false.B }
276    if (!replayInst) { ctrl.replayInst := false.B }
277    this
278  }
279  // Assume only the LUI instruction is decoded with IMM_U in ALU.
280  def isLUI: Bool = ctrl.selImm === SelImm.IMM_U && ctrl.fuType === FuType.alu
281  // This MicroOp is used to wakeup another uop (the successor: (psrc, srcType).
282  def wakeup(successor: Seq[(UInt, UInt)], exuCfg: ExuConfig): Seq[(Bool, Bool)] = {
283    successor.map{ case (src, srcType) =>
284      val pdestMatch = pdest === src
285      // For state: no need to check whether src is x0/imm/pc because they are always ready.
286      val rfStateMatch = if (exuCfg.readIntRf) ctrl.rfWen else false.B
287      // FIXME: divide fpMatch and vecMatch then
288      val fpMatch = if (exuCfg.readFpRf) ctrl.fpWen else false.B
289      val vecMatch = if (exuCfg.readVecRf) ctrl.vecWen else false.B
290      val allIntFpVec = exuCfg.readIntRf && exuCfg.readFpVecRf
291      val allStateMatch = Mux(SrcType.isVp(srcType), vecMatch, Mux(SrcType.isFp(srcType), fpMatch, rfStateMatch))
292      val stateCond = pdestMatch && (if (allIntFpVec) allStateMatch else rfStateMatch || fpMatch || vecMatch)
293      // For data: types are matched and int pdest is not $zero.
294      val rfDataMatch = if (exuCfg.readIntRf) ctrl.rfWen && src =/= 0.U else false.B
295      val dataCond = pdestMatch && (rfDataMatch && SrcType.isReg(srcType) || fpMatch && SrcType.isFp(srcType) || vecMatch && SrcType.isVp(srcType))
296      (stateCond, dataCond)
297    }
298  }
299  // This MicroOp is used to wakeup another uop (the successor: MicroOp).
300  def wakeup(successor: MicroOp, exuCfg: ExuConfig): Seq[(Bool, Bool)] = {
301    wakeup(successor.psrc.zip(successor.ctrl.srcType), exuCfg)
302  }
303  def isJump: Bool = FuType.isJumpExu(ctrl.fuType)
304}
305
306class XSBundleWithMicroOp(implicit p: Parameters) extends XSBundle {
307  val uop = new MicroOp
308}
309
310class MicroOpRbExt(implicit p: Parameters) extends XSBundleWithMicroOp {
311  val flag = UInt(1.W)
312}
313
314class Redirect(implicit p: Parameters) extends XSBundle {
315  val robIdx = new RobPtr
316  val ftqIdx = new FtqPtr
317  val ftqOffset = UInt(log2Up(PredictWidth).W)
318  val level = RedirectLevel()
319  val interrupt = Bool()
320  val cfiUpdate = new CfiUpdateInfo
321
322  val stFtqIdx = new FtqPtr // for load violation predict
323  val stFtqOffset = UInt(log2Up(PredictWidth).W)
324
325  val debug_runahead_checkpoint_id = UInt(64.W)
326
327  // def isUnconditional() = RedirectLevel.isUnconditional(level)
328  def flushItself() = RedirectLevel.flushItself(level)
329  // def isException() = RedirectLevel.isException(level)
330}
331
332class Dp1ToDp2IO(implicit p: Parameters) extends XSBundle {
333  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
334  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
335  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
336}
337
338class ResetPregStateReq(implicit p: Parameters) extends XSBundle {
339  // NOTE: set isInt and isFp both to 'false' when invalid
340  val isInt = Bool()
341  val isFp = Bool()
342  val preg = UInt(PhyRegIdxWidth.W)
343}
344
345class DebugBundle(implicit p: Parameters) extends XSBundle {
346  val isMMIO = Bool()
347  val isPerfCnt = Bool()
348  val paddr = UInt(PAddrBits.W)
349  val vaddr = UInt(VAddrBits.W)
350  /* add L/S inst info in EXU */
351  // val L1toL2TlbLatency = UInt(XLEN.W)
352  // val levelTlbHit = UInt(2.W)
353}
354
355class ExuInput(isVpu: Boolean = false)(implicit p: Parameters) extends XSBundleWithMicroOp {
356  val dataWidth = if (isVpu) VLEN else XLEN
357
358  val src = Vec(4, UInt(dataWidth.W))
359}
360
361class ExuOutput(isVpu: Boolean = false)(implicit p: Parameters) extends XSBundleWithMicroOp {
362  val dataWidth = if (isVpu) VLEN else XLEN
363
364  val data = UInt(dataWidth.W)
365  val fflags = UInt(5.W)
366  val vxsat = UInt(1.W)
367  val redirectValid = Bool()
368  val redirect = new Redirect
369  val debug = new DebugBundle
370}
371
372class ExternalInterruptIO(implicit p: Parameters) extends XSBundle {
373  val mtip = Input(Bool())
374  val msip = Input(Bool())
375  val meip = Input(Bool())
376  val seip = Input(Bool())
377  val debug = Input(Bool())
378}
379
380class CSRSpecialIO(implicit p: Parameters) extends XSBundle {
381  val exception = Flipped(ValidIO(new MicroOp))
382  val isInterrupt = Input(Bool())
383  val memExceptionVAddr = Input(UInt(VAddrBits.W))
384  val trapTarget = Output(UInt(VAddrBits.W))
385  val externalInterrupt = new ExternalInterruptIO
386  val interrupt = Output(Bool())
387}
388
389class ExceptionInfo(implicit p: Parameters) extends XSBundleWithMicroOp {
390  val isInterrupt = Bool()
391}
392
393class RobCommitInfo(implicit p: Parameters) extends XSBundle {
394  val ldest = UInt(6.W)
395  val rfWen = Bool()
396  val fpWen = Bool()
397  val vecWen = Bool()
398  def fpVecWen = fpWen || vecWen
399  val wflags = Bool()
400  val commitType = CommitType()
401  val pdest = UInt(PhyRegIdxWidth.W)
402  val old_pdest = UInt(PhyRegIdxWidth.W)
403  val ftqIdx = new FtqPtr
404  val ftqOffset = UInt(log2Up(PredictWidth).W)
405  val isMove = Bool()
406
407  // these should be optimized for synthesis verilog
408  val pc = UInt(VAddrBits.W)
409
410  val uopIdx = UInt(log2Up(MaxUopSize).W)
411  val vconfig = new VConfig
412}
413
414class RobCommitIO(implicit p: Parameters) extends XSBundle {
415  val isCommit = Bool()
416  val commitValid = Vec(CommitWidth, Bool())
417
418  val isWalk = Bool()
419  // valid bits optimized for walk
420  val walkValid = Vec(CommitWidth, Bool())
421
422  val info = Vec(CommitWidth, new RobCommitInfo)
423
424  def hasWalkInstr: Bool = isWalk && walkValid.asUInt.orR
425  def hasCommitInstr: Bool = isCommit && commitValid.asUInt.orR
426}
427
428class DiffCommitIO(implicit p: Parameters) extends XSBundle {
429  val isCommit = Bool()
430  val commitValid = Vec(CommitWidth * MaxUopSize, Bool())
431
432  val info = Vec(CommitWidth * MaxUopSize, new RobCommitInfo)
433
434  def hasCommitInstr: Bool = isCommit && commitValid.asUInt.orR
435}
436
437class RabCommitInfo(implicit p: Parameters) extends XSBundle {
438  val ldest = UInt(6.W)
439  val pdest = UInt(PhyRegIdxWidth.W)
440  val old_pdest = UInt(PhyRegIdxWidth.W)
441  val rfWen = Bool()
442  val fpWen = Bool()
443  val vecWen = Bool()
444}
445
446class RabCommitIO(implicit p: Parameters) extends XSBundle {
447  val isCommit = Bool()
448  val commitValid = Vec(CommitWidth, Bool())
449  val isWalk = Bool()
450  val walkValid = Vec(CommitWidth, Bool())
451  val info = Vec(CommitWidth, new RabCommitInfo)
452}
453
454class RSFeedback(implicit p: Parameters) extends XSBundle {
455  val rsIdx = UInt(log2Up(IssQueSize).W)
456  val hit = Bool()
457  val flushState = Bool()
458  val sourceType = RSFeedbackType()
459  val dataInvalidSqIdx = new SqPtr
460}
461
462class MemRSFeedbackIO(implicit p: Parameters) extends XSBundle {
463  // Note: you need to update in implicit Parameters p before imp MemRSFeedbackIO
464  // for instance: MemRSFeedbackIO()(updateP)
465  val feedbackSlow = ValidIO(new RSFeedback()) // dcache miss queue full, dtlb miss
466  val feedbackFast = ValidIO(new RSFeedback()) // bank conflict
467  val rsIdx = Input(UInt(log2Up(IssQueSize).W))
468  val isFirstIssue = Input(Bool())
469}
470
471class FrontendToCtrlIO(implicit p: Parameters) extends XSBundle {
472  // to backend end
473  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
474  val fromFtq = new FtqToCtrlIO
475  // from backend
476  val toFtq = Flipped(new CtrlToFtqIO)
477}
478
479class SatpStruct(implicit p: Parameters) extends XSBundle {
480  val mode = UInt(4.W)
481  val asid = UInt(16.W)
482  val ppn  = UInt(44.W)
483}
484
485class TlbSatpBundle(implicit p: Parameters) extends SatpStruct {
486  val changed = Bool()
487
488  def apply(satp_value: UInt): Unit = {
489    require(satp_value.getWidth == XLEN)
490    val sa = satp_value.asTypeOf(new SatpStruct)
491    mode := sa.mode
492    asid := sa.asid
493    ppn := Cat(0.U(44-PAddrBits), sa.ppn(PAddrBits-1, 0)).asUInt()
494    changed := DataChanged(sa.asid) // when ppn is changed, software need do the flush
495  }
496}
497
498class TlbCsrBundle(implicit p: Parameters) extends XSBundle {
499  val satp = new TlbSatpBundle()
500  val priv = new Bundle {
501    val mxr = Bool()
502    val sum = Bool()
503    val imode = UInt(2.W)
504    val dmode = UInt(2.W)
505  }
506
507  override def toPrintable: Printable = {
508    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
509      p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
510  }
511}
512
513class SfenceBundle(implicit p: Parameters) extends XSBundle {
514  val valid = Bool()
515  val bits = new Bundle {
516    val rs1 = Bool()
517    val rs2 = Bool()
518    val addr = UInt(VAddrBits.W)
519    val asid = UInt(AsidLength.W)
520    val flushPipe = Bool()
521  }
522
523  override def toPrintable: Printable = {
524    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}, flushPipe:${bits.flushPipe}"
525  }
526}
527
528// Bundle for load violation predictor updating
529class MemPredUpdateReq(implicit p: Parameters) extends XSBundle  {
530  val valid = Bool()
531
532  // wait table update
533  val waddr = UInt(MemPredPCWidth.W)
534  val wdata = Bool() // true.B by default
535
536  // store set update
537  // by default, ldpc/stpc should be xor folded
538  val ldpc = UInt(MemPredPCWidth.W)
539  val stpc = UInt(MemPredPCWidth.W)
540}
541
542class CustomCSRCtrlIO(implicit p: Parameters) extends XSBundle {
543  // Prefetcher
544  val l1I_pf_enable = Output(Bool())
545  val l2_pf_enable = Output(Bool())
546  val l1D_pf_enable = Output(Bool())
547  val l1D_pf_train_on_hit = Output(Bool())
548  val l1D_pf_enable_agt = Output(Bool())
549  val l1D_pf_enable_pht = Output(Bool())
550  val l1D_pf_active_threshold = Output(UInt(4.W))
551  val l1D_pf_active_stride = Output(UInt(6.W))
552  val l1D_pf_enable_stride = Output(Bool())
553  val l2_pf_store_only = Output(Bool())
554  // ICache
555  val icache_parity_enable = Output(Bool())
556  // Labeled XiangShan
557  val dsid = Output(UInt(8.W)) // TODO: DsidWidth as parameter
558  // Load violation predictor
559  val lvpred_disable = Output(Bool())
560  val no_spec_load = Output(Bool())
561  val storeset_wait_store = Output(Bool())
562  val storeset_no_fast_wakeup = Output(Bool())
563  val lvpred_timeout = Output(UInt(5.W))
564  // Branch predictor
565  val bp_ctrl = Output(new BPUCtrl)
566  // Memory Block
567  val sbuffer_threshold = Output(UInt(4.W))
568  val ldld_vio_check_enable = Output(Bool())
569  val soft_prefetch_enable = Output(Bool())
570  val cache_error_enable = Output(Bool())
571  val uncache_write_outstanding_enable = Output(Bool())
572  // Rename
573  val fusion_enable = Output(Bool())
574  val wfi_enable = Output(Bool())
575  // Decode
576  val svinval_enable = Output(Bool())
577
578  // distribute csr write signal
579  val distribute_csr = new DistributedCSRIO()
580
581  val singlestep = Output(Bool())
582  val frontend_trigger = new FrontendTdataDistributeIO()
583  val mem_trigger = new MemTdataDistributeIO()
584  val trigger_enable = Output(Vec(10, Bool()))
585}
586
587class DistributedCSRIO(implicit p: Parameters) extends XSBundle {
588  // CSR has been written by csr inst, copies of csr should be updated
589  val w = ValidIO(new Bundle {
590    val addr = Output(UInt(12.W))
591    val data = Output(UInt(XLEN.W))
592  })
593}
594
595class DistributedCSRUpdateReq(implicit p: Parameters) extends XSBundle {
596  // Request csr to be updated
597  //
598  // Note that this request will ONLY update CSR Module it self,
599  // copies of csr will NOT be updated, use it with care!
600  //
601  // For each cycle, no more than 1 DistributedCSRUpdateReq is valid
602  val w = ValidIO(new Bundle {
603    val addr = Output(UInt(12.W))
604    val data = Output(UInt(XLEN.W))
605  })
606  def apply(valid: Bool, addr: UInt, data: UInt, src_description: String) = {
607    when(valid){
608      w.bits.addr := addr
609      w.bits.data := data
610    }
611    println("Distributed CSR update req registered for " + src_description)
612  }
613}
614
615class L1CacheErrorInfo(implicit p: Parameters) extends XSBundle {
616  // L1CacheErrorInfo is also used to encode customized CACHE_ERROR CSR
617  val source = Output(new Bundle() {
618    val tag = Bool() // l1 tag array
619    val data = Bool() // l1 data array
620    val l2 = Bool()
621  })
622  val opType = Output(new Bundle() {
623    val fetch = Bool()
624    val load = Bool()
625    val store = Bool()
626    val probe = Bool()
627    val release = Bool()
628    val atom = Bool()
629  })
630  val paddr = Output(UInt(PAddrBits.W))
631
632  // report error and paddr to beu
633  // bus error unit will receive error info iff ecc_error.valid
634  val report_to_beu = Output(Bool())
635
636  // there is an valid error
637  // l1 cache error will always be report to CACHE_ERROR csr
638  val valid = Output(Bool())
639
640  def toL1BusErrorUnitInfo(): L1BusErrorUnitInfo = {
641    val beu_info = Wire(new L1BusErrorUnitInfo)
642    beu_info.ecc_error.valid := report_to_beu
643    beu_info.ecc_error.bits := paddr
644    beu_info
645  }
646}
647
648/* TODO how to trigger on next inst?
6491. If hit is determined at frontend, then set a "next instr" trap at dispatch like singlestep
6502. If it is determined at Load(meaning it must be "hit after", then it must not be a jump. So we can let it commit and set
651xret csr to pc + 4/ + 2
6522.5 The problem is to let it commit. This is the real TODO
6533. If it is load and hit before just treat it as regular load exception
654 */
655
656// This bundle carries trigger hit info along the pipeline
657// Now there are 10 triggers divided into 5 groups of 2
658// These groups are
659// (if if) (store store) (load loid) (if store) (if load)
660
661// Triggers in the same group can chain, meaning that they only
662// fire is both triggers in the group matches (the triggerHitVec bit is asserted)
663// Chaining of trigger No. (2i) and (2i+1) is indicated by triggerChainVec(i)
664// Timing of 0 means trap at current inst, 1 means trap at next inst
665// Chaining and timing and the validness of a trigger is controlled by csr
666// In two chained triggers, if they have different timing, both won't fire
667//class TriggerCf (implicit p: Parameters) extends XSBundle {
668//  val triggerHitVec = Vec(10, Bool())
669//  val triggerTiming = Vec(10, Bool())
670//  val triggerChainVec = Vec(5, Bool())
671//}
672
673class TriggerCf(implicit p: Parameters) extends XSBundle {
674  // frontend
675  val frontendHit = Vec(4, Bool())
676//  val frontendTiming = Vec(4, Bool())
677//  val frontendHitNext = Vec(4, Bool())
678
679//  val frontendException = Bool()
680  // backend
681  val backendEn = Vec(2, Bool()) // Hit(6) && chain(4) , Hit(8) && chain(4)
682  val backendHit = Vec(6, Bool())
683//  val backendTiming = Vec(6, Bool()) // trigger enable fro chain
684
685  // Two situations not allowed:
686  // 1. load data comparison
687  // 2. store chaining with store
688  def getHitFrontend = frontendHit.reduce(_ || _)
689  def getHitBackend = backendHit.reduce(_ || _)
690  def hit = getHitFrontend || getHitBackend
691  def clear(): Unit = {
692    frontendHit.foreach(_ := false.B)
693    backendEn.foreach(_ := false.B)
694    backendHit.foreach(_ := false.B)
695  }
696}
697
698// these 3 bundles help distribute trigger control signals from CSR
699// to Frontend, Load and Store.
700class FrontendTdataDistributeIO(implicit p: Parameters)  extends XSBundle {
701    val t = Valid(new Bundle {
702      val addr = Output(UInt(2.W))
703      val tdata = new MatchTriggerIO
704    })
705  }
706
707class MemTdataDistributeIO(implicit p: Parameters)  extends XSBundle {
708  val t = Valid(new Bundle {
709    val addr = Output(UInt(3.W))
710    val tdata = new MatchTriggerIO
711  })
712}
713
714class MatchTriggerIO(implicit p: Parameters) extends XSBundle {
715  val matchType = Output(UInt(2.W))
716  val select = Output(Bool())
717  val timing = Output(Bool())
718  val action = Output(Bool())
719  val chain = Output(Bool())
720  val tdata2 = Output(UInt(64.W))
721}
722