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