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.NewCSR.CSREvents.TargetPCBundle 36import xiangshan.backend.fu.vector.Bundles.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.debugEn, UInt(LogicRegsWidth.W)) 87 val debug_pdest = OptionWrapper(backendParams.debugEn, 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 commitType = CommitType() 109 val ftqIdx = new FtqPtr 110 val ftqOffset = UInt(log2Up(PredictWidth).W) 111 val instrSize = UInt(log2Ceil(RenameWidth + 1).W) 112 val fpWen = Bool() 113 val rfWen = Bool() 114 val loadWaitBit = Bool() // for perfEvents 115 val isMove = Bool() // for perfEvents 116 val needFlush = Bool() 117 // trace 118 val traceBlockInPipe = new TracePipe(log2Up(RenameWidth * 2)) 119 // debug_begin 120 val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W)) 121 val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W)) 122 val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(LogicRegsWidth.W)) 123 val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W)) 124 val debug_fuType = OptionWrapper(backendParams.debugEn, FuType()) 125 // debug_end 126 val dirtyFs = Bool() 127 val dirtyVs = Bool() 128 } 129 130 def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = { 131 robEntry.wflags := robEnq.wfflags 132 robEntry.commitType := robEnq.commitType 133 robEntry.ftqIdx := robEnq.ftqPtr 134 robEntry.ftqOffset := robEnq.ftqOffset 135 robEntry.isRVC := robEnq.preDecodeInfo.isRVC 136 robEntry.isVset := robEnq.isVset 137 robEntry.isHls := robEnq.isHls 138 robEntry.instrSize := robEnq.instrSize 139 robEntry.rfWen := robEnq.rfWen 140 robEntry.fpWen := robEnq.dirtyFs 141 robEntry.dirtyVs := robEnq.dirtyVs 142 robEntry.loadWaitBit := robEnq.loadWaitBit 143 robEntry.eliminatedMove := robEnq.eliminatedMove 144 // flushPipe needFlush but not exception 145 robEntry.needFlush := robEnq.hasException || robEnq.flushPipe 146 // trace 147 robEntry.traceBlockInPipe := robEnq.traceBlockInPipe 148 robEntry.debug_pc.foreach(_ := robEnq.pc) 149 robEntry.debug_instr.foreach(_ := robEnq.instr) 150 robEntry.debug_ldest.foreach(_ := robEnq.ldest) 151 robEntry.debug_pdest.foreach(_ := robEnq.pdest) 152 robEntry.debug_fuType.foreach(_ := robEnq.fuType) 153 } 154 155 def connectCommitEntry(robCommitEntry: RobCommitEntryBundle, robEntry: RobEntryBundle): Unit = { 156 robCommitEntry.walk_v := robEntry.valid 157 robCommitEntry.commit_v := robEntry.valid 158 robCommitEntry.commit_w := (robEntry.uopNum === 0.U) && (robEntry.stdWritebacked === true.B) 159 robCommitEntry.realDestSize := robEntry.realDestSize 160 robCommitEntry.interrupt_safe := robEntry.interrupt_safe 161 robCommitEntry.rfWen := robEntry.rfWen 162 robCommitEntry.fpWen := robEntry.fpWen 163 robCommitEntry.fflags := robEntry.fflags 164 robCommitEntry.wflags := robEntry.wflags 165 robCommitEntry.vxsat := robEntry.vxsat 166 robCommitEntry.isRVC := robEntry.isRVC 167 robCommitEntry.isVset := robEntry.isVset 168 robCommitEntry.isHls := robEntry.isHls 169 robCommitEntry.ftqIdx := robEntry.ftqIdx 170 robCommitEntry.ftqOffset := robEntry.ftqOffset 171 robCommitEntry.commitType := robEntry.commitType 172 robCommitEntry.instrSize := robEntry.instrSize 173 robCommitEntry.loadWaitBit := robEntry.loadWaitBit 174 robCommitEntry.isMove := robEntry.eliminatedMove 175 robCommitEntry.dirtyFs := robEntry.fpWen || robEntry.wflags 176 robCommitEntry.dirtyVs := robEntry.dirtyVs 177 robCommitEntry.needFlush := robEntry.needFlush 178 robCommitEntry.traceBlockInPipe := robEntry.traceBlockInPipe 179 robCommitEntry.debug_pc.foreach(_ := robEntry.debug_pc.get) 180 robCommitEntry.debug_instr.foreach(_ := robEntry.debug_instr.get) 181 robCommitEntry.debug_ldest.foreach(_ := robEntry.debug_ldest.get) 182 robCommitEntry.debug_pdest.foreach(_ := robEntry.debug_pdest.get) 183 robCommitEntry.debug_fuType.foreach(_ := robEntry.debug_fuType.get) 184 } 185} 186 187import RobBundles._ 188 189class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr]( 190 entries 191) with HasCircularQueuePtrHelper { 192 193 def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize) 194 195 def needFlush(redirect: Valid[Redirect]): Bool = { 196 val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx 197 redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx)) 198 } 199 200 def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR 201 202 def lineHeadPtr(implicit p: Parameters): RobPtr = { 203 val CommitWidth = p(XSCoreParamsKey).CommitWidth 204 val out = Wire(new RobPtr) 205 out.flag := this.flag 206 out.value := Cat(this.value(this.PTR_WIDTH-1, log2Up(CommitWidth)), 0.U(log2Up(CommitWidth).W)) 207 out 208 } 209 210} 211 212object RobPtr { 213 def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = { 214 val ptr = Wire(new RobPtr) 215 ptr.flag := f 216 ptr.value := v 217 ptr 218 } 219} 220 221class RobCSRIO(implicit p: Parameters) extends XSBundle { 222 val intrBitSet = Input(Bool()) 223 val trapTarget = Input(new TargetPCBundle) 224 val isXRet = Input(Bool()) 225 val wfiEvent = Input(Bool()) 226 227 val fflags = Output(Valid(UInt(5.W))) 228 val vxsat = Output(Valid(Bool())) 229 val vstart = Output(Valid(UInt(XLEN.W))) 230 val dirty_fs = Output(Bool()) 231 val dirty_vs = Output(Bool()) 232 val perfinfo = new Bundle { 233 val retiredInstr = Output(UInt(3.W)) 234 } 235} 236 237class RobLsqIO(implicit p: Parameters) extends XSBundle { 238 val lcommit = Output(UInt(log2Up(CommitWidth + 1).W)) 239 val scommit = Output(UInt(log2Up(CommitWidth + 1).W)) 240 val pendingUncacheld = Output(Bool()) 241 val pendingld = Output(Bool()) 242 val pendingst = Output(Bool()) 243 // set when vector store at the head of ROB 244 val pendingVst = Output(Bool()) 245 val commit = Output(Bool()) 246 val pendingPtr = Output(new RobPtr) 247 val pendingPtrNext = Output(new RobPtr) 248 249 val mmio = Input(Vec(LoadPipelineWidth, Bool())) 250 // Todo: what's this? 251 val uop = Input(Vec(LoadPipelineWidth, new DynInst)) 252} 253 254class RobEnqIO(implicit p: Parameters) extends XSBundle { 255 val canAccept = Output(Bool()) 256 val isEmpty = Output(Bool()) 257 // valid vector, for robIdx gen and walk 258 val needAlloc = Vec(RenameWidth, Input(Bool())) 259 val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst))) 260 val resp = Vec(RenameWidth, Output(new RobPtr)) 261} 262 263class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle { 264 val robHeadVaddr = Valid(UInt(VAddrBits.W)) 265 val robHeadPaddr = Valid(UInt(PAddrBits.W)) 266} 267 268class RobDispatchTopDownIO extends Bundle { 269 val robTrueCommit = Output(UInt(64.W)) 270 val robHeadLsIssue = Output(Bool()) 271} 272 273class RobDebugRollingIO extends Bundle { 274 val robTrueCommit = Output(UInt(64.W)) 275} 276 277class RobExceptionInfo(implicit p: Parameters) extends XSBundle { 278 // val valid = Bool() 279 val robIdx = new RobPtr 280 val ftqPtr = new FtqPtr 281 val ftqOffset = UInt(log2Up(PredictWidth).W) 282 // set 1 if there is 1 exists in exceptionVec 283 val hasException = Bool() 284 val exceptionVec = ExceptionVec() 285 val isFetchMalAddr = Bool() 286 val flushPipe = Bool() 287 val isVset = Bool() 288 val replayInst = Bool() // redirect to that inst itself 289 val singleStep = Bool() // TODO add frontend hit beneath 290 val crossPageIPFFix = Bool() 291 val trigger = TriggerAction() 292 val vstartEn = Bool() 293 val vstart = UInt(XLEN.W) 294 295 def has_exception = hasException || flushPipe || singleStep || replayInst || TriggerAction.isDmode(trigger) 296 def not_commit = hasException || singleStep || replayInst || TriggerAction.isDmode(trigger) 297 // only exceptions are allowed to writeback when enqueue 298 def can_writeback = hasException || singleStep || TriggerAction.isDmode(trigger) 299} 300 301class RobFlushInfo(implicit p: Parameters) extends XSBundle { 302 val ftqIdx = new FtqPtr 303 val robIdx = new RobPtr 304 val ftqOffset = UInt(log2Up(PredictWidth).W) 305 val replayInst = Bool() 306} 307