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