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