xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision c4b56310b9f6edacd8ee65bfdd7dd13b260a316c)
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(_ >= s1_s3_redirect.bits.robIdx)
272    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
273  })
274  val flushVecNext = RegNext(flushVec, 0.U.asTypeOf(flushVec))
275  snpt.io.flushVec := flushVecNext
276
277  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
278    snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx).robIdx.head
279  ).reduceTree(_ || _)
280  val snptSelect = MuxCase(
281    0.U(log2Ceil(RenameSnapshotNum).W),
282    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
283      (snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx).robIdx.head, idx)
284    )
285  )
286
287  rob.io.snpt.snptEnq := DontCare
288  rob.io.snpt.snptDeq := snpt.io.deq
289  rob.io.snpt.useSnpt := useSnpt
290  rob.io.snpt.snptSelect := snptSelect
291  rob.io.snpt.flushVec := flushVecNext
292  rat.io.snpt.snptEnq := genSnapshot
293  rat.io.snpt.snptDeq := snpt.io.deq
294  rat.io.snpt.useSnpt := useSnpt
295  rat.io.snpt.snptSelect := snptSelect
296  rat.io.snpt.flushVec := flushVec
297
298  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
299  // fusion decoder
300  for (i <- 0 until DecodeWidth) {
301    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
302    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
303    if (i > 0) {
304      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
305    }
306  }
307
308  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
309
310  for (i <- 0 until RenameWidth) {
311    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
312      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
313
314    decodePipeRename(i).ready := rename.io.in(i).ready
315    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
316    rename.io.in(i).bits := decodePipeRename(i).bits
317  }
318
319  for (i <- 0 until RenameWidth - 1) {
320    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
321    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
322
323    // update the first RenameWidth - 1 instructions
324    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
325    when (fusionDecoder.io.out(i).valid) {
326      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
327      // TODO: remove this dirty code for ftq update
328      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
329      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
330      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
331      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
332      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
333      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
334      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
335      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
336      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
337      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
338    }
339
340  }
341
342  // memory dependency predict
343  // when decode, send fold pc to mdp
344  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
345  for (i <- 0 until DecodeWidth) {
346    mdpFlodPcVec(i) := Mux(
347      decode.io.out(i).fire,
348      decode.io.in(i).bits.foldpc,
349      rename.io.in(i).bits.foldpc
350    )
351  }
352
353  // currently, we only update mdp info when isReplay
354  memCtrl.io.redirect := s1_s3_redirect
355  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
356  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
357  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
358  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
359  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
360
361  rat.io.redirect := s1_s3_redirect.valid
362  rat.io.robCommits := rob.io.rabCommits
363  rat.io.diffCommits := rob.io.diffCommits
364  rat.io.intRenamePorts := rename.io.intRenamePorts
365  rat.io.fpRenamePorts := rename.io.fpRenamePorts
366  rat.io.vecRenamePorts := rename.io.vecRenamePorts
367
368  rename.io.redirect := s1_s3_redirect
369  rename.io.robCommits <> rob.io.rabCommits
370  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
371    RegEnable(waittable2rename, decodeOut.fire)
372  }
373  rename.io.ssit := memCtrl.io.ssit2Rename
374  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
375  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
376  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
377  rename.io.int_need_free := rat.io.int_need_free
378  rename.io.int_old_pdest := rat.io.int_old_pdest
379  rename.io.fp_old_pdest := rat.io.fp_old_pdest
380  rename.io.vec_old_pdest := rat.io.vec_old_pdest
381  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
382  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
383  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
384  rename.io.debug_vconfig_rat.foreach(_ := rat.io.debug_vconfig_rat.get)
385  rename.io.stallReason.in <> decode.io.stallReason.out
386  rename.io.snpt.snptEnq := DontCare
387  rename.io.snpt.snptDeq := snpt.io.deq
388  rename.io.snpt.useSnpt := useSnpt
389  rename.io.snpt.snptSelect := snptSelect
390  rename.io.snpt.flushVec := flushVecNext
391  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
392  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
393
394  // prevent rob from generating snapshot when full here
395  val renameOut = Wire(chiselTypeOf(rename.io.out))
396  renameOut <> rename.io.out
397  // pass all snapshot in the first element for correctness of blockBackward
398  renameOut.tail.foreach(_.bits.snapshot := false.B)
399  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
400    false.B,
401    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
402  )
403
404
405  // pipeline between rename and dispatch
406  for (i <- 0 until RenameWidth) {
407    PipelineConnect(renameOut(i), dispatch.io.fromRename(i), dispatch.io.recv(i), s1_s3_redirect.valid)
408  }
409
410  dispatch.io.hartId := io.fromTop.hartId
411  dispatch.io.redirect := s1_s3_redirect
412  dispatch.io.enqRob <> rob.io.enq
413  dispatch.io.robHead := rob.io.debugRobHead
414  dispatch.io.stallReason <> rename.io.stallReason.out
415  dispatch.io.lqCanAccept := io.lqCanAccept
416  dispatch.io.sqCanAccept := io.sqCanAccept
417  dispatch.io.robHeadNotReady := rob.io.headNotReady
418  dispatch.io.robFull := rob.io.robFull
419  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
420
421  intDq.io.enq <> dispatch.io.toIntDq
422  intDq.io.redirect <> s2_s4_redirect
423
424  fpDq.io.enq <> dispatch.io.toFpDq
425  fpDq.io.redirect <> s2_s4_redirect
426
427  lsDq.io.enq <> dispatch.io.toLsDq
428  lsDq.io.redirect <> s2_s4_redirect
429
430  io.toIssueBlock.intUops <> intDq.io.deq
431  io.toIssueBlock.vfUops  <> fpDq.io.deq
432  io.toIssueBlock.memUops <> lsDq.io.deq
433  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
434  io.toIssueBlock.flush   <> s2_s4_redirect
435
436  pcMem.io.wen.head   := RegNext(io.frontend.fromFtq.pc_mem_wen)
437  pcMem.io.waddr.head := RegNext(io.frontend.fromFtq.pc_mem_waddr)
438  pcMem.io.wdata.head := RegNext(io.frontend.fromFtq.pc_mem_wdata)
439
440  private val jumpPcVec         : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
441  io.toIssueBlock.pcVec := jumpPcVec
442
443  io.toDataPath.flush := s2_s4_redirect
444  io.toExuBlock.flush := s2_s4_redirect
445
446  for ((pcMemIdx, i) <- pcMemRdIndexes("exu").zipWithIndex) {
447    pcMem.io.raddr(pcMemIdx) := intDq.io.deqNext(i).ftqPtr.value
448    jumpPcVec(i) := pcMem.io.rdata(pcMemIdx).getPc(RegNext(intDq.io.deqNext(i).ftqOffset))
449  }
450
451  val dqOuts = Seq(io.toIssueBlock.intUops) ++ Seq(io.toIssueBlock.vfUops) ++ Seq(io.toIssueBlock.memUops)
452  dqOuts.zipWithIndex.foreach { case (dqOut, dqIdx) =>
453    dqOut.map(_.bits.pc).zipWithIndex.map{ case (pc, portIdx) =>
454      if(params.allSchdParams(dqIdx).numPcReadPort > 0){
455        val realJumpPcVec = jumpPcVec.drop(params.allSchdParams.take(dqIdx).map(_.numPcReadPort).sum).take(params.allSchdParams(dqIdx).numPcReadPort)
456        pc := realJumpPcVec(portIdx)
457      }
458    }
459  }
460
461  rob.io.hartId := io.fromTop.hartId
462  rob.io.redirect := s1_s3_redirect
463  rob.io.writeback := delayedNotFlushedWriteBack
464
465  io.redirect := s1_s3_redirect
466
467  // rob to int block
468  io.robio.csr <> rob.io.csr
469  // When wfi is disabled, it will not block ROB commit.
470  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
471  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
472
473  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
474
475  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
476  io.robio.exception := rob.io.exception
477  io.robio.exception.bits.pc := s1_robFlushPc
478
479  // rob to mem block
480  io.robio.lsq <> rob.io.lsq
481
482  io.debug_int_rat    .foreach(_ := rat.io.diff_int_rat.get)
483  io.debug_fp_rat     .foreach(_ := rat.io.diff_fp_rat.get)
484  io.debug_vec_rat    .foreach(_ := rat.io.diff_vec_rat.get)
485  io.debug_vconfig_rat.foreach(_ := rat.io.diff_vconfig_rat.get)
486
487  rob.io.debug_ls := io.robio.debug_ls
488  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
489  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
490  rob.io.debugEnqLsq := io.debugEnqLsq
491
492  io.robio.robDeqPtr := rob.io.robDeqPtr
493
494  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
495  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
496  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
497  io.debugRolling := rob.io.debugRolling
498
499  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
500  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
501  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
502  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
503
504  val pfevent = Module(new PFEvent)
505  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
506  val csrevents = pfevent.io.hpmevent.slice(8,16)
507
508  val perfinfo = IO(new Bundle(){
509    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
510    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
511    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
512  })
513
514  val allPerfEvents = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerf)
515  val hpmEvents = allPerfEvents ++ perfinfo.perfEventsEu0 ++ perfinfo.perfEventsEu1 ++ perfinfo.perfEventsRs
516  val perfEvents = HPerfMonitor(csrevents, hpmEvents).getPerfEvents
517  generatePerfEvent()
518}
519
520class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
521  val fromTop = new Bundle {
522    val hartId = Input(UInt(8.W))
523  }
524  val toTop = new Bundle {
525    val cpuHalt = Output(Bool())
526  }
527  val frontend = Flipped(new FrontendToCtrlIO())
528  val toIssueBlock = new Bundle {
529    val flush = ValidIO(new Redirect)
530    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
531    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
532    val vfUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
533    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
534    val pcVec = Output(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
535  }
536  val fromDataPath = new Bundle{
537    val vtype = Input(new VType)
538  }
539  val toDataPath = new Bundle {
540    val vtypeAddr = Output(UInt(PhyRegIdxWidth.W))
541    val flush = ValidIO(new Redirect)
542  }
543  val toExuBlock = new Bundle {
544    val flush = ValidIO(new Redirect)
545  }
546  val fromWB = new Bundle {
547    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
548  }
549  val redirect = ValidIO(new Redirect)
550  val fromMem = new Bundle {
551    val stIn = Vec(params.StaCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
552    val violation = Flipped(ValidIO(new Redirect))
553  }
554  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
555  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
556
557  val csrCtrl = Input(new CustomCSRCtrlIO)
558  val robio = new Bundle {
559    val csr = new RobCSRIO
560    val exception = ValidIO(new ExceptionInfo)
561    val lsq = new RobLsqIO
562    val lsTopdownInfo = Vec(params.LduCnt, Input(new LsTopdownInfo))
563    val debug_ls = Input(new DebugLSIO())
564    val robHeadLsIssue = Input(Bool())
565    val robDeqPtr = Output(new RobPtr)
566  }
567
568  val perfInfo = Output(new Bundle{
569    val ctrlInfo = new Bundle {
570      val robFull   = Bool()
571      val intdqFull = Bool()
572      val fpdqFull  = Bool()
573      val lsdqFull  = Bool()
574    }
575  })
576  val debug_int_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
577  val debug_fp_rat      = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
578  val debug_vec_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
579  val debug_vconfig_rat = if (params.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None // TODO: use me
580
581  val sqCanAccept = Input(Bool())
582  val lqCanAccept = Input(Bool())
583
584  val debugTopDown = new Bundle {
585    val fromRob = new RobCoreTopDownIO
586    val fromCore = new CoreDispatchTopDownIO
587  }
588  val debugRolling = new RobDebugRollingIO
589  val debugEnqLsq = Input(new LsqEnqIO)
590}
591
592class NamedIndexes(namedCnt: Seq[(String, Int)]) {
593  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
594
595  val maxIdx = namedCnt.map(_._2).sum
596  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
597    val begin = namedCnt.slice(0, i).map(_._2).sum
598    val end = begin + namedCnt(i)._2
599    (namedCnt(i)._1, (begin, end))
600  }.toMap
601
602  def apply(name: String): Seq[Int] = {
603    require(nameRangeMap.contains(name))
604    nameRangeMap(name)._1 until nameRangeMap(name)._2
605  }
606}
607