xref: /XiangShan/src/main/scala/xiangshan/backend/rob/RobBundles.scala (revision 887862dbb8debde8ab099befc426493834a69ee7)
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}
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.vector.Bundles.VType
36import xiangshan.backend.rename.SnapshotGenerator
37import xiangshan.backend.trace._
38
39import scala.collection.immutable.Nil
40
41
42
43object RobBundles extends HasCircularQueuePtrHelper {
44
45  class RobEntryBundle(implicit p: Parameters) extends XSBundle {
46
47    // data begin
48    val vls = Bool()
49    // some instructions are not allowed to trigger interrupts
50    // They have side effects on the states of the processor before they write back
51    val interrupt_safe = Bool()
52    val fpWen = Bool()
53    val rfWen = Bool()
54    val wflags = Bool()
55    val dirtyVs = Bool()
56    val commitType = CommitType()
57    val ftqIdx = new FtqPtr
58    val ftqOffset = UInt(log2Up(PredictWidth).W)
59    val isRVC = Bool()
60    val isVset = Bool()
61    val isHls = Bool()
62    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
63    val loadWaitBit = Bool()    // for perfEvents
64    val eliminatedMove = Bool() // for perfEvents
65    // data end
66
67    // trace
68    val traceBlockInPipe = new TracePipe(log2Up(RenameWidth * 2))
69    // status begin
70    val valid = Bool()
71    val fflags = UInt(5.W)
72    val mmio = Bool()
73    // store will be commited if both sta & std have been writebacked
74    val stdWritebacked = Bool()
75    val vxsat = Bool()
76    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
77    val uopNum = UInt(log2Up(MaxUopSize + 1).W)
78    val commitTrigger = Bool()
79    val needFlush = Bool()
80    // status end
81
82    // debug_begin
83    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
84    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
85    val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(LogicRegsWidth.W))
86    val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W))
87    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
88    // debug_end
89
90    def isWritebacked: Bool = !uopNum.orR && stdWritebacked
91    def isUopWritebacked: Bool = !uopNum.orR
92
93  }
94
95  class RobCommitEntryBundle(implicit p: Parameters) extends XSBundle {
96    val walk_v = Bool()
97    val commit_v = Bool()
98    val commit_w = Bool()
99    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
100    val interrupt_safe = Bool()
101    val wflags = Bool()
102    val fflags = UInt(5.W)
103    val vxsat = Bool()
104    val isRVC = Bool()
105    val isVset = Bool()
106    val isHls = Bool()
107    val commitType = CommitType()
108    val ftqIdx = new FtqPtr
109    val ftqOffset = UInt(log2Up(PredictWidth).W)
110    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
111    val fpWen = Bool()
112    val rfWen = Bool()
113    val loadWaitBit = Bool() // for perfEvents
114    val isMove = Bool()      // for perfEvents
115    val needFlush = Bool()
116    // trace
117    val traceBlockInPipe = new TracePipe(log2Up(RenameWidth * 2))
118    // debug_begin
119    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
120    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
121    val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(LogicRegsWidth.W))
122    val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W))
123    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
124    // debug_end
125    val dirtyFs = Bool()
126    val dirtyVs = Bool()
127  }
128
129  def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = {
130    robEntry.wflags := robEnq.wfflags
131    robEntry.commitType := robEnq.commitType
132    robEntry.ftqIdx := robEnq.ftqPtr
133    robEntry.ftqOffset := robEnq.ftqOffset
134    robEntry.isRVC := robEnq.preDecodeInfo.isRVC
135    robEntry.isVset := robEnq.isVset
136    robEntry.isHls := robEnq.isHls
137    robEntry.instrSize := robEnq.instrSize
138    robEntry.rfWen := robEnq.rfWen
139    robEntry.fpWen := robEnq.dirtyFs
140    robEntry.dirtyVs := robEnq.dirtyVs
141    robEntry.loadWaitBit := robEnq.loadWaitBit
142    robEntry.eliminatedMove := robEnq.eliminatedMove
143    // flushPipe needFlush but not exception
144    robEntry.needFlush := robEnq.hasException || robEnq.flushPipe
145    // trace
146    robEntry.traceBlockInPipe := robEnq.traceBlockInPipe
147    robEntry.debug_pc.foreach(_ := robEnq.pc)
148    robEntry.debug_instr.foreach(_ := robEnq.instr)
149    robEntry.debug_ldest.foreach(_ := robEnq.ldest)
150    robEntry.debug_pdest.foreach(_ := robEnq.pdest)
151    robEntry.debug_fuType.foreach(_ := robEnq.fuType)
152  }
153
154  def connectCommitEntry(robCommitEntry: RobCommitEntryBundle, robEntry: RobEntryBundle): Unit = {
155    robCommitEntry.walk_v := robEntry.valid
156    robCommitEntry.commit_v := robEntry.valid
157    robCommitEntry.commit_w := (robEntry.uopNum === 0.U) && (robEntry.stdWritebacked === true.B)
158    robCommitEntry.realDestSize := robEntry.realDestSize
159    robCommitEntry.interrupt_safe := robEntry.interrupt_safe
160    robCommitEntry.rfWen := robEntry.rfWen
161    robCommitEntry.fpWen := robEntry.fpWen
162    robCommitEntry.fflags := robEntry.fflags
163    robCommitEntry.wflags := robEntry.wflags
164    robCommitEntry.vxsat := robEntry.vxsat
165    robCommitEntry.isRVC := robEntry.isRVC
166    robCommitEntry.isVset := robEntry.isVset
167    robCommitEntry.isHls := robEntry.isHls
168    robCommitEntry.ftqIdx := robEntry.ftqIdx
169    robCommitEntry.ftqOffset := robEntry.ftqOffset
170    robCommitEntry.commitType := robEntry.commitType
171    robCommitEntry.instrSize := robEntry.instrSize
172    robCommitEntry.loadWaitBit := robEntry.loadWaitBit
173    robCommitEntry.isMove := robEntry.eliminatedMove
174    robCommitEntry.dirtyFs := robEntry.fpWen || robEntry.wflags
175    robCommitEntry.dirtyVs := robEntry.dirtyVs
176    robCommitEntry.needFlush := robEntry.needFlush
177    robCommitEntry.traceBlockInPipe := robEntry.traceBlockInPipe
178    robCommitEntry.debug_pc.foreach(_ := robEntry.debug_pc.get)
179    robCommitEntry.debug_instr.foreach(_ := robEntry.debug_instr.get)
180    robCommitEntry.debug_ldest.foreach(_ := robEntry.debug_ldest.get)
181    robCommitEntry.debug_pdest.foreach(_ := robEntry.debug_pdest.get)
182    robCommitEntry.debug_fuType.foreach(_ := robEntry.debug_fuType.get)
183  }
184}
185
186import RobBundles._
187
188class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr](
189  entries
190) with HasCircularQueuePtrHelper {
191
192  def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize)
193
194  def needFlush(redirect: Valid[Redirect]): Bool = {
195    val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx
196    redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx))
197  }
198
199  def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR
200
201  def lineHeadPtr(implicit p: Parameters): RobPtr = {
202    val CommitWidth = p(XSCoreParamsKey).CommitWidth
203    val out = Wire(new RobPtr)
204    out.flag := this.flag
205    out.value := Cat(this.value(this.PTR_WIDTH-1, log2Up(CommitWidth)), 0.U(log2Up(CommitWidth).W))
206    out
207  }
208
209}
210
211object RobPtr {
212  def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = {
213    val ptr = Wire(new RobPtr)
214    ptr.flag := f
215    ptr.value := v
216    ptr
217  }
218}
219
220class RobCSRIO(implicit p: Parameters) extends XSBundle {
221  val intrBitSet = Input(Bool())
222  val trapTarget = Input(UInt(VAddrBits.W))
223  val isXRet     = Input(Bool())
224  val wfiEvent   = Input(Bool())
225
226  val fflags     = Output(Valid(UInt(5.W)))
227  val vxsat      = Output(Valid(Bool()))
228  val vstart     = Output(Valid(UInt(XLEN.W)))
229  val dirty_fs   = Output(Bool())
230  val dirty_vs   = Output(Bool())
231  val perfinfo   = new Bundle {
232    val retiredInstr = Output(UInt(3.W))
233  }
234}
235
236class RobLsqIO(implicit p: Parameters) extends XSBundle {
237  val lcommit = Output(UInt(log2Up(CommitWidth + 1).W))
238  val scommit = Output(UInt(log2Up(CommitWidth + 1).W))
239  val pendingUncacheld = Output(Bool())
240  val pendingld = Output(Bool())
241  val pendingst = Output(Bool())
242  // set when vector store at the head of ROB
243  val pendingVst = Output(Bool())
244  val commit = Output(Bool())
245  val pendingPtr = Output(new RobPtr)
246  val pendingPtrNext = Output(new RobPtr)
247
248  val mmio = Input(Vec(LoadPipelineWidth, Bool()))
249  // Todo: what's this?
250  val uop = Input(Vec(LoadPipelineWidth, new DynInst))
251}
252
253class RobEnqIO(implicit p: Parameters) extends XSBundle {
254  val canAccept = Output(Bool())
255  val isEmpty = Output(Bool())
256  // valid vector, for robIdx gen and walk
257  val needAlloc = Vec(RenameWidth, Input(Bool()))
258  val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst)))
259  val resp = Vec(RenameWidth, Output(new RobPtr))
260}
261
262class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle {
263  val robHeadVaddr = Valid(UInt(VAddrBits.W))
264  val robHeadPaddr = Valid(UInt(PAddrBits.W))
265}
266
267class RobDispatchTopDownIO extends Bundle {
268  val robTrueCommit = Output(UInt(64.W))
269  val robHeadLsIssue = Output(Bool())
270}
271
272class RobDebugRollingIO extends Bundle {
273  val robTrueCommit = Output(UInt(64.W))
274}
275
276class RobExceptionInfo(implicit p: Parameters) extends XSBundle {
277  // val valid = Bool()
278  val robIdx = new RobPtr
279  val ftqPtr = new FtqPtr
280  val ftqOffset = UInt(log2Up(PredictWidth).W)
281  // set 1 if there is 1 exists in exceptionVec
282  val hasException = Bool()
283  val exceptionVec = ExceptionVec()
284  val flushPipe = Bool()
285  val isVset = Bool()
286  val replayInst = Bool() // redirect to that inst itself
287  val singleStep = Bool() // TODO add frontend hit beneath
288  val crossPageIPFFix = Bool()
289  val trigger = TriggerAction()
290  val vstartEn = Bool()
291  val vstart = UInt(XLEN.W)
292
293  def has_exception = hasException || flushPipe || singleStep || replayInst || TriggerAction.isDmode(trigger)
294  def not_commit = hasException || singleStep || replayInst || TriggerAction.isDmode(trigger)
295  // only exceptions are allowed to writeback when enqueue
296  def can_writeback = hasException || singleStep || TriggerAction.isDmode(trigger)
297}
298
299class RobFlushInfo(implicit p: Parameters) extends XSBundle {
300  val ftqIdx = new FtqPtr
301  val robIdx = new RobPtr
302  val ftqOffset = UInt(log2Up(PredictWidth).W)
303  val replayInst = Bool()
304}
305