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