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