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