xref: /XiangShan/src/main/scala/xiangshan/backend/rob/RobBundles.scala (revision 1bc48dd1fa0af361fd194c65bad3b86349ec2903)
1/***************************************************************************************
2 * Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3 * Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4 * Copyright (c) 2020-2021 Peng Cheng Laboratory
5 *
6 * XiangShan is licensed under Mulan PSL v2.
7 * You can use this software according to the terms and conditions of the Mulan PSL v2.
8 * You may obtain a copy of Mulan PSL v2 at:
9 *          http://license.coscl.org.cn/MulanPSL2
10 *
11 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14 *
15 * See the Mulan PSL v2 for more details.
16 ***************************************************************************************/
17
18package xiangshan.backend.rob
19
20import org.chipsalliance.cde.config.Parameters
21import chisel3.{Mem, Mux, Vec, _}
22import chisel3.util._
23import difftest._
24import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
25import utility._
26import utils._
27import xiangshan._
28import xiangshan.backend.BackendParams
29import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput, UopIdx}
30import xiangshan.backend.fu.{FuConfig, FuType}
31import xiangshan.frontend.FtqPtr
32import xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr}
33import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput}
34import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfo, LsTopdownInfo}
35import xiangshan.backend.fu.NewCSR.CSREvents.TargetPCBundle
36import xiangshan.backend.fu.vector.Bundles.{Nf, VLmul, VSew, VType}
37import xiangshan.backend.rename.SnapshotGenerator
38import xiangshan.backend.trace._
39
40import scala.collection.immutable.Nil
41
42
43
44object RobBundles extends HasCircularQueuePtrHelper {
45
46  class RobEntryBundle(implicit p: Parameters) extends XSBundle {
47
48    // data begin
49    val vls = Bool()
50    // some instructions are not allowed to trigger interrupts
51    // They have side effects on the states of the processor before they write back
52    val interrupt_safe = Bool()
53    val fpWen = Bool()
54    val rfWen = Bool()
55    val wflags = Bool()
56    val dirtyVs = Bool()
57    val commitType = CommitType()
58    val ftqIdx = new FtqPtr
59    val ftqOffset = UInt(log2Up(PredictWidth).W)
60    val isRVC = Bool()
61    val isVset = Bool()
62    val isHls = Bool()
63    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
64    val loadWaitBit = Bool()    // for perfEvents
65    val eliminatedMove = Bool() // for perfEvents
66    // data end
67
68    // trace
69    val traceBlockInPipe = new TracePipe(log2Up(RenameWidth * 2))
70    // status begin
71    val valid = Bool()
72    val fflags = UInt(5.W)
73    val mmio = Bool()
74    // store will be commited if both sta & std have been writebacked
75    val stdWritebacked = Bool()
76    val vxsat = Bool()
77    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
78    val uopNum = UInt(log2Up(MaxUopSize + 1).W)
79    val commitTrigger = Bool()
80    val needFlush = Bool()
81    // status end
82
83    // debug_begin
84    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
85    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
86    val debug_ldest = OptionWrapper(backendParams.basicDebugEn, UInt(LogicRegsWidth.W))
87    val debug_pdest = OptionWrapper(backendParams.basicDebugEn, UInt(PhyRegIdxWidth.W))
88    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
89    // debug_end
90
91    def isWritebacked: Bool = !uopNum.orR && stdWritebacked
92    def isUopWritebacked: Bool = !uopNum.orR
93
94  }
95
96  class RobCommitEntryBundle(implicit p: Parameters) extends XSBundle {
97    val walk_v = Bool()
98    val commit_v = Bool()
99    val commit_w = Bool()
100    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
101    val interrupt_safe = Bool()
102    val wflags = Bool()
103    val fflags = UInt(5.W)
104    val vxsat = Bool()
105    val isRVC = Bool()
106    val isVset = Bool()
107    val isHls = Bool()
108    val isVls = Bool()
109    val commitType = CommitType()
110    val ftqIdx = new FtqPtr
111    val ftqOffset = UInt(log2Up(PredictWidth).W)
112    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
113    val fpWen = Bool()
114    val rfWen = Bool()
115    val loadWaitBit = Bool() // for perfEvents
116    val isMove = Bool()      // for perfEvents
117    val needFlush = Bool()
118    // trace
119    val traceBlockInPipe = new TracePipe(log2Up(RenameWidth * 2))
120    // debug_begin
121    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
122    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
123    val debug_ldest = OptionWrapper(backendParams.basicDebugEn, UInt(LogicRegsWidth.W))
124    val debug_pdest = OptionWrapper(backendParams.basicDebugEn, UInt(PhyRegIdxWidth.W))
125    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
126    // debug_end
127    val dirtyFs = Bool()
128    val dirtyVs = Bool()
129  }
130
131  def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = {
132    robEntry.wflags := robEnq.wfflags
133    robEntry.commitType := robEnq.commitType
134    robEntry.ftqIdx := robEnq.ftqPtr
135    robEntry.ftqOffset := robEnq.ftqOffset
136    robEntry.isRVC := robEnq.preDecodeInfo.isRVC
137    robEntry.isVset := robEnq.isVset
138    robEntry.isHls := robEnq.isHls
139    robEntry.instrSize := robEnq.instrSize
140    robEntry.rfWen := robEnq.rfWen
141    robEntry.fpWen := robEnq.dirtyFs
142    robEntry.dirtyVs := robEnq.dirtyVs
143    robEntry.loadWaitBit := robEnq.loadWaitBit
144    robEntry.eliminatedMove := robEnq.eliminatedMove
145    // flushPipe needFlush but not exception
146    robEntry.needFlush := robEnq.hasException || robEnq.flushPipe
147    // trace
148    robEntry.traceBlockInPipe := robEnq.traceBlockInPipe
149    robEntry.debug_pc.foreach(_ := robEnq.pc)
150    robEntry.debug_instr.foreach(_ := robEnq.instr)
151    robEntry.debug_ldest.foreach(_ := robEnq.ldest)
152    robEntry.debug_pdest.foreach(_ := robEnq.pdest)
153    robEntry.debug_fuType.foreach(_ := robEnq.fuType)
154  }
155
156  def connectCommitEntry(robCommitEntry: RobCommitEntryBundle, robEntry: RobEntryBundle): Unit = {
157    robCommitEntry.walk_v := robEntry.valid
158    robCommitEntry.commit_v := robEntry.valid
159    robCommitEntry.commit_w := (robEntry.uopNum === 0.U) && (robEntry.stdWritebacked === true.B)
160    robCommitEntry.realDestSize := robEntry.realDestSize
161    robCommitEntry.interrupt_safe := robEntry.interrupt_safe
162    robCommitEntry.rfWen := robEntry.rfWen
163    robCommitEntry.fpWen := robEntry.fpWen
164    robCommitEntry.fflags := robEntry.fflags
165    robCommitEntry.wflags := robEntry.wflags
166    robCommitEntry.vxsat := robEntry.vxsat
167    robCommitEntry.isRVC := robEntry.isRVC
168    robCommitEntry.isVset := robEntry.isVset
169    robCommitEntry.isHls := robEntry.isHls
170    robCommitEntry.isVls := robEntry.vls
171    robCommitEntry.ftqIdx := robEntry.ftqIdx
172    robCommitEntry.ftqOffset := robEntry.ftqOffset
173    robCommitEntry.commitType := robEntry.commitType
174    robCommitEntry.instrSize := robEntry.instrSize
175    robCommitEntry.loadWaitBit := robEntry.loadWaitBit
176    robCommitEntry.isMove := robEntry.eliminatedMove
177    robCommitEntry.dirtyFs := robEntry.fpWen || robEntry.wflags
178    robCommitEntry.dirtyVs := robEntry.dirtyVs
179    robCommitEntry.needFlush := robEntry.needFlush
180    robCommitEntry.traceBlockInPipe := robEntry.traceBlockInPipe
181    robCommitEntry.debug_pc.foreach(_ := robEntry.debug_pc.get)
182    robCommitEntry.debug_instr.foreach(_ := robEntry.debug_instr.get)
183    robCommitEntry.debug_ldest.foreach(_ := robEntry.debug_ldest.get)
184    robCommitEntry.debug_pdest.foreach(_ := robEntry.debug_pdest.get)
185    robCommitEntry.debug_fuType.foreach(_ := robEntry.debug_fuType.get)
186  }
187}
188
189import RobBundles._
190
191class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr](
192  entries
193) with HasCircularQueuePtrHelper {
194
195  def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize)
196
197  def needFlush(redirect: Valid[Redirect]): Bool = {
198    val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx
199    redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx))
200  }
201
202  def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR
203
204  def lineHeadPtr(implicit p: Parameters): RobPtr = {
205    val CommitWidth = p(XSCoreParamsKey).CommitWidth
206    val out = Wire(new RobPtr)
207    out.flag := this.flag
208    out.value := Cat(this.value(this.PTR_WIDTH-1, log2Up(CommitWidth)), 0.U(log2Up(CommitWidth).W))
209    out
210  }
211
212}
213
214object RobPtr {
215  def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = {
216    val ptr = Wire(new RobPtr)
217    ptr.flag := f
218    ptr.value := v
219    ptr
220  }
221}
222
223class RobCSRIO(implicit p: Parameters) extends XSBundle {
224  val intrBitSet = Input(Bool())
225  val trapTarget = Input(new TargetPCBundle)
226  val isXRet     = Input(Bool())
227  val wfiEvent   = Input(Bool())
228
229  val fflags     = Output(Valid(UInt(5.W)))
230  val vxsat      = Output(Valid(Bool()))
231  val vstart     = Output(Valid(UInt(XLEN.W)))
232  val dirty_fs   = Output(Bool())
233  val dirty_vs   = Output(Bool())
234  val perfinfo   = new Bundle {
235    val retiredInstr = Output(UInt(3.W))
236  }
237}
238
239class RobLsqIO(implicit p: Parameters) extends XSBundle {
240  val lcommit = Output(UInt(log2Up(CommitWidth + 1).W))
241  val scommit = Output(UInt(log2Up(CommitWidth + 1).W))
242  val pendingUncacheld = Output(Bool())
243  val pendingld = Output(Bool())
244  val pendingst = Output(Bool())
245  // set when vector store at the head of ROB
246  val pendingVst = Output(Bool())
247  val commit = Output(Bool())
248  val pendingPtr = Output(new RobPtr)
249  val pendingPtrNext = Output(new RobPtr)
250
251  val mmio = Input(Vec(LoadPipelineWidth, Bool()))
252  // Todo: what's this?
253  val uop = Input(Vec(LoadPipelineWidth, new DynInst))
254}
255
256class RobEnqIO(implicit p: Parameters) extends XSBundle {
257  val canAccept = Output(Bool())
258  val isEmpty = Output(Bool())
259  // valid vector, for robIdx gen and walk
260  val needAlloc = Vec(RenameWidth, Input(Bool()))
261  val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst)))
262  val resp = Vec(RenameWidth, Output(new RobPtr))
263}
264
265class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle {
266  val robHeadVaddr = Valid(UInt(VAddrBits.W))
267  val robHeadPaddr = Valid(UInt(PAddrBits.W))
268}
269
270class RobDispatchTopDownIO extends Bundle {
271  val robTrueCommit = Output(UInt(64.W))
272  val robHeadLsIssue = Output(Bool())
273}
274
275class RobDebugRollingIO extends Bundle {
276  val robTrueCommit = Output(UInt(64.W))
277}
278
279class RobExceptionInfo(implicit p: Parameters) extends XSBundle {
280  // val valid = Bool()
281  val robIdx = new RobPtr
282  val ftqPtr = new FtqPtr
283  val ftqOffset = UInt(log2Up(PredictWidth).W)
284  // set 1 if there is 1 exists in exceptionVec
285  val hasException = Bool()
286  val exceptionVec = ExceptionVec()
287  val isFetchMalAddr = Bool()
288  val flushPipe = Bool()
289  val isVset = Bool()
290  val replayInst = Bool() // redirect to that inst itself
291  val singleStep = Bool() // TODO add frontend hit beneath
292  val crossPageIPFFix = Bool()
293  val trigger = TriggerAction()
294  // if vstart is udpated by vector unit
295  val vstartEn = Bool()
296  val vstart = UInt(XLEN.W)
297  val vuopIdx = UopIdx()
298  val isVecLoad = Bool()
299  val isVlm = Bool()
300  val isStrided = Bool()
301  val isIndexed = Bool()
302  val isWhole = Bool()
303  val nf = Nf()
304  val vsew = VSew()
305  val veew = VSew()
306  val vlmul = VLmul()
307
308  def has_exception = hasException || flushPipe || singleStep || replayInst || TriggerAction.isDmode(trigger)
309  def not_commit = hasException || singleStep || replayInst || TriggerAction.isDmode(trigger)
310  // only exceptions are allowed to writeback when enqueue
311  def can_writeback = hasException || singleStep || TriggerAction.isDmode(trigger)
312}
313
314class RobFlushInfo(implicit p: Parameters) extends XSBundle {
315  val ftqIdx = new FtqPtr
316  val robIdx = new RobPtr
317  val ftqOffset = UInt(log2Up(PredictWidth).W)
318  val replayInst = Bool()
319}
320