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