1/*************************************************************************************** 2 * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3 * Copyright (c) 2020-2021 Peng Cheng Laboratory 4 * 5 * XiangShan is licensed under Mulan PSL v2. 6 * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 * You may obtain a copy of Mulan PSL v2 at: 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * 14 * See the Mulan PSL v2 for more details. 15 ***************************************************************************************/ 16 17package xiangshan.backend.rob 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3.{Mem, Mux, Vec, _} 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 24import utility._ 25import utils._ 26import xiangshan._ 27import xiangshan.backend.BackendParams 28import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput} 29import xiangshan.backend.fu.{FuConfig, FuType} 30import xiangshan.frontend.FtqPtr 31import xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr} 32import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput} 33import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfo, LsTopdownInfo} 34import xiangshan.backend.fu.vector.Bundles.VType 35import xiangshan.backend.rename.SnapshotGenerator 36 37import scala.collection.immutable.Nil 38 39 40 41object RobBundles extends HasCircularQueuePtrHelper { 42 43 class RobEntryBundle(implicit p: Parameters) extends XSBundle { 44 45 // data begin 46 val vls = Bool() 47 // some instructions are not allowed to trigger interrupts 48 // They have side effects on the states of the processor before they write back 49 val interrupt_safe = Bool() 50 val fpWen = Bool() 51 val rfWen = Bool() 52 val wflags = Bool() 53 val dirtyVs = Bool() 54 val commitType = CommitType() 55 val ftqIdx = new FtqPtr 56 val ftqOffset = UInt(log2Up(PredictWidth).W) 57 val isRVC = Bool() 58 val isVset = Bool() 59 val isHls = Bool() 60 val isVecOPF = Bool() 61 val instrSize = UInt(log2Ceil(RenameWidth + 1).W) 62 val loadWaitBit = Bool() // for perfEvents 63 val eliminatedMove = Bool() // for perfEvents 64 // data end 65 66 // status begin 67 val valid = Bool() 68 val fflags = UInt(5.W) 69 val mmio = Bool() 70 // store will be commited if both sta & std have been writebacked 71 val stdWritebacked = Bool() 72 val vxsat = Bool() 73 val realDestSize = UInt(log2Up(MaxUopSize + 1).W) 74 val uopNum = UInt(log2Up(MaxUopSize + 1).W) 75 val commitTrigger = Bool() 76 val needFlush = Bool() 77 // status end 78 79 // debug_begin 80 val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W)) 81 val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W)) 82 val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(LogicRegsWidth.W)) 83 val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W)) 84 val debug_fuType = OptionWrapper(backendParams.debugEn, FuType()) 85 // debug_end 86 87 def isWritebacked: Bool = !uopNum.orR && stdWritebacked 88 def isUopWritebacked: Bool = !uopNum.orR 89 90 } 91 92 class RobCommitEntryBundle(implicit p: Parameters) extends XSBundle { 93 val walk_v = Bool() 94 val commit_v = Bool() 95 val commit_w = Bool() 96 val realDestSize = UInt(log2Up(MaxUopSize + 1).W) 97 val interrupt_safe = Bool() 98 val wflags = Bool() 99 val fflags = UInt(5.W) 100 val vxsat = Bool() 101 val isRVC = Bool() 102 val isVset = Bool() 103 val isHls = Bool() 104 val isVecOPF = Bool() 105 val commitType = CommitType() 106 val ftqIdx = new FtqPtr 107 val ftqOffset = UInt(log2Up(PredictWidth).W) 108 val instrSize = UInt(log2Ceil(RenameWidth + 1).W) 109 val fpWen = Bool() 110 val rfWen = Bool() 111 val loadWaitBit = Bool() // for perfEvents 112 val isMove = Bool() // for perfEvents 113 val needFlush = Bool() 114 // debug_begin 115 val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W)) 116 val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W)) 117 val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(LogicRegsWidth.W)) 118 val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W)) 119 val debug_fuType = OptionWrapper(backendParams.debugEn, FuType()) 120 // debug_end 121 def dirtyFs = fpWen || isVecOPF 122 val dirtyVs = Bool() 123 } 124 125 def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = { 126 robEntry.wflags := robEnq.wfflags 127 robEntry.commitType := robEnq.commitType 128 robEntry.ftqIdx := robEnq.ftqPtr 129 robEntry.ftqOffset := robEnq.ftqOffset 130 robEntry.isRVC := robEnq.preDecodeInfo.isRVC 131 robEntry.isVset := robEnq.isVset 132 robEntry.isHls := robEnq.isHls 133 robEntry.isVecOPF := robEnq.isVecOPF 134 robEntry.instrSize := robEnq.instrSize 135 robEntry.rfWen := robEnq.rfWen 136 robEntry.fpWen := robEnq.dirtyFs 137 robEntry.dirtyVs := robEnq.dirtyVs 138 robEntry.loadWaitBit := robEnq.loadWaitBit 139 robEntry.eliminatedMove := robEnq.eliminatedMove 140 // flushPipe needFlush but not exception 141 robEntry.needFlush := robEnq.hasException || robEnq.flushPipe 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.isVecOPF := robEntry.isVecOPF 164 robCommitEntry.ftqIdx := robEntry.ftqIdx 165 robCommitEntry.ftqOffset := robEntry.ftqOffset 166 robCommitEntry.commitType := robEntry.commitType 167 robCommitEntry.instrSize := robEntry.instrSize 168 robCommitEntry.loadWaitBit := robEntry.loadWaitBit 169 robCommitEntry.isMove := robEntry.eliminatedMove 170 robCommitEntry.dirtyVs := robEntry.dirtyVs 171 robCommitEntry.needFlush := robEntry.needFlush 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(UInt(VAddrBits.W)) 217 val isXRet = Input(Bool()) 218 val wfiEvent = Input(Bool()) 219 220 val fflags = Output(Valid(UInt(5.W))) 221 val vxsat = Output(Valid(Bool())) 222 val vstart = Output(Valid(UInt(XLEN.W))) 223 val dirty_fs = Output(Bool()) 224 val dirty_vs = Output(Bool()) 225 val perfinfo = new Bundle { 226 val retiredInstr = Output(UInt(3.W)) 227 } 228} 229 230class RobLsqIO(implicit p: Parameters) extends XSBundle { 231 val lcommit = Output(UInt(log2Up(CommitWidth + 1).W)) 232 val scommit = Output(UInt(log2Up(CommitWidth + 1).W)) 233 val pendingld = Output(Bool()) 234 val pendingst = Output(Bool()) 235 // set when vector store at the head of ROB 236 val pendingVst = Output(Bool()) 237 val commit = Output(Bool()) 238 val pendingPtr = Output(new RobPtr) 239 val pendingPtrNext = Output(new RobPtr) 240 241 val mmio = Input(Vec(LoadPipelineWidth, Bool())) 242 // Todo: what's this? 243 val uop = Input(Vec(LoadPipelineWidth, new DynInst)) 244} 245 246class RobEnqIO(implicit p: Parameters) extends XSBundle { 247 val canAccept = Output(Bool()) 248 val isEmpty = Output(Bool()) 249 // valid vector, for robIdx gen and walk 250 val needAlloc = Vec(RenameWidth, Input(Bool())) 251 val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst))) 252 val resp = Vec(RenameWidth, Output(new RobPtr)) 253} 254 255class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle { 256 val robHeadVaddr = Valid(UInt(VAddrBits.W)) 257 val robHeadPaddr = Valid(UInt(PAddrBits.W)) 258} 259 260class RobDispatchTopDownIO extends Bundle { 261 val robTrueCommit = Output(UInt(64.W)) 262 val robHeadLsIssue = Output(Bool()) 263} 264 265class RobDebugRollingIO extends Bundle { 266 val robTrueCommit = Output(UInt(64.W)) 267} 268 269class RobExceptionInfo(implicit p: Parameters) extends XSBundle { 270 // val valid = Bool() 271 val robIdx = new RobPtr 272 val ftqPtr = new FtqPtr 273 val ftqOffset = UInt(log2Up(PredictWidth).W) 274 val exceptionVec = ExceptionVec() 275 val flushPipe = Bool() 276 val isVset = Bool() 277 val replayInst = Bool() // redirect to that inst itself 278 val singleStep = Bool() // TODO add frontend hit beneath 279 val crossPageIPFFix = Bool() 280 val trigger = new TriggerCf 281 val vstartEn = Bool() 282 val vstart = UInt(XLEN.W) 283 284 def has_exception = exceptionVec.asUInt.orR || flushPipe || singleStep || replayInst || trigger.canFire 285 def not_commit = exceptionVec.asUInt.orR || singleStep || replayInst || trigger.canFire 286 // only exceptions are allowed to writeback when enqueue 287 def can_writeback = exceptionVec.asUInt.orR || singleStep || trigger.canFire 288} 289 290class RobFlushInfo(implicit p: Parameters) extends XSBundle { 291 val ftqIdx = new FtqPtr 292 val robIdx = new RobPtr 293 val ftqOffset = UInt(log2Up(PredictWidth).W) 294 val replayInst = Bool() 295} 296