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