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