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