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