xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision ff7f931d756b3ca58ed45972e9b161fb54850bf2)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
23import utility._
24import utils._
25import xiangshan.ExceptionNO._
26import xiangshan._
27import xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput}
28import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator}
29import xiangshan.backend.datapath.DataConfig.VAddrData
30import xiangshan.backend.decode.{DecodeStage, FusionDecoder}
31import xiangshan.backend.dispatch.{CoreDispatchTopDownIO, Dispatch, DispatchQueue}
32import xiangshan.backend.fu.PFEvent
33import xiangshan.backend.fu.vector.Bundles.VType
34import xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator}
35import xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr}
36import xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
37import xiangshan.mem.{LqPtr, LsqEnqIO}
38
39class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
40  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
41  val redirect = Valid(new Redirect)
42  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
43  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
44}
45
46class CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
47  val rob = LazyModule(new Rob(params))
48
49  lazy val module = new CtrlBlockImp(this)(p, params)
50
51}
52
53class CtrlBlockImp(
54  override val wrapper: CtrlBlock
55)(implicit
56  p: Parameters,
57  params: BackendParams
58) extends LazyModuleImp(wrapper)
59  with HasXSParameter
60  with HasCircularQueuePtrHelper
61  with HasPerfEvents
62{
63  val pcMemRdIndexes = new NamedIndexes(Seq(
64    "exu"       -> params.numPcReadPort,
65    "redirect"  -> 1,
66    "memPred"   -> 1,
67    "robFlush"  -> 1,
68    "load"      -> params.LduCnt,
69    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0)
70  ))
71
72  private val numPcMemReadForExu = params.numPcReadPort
73  private val numPcMemRead = pcMemRdIndexes.maxIdx
74
75  println(s"pcMem read num: $numPcMemRead")
76  println(s"pcMem read num for exu: $numPcMemReadForExu")
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 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.bits.debugInfo.writebackTime := GTimer()
122    delayed
123  }).toSeq
124
125  private val exuPredecode = VecInit(
126    delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
127  )
128
129  private val exuRedirects: Seq[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  }).toSeq
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  if (EnableStorePrefetchSMS) {
160    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
161      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
162      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegNext(io.memStPcRead(i).offset))
163    }
164  } else {
165    io.memStPcRead.foreach(_.data := 0.U)
166  }
167
168  redirectGen.io.hartId := io.fromTop.hartId
169  redirectGen.io.exuRedirect := exuRedirects.toSeq
170  redirectGen.io.exuOutPredecode := exuPredecode // guarded by exuRedirect.valid
171  redirectGen.io.loadReplay <> loadReplay
172
173  redirectGen.io.robFlush := s1_robFlushRedirect.valid
174
175  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
176  val s6_flushFromRobValid = RegNext(s5_flushFromRobValidAhead)
177  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
178  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
179  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
180  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
181  for (i <- 0 until CommitWidth) {
182    // why flushOut: instructions with flushPipe are not commited to frontend
183    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
184    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
185    io.frontend.toFtq.rob_commits(i).valid := RegNext(s1_isCommit)
186    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
187  }
188  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
189  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
190  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
191  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
192
193  //jmp/brh
194  for (i <- 0 until NumRedirect) {
195    io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirects(i).valid && exuRedirects(i).bits.cfiUpdate.isMisPred && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
196    io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirects(i).bits.ftqIdx
197  }
198  //loadreplay
199  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
200  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
201  //exception
202  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
203  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
204  // Be careful here:
205  // T0: rob.io.flushOut, s0_robFlushRedirect
206  // T1: s1_robFlushRedirect, rob.io.exception.valid
207  // T2: csr.redirect.valid
208  // T3: csr.exception.valid
209  // T4: csr.trapTarget
210  // T5: ctrlBlock.trapTarget
211  // T6: io.frontend.toFtq.stage2Redirect.valid
212  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
213    s1_robFlushPc, // replay inst
214    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
215  ), s1_robFlushRedirect.valid)
216  private val s2_csrIsXRet = io.robio.csr.isXRet
217  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
218  private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget
219
220  val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc)
221  when (s6_flushFromRobValid) {
222    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
223    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegNext(flushTarget)
224  }
225
226  // vtype commit
227  decode.io.commitVType.bits := io.fromDataPath.vtype
228  decode.io.commitVType.valid := RegNext(rob.io.isVsetFlushPipe)
229
230  io.toDataPath.vtypeAddr := rob.io.vconfigPdest
231
232  // vtype walk
233  val isVsetSeq = rob.io.commits.walkValid.zip(rob.io.commits.info).map { case (valid, info) => valid && info.isVset }.reverse
234  val walkVTypeReverse = rob.io.commits.info.map(info => info.vtype).reverse
235  val walkVType = PriorityMux(isVsetSeq, walkVTypeReverse)
236
237  decode.io.walkVType.bits := walkVType.asTypeOf(new VType)
238  decode.io.walkVType.valid := rob.io.commits.isWalk && isVsetSeq.reduce(_ || _)
239
240  decode.io.isRedirect := s1_s3_redirect.valid
241
242  decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) =>
243    decodeIn.valid := frontendCf.valid
244    frontendCf.ready := decodeIn.ready
245    decodeIn.bits.connectCtrlFlow(frontendCf.bits)
246  }
247  decode.io.csrCtrl := RegNext(io.csrCtrl)
248  decode.io.intRat <> rat.io.intReadPorts
249  decode.io.fpRat <> rat.io.fpReadPorts
250  decode.io.vecRat <> rat.io.vecReadPorts
251  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
252  decode.io.stallReason.in <> io.frontend.stallReason
253
254  // snapshot check
255  val snpt = Module(new SnapshotGenerator(rename.io.out.head.bits.robIdx))
256  snpt.io.enq := rename.io.out.head.bits.snapshot && rename.io.out.head.fire
257  snpt.io.enqData.head := rename.io.out.head.bits.robIdx
258  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
259    Cat(rob.io.commits.commitValid.zip(rob.io.commits.robIdx).map(x => x._1 && x._2 === snpt.io.snapshots(snpt.io.deqPtr.value))).orR
260  snpt.io.flush := s1_s3_redirect.valid
261
262  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
263    snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx)
264  ).reduceTree(_ || _)
265  val snptSelect = MuxCase(
266    0.U(log2Ceil(RenameSnapshotNum).W),
267    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
268      (snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx), idx)
269    )
270  )
271
272  rob.io.snpt.snptEnq := DontCare
273  rob.io.snpt.snptDeq := snpt.io.deq
274  rob.io.snpt.useSnpt := useSnpt
275  rob.io.snpt.snptSelect := snptSelect
276  rat.io.snpt.snptEnq := rename.io.out.head.bits.snapshot && rename.io.out.head.fire
277  rat.io.snpt.snptDeq := snpt.io.deq
278  rat.io.snpt.useSnpt := useSnpt
279  rat.io.snpt.snptSelect := snptSelect
280
281  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
282  // fusion decoder
283  for (i <- 0 until DecodeWidth) {
284    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
285    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
286    if (i > 0) {
287      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
288    }
289  }
290
291  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
292
293  for (i <- 0 until RenameWidth) {
294    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
295      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
296
297    decodePipeRename(i).ready := rename.io.in(i).ready
298    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
299    rename.io.in(i).bits := decodePipeRename(i).bits
300  }
301
302  for (i <- 0 until RenameWidth - 1) {
303    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
304    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
305
306    // update the first RenameWidth - 1 instructions
307    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
308    when (fusionDecoder.io.out(i).valid) {
309      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
310      // TODO: remove this dirty code for ftq update
311      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
312      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
313      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
314      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
315      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
316      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
317      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
318      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
319      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
320      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
321    }
322
323  }
324
325  // memory dependency predict
326  // when decode, send fold pc to mdp
327  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
328  for (i <- 0 until DecodeWidth) {
329    mdpFlodPcVec(i) := Mux(
330      decode.io.out(i).fire,
331      decode.io.in(i).bits.foldpc,
332      rename.io.in(i).bits.foldpc
333    )
334  }
335
336  // currently, we only update mdp info when isReplay
337  memCtrl.io.redirect := s1_s3_redirect
338  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
339  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
340  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
341  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
342  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
343
344  rat.io.redirect := s1_s3_redirect.valid
345  rat.io.robCommits := rob.io.rabCommits
346  rat.io.diffCommits := rob.io.diffCommits
347  rat.io.intRenamePorts := rename.io.intRenamePorts
348  rat.io.fpRenamePorts := rename.io.fpRenamePorts
349  rat.io.vecRenamePorts := rename.io.vecRenamePorts
350
351  rename.io.redirect := s1_s3_redirect
352  rename.io.robCommits <> rob.io.rabCommits
353  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
354    RegEnable(waittable2rename, decodeOut.fire)
355  }
356  rename.io.ssit := memCtrl.io.ssit2Rename
357  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
358  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
359  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
360  rename.io.int_need_free := rat.io.int_need_free
361  rename.io.int_old_pdest := rat.io.int_old_pdest
362  rename.io.fp_old_pdest := rat.io.fp_old_pdest
363  rename.io.vec_old_pdest := rat.io.vec_old_pdest
364  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
365  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
366  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
367  rename.io.debug_vconfig_rat.foreach(_ := rat.io.debug_vconfig_rat.get)
368  rename.io.stallReason.in <> decode.io.stallReason.out
369  rename.io.snpt.snptEnq := DontCare
370  rename.io.snpt.snptDeq := snpt.io.deq
371  rename.io.snpt.useSnpt := useSnpt
372  rename.io.snpt.snptSelect := snptSelect
373
374  // prevent rob from generating snapshot when full here
375  val renameOut = Wire(chiselTypeOf(rename.io.out))
376  renameOut <> rename.io.out
377  when(isFull(snpt.io.enqPtr, snpt.io.deqPtr)) {
378    renameOut.head.bits.snapshot := false.B
379  }
380
381
382  // pipeline between rename and dispatch
383  for (i <- 0 until RenameWidth) {
384    PipelineConnect(renameOut(i), dispatch.io.fromRename(i), dispatch.io.recv(i), s1_s3_redirect.valid)
385  }
386
387  dispatch.io.hartId := io.fromTop.hartId
388  dispatch.io.redirect := s1_s3_redirect
389  dispatch.io.enqRob <> rob.io.enq
390  dispatch.io.robHead := rob.io.debugRobHead
391  dispatch.io.stallReason <> rename.io.stallReason.out
392  dispatch.io.lqCanAccept := io.lqCanAccept
393  dispatch.io.sqCanAccept := io.sqCanAccept
394  dispatch.io.robHeadNotReady := rob.io.headNotReady
395  dispatch.io.robFull := rob.io.robFull
396  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
397
398  intDq.io.enq <> dispatch.io.toIntDq
399  intDq.io.redirect <> s2_s4_redirect
400
401  fpDq.io.enq <> dispatch.io.toFpDq
402  fpDq.io.redirect <> s2_s4_redirect
403
404  lsDq.io.enq <> dispatch.io.toLsDq
405  lsDq.io.redirect <> s2_s4_redirect
406
407  io.toIssueBlock.intUops <> intDq.io.deq
408  io.toIssueBlock.vfUops  <> fpDq.io.deq
409  io.toIssueBlock.memUops <> lsDq.io.deq
410  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
411  io.toIssueBlock.flush   <> s2_s4_redirect
412
413  pcMem.io.wen.head   := RegNext(io.frontend.fromFtq.pc_mem_wen)
414  pcMem.io.waddr.head := RegNext(io.frontend.fromFtq.pc_mem_waddr)
415  pcMem.io.wdata.head := RegNext(io.frontend.fromFtq.pc_mem_wdata)
416
417  private val jumpPcVec         : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
418  io.toIssueBlock.pcVec := jumpPcVec
419
420  io.toDataPath.flush := s2_s4_redirect
421  io.toExuBlock.flush := s2_s4_redirect
422
423  for ((pcMemIdx, i) <- pcMemRdIndexes("exu").zipWithIndex) {
424    pcMem.io.raddr(pcMemIdx) := intDq.io.deqNext(i).ftqPtr.value
425    jumpPcVec(i) := pcMem.io.rdata(pcMemIdx).getPc(RegNext(intDq.io.deqNext(i).ftqOffset))
426  }
427
428  val dqOuts = Seq(io.toIssueBlock.intUops) ++ Seq(io.toIssueBlock.vfUops) ++ Seq(io.toIssueBlock.memUops)
429  dqOuts.zipWithIndex.foreach { case (dqOut, dqIdx) =>
430    dqOut.map(_.bits.pc).zipWithIndex.map{ case (pc, portIdx) =>
431      if(params.allSchdParams(dqIdx).numPcReadPort > 0){
432        val realJumpPcVec = jumpPcVec.drop(params.allSchdParams.take(dqIdx).map(_.numPcReadPort).sum).take(params.allSchdParams(dqIdx).numPcReadPort)
433        pc := realJumpPcVec(portIdx)
434      }
435    }
436  }
437
438  rob.io.hartId := io.fromTop.hartId
439  rob.io.redirect := s1_s3_redirect
440  rob.io.writeback := delayedNotFlushedWriteBack
441
442  io.redirect := s1_s3_redirect
443
444  // rob to int block
445  io.robio.csr <> rob.io.csr
446  // When wfi is disabled, it will not block ROB commit.
447  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
448  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
449
450  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
451
452  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
453  io.robio.exception := rob.io.exception
454  io.robio.exception.bits.pc := s1_robFlushPc
455
456  // rob to mem block
457  io.robio.lsq <> rob.io.lsq
458
459  io.debug_int_rat    .foreach(_ := rat.io.diff_int_rat.get)
460  io.debug_fp_rat     .foreach(_ := rat.io.diff_fp_rat.get)
461  io.debug_vec_rat    .foreach(_ := rat.io.diff_vec_rat.get)
462  io.debug_vconfig_rat.foreach(_ := rat.io.diff_vconfig_rat.get)
463
464  rob.io.debug_ls := io.robio.debug_ls
465  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
466  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
467  rob.io.debugEnqLsq := io.debugEnqLsq
468
469  io.robio.robDeqPtr := rob.io.robDeqPtr
470
471  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
472  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
473  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
474  io.debugRolling := rob.io.debugRolling
475
476  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
477  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
478  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
479  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
480
481  val pfevent = Module(new PFEvent)
482  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
483  val csrevents = pfevent.io.hpmevent.slice(8,16)
484
485  val perfinfo = IO(new Bundle(){
486    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
487    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
488    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
489  })
490
491  val allPerfEvents = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerf)
492  val hpmEvents = allPerfEvents ++ perfinfo.perfEventsEu0 ++ perfinfo.perfEventsEu1 ++ perfinfo.perfEventsRs
493  val perfEvents = HPerfMonitor(csrevents, hpmEvents).getPerfEvents
494  generatePerfEvent()
495}
496
497class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
498  val fromTop = new Bundle {
499    val hartId = Input(UInt(8.W))
500  }
501  val toTop = new Bundle {
502    val cpuHalt = Output(Bool())
503  }
504  val frontend = Flipped(new FrontendToCtrlIO())
505  val toIssueBlock = new Bundle {
506    val flush = ValidIO(new Redirect)
507    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
508    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
509    val vfUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
510    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
511    val pcVec = Output(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
512  }
513  val fromDataPath = new Bundle{
514    val vtype = Input(new VType)
515  }
516  val toDataPath = new Bundle {
517    val vtypeAddr = Output(UInt(PhyRegIdxWidth.W))
518    val flush = ValidIO(new Redirect)
519  }
520  val toExuBlock = new Bundle {
521    val flush = ValidIO(new Redirect)
522  }
523  val fromWB = new Bundle {
524    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
525  }
526  val redirect = ValidIO(new Redirect)
527  val fromMem = new Bundle {
528    val stIn = Vec(params.StaCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
529    val violation = Flipped(ValidIO(new Redirect))
530  }
531  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
532  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
533
534  val csrCtrl = Input(new CustomCSRCtrlIO)
535  val robio = new Bundle {
536    val csr = new RobCSRIO
537    val exception = ValidIO(new ExceptionInfo)
538    val lsq = new RobLsqIO
539    val lsTopdownInfo = Vec(params.LduCnt, Input(new LsTopdownInfo))
540    val debug_ls = Input(new DebugLSIO())
541    val robHeadLsIssue = Input(Bool())
542    val robDeqPtr = Output(new RobPtr)
543  }
544
545  val perfInfo = Output(new Bundle{
546    val ctrlInfo = new Bundle {
547      val robFull   = Bool()
548      val intdqFull = Bool()
549      val fpdqFull  = Bool()
550      val lsdqFull  = Bool()
551    }
552  })
553  val debug_int_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
554  val debug_fp_rat      = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
555  val debug_vec_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
556  val debug_vconfig_rat = if (params.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None // TODO: use me
557
558  val sqCanAccept = Input(Bool())
559  val lqCanAccept = Input(Bool())
560
561  val debugTopDown = new Bundle {
562    val fromRob = new RobCoreTopDownIO
563    val fromCore = new CoreDispatchTopDownIO
564  }
565  val debugRolling = new RobDebugRollingIO
566  val debugEnqLsq = Input(new LsqEnqIO)
567}
568
569class NamedIndexes(namedCnt: Seq[(String, Int)]) {
570  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
571
572  val maxIdx = namedCnt.map(_._2).sum
573  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
574    val begin = namedCnt.slice(0, i).map(_._2).sum
575    val end = begin + namedCnt(i)._2
576    (namedCnt(i)._1, (begin, end))
577  }.toMap
578
579  def apply(name: String): Seq[Int] = {
580    require(nameRangeMap.contains(name))
581    nameRangeMap(name)._1 until nameRangeMap(name)._2
582  }
583}
584