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 // data end 65 66 // trace 67 val traceBlockInPipe = new TracePipe(IretireWidthInPipe) 68 // status begin 69 val valid = Bool() 70 val fflags = UInt(5.W) 71 val mmio = Bool() 72 // store will be commited if both sta & std have been writebacked 73 val stdWritebacked = Bool() 74 val vxsat = Bool() 75 val realDestSize = UInt(log2Up(MaxUopSize + 1).W) 76 val uopNum = UInt(log2Up(MaxUopSize + 1).W) 77 val needFlush = Bool() 78 // status end 79 80 // debug_begin 81 val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W)) 82 val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W)) 83 val debug_ldest = OptionWrapper(backendParams.basicDebugEn, UInt(LogicRegsWidth.W)) 84 val debug_pdest = OptionWrapper(backendParams.basicDebugEn, UInt(PhyRegIdxWidth.W)) 85 val debug_fuType = OptionWrapper(backendParams.debugEn, FuType()) 86 // debug_end 87 88 def isWritebacked: Bool = !uopNum.orR && stdWritebacked 89 def isUopWritebacked: Bool = !uopNum.orR 90 91 } 92 93 class RobCommitEntryBundle(implicit p: Parameters) extends XSBundle { 94 val walk_v = Bool() 95 val commit_v = Bool() 96 val commit_w = Bool() 97 val realDestSize = UInt(log2Up(MaxUopSize + 1).W) 98 val interrupt_safe = Bool() 99 val wflags = Bool() 100 val fflags = UInt(5.W) 101 val vxsat = Bool() 102 val isRVC = Bool() 103 val isVset = Bool() 104 val isHls = Bool() 105 val isVls = Bool() 106 val vls = Bool() 107 val mmio = 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 needFlush = Bool() 115 // trace 116 val traceBlockInPipe = new TracePipe(IretireWidthInPipe) 117 // debug_begin 118 val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W)) 119 val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W)) 120 val debug_ldest = OptionWrapper(backendParams.basicDebugEn, UInt(LogicRegsWidth.W)) 121 val debug_pdest = OptionWrapper(backendParams.basicDebugEn, UInt(PhyRegIdxWidth.W)) 122 val debug_fuType = OptionWrapper(backendParams.debugEn, FuType()) 123 // debug_end 124 val dirtyFs = Bool() 125 val dirtyVs = Bool() 126 } 127 128 def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = { 129 robEntry.wflags := robEnq.wfflags 130 robEntry.commitType := robEnq.commitType 131 robEntry.ftqIdx := robEnq.ftqPtr 132 robEntry.ftqOffset := robEnq.ftqOffset 133 robEntry.isRVC := robEnq.preDecodeInfo.isRVC 134 robEntry.isVset := robEnq.isVset 135 robEntry.isHls := robEnq.isHls 136 robEntry.instrSize := robEnq.instrSize 137 robEntry.rfWen := robEnq.rfWen 138 robEntry.fpWen := robEnq.dirtyFs 139 robEntry.dirtyVs := robEnq.dirtyVs 140 // flushPipe needFlush but not exception 141 robEntry.needFlush := robEnq.hasException || robEnq.flushPipe 142 // trace 143 robEntry.traceBlockInPipe := robEnq.traceBlockInPipe 144 robEntry.debug_pc.foreach(_ := robEnq.pc) 145 robEntry.debug_instr.foreach(_ := robEnq.instr) 146 robEntry.debug_ldest.foreach(_ := robEnq.ldest) 147 robEntry.debug_pdest.foreach(_ := robEnq.pdest) 148 robEntry.debug_fuType.foreach(_ := robEnq.fuType) 149 } 150 151 def connectCommitEntry(robCommitEntry: RobCommitEntryBundle, robEntry: RobEntryBundle): Unit = { 152 robCommitEntry.walk_v := robEntry.valid 153 robCommitEntry.commit_v := robEntry.valid 154 robCommitEntry.commit_w := (robEntry.uopNum === 0.U) && (robEntry.stdWritebacked === true.B) 155 robCommitEntry.realDestSize := robEntry.realDestSize 156 robCommitEntry.interrupt_safe := robEntry.interrupt_safe 157 robCommitEntry.rfWen := robEntry.rfWen 158 robCommitEntry.fpWen := robEntry.fpWen 159 robCommitEntry.fflags := robEntry.fflags 160 robCommitEntry.wflags := robEntry.wflags 161 robCommitEntry.vxsat := robEntry.vxsat 162 robCommitEntry.isRVC := robEntry.isRVC 163 robCommitEntry.isVset := robEntry.isVset 164 robCommitEntry.isHls := robEntry.isHls 165 robCommitEntry.isVls := robEntry.vls 166 robCommitEntry.vls := robEntry.vls 167 robCommitEntry.mmio := robEntry.mmio 168 robCommitEntry.ftqIdx := robEntry.ftqIdx 169 robCommitEntry.ftqOffset := robEntry.ftqOffset 170 robCommitEntry.commitType := robEntry.commitType 171 robCommitEntry.instrSize := robEntry.instrSize 172 robCommitEntry.dirtyFs := robEntry.fpWen || robEntry.wflags 173 robCommitEntry.dirtyVs := robEntry.dirtyVs 174 robCommitEntry.needFlush := robEntry.needFlush 175 robCommitEntry.traceBlockInPipe := robEntry.traceBlockInPipe 176 robCommitEntry.debug_pc.foreach(_ := robEntry.debug_pc.get) 177 robCommitEntry.debug_instr.foreach(_ := robEntry.debug_instr.get) 178 robCommitEntry.debug_ldest.foreach(_ := robEntry.debug_ldest.get) 179 robCommitEntry.debug_pdest.foreach(_ := robEntry.debug_pdest.get) 180 robCommitEntry.debug_fuType.foreach(_ := robEntry.debug_fuType.get) 181 } 182} 183 184import RobBundles._ 185 186class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr]( 187 entries 188) with HasCircularQueuePtrHelper { 189 190 def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize) 191 192 def needFlush(redirect: Valid[Redirect]): Bool = { 193 val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx 194 redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx)) 195 } 196 197 def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR 198 199 def lineHeadPtr(implicit p: Parameters): RobPtr = { 200 val CommitWidth = p(XSCoreParamsKey).CommitWidth 201 val out = Wire(new RobPtr) 202 out.flag := this.flag 203 out.value := Cat(this.value(this.PTR_WIDTH-1, log2Up(CommitWidth)), 0.U(log2Up(CommitWidth).W)) 204 out 205 } 206 207} 208 209object RobPtr { 210 def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = { 211 val ptr = Wire(new RobPtr) 212 ptr.flag := f 213 ptr.value := v 214 ptr 215 } 216} 217 218class RobCSRIO(implicit p: Parameters) extends XSBundle { 219 val intrBitSet = Input(Bool()) 220 val trapTarget = Input(new TargetPCBundle) 221 val isXRet = Input(Bool()) 222 val wfiEvent = Input(Bool()) 223 val criticalErrorState = Input(Bool()) 224 225 val fflags = Output(Valid(UInt(5.W))) 226 val vxsat = Output(Valid(Bool())) 227 val vstart = Output(Valid(UInt(XLEN.W))) 228 val dirty_fs = Output(Bool()) 229 val dirty_vs = Output(Bool()) 230 val perfinfo = new Bundle { 231 val retiredInstr = Output(UInt(7.W)) 232 } 233} 234 235class RobLsqIO(implicit p: Parameters) extends XSBundle { 236 val lcommit = Output(UInt(log2Up(CommitWidth + 1).W)) 237 val scommit = Output(UInt(log2Up(CommitWidth + 1).W)) 238 val pendingMMIOld = Output(Bool()) 239 val pendingld = Output(Bool()) 240 val pendingst = Output(Bool()) 241 // set when vector store at the head of ROB 242 val pendingVst = Output(Bool()) 243 val commit = Output(Bool()) 244 val pendingPtr = Output(new RobPtr) 245 val pendingPtrNext = Output(new RobPtr) 246 247 val mmio = Input(Vec(LoadPipelineWidth, Bool())) 248 // Todo: what's this? 249 val uop = Input(Vec(LoadPipelineWidth, new DynInst)) 250} 251 252class RobEnqIO(implicit p: Parameters) extends XSBundle { 253 val canAccept = Output(Bool()) 254 val canAcceptForDispatch = 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 // This signal is valid iff currentValid is true 284 // 0: is execute exception, 1: is fetch exception 285 val isEnqExcp = 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