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