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 decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid 309 decode.io.vtypeRedirect := s1_s3_redirect.valid 310 311 decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) => 312 decodeIn.valid := frontendCf.valid 313 frontendCf.ready := decodeIn.ready 314 decodeIn.bits.connectCtrlFlow(frontendCf.bits) 315 } 316 decode.io.csrCtrl := RegNext(io.csrCtrl) 317 decode.io.intRat <> rat.io.intReadPorts 318 decode.io.fpRat <> rat.io.fpReadPorts 319 decode.io.vecRat <> rat.io.vecReadPorts 320 decode.io.v0Rat <> rat.io.v0ReadPorts 321 decode.io.vlRat <> rat.io.vlReadPorts 322 decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo 323 decode.io.stallReason.in <> io.frontend.stallReason 324 325 // snapshot check 326 class CFIRobIdx extends Bundle { 327 val robIdx = Vec(RenameWidth, new RobPtr) 328 val isCFI = Vec(RenameWidth, Bool()) 329 } 330 val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR 331 val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx))) 332 snpt.io.enq := genSnapshot 333 snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx) 334 snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot) 335 snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit && 336 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 337 snpt.io.redirect := s1_s3_redirect.valid 338 val flushVec = VecInit(snpt.io.snapshots.map { snapshot => 339 val notCFIMask = snapshot.isCFI.map(~_) 340 val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value) 341 val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _)) 342 s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR 343 }) 344 val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B)) 345 snpt.io.flushVec := flushVecNext 346 347 val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx => 348 snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head || 349 !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head) 350 ).reduceTree(_ || _) 351 val snptSelect = MuxCase( 352 0.U(log2Ceil(RenameSnapshotNum).W), 353 (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx => 354 (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head || 355 !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx) 356 ) 357 ) 358 359 rob.io.snpt.snptEnq := DontCare 360 rob.io.snpt.snptDeq := snpt.io.deq 361 rob.io.snpt.useSnpt := useSnpt 362 rob.io.snpt.snptSelect := snptSelect 363 rob.io.snpt.flushVec := flushVecNext 364 rat.io.snpt.snptEnq := genSnapshot 365 rat.io.snpt.snptDeq := snpt.io.deq 366 rat.io.snpt.useSnpt := useSnpt 367 rat.io.snpt.snptSelect := snptSelect 368 rat.io.snpt.flushVec := flushVec 369 370 val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault)) 371 // fusion decoder 372 for (i <- 0 until DecodeWidth) { 373 fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion) 374 fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr 375 if (i > 0) { 376 fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready 377 } 378 } 379 380 private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst))) 381 382 for (i <- 0 until RenameWidth) { 383 PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready, 384 s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule")) 385 386 decodePipeRename(i).ready := rename.io.in(i).ready 387 rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i) 388 rename.io.in(i).bits := decodePipeRename(i).bits 389 } 390 391 for (i <- 0 until RenameWidth - 1) { 392 fusionDecoder.io.dec(i) := decodePipeRename(i).bits 393 rename.io.fusionInfo(i) := fusionDecoder.io.info(i) 394 395 // update the first RenameWidth - 1 instructions 396 decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire 397 when (fusionDecoder.io.out(i).valid) { 398 fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits) 399 // TODO: remove this dirty code for ftq update 400 val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value 401 val ftqOffset0 = rename.io.in(i).bits.ftqOffset 402 val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset 403 val ftqOffsetDiff = ftqOffset1 - ftqOffset0 404 val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U 405 val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U 406 val cond3 = !sameFtqPtr && ftqOffset1 === 0.U 407 val cond4 = !sameFtqPtr && ftqOffset1 === 1.U 408 rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U))) 409 XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n") 410 } 411 412 } 413 414 // memory dependency predict 415 // when decode, send fold pc to mdp 416 private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool())) 417 private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W))) 418 for (i <- 0 until DecodeWidth) { 419 mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire) 420 mdpFlodPcVec(i) := Mux( 421 decode.io.out(i).fire, 422 decode.io.in(i).bits.foldpc, 423 rename.io.in(i).bits.foldpc 424 ) 425 } 426 427 // currently, we only update mdp info when isReplay 428 memCtrl.io.redirect := s1_s3_redirect 429 memCtrl.io.csrCtrl := io.csrCtrl // RegNext in memCtrl 430 memCtrl.io.stIn := io.fromMem.stIn // RegNext in memCtrl 431 memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate // RegNext in memCtrl 432 memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld 433 memCtrl.io.mdpFlodPcVec := mdpFlodPcVec 434 memCtrl.io.dispatchLFSTio <> dispatch.io.lfst 435 436 rat.io.redirect := s1_s3_redirect.valid 437 rat.io.rabCommits := rob.io.rabCommits 438 rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get) 439 rat.io.intRenamePorts := rename.io.intRenamePorts 440 rat.io.fpRenamePorts := rename.io.fpRenamePorts 441 rat.io.vecRenamePorts := rename.io.vecRenamePorts 442 rat.io.v0RenamePorts := rename.io.v0RenamePorts 443 rat.io.vlRenamePorts := rename.io.vlRenamePorts 444 445 rename.io.redirect := s1_s3_redirect 446 rename.io.rabCommits := rob.io.rabCommits 447 rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) => 448 RegEnable(waittable2rename, decodeOut.fire) 449 } 450 rename.io.ssit := memCtrl.io.ssit2Rename 451 rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data)))) 452 rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data)))) 453 rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data)))) 454 rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data))) 455 rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data))) 456 rename.io.int_need_free := rat.io.int_need_free 457 rename.io.int_old_pdest := rat.io.int_old_pdest 458 rename.io.fp_old_pdest := rat.io.fp_old_pdest 459 rename.io.vec_old_pdest := rat.io.vec_old_pdest 460 rename.io.v0_old_pdest := rat.io.v0_old_pdest 461 rename.io.vl_old_pdest := rat.io.vl_old_pdest 462 rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get) 463 rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get) 464 rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get) 465 rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get) 466 rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get) 467 rename.io.stallReason.in <> decode.io.stallReason.out 468 rename.io.snpt.snptEnq := DontCare 469 rename.io.snpt.snptDeq := snpt.io.deq 470 rename.io.snpt.useSnpt := useSnpt 471 rename.io.snpt.snptSelect := snptSelect 472 rename.io.snptIsFull := snpt.io.valids.asUInt.andR 473 rename.io.snpt.flushVec := flushVecNext 474 rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr) 475 rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head 476 477 val renameOut = Wire(chiselTypeOf(rename.io.out)) 478 renameOut <> rename.io.out 479 // pass all snapshot in the first element for correctness of blockBackward 480 renameOut.tail.foreach(_.bits.snapshot := false.B) 481 renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr), 482 false.B, 483 Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR 484 ) 485 486 // pipeline between rename and dispatch 487 PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch") 488 dispatch.io.intIQValidNumVec := io.intIQValidNumVec 489 dispatch.io.fpIQValidNumVec := io.fpIQValidNumVec 490 dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num 491 dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num 492 dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num 493 dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num 494 495 dispatch.io.hartId := io.fromTop.hartId 496 dispatch.io.redirect := s1_s3_redirect 497 dispatch.io.enqRob <> rob.io.enq 498 dispatch.io.robHead := rob.io.debugRobHead 499 dispatch.io.stallReason <> rename.io.stallReason.out 500 dispatch.io.lqCanAccept := io.lqCanAccept 501 dispatch.io.sqCanAccept := io.sqCanAccept 502 dispatch.io.robHeadNotReady := rob.io.headNotReady 503 dispatch.io.robFull := rob.io.robFull 504 dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep) 505 506 intDq0.io.enq <> dispatch.io.toIntDq0 507 intDq0.io.redirect <> s2_s4_redirect 508 intDq1.io.enq <> dispatch.io.toIntDq1 509 intDq1.io.redirect <> s2_s4_redirect 510 511 fpDq.io.enq <> dispatch.io.toFpDq 512 fpDq.io.redirect <> s2_s4_redirect 513 514 vecDq.io.enq <> dispatch.io.toVecDq 515 vecDq.io.redirect <> s2_s4_redirect 516 517 lsDq.io.enq <> dispatch.io.toLsDq 518 lsDq.io.redirect <> s2_s4_redirect 519 520 io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq) 521 io.toIssueBlock.fpUops <> fpDq.io.deq 522 io.toIssueBlock.vfUops <> vecDq.io.deq 523 io.toIssueBlock.memUops <> lsDq.io.deq 524 io.toIssueBlock.allocPregs <> dispatch.io.allocPregs 525 io.toIssueBlock.flush <> s2_s4_redirect 526 527 pcMem.io.wen.head := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen) 528 pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen) 529 pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen) 530 531 io.toDataPath.flush := s2_s4_redirect 532 io.toExuBlock.flush := s2_s4_redirect 533 534 535 rob.io.hartId := io.fromTop.hartId 536 rob.io.redirect := s1_s3_redirect 537 rob.io.writeback := delayedNotFlushedWriteBack 538 rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums) 539 rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush 540 rob.io.readGPAMemData := gpaMem.io.exceptionReadData 541 542 io.redirect := s1_s3_redirect 543 544 // rob to int block 545 io.robio.csr <> rob.io.csr 546 // When wfi is disabled, it will not block ROB commit. 547 rob.io.csr.wfiEvent := io.robio.csr.wfiEvent 548 rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable 549 550 io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5) 551 552 io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr) 553 io.robio.exception := rob.io.exception 554 io.robio.exception.bits.pc := s1_robFlushPc 555 556 // rob to mem block 557 io.robio.lsq <> rob.io.lsq 558 559 io.debug_int_rat .foreach(_ := rat.io.diff_int_rat.get) 560 io.debug_fp_rat .foreach(_ := rat.io.diff_fp_rat.get) 561 io.debug_vec_rat .foreach(_ := rat.io.diff_vec_rat.get) 562 io.debug_v0_rat.foreach(_ := rat.io.diff_v0_rat.get) 563 io.debug_vl_rat.foreach(_ := rat.io.diff_vl_rat.get) 564 565 rob.io.debug_ls := io.robio.debug_ls 566 rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue 567 rob.io.lsTopdownInfo := io.robio.lsTopdownInfo 568 rob.io.debugEnqLsq := io.debugEnqLsq 569 570 io.robio.robDeqPtr := rob.io.robDeqPtr 571 572 // rob to backend 573 io.robio.commitVType := rob.io.toDecode.commitVType 574 // exu block to decode 575 decode.io.vsetvlVType := io.toDecode.vsetvlVType 576 // backend to decode 577 decode.io.vstart := io.toDecode.vstart 578 // backend to rob 579 rob.io.vstartIsZero := io.toDecode.vstart === 0.U 580 581 io.debugTopDown.fromRob := rob.io.debugTopDown.toCore 582 dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch 583 dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore 584 io.debugRolling := rob.io.debugRolling 585 586 io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull) 587 io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull) 588 io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull) 589 io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull) 590 591 val pfevent = Module(new PFEvent) 592 pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr) 593 val csrevents = pfevent.io.hpmevent.slice(8,16) 594 595 val perfinfo = IO(new Bundle(){ 596 val perfEventsRs = Input(Vec(params.IqCnt, new PerfEvent)) 597 val perfEventsEu0 = Input(Vec(6, new PerfEvent)) 598 val perfEventsEu1 = Input(Vec(6, new PerfEvent)) 599 }) 600 601 val perfFromUnits = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents) 602 val perfFromIO = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++ 603 perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++ 604 perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value)) 605 val perfBlock = Seq() 606 // let index = 0 be no event 607 val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock 608 609 if (printEventCoding) { 610 for (((name, inc), i) <- allPerfEvents.zipWithIndex) { 611 println("CtrlBlock perfEvents Set", name, inc, i) 612 } 613 } 614 615 val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent)) 616 val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents 617 generatePerfEvent() 618} 619 620class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle { 621 val fromTop = new Bundle { 622 val hartId = Input(UInt(8.W)) 623 } 624 val toTop = new Bundle { 625 val cpuHalt = Output(Bool()) 626 } 627 val frontend = Flipped(new FrontendToCtrlIO()) 628 val toIssueBlock = new Bundle { 629 val flush = ValidIO(new Redirect) 630 val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq)) 631 val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst)) 632 val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst)) 633 val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst)) 634 val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst)) 635 } 636 val toDataPath = new Bundle { 637 val flush = ValidIO(new Redirect) 638 } 639 val toExuBlock = new Bundle { 640 val flush = ValidIO(new Redirect) 641 } 642 val intIQValidNumVec = Input(MixedVec(params.genIntIQValidNumBundle)) 643 val fpIQValidNumVec = Input(MixedVec(params.genFpIQValidNumBundle)) 644 val fromWB = new Bundle { 645 val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles)) 646 } 647 val redirect = ValidIO(new Redirect) 648 val fromMem = new Bundle { 649 val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx 650 val violation = Flipped(ValidIO(new Redirect)) 651 } 652 val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 653 val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 654 val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W)))) 655 656 val csrCtrl = Input(new CustomCSRCtrlIO) 657 val robio = new Bundle { 658 val csr = new RobCSRIO 659 val exception = ValidIO(new ExceptionInfo) 660 val lsq = new RobLsqIO 661 val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo)) 662 val debug_ls = Input(new DebugLSIO()) 663 val robHeadLsIssue = Input(Bool()) 664 val robDeqPtr = Output(new RobPtr) 665 val commitVType = new Bundle { 666 val vtype = Output(ValidIO(VType())) 667 val hasVsetvl = Output(Bool()) 668 } 669 } 670 671 val toDecode = new Bundle { 672 val vsetvlVType = Input(VType()) 673 val vstart = Input(Vl()) 674 } 675 676 val perfInfo = Output(new Bundle{ 677 val ctrlInfo = new Bundle { 678 val robFull = Bool() 679 val intdqFull = Bool() 680 val fpdqFull = Bool() 681 val lsdqFull = Bool() 682 } 683 }) 684 val debug_int_rat = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 685 val debug_fp_rat = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 686 val debug_vec_rat = if (params.debugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None 687 val debug_v0_rat = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None 688 val debug_vl_rat = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None 689 690 val sqCanAccept = Input(Bool()) 691 val lqCanAccept = Input(Bool()) 692 693 val debugTopDown = new Bundle { 694 val fromRob = new RobCoreTopDownIO 695 val fromCore = new CoreDispatchTopDownIO 696 } 697 val debugRolling = new RobDebugRollingIO 698 val debugEnqLsq = Input(new LsqEnqIO) 699} 700 701class NamedIndexes(namedCnt: Seq[(String, Int)]) { 702 require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name") 703 704 val maxIdx = namedCnt.map(_._2).sum 705 val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i => 706 val begin = namedCnt.slice(0, i).map(_._2).sum 707 val end = begin + namedCnt(i)._2 708 (namedCnt(i)._1, (begin, end)) 709 }.toMap 710 711 def apply(name: String): Seq[Int] = { 712 require(nameRangeMap.contains(name)) 713 nameRangeMap(name)._1 until nameRangeMap(name)._2 714 } 715} 716