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 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 23import utility._ 24import utils._ 25import xiangshan.ExceptionNO._ 26import xiangshan._ 27import xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput} 28import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator} 29import xiangshan.backend.datapath.DataConfig.VAddrData 30import xiangshan.backend.decode.{DecodeStage, FusionDecoder} 31import xiangshan.backend.dispatch.{CoreDispatchTopDownIO, Dispatch, DispatchQueue} 32import xiangshan.backend.fu.PFEvent 33import xiangshan.backend.fu.vector.Bundles.{VType, Vl} 34import xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator} 35import xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr} 36import xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components} 37import xiangshan.mem.{LqPtr, LsqEnqIO} 38import xiangshan.backend.issue.{IntScheduler, FpScheduler, VfScheduler, MemScheduler} 39 40class CtrlToFtqIO(implicit p: Parameters) extends XSBundle { 41 val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo)) 42 val redirect = Valid(new Redirect) 43 val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr)) 44 val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W)) 45} 46 47class CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule { 48 override def shouldBeInlined: Boolean = false 49 50 val rob = LazyModule(new Rob(params)) 51 52 lazy val module = new CtrlBlockImp(this)(p, params) 53 54 val gpaMem = LazyModule(new GPAMem()) 55} 56 57class CtrlBlockImp( 58 override val wrapper: CtrlBlock 59)(implicit 60 p: Parameters, 61 params: BackendParams 62) extends LazyModuleImp(wrapper) 63 with HasXSParameter 64 with HasCircularQueuePtrHelper 65 with HasPerfEvents 66{ 67 val pcMemRdIndexes = new NamedIndexes(Seq( 68 "redirect" -> 1, 69 "memPred" -> 1, 70 "robFlush" -> 1, 71 "load" -> params.LduCnt, 72 "hybrid" -> params.HyuCnt, 73 "store" -> (if(EnableStorePrefetchSMS) params.StaCnt else 0) 74 )) 75 76 private val numPcMemReadForExu = params.numPcReadPort 77 private val numPcMemRead = pcMemRdIndexes.maxIdx 78 79 // now pcMem read for exu is moved to PcTargetMem (OG0) 80 println(s"pcMem read num: $numPcMemRead") 81 println(s"pcMem read num for exu: $numPcMemReadForExu") 82 83 val io = IO(new CtrlBlockIO()) 84 85 val gpaMem = wrapper.gpaMem.module 86 val decode = Module(new DecodeStage) 87 val fusionDecoder = Module(new FusionDecoder) 88 val rat = Module(new RenameTableWrapper) 89 val rename = Module(new Rename) 90 val dispatch = Module(new Dispatch) 91 val intDq0 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 0)) 92 val intDq1 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 1)) 93 val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth)) 94 val vecDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth)) 95 val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth)) 96 val redirectGen = Module(new RedirectGenerator) 97 private def hasRen: Boolean = true 98 private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC", hasRen = hasRen)) 99 private val rob = wrapper.rob.module 100 private val memCtrl = Module(new MemCtrl(params)) 101 102 private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable 103 104 private val s0_robFlushRedirect = rob.io.flushOut 105 private val s1_robFlushRedirect = Wire(Valid(new Redirect)) 106 s1_robFlushRedirect.valid := GatedValidRegNext(s0_robFlushRedirect.valid, false.B) 107 s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid) 108 109 pcMem.io.ren.get(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.valid 110 pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value 111 private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).getPc(RegEnable(s0_robFlushRedirect.bits.ftqOffset, s0_robFlushRedirect.valid)) 112 private val s3_redirectGen = redirectGen.io.stage2Redirect 113 private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen) 114 private val s2_s4_pendingRedirectValid = RegInit(false.B) 115 when (s1_s3_redirect.valid) { 116 s2_s4_pendingRedirectValid := true.B 117 }.elsewhen (GatedValidRegNext(io.frontend.toFtq.redirect.valid)) { 118 s2_s4_pendingRedirectValid := false.B 119 } 120 121 // Redirect will be RegNext at ExuBlocks and IssueBlocks 122 val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect) 123 val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect) 124 125 private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => { 126 val valid = x.valid 127 val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect)) 128 val delayed = Wire(Valid(new ExuOutput(x.bits.params))) 129 delayed.valid := GatedValidRegNext(valid && !killedByOlder) 130 delayed.bits := RegEnable(x.bits, x.valid) 131 delayed.bits.debugInfo.writebackTime := GTimer() 132 delayed 133 }).toSeq 134 val delayedNotFlushedWriteBackNeedFlush = Wire(Vec(params.allExuParams.filter(_.needExceptionGen).length, Bool())) 135 delayedNotFlushedWriteBackNeedFlush := delayedNotFlushedWriteBack.filter(_.bits.params.needExceptionGen).map{ x => 136 x.bits.exceptionVec.get.asUInt.orR || x.bits.flushPipe.getOrElse(false.B) || x.bits.replay.getOrElse(false.B) || 137 (if (x.bits.trigger.nonEmpty) x.bits.trigger.get.getBackendCanFire else false.B) 138 } 139 140 val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu) 141 val intScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[IntScheduler]) 142 val fpScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[FpScheduler]) 143 val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler]) 144 val intCanCompress = intScheWbData.filter(_.bits.params.CanCompress) 145 val i2vWbData = intScheWbData.filter(_.bits.params.writeVecRf) 146 val f2vWbData = fpScheWbData.filter(_.bits.params.writeVecRf) 147 val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu) 148 private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => { 149 val valid = x.valid 150 val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect)) 151 val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W))) 152 delayed.valid := GatedValidRegNext(valid && !killedByOlder) 153 val isIntSche = intCanCompress.contains(x) 154 val isFpSche = fpScheWbData.contains(x) 155 val isVfSche = vfScheWbData.contains(x) 156 val isMemVload = memVloadWbData.contains(x) 157 val isi2v = i2vWbData.contains(x) 158 val isf2v = f2vWbData.contains(x) 159 val canSameRobidxWbData = if(isVfSche) { 160 i2vWbData ++ f2vWbData ++ vfScheWbData 161 } else if(isi2v) { 162 intCanCompress ++ fpScheWbData ++ vfScheWbData 163 } else if (isf2v) { 164 intCanCompress ++ fpScheWbData ++ vfScheWbData 165 } else if (isIntSche) { 166 intCanCompress ++ fpScheWbData 167 } else if (isFpSche) { 168 intCanCompress ++ fpScheWbData 169 } else if (isMemVload) { 170 memVloadWbData 171 } else { 172 Seq(x) 173 } 174 val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => { 175 val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect)) 176 (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder 177 }).toSeq) 178 delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid) 179 delayed 180 }).toSeq 181 182 private val exuPredecode = VecInit( 183 delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq 184 ) 185 186 private val exuRedirects: Seq[ValidIO[Redirect]] = delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => { 187 val out = Wire(Valid(new Redirect())) 188 out.valid := x.valid && x.bits.redirect.get.valid && x.bits.redirect.get.bits.cfiUpdate.isMisPred 189 out.bits := x.bits.redirect.get.bits 190 out.bits.debugIsCtrl := true.B 191 out.bits.debugIsMemVio := false.B 192 out 193 }).toSeq 194 195 private val memViolation = io.fromMem.violation 196 val loadReplay = Wire(ValidIO(new Redirect)) 197 loadReplay.valid := GatedValidRegNext(memViolation.valid && 198 !memViolation.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect)) 199 ) 200 loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid) 201 loadReplay.bits.debugIsCtrl := false.B 202 loadReplay.bits.debugIsMemVio := true.B 203 204 pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.vld 205 pcMem.io.raddr(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.ptr.value 206 redirectGen.io.redirectPcRead.data := pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(redirectGen.io.redirectPcRead.offset, redirectGen.io.redirectPcRead.vld)) 207 pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.vld 208 pcMem.io.raddr(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.ptr.value 209 redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegEnable(redirectGen.io.memPredPcRead.offset, redirectGen.io.memPredPcRead.vld)) 210 211 for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) { 212 // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3) 213 pcMem.io.ren.get(pcMemIdx) := io.memLdPcRead(i).vld 214 pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value 215 io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memLdPcRead(i).offset, io.memLdPcRead(i).vld)) 216 } 217 218 for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) { 219 // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3) 220 pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).vld 221 pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value 222 io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).vld)) 223 } 224 225 if (EnableStorePrefetchSMS) { 226 for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) { 227 pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).vld 228 pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value 229 io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).vld)) 230 } 231 } else { 232 io.memStPcRead.foreach(_.data := 0.U) 233 } 234 235 redirectGen.io.hartId := io.fromTop.hartId 236 redirectGen.io.exuRedirect := exuRedirects.toSeq 237 redirectGen.io.exuOutPredecode := exuPredecode // guarded by exuRedirect.valid 238 redirectGen.io.loadReplay <> loadReplay 239 240 redirectGen.io.robFlush := s1_robFlushRedirect.valid 241 242 val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4) 243 val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead) 244 val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ?? 245 // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit. 246 // Flushes to frontend may be delayed by some cycles and commit before flush causes errors. 247 // Thus, we make all flush reasons to behave the same as exceptions for frontend. 248 for (i <- 0 until CommitWidth) { 249 // why flushOut: instructions with flushPipe are not commited to frontend 250 // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend. 251 val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid 252 io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit) 253 io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit) 254 } 255 io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid 256 io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits) 257 io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid 258 io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid)) 259 260 //jmp/brh 261 for (i <- 0 until NumRedirect) { 262 io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirects(i).valid && exuRedirects(i).bits.cfiUpdate.isMisPred && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead 263 io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirects(i).bits.ftqIdx 264 } 265 //loadreplay 266 io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead 267 io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx 268 //exception 269 io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead 270 io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx 271 272 io.frontend.canAccept := decode.io.canAccept 273 274 // Be careful here: 275 // T0: rob.io.flushOut, s0_robFlushRedirect 276 // T1: s1_robFlushRedirect, rob.io.exception.valid 277 // T2: csr.redirect.valid 278 // T3: csr.exception.valid 279 // T4: csr.trapTarget 280 // T5: ctrlBlock.trapTarget 281 // T6: io.frontend.toFtq.stage2Redirect.valid 282 val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(), 283 s1_robFlushPc, // replay inst 284 s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe 285 ), s1_robFlushRedirect.valid) 286 private val s2_csrIsXRet = io.robio.csr.isXRet 287 private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4) 288 private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget 289 290 val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc) 291 when (s6_flushFromRobValid) { 292 io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush 293 io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead) 294 } 295 296 for (i <- 0 until DecodeWidth) { 297 gpaMem.io.fromIFU := io.frontend.fromIfu 298 gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid 299 gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr 300 gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset 301 } 302 303 // vtype commit 304 decode.io.isResumeVType := rob.io.toDecode.isResumeVType 305 decode.io.commitVType := rob.io.toDecode.commitVType 306 decode.io.walkVType := rob.io.toDecode.walkVType 307 308 // spec vtype, from vtypegen to vtpebuffer 309 rob.io.fromDecode.lastSpecVType := decode.io.lastSpecVType 310 rob.io.fromDecode.specVtype := decode.io.specVtype 311 312 decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid 313 decode.io.vtypeRedirect := s1_s3_redirect.valid 314 315 decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) => 316 decodeIn.valid := frontendCf.valid 317 frontendCf.ready := decodeIn.ready 318 decodeIn.bits.connectCtrlFlow(frontendCf.bits) 319 } 320 decode.io.csrCtrl := RegNext(io.csrCtrl) 321 decode.io.intRat <> rat.io.intReadPorts 322 decode.io.fpRat <> rat.io.fpReadPorts 323 decode.io.vecRat <> rat.io.vecReadPorts 324 decode.io.v0Rat <> rat.io.v0ReadPorts 325 decode.io.vlRat <> rat.io.vlReadPorts 326 decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo 327 decode.io.stallReason.in <> io.frontend.stallReason 328 329 // snapshot check 330 class CFIRobIdx extends Bundle { 331 val robIdx = Vec(RenameWidth, new RobPtr) 332 val isCFI = Vec(RenameWidth, Bool()) 333 } 334 val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR 335 val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx))) 336 snpt.io.enq := genSnapshot 337 snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx) 338 snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot) 339 snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit && 340 Cat(rob.io.commits.commitValid.zip(rob.io.commits.robIdx).map(x => x._1 && x._2 === snpt.io.snapshots(snpt.io.deqPtr.value).robIdx.head)).orR 341 snpt.io.redirect := s1_s3_redirect.valid 342 val flushVec = VecInit(snpt.io.snapshots.map { snapshot => 343 val notCFIMask = snapshot.isCFI.map(~_) 344 val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value) 345 val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _)) 346 s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR 347 }) 348 val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B)) 349 snpt.io.flushVec := flushVecNext 350 351 val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx => 352 snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head || 353 !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head) 354 ).reduceTree(_ || _) 355 val snptSelect = MuxCase( 356 0.U(log2Ceil(RenameSnapshotNum).W), 357 (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx => 358 (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head || 359 !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx) 360 ) 361 ) 362 363 rob.io.snpt.snptEnq := DontCare 364 rob.io.snpt.snptDeq := snpt.io.deq 365 rob.io.snpt.useSnpt := useSnpt 366 rob.io.snpt.snptSelect := snptSelect 367 rob.io.snpt.flushVec := flushVecNext 368 rat.io.snpt.snptEnq := genSnapshot 369 rat.io.snpt.snptDeq := snpt.io.deq 370 rat.io.snpt.useSnpt := useSnpt 371 rat.io.snpt.snptSelect := snptSelect 372 rat.io.snpt.flushVec := flushVec 373 374 val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault)) 375 // fusion decoder 376 for (i <- 0 until DecodeWidth) { 377 fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion) 378 fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr 379 if (i > 0) { 380 fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready 381 } 382 } 383 384 private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst))) 385 386 for (i <- 0 until RenameWidth) { 387 PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready, 388 s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule")) 389 390 decodePipeRename(i).ready := rename.io.in(i).ready 391 rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i) 392 rename.io.in(i).bits := decodePipeRename(i).bits 393 } 394 395 for (i <- 0 until RenameWidth - 1) { 396 fusionDecoder.io.dec(i) := decodePipeRename(i).bits 397 rename.io.fusionInfo(i) := fusionDecoder.io.info(i) 398 399 // update the first RenameWidth - 1 instructions 400 decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire 401 when (fusionDecoder.io.out(i).valid) { 402 fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits) 403 // TODO: remove this dirty code for ftq update 404 val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value 405 val ftqOffset0 = rename.io.in(i).bits.ftqOffset 406 val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset 407 val ftqOffsetDiff = ftqOffset1 - ftqOffset0 408 val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U 409 val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U 410 val cond3 = !sameFtqPtr && ftqOffset1 === 0.U 411 val cond4 = !sameFtqPtr && ftqOffset1 === 1.U 412 rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U))) 413 XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n") 414 } 415 416 } 417 418 // memory dependency predict 419 // when decode, send fold pc to mdp 420 private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool())) 421 private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W))) 422 for (i <- 0 until DecodeWidth) { 423 mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire) 424 mdpFlodPcVec(i) := Mux( 425 decode.io.out(i).fire, 426 decode.io.in(i).bits.foldpc, 427 rename.io.in(i).bits.foldpc 428 ) 429 } 430 431 // currently, we only update mdp info when isReplay 432 memCtrl.io.redirect := s1_s3_redirect 433 memCtrl.io.csrCtrl := io.csrCtrl // RegNext in memCtrl 434 memCtrl.io.stIn := io.fromMem.stIn // RegNext in memCtrl 435 memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate // RegNext in memCtrl 436 memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld 437 memCtrl.io.mdpFlodPcVec := mdpFlodPcVec 438 memCtrl.io.dispatchLFSTio <> dispatch.io.lfst 439 440 rat.io.redirect := s1_s3_redirect.valid 441 rat.io.rabCommits := rob.io.rabCommits 442 rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get) 443 rat.io.intRenamePorts := rename.io.intRenamePorts 444 rat.io.fpRenamePorts := rename.io.fpRenamePorts 445 rat.io.vecRenamePorts := rename.io.vecRenamePorts 446 rat.io.v0RenamePorts := rename.io.v0RenamePorts 447 rat.io.vlRenamePorts := rename.io.vlRenamePorts 448 449 rename.io.redirect := s1_s3_redirect 450 rename.io.rabCommits := rob.io.rabCommits 451 rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) => 452 RegEnable(waittable2rename, decodeOut.fire) 453 } 454 rename.io.ssit := memCtrl.io.ssit2Rename 455 rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data)))) 456 rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data)))) 457 rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data)))) 458 rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data))) 459 rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data))) 460 rename.io.int_need_free := rat.io.int_need_free 461 rename.io.int_old_pdest := rat.io.int_old_pdest 462 rename.io.fp_old_pdest := rat.io.fp_old_pdest 463 rename.io.vec_old_pdest := rat.io.vec_old_pdest 464 rename.io.v0_old_pdest := rat.io.v0_old_pdest 465 rename.io.vl_old_pdest := rat.io.vl_old_pdest 466 rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get) 467 rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get) 468 rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get) 469 rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get) 470 rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get) 471 rename.io.stallReason.in <> decode.io.stallReason.out 472 rename.io.snpt.snptEnq := DontCare 473 rename.io.snpt.snptDeq := snpt.io.deq 474 rename.io.snpt.useSnpt := useSnpt 475 rename.io.snpt.snptSelect := snptSelect 476 rename.io.snptIsFull := snpt.io.valids.asUInt.andR 477 rename.io.snpt.flushVec := flushVecNext 478 rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr) 479 rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head 480 481 val renameOut = Wire(chiselTypeOf(rename.io.out)) 482 renameOut <> rename.io.out 483 // pass all snapshot in the first element for correctness of blockBackward 484 renameOut.tail.foreach(_.bits.snapshot := false.B) 485 renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr), 486 false.B, 487 Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR 488 ) 489 490 // pipeline between rename and dispatch 491 PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch") 492 dispatch.io.intIQValidNumVec := io.intIQValidNumVec 493 dispatch.io.fpIQValidNumVec := io.fpIQValidNumVec 494 dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num 495 dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num 496 dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num 497 dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num 498 499 dispatch.io.hartId := io.fromTop.hartId 500 dispatch.io.redirect := s1_s3_redirect 501 dispatch.io.enqRob <> rob.io.enq 502 dispatch.io.robHead := rob.io.debugRobHead 503 dispatch.io.stallReason <> rename.io.stallReason.out 504 dispatch.io.lqCanAccept := io.lqCanAccept 505 dispatch.io.sqCanAccept := io.sqCanAccept 506 dispatch.io.robHeadNotReady := rob.io.headNotReady 507 dispatch.io.robFull := rob.io.robFull 508 dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep) 509 510 intDq0.io.enq <> dispatch.io.toIntDq0 511 intDq0.io.redirect <> s2_s4_redirect 512 intDq1.io.enq <> dispatch.io.toIntDq1 513 intDq1.io.redirect <> s2_s4_redirect 514 515 fpDq.io.enq <> dispatch.io.toFpDq 516 fpDq.io.redirect <> s2_s4_redirect 517 518 vecDq.io.enq <> dispatch.io.toVecDq 519 vecDq.io.redirect <> s2_s4_redirect 520 521 lsDq.io.enq <> dispatch.io.toLsDq 522 lsDq.io.redirect <> s2_s4_redirect 523 524 io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq) 525 io.toIssueBlock.fpUops <> fpDq.io.deq 526 io.toIssueBlock.vfUops <> vecDq.io.deq 527 io.toIssueBlock.memUops <> lsDq.io.deq 528 io.toIssueBlock.allocPregs <> dispatch.io.allocPregs 529 io.toIssueBlock.flush <> s2_s4_redirect 530 531 pcMem.io.wen.head := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen) 532 pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen) 533 pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen) 534 535 io.toDataPath.flush := s2_s4_redirect 536 io.toExuBlock.flush := s2_s4_redirect 537 538 539 rob.io.hartId := io.fromTop.hartId 540 rob.io.redirect := s1_s3_redirect 541 rob.io.writeback := delayedNotFlushedWriteBack 542 rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums) 543 rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush 544 rob.io.readGPAMemData := gpaMem.io.exceptionReadData 545 546 io.redirect := s1_s3_redirect 547 548 // rob to int block 549 io.robio.csr <> rob.io.csr 550 // When wfi is disabled, it will not block ROB commit. 551 rob.io.csr.wfiEvent := io.robio.csr.wfiEvent 552 rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable 553 554 io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5) 555 556 io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr) 557 io.robio.exception := rob.io.exception 558 io.robio.exception.bits.pc := s1_robFlushPc 559 560 // rob to mem block 561 io.robio.lsq <> rob.io.lsq 562 563 io.debug_int_rat .foreach(_ := rat.io.diff_int_rat.get) 564 io.debug_fp_rat .foreach(_ := rat.io.diff_fp_rat.get) 565 io.debug_vec_rat .foreach(_ := rat.io.diff_vec_rat.get) 566 io.debug_v0_rat.foreach(_ := rat.io.diff_v0_rat.get) 567 io.debug_vl_rat.foreach(_ := rat.io.diff_vl_rat.get) 568 569 rob.io.debug_ls := io.robio.debug_ls 570 rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue 571 rob.io.lsTopdownInfo := io.robio.lsTopdownInfo 572 rob.io.debugEnqLsq := io.debugEnqLsq 573 574 io.robio.robDeqPtr := rob.io.robDeqPtr 575 576 // rob to backend 577 io.robio.commitVType := rob.io.toDecode.commitVType 578 // exu block to decode 579 decode.io.vsetvlVType := io.toDecode.vsetvlVType 580 // backend to decode 581 decode.io.vstart := io.toDecode.vstart 582 // backend to rob 583 rob.io.vstartIsZero := io.toDecode.vstart === 0.U 584 585 io.debugTopDown.fromRob := rob.io.debugTopDown.toCore 586 dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch 587 dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore 588 io.debugRolling := rob.io.debugRolling 589 590 io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull) 591 io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull) 592 io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull) 593 io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull) 594 595 val pfevent = Module(new PFEvent) 596 pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr) 597 val csrevents = pfevent.io.hpmevent.slice(8,16) 598 599 val perfinfo = IO(new Bundle(){ 600 val perfEventsRs = Input(Vec(params.IqCnt, new PerfEvent)) 601 val perfEventsEu0 = Input(Vec(6, new PerfEvent)) 602 val perfEventsEu1 = Input(Vec(6, new PerfEvent)) 603 }) 604 605 val perfFromUnits = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents) 606 val perfFromIO = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++ 607 perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++ 608 perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value)) 609 val perfBlock = Seq() 610 // let index = 0 be no event 611 val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock 612 613 if (printEventCoding) { 614 for (((name, inc), i) <- allPerfEvents.zipWithIndex) { 615 println("CtrlBlock perfEvents Set", name, inc, i) 616 } 617 } 618 619 val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent)) 620 val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents 621 generatePerfEvent() 622} 623 624class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle { 625 val fromTop = new Bundle { 626 val hartId = Input(UInt(8.W)) 627 } 628 val toTop = new Bundle { 629 val cpuHalt = Output(Bool()) 630 } 631 val frontend = Flipped(new FrontendToCtrlIO()) 632 val toIssueBlock = new Bundle { 633 val flush = ValidIO(new Redirect) 634 val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq)) 635 val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst)) 636 val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst)) 637 val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst)) 638 val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst)) 639 } 640 val toDataPath = new Bundle { 641 val flush = ValidIO(new Redirect) 642 } 643 val toExuBlock = new Bundle { 644 val flush = ValidIO(new Redirect) 645 } 646 val intIQValidNumVec = Input(MixedVec(params.genIntIQValidNumBundle)) 647 val fpIQValidNumVec = Input(MixedVec(params.genFpIQValidNumBundle)) 648 val fromWB = new Bundle { 649 val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles)) 650 } 651 val redirect = ValidIO(new Redirect) 652 val fromMem = new Bundle { 653 val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx 654 val violation = Flipped(ValidIO(new Redirect)) 655 } 656 val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 657 val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 658 val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 659 660 val csrCtrl = Input(new CustomCSRCtrlIO) 661 val robio = new Bundle { 662 val csr = new RobCSRIO 663 val exception = ValidIO(new ExceptionInfo) 664 val lsq = new RobLsqIO 665 val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo)) 666 val debug_ls = Input(new DebugLSIO()) 667 val robHeadLsIssue = Input(Bool()) 668 val robDeqPtr = Output(new RobPtr) 669 val commitVType = new Bundle { 670 val vtype = Output(ValidIO(VType())) 671 val hasVsetvl = Output(Bool()) 672 } 673 } 674 675 val toDecode = new Bundle { 676 val vsetvlVType = Input(VType()) 677 val vstart = Input(Vl()) 678 } 679 680 val perfInfo = Output(new Bundle{ 681 val ctrlInfo = new Bundle { 682 val robFull = Bool() 683 val intdqFull = Bool() 684 val fpdqFull = Bool() 685 val lsdqFull = Bool() 686 } 687 }) 688 val debug_int_rat = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 689 val debug_fp_rat = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 690 val debug_vec_rat = if (params.debugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None 691 val debug_v0_rat = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None 692 val debug_vl_rat = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None 693 694 val sqCanAccept = Input(Bool()) 695 val lqCanAccept = Input(Bool()) 696 697 val debugTopDown = new Bundle { 698 val fromRob = new RobCoreTopDownIO 699 val fromCore = new CoreDispatchTopDownIO 700 } 701 val debugRolling = new RobDebugRollingIO 702 val debugEnqLsq = Input(new LsqEnqIO) 703} 704 705class NamedIndexes(namedCnt: Seq[(String, Int)]) { 706 require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name") 707 708 val maxIdx = namedCnt.map(_._2).sum 709 val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i => 710 val begin = namedCnt.slice(0, i).map(_._2).sum 711 val end = begin + namedCnt(i)._2 712 (namedCnt(i)._1, (begin, end)) 713 }.toMap 714 715 def apply(name: String): Seq[Int] = { 716 require(nameRangeMap.contains(name)) 717 nameRangeMap(name)._1 until nameRangeMap(name)._2 718 } 719} 720