xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 45f43e6e5f88874a7573ff096d1e5c2855bd16c7)
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 utils._
24import utility._
25import xiangshan._
26import xiangshan.backend.decode.{DecodeStage, FusionDecoder, ImmUnion}
27import xiangshan.backend.dispatch._
28import xiangshan.backend.fu.PFEvent
29import xiangshan.backend.rename.{Rename, RenameTableWrapper}
30import xiangshan.backend.rob._
31import xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
32import xiangshan.mem.mdp.{LFST, SSIT, WaitTable}
33import xiangshan.ExceptionNO._
34import xiangshan.backend.exu.ExuConfig
35import xiangshan.mem.{LsqEnqCtrl, LsqEnqIO}
36
37class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
38  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
39  val redirect = Valid(new Redirect)
40  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
41  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
42}
43
44class SnapshotPtr(implicit p: Parameters) extends CircularQueuePtr[SnapshotPtr](
45  p => p(XSCoreParamsKey).RenameSnapshotNum
46)
47
48object SnapshotGenerator extends HasCircularQueuePtrHelper {
49  def apply[T <: Data](enqData: T, enq: Bool, deq: Bool, flush: Bool)(implicit p: Parameters): Vec[T] = {
50    val snapshotGen = Module(new SnapshotGenerator(enqData))
51    snapshotGen.io.enq := enq
52    snapshotGen.io.enqData.head := enqData
53    snapshotGen.io.deq := deq
54    snapshotGen.io.flush := flush
55    snapshotGen.io.snapshots
56  }
57}
58
59class SnapshotGenerator[T <: Data](dataType: T)(implicit p: Parameters) extends XSModule
60  with HasCircularQueuePtrHelper {
61
62  class SnapshotGeneratorIO extends Bundle {
63    val enq = Input(Bool())
64    val enqData = Input(Vec(1, chiselTypeOf(dataType))) // make chisel happy
65    val deq = Input(Bool())
66    val flush = Input(Bool())
67    val snapshots = Output(Vec(RenameSnapshotNum, chiselTypeOf(dataType)))
68    val enqPtr = Output(new SnapshotPtr)
69    val deqPtr = Output(new SnapshotPtr)
70    val valids = Output(Vec(RenameSnapshotNum, Bool()))
71  }
72
73  val io = IO(new SnapshotGeneratorIO)
74
75  val snapshots = Reg(Vec(RenameSnapshotNum, chiselTypeOf(dataType)))
76  val snptEnqPtr = RegInit(0.U.asTypeOf(new SnapshotPtr))
77  val snptDeqPtr = RegInit(0.U.asTypeOf(new SnapshotPtr))
78  val snptValids = RegInit(VecInit.fill(RenameSnapshotNum)(false.B))
79
80  io.snapshots := snapshots
81  io.enqPtr := snptEnqPtr
82  io.deqPtr := snptDeqPtr
83  io.valids := snptValids
84
85  when(!isFull(snptEnqPtr, snptDeqPtr) && io.enq) {
86    snapshots(snptEnqPtr.value) := io.enqData.head
87    snptValids(snptEnqPtr.value) := true.B
88    snptEnqPtr := snptEnqPtr + 1.U
89  }
90  when(io.deq) {
91    snptValids(snptDeqPtr.value) := false.B
92    snptDeqPtr := snptDeqPtr + 1.U
93    XSError(isEmpty(snptEnqPtr, snptDeqPtr), "snapshots should not be empty when dequeue!\n")
94  }
95  when(io.flush) {
96    snptValids := 0.U.asTypeOf(snptValids)
97    snptEnqPtr := 0.U.asTypeOf(new SnapshotPtr)
98    snptDeqPtr := 0.U.asTypeOf(new SnapshotPtr)
99  }
100}
101
102class RedirectGenerator(implicit p: Parameters) extends XSModule
103  with HasCircularQueuePtrHelper {
104
105  class RedirectGeneratorIO(implicit p: Parameters) extends XSBundle {
106    val hartId = Input(UInt(8.W))
107    val exuMispredict = Vec(NumRedirect, Flipped(ValidIO(new ExuOutput)))
108    val loadReplay = Flipped(ValidIO(new Redirect))
109    val flush = Input(Bool())
110    val redirectPcRead = new FtqRead(UInt(VAddrBits.W))
111    val stage2Redirect = ValidIO(new Redirect)
112    val stage3Redirect = ValidIO(new Redirect)
113    val memPredUpdate = Output(new MemPredUpdateReq)
114    val memPredPcRead = new FtqRead(UInt(VAddrBits.W)) // read req send form stage 2
115    val isMisspreRedirect = Output(Bool())
116    val stage2oldestOH = Output(UInt((NumRedirect + 1).W))
117  }
118  val io = IO(new RedirectGeneratorIO)
119  /*
120        LoadQueue  Jump  ALU0  ALU1  ALU2  ALU3   exception    Stage1
121          |         |      |    |     |     |         |
122          |============= reg & compare =====|         |       ========
123                            |                         |
124                            |                         |
125                            |                         |        Stage2
126                            |                         |
127                    redirect (flush backend)          |
128                    |                                 |
129               === reg ===                            |       ========
130                    |                                 |
131                    |----- mux (exception first) -----|        Stage3
132                            |
133                redirect (send to frontend)
134   */
135  def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = {
136    val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.robIdx, xs(i).bits.robIdx)))
137    val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j =>
138      (if (j < i) !xs(j).valid || compareVec(i)(j)
139      else if (j == i) xs(i).valid
140      else !xs(j).valid || !compareVec(j)(i))
141    )).andR))
142    resultOnehot
143  }
144
145  def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = {
146    val redirect = Wire(Valid(new Redirect))
147    redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred
148    redirect.bits := exuOut.bits.redirect
149    redirect.bits.debugIsCtrl := true.B
150    redirect.bits.debugIsMemVio := false.B
151    redirect
152  }
153
154  val jumpOut = io.exuMispredict.head
155  val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay)
156  val oldestOneHot = selectOldestRedirect(allRedirect)
157  val needFlushVec = VecInit(allRedirect.map(_.bits.robIdx.needFlush(io.stage2Redirect) || io.flush))
158  val oldestValid = VecInit(oldestOneHot.zip(needFlushVec).map{ case (v, f) => v && !f }).asUInt.orR
159  val oldestExuOutput = Mux1H(io.exuMispredict.indices.map(oldestOneHot), io.exuMispredict)
160  val oldestRedirect = Mux1H(oldestOneHot, allRedirect)
161  io.isMisspreRedirect := VecInit(io.exuMispredict.map(x => getRedirect(x).valid)).asUInt.orR
162  io.redirectPcRead.ptr := oldestRedirect.bits.ftqIdx
163  io.redirectPcRead.offset := oldestRedirect.bits.ftqOffset
164
165  val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid)
166  val s1_imm12_reg = RegNext(oldestExuOutput.bits.uop.ctrl.imm(11, 0))
167  val s1_pd = RegNext(oldestExuOutput.bits.uop.cf.pd)
168  val s1_redirect_bits_reg = RegNext(oldestRedirect.bits)
169  val s1_redirect_valid_reg = RegNext(oldestValid)
170  val s1_redirect_onehot = RegNext(oldestOneHot)
171
172  // stage1 -> stage2
173  io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush
174  io.stage2Redirect.bits := s1_redirect_bits_reg
175  io.stage2oldestOH := s1_redirect_onehot.asUInt
176
177  val s1_isReplay = s1_redirect_onehot.last
178  val s1_isJump = s1_redirect_onehot.head
179  val real_pc = io.redirectPcRead.data
180  val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN)
181  val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U)
182  val target = Mux(s1_isReplay,
183    Mux(s1_redirect_bits_reg.flushItself(), real_pc, real_pc + Mux(s1_redirect_bits_reg.isRVC, 2.U, 4.U)),
184    Mux(s1_redirect_bits_reg.cfiUpdate.taken,
185      Mux(s1_isJump, s1_jumpTarget, brTarget),
186      snpc
187    )
188  )
189
190  val stage2CfiUpdate = io.stage2Redirect.bits.cfiUpdate
191  stage2CfiUpdate.pc := real_pc
192  stage2CfiUpdate.pd := s1_pd
193  // stage2CfiUpdate.predTaken := s1_redirect_bits_reg.cfiUpdate.predTaken
194  stage2CfiUpdate.target := target
195  // stage2CfiUpdate.taken := s1_redirect_bits_reg.cfiUpdate.taken
196  // stage2CfiUpdate.isMisPred := s1_redirect_bits_reg.cfiUpdate.isMisPred
197
198  val s2_target = RegEnable(target, s1_redirect_valid_reg)
199  val s2_pc = RegEnable(real_pc, s1_redirect_valid_reg)
200  val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, s1_redirect_valid_reg)
201  val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B)
202
203  io.stage3Redirect.valid := s2_redirect_valid_reg
204  io.stage3Redirect.bits := s2_redirect_bits_reg
205
206  // get pc from ftq
207  // valid only if redirect is caused by load violation
208  // store_pc is used to update store set
209  val store_pc = io.memPredPcRead(s1_redirect_bits_reg.stFtqIdx, s1_redirect_bits_reg.stFtqOffset)
210
211  // update load violation predictor if load violation redirect triggered
212  io.memPredUpdate.valid := RegNext(s1_isReplay && s1_redirect_valid_reg && s2_redirect_bits_reg.flushItself(), init = false.B)
213  // update wait table
214  io.memPredUpdate.waddr := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth))
215  io.memPredUpdate.wdata := true.B
216  // update store set
217  io.memPredUpdate.ldpc := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth))
218  // store pc is ready 1 cycle after s1_isReplay is judged
219  io.memPredUpdate.stpc := XORFold(store_pc(VAddrBits-1, 1), MemPredPCWidth)
220}
221
222class CtrlBlock(dpExuConfigs: Seq[Seq[Seq[ExuConfig]]])(implicit p: Parameters) extends LazyModule
223  with HasWritebackSink with HasWritebackSource {
224  override def shouldBeInlined: Boolean = false
225  val rob = LazyModule(new Rob)
226
227  override def addWritebackSink(source: Seq[HasWritebackSource], index: Option[Seq[Int]]): HasWritebackSink = {
228    rob.addWritebackSink(Seq(this), Some(Seq(writebackSinks.length)))
229    super.addWritebackSink(source, index)
230  }
231
232  // duplicated dispatch2 here to avoid cross-module timing path loop.
233  val dispatch2 = dpExuConfigs.map(c => LazyModule(new Dispatch2Rs(c)))
234  lazy val module = new CtrlBlockImp(this)
235
236  override lazy val writebackSourceParams: Seq[WritebackSourceParams] = {
237    writebackSinksParams
238  }
239  override lazy val writebackSourceImp: HasWritebackSourceImp = module
240
241  override def generateWritebackIO(
242    thisMod: Option[HasWritebackSource] = None,
243    thisModImp: Option[HasWritebackSourceImp] = None
244  ): Unit = {
245    module.io.writeback.zip(writebackSinksImp(thisMod, thisModImp)).foreach(x => x._1 := x._2)
246  }
247}
248
249class CtrlBlockImp(outer: CtrlBlock)(implicit p: Parameters) extends LazyModuleImp(outer)
250  with HasXSParameter
251  with HasCircularQueuePtrHelper
252  with HasWritebackSourceImp
253  with HasPerfEvents
254{
255  val writebackLengths = outer.writebackSinksParams.map(_.length)
256
257  val io = IO(new Bundle {
258    val hartId = Input(UInt(8.W))
259    val cpu_halt = Output(Bool())
260    val frontend = Flipped(new FrontendToCtrlIO)
261    // to exu blocks
262    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
263    val dispatch = Vec(3*dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
264    val rsReady = Vec(outer.dispatch2.map(_.module.io.out.length).sum, Input(Bool()))
265    val enqLsq = Flipped(new LsqEnqIO)
266    val lqCancelCnt = Input(UInt(log2Up(VirtualLoadQueueSize + 1).W))
267    val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W))
268    val lqDeq = Input(UInt(log2Up(CommitWidth + 1).W))
269    val sqDeq = Input(UInt(log2Ceil(EnsbufferWidth + 1).W))
270    val sqCanAccept = Input(Bool())
271    val lqCanAccept = Input(Bool())
272    val ld_pc_read = Vec(exuParameters.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
273    val st_pc_read = Vec(exuParameters.StuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
274    // from int block
275    val exuRedirect = Vec(exuParameters.AluCnt + exuParameters.JmpCnt, Flipped(ValidIO(new ExuOutput)))
276    val stIn = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuInput)))
277    val memoryViolation = Flipped(ValidIO(new Redirect))
278    val jumpPc = Output(UInt(VAddrBits.W))
279    val jalr_target = Output(UInt(VAddrBits.W))
280    val robio = new Bundle {
281      // to int block
282      val toCSR = new RobCSRIO
283      val exception = ValidIO(new ExceptionInfo)
284      // to mem block
285      val lsq = new RobLsqIO
286      // debug
287      val debug_ls = Flipped(new DebugLSIO)
288      val lsTopdownInfo = Vec(exuParameters.LduCnt, Input(new LsTopdownInfo))
289    }
290    val csrCtrl = Input(new CustomCSRCtrlIO)
291    val perfInfo = Output(new Bundle{
292      val ctrlInfo = new Bundle {
293        val robFull   = Input(Bool())
294        val intdqFull = Input(Bool())
295        val fpdqFull  = Input(Bool())
296        val lsdqFull  = Input(Bool())
297      }
298    })
299    val writeback = MixedVec(writebackLengths.map(num => Vec(num, Flipped(ValidIO(new ExuOutput)))))
300    // redirect out
301    val redirect = ValidIO(new Redirect)
302    // debug
303    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
304    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
305    val robDeqPtr = Output(new RobPtr)
306    val robHeadLsIssue = Input(Bool())
307    val debugTopDown = new Bundle {
308      val fromRob = new RobCoreTopDownIO
309      val fromCore = new CoreDispatchTopDownIO
310    }
311    val debugRolling = new RobDebugRollingIO
312  })
313
314  override def writebackSource: Option[Seq[Seq[Valid[ExuOutput]]]] = {
315    Some(io.writeback.map(writeback => {
316      val exuOutput = WireInit(writeback)
317      val timer = GTimer()
318      for ((wb_next, wb) <- exuOutput.zip(writeback)) {
319        wb_next.valid := RegNext(wb.valid && !wb.bits.uop.robIdx.needFlush(Seq(stage2Redirect, redirectForExu)))
320        wb_next.bits := RegNext(wb.bits)
321        wb_next.bits.uop.debugInfo.writebackTime := timer
322      }
323      exuOutput
324    }).toSeq)
325  }
326
327  val decode = Module(new DecodeStage)
328  val fusionDecoder = Module(new FusionDecoder)
329  val rat = Module(new RenameTableWrapper)
330  val ssit = Module(new SSIT)
331  val waittable = Module(new WaitTable)
332  val rename = Module(new Rename)
333  val dispatch = Module(new Dispatch)
334  val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth))
335  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth))
336  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
337  val redirectGen = Module(new RedirectGenerator)
338  val rob = outer.rob.module
339
340  // jumpPc (2) + redirects (1) + loadPredUpdate (1) + jalr_target (1) + [ld pc (LduCnt)] + robWriteback (sum(writebackLengths)) + robFlush (1)
341  val PCMEMIDX_LD = 5
342  val PCMEMIDX_ST = PCMEMIDX_LD + exuParameters.LduCnt
343  val PCMEM_READ_PORT_COUNT = if(EnableStorePrefetchSMS) 6 + exuParameters.LduCnt + exuParameters.StuCnt else 6 + exuParameters.LduCnt
344  val pcMem = Module(new SyncDataModuleTemplate(
345    new Ftq_RF_Components, FtqSize,
346    PCMEM_READ_PORT_COUNT, 1, "CtrlPcMem")
347  )
348  pcMem.io.wen.head   := RegNext(io.frontend.fromFtq.pc_mem_wen)
349  pcMem.io.waddr.head := RegNext(io.frontend.fromFtq.pc_mem_waddr)
350  pcMem.io.wdata.head := RegNext(io.frontend.fromFtq.pc_mem_wdata)
351
352  pcMem.io.raddr.last := rob.io.flushOut.bits.ftqIdx.value
353  val flushPC = pcMem.io.rdata.last.getPc(RegNext(rob.io.flushOut.bits.ftqOffset))
354
355  val flushRedirect = Wire(Valid(new Redirect))
356  flushRedirect.valid := RegNext(rob.io.flushOut.valid)
357  flushRedirect.bits := RegEnable(rob.io.flushOut.bits, rob.io.flushOut.valid)
358  flushRedirect.bits.debugIsCtrl := false.B
359  flushRedirect.bits.debugIsMemVio := false.B
360
361  val flushRedirectReg = Wire(Valid(new Redirect))
362  flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B)
363  flushRedirectReg.bits := RegEnable(flushRedirect.bits, flushRedirect.valid)
364
365  val stage2Redirect = Mux(flushRedirect.valid, flushRedirect, redirectGen.io.stage2Redirect)
366  // Redirect will be RegNext at ExuBlocks.
367  val redirectForExu = RegNextWithEnable(stage2Redirect)
368
369  val exuRedirect = io.exuRedirect.map(x => {
370    val valid = x.valid && x.bits.redirectValid
371    val killedByOlder = x.bits.uop.robIdx.needFlush(Seq(stage2Redirect, redirectForExu))
372    val delayed = Wire(Valid(new ExuOutput))
373    delayed.valid := RegNext(valid && !killedByOlder, init = false.B)
374    delayed.bits := RegEnable(x.bits, x.valid)
375    delayed
376  })
377  val loadReplay = Wire(Valid(new Redirect))
378  loadReplay.valid := RegNext(io.memoryViolation.valid &&
379    !io.memoryViolation.bits.robIdx.needFlush(Seq(stage2Redirect, redirectForExu)),
380    init = false.B
381  )
382  val memVioBits = WireDefault(io.memoryViolation.bits)
383  memVioBits.debugIsCtrl := false.B
384  memVioBits.debugIsMemVio := true.B
385  loadReplay.bits := RegEnable(memVioBits, io.memoryViolation.valid)
386  pcMem.io.raddr(2) := redirectGen.io.redirectPcRead.ptr.value
387  redirectGen.io.redirectPcRead.data := pcMem.io.rdata(2).getPc(RegNext(redirectGen.io.redirectPcRead.offset))
388  pcMem.io.raddr(3) := redirectGen.io.memPredPcRead.ptr.value
389  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(3).getPc(RegNext(redirectGen.io.memPredPcRead.offset))
390  redirectGen.io.hartId := io.hartId
391  redirectGen.io.exuMispredict <> exuRedirect
392  redirectGen.io.loadReplay <> loadReplay
393  redirectGen.io.flush := flushRedirect.valid
394
395  val frontendFlushValidAhead = DelayN(flushRedirect.valid, 4)
396  val frontendFlushValid = RegNext(frontendFlushValidAhead)
397  val frontendFlushBits = RegEnable(flushRedirect.bits, flushRedirect.valid)
398  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
399  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
400  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
401  for (i <- 0 until CommitWidth) {
402    // why flushOut: instructions with flushPipe are not commited to frontend
403    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
404    val is_commit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !rob.io.flushOut.valid
405    io.frontend.toFtq.rob_commits(i).valid := RegNext(is_commit)
406    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), is_commit)
407  }
408  io.frontend.toFtq.redirect.valid := frontendFlushValid || redirectGen.io.stage2Redirect.valid
409  io.frontend.toFtq.redirect.bits := Mux(frontendFlushValid, frontendFlushBits, redirectGen.io.stage2Redirect.bits)
410  io.frontend.toFtq.ftqIdxSelOH.valid := frontendFlushValid || redirectGen.io.stage2Redirect.valid
411  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(frontendFlushValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !frontendFlushValid))
412
413  //jmp/brh
414  for (i <- 0 until NumRedirect) {
415    io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirect(i).valid && exuRedirect(i).bits.redirect.cfiUpdate.isMisPred && !flushRedirect.valid && !frontendFlushValidAhead
416    io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirect(i).bits.redirect.ftqIdx
417  }
418  //loadreplay
419  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !flushRedirect.valid && !frontendFlushValidAhead
420  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
421  //exception
422  io.frontend.toFtq.ftqIdxAhead.last.valid := frontendFlushValidAhead
423  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
424
425  // Be careful here:
426  // T0: flushRedirect.valid, exception.valid
427  // T1: csr.redirect.valid
428  // T2: csr.exception.valid
429  // T3: csr.trapTarget
430  // T4: ctrlBlock.trapTarget
431  // T5: io.frontend.toFtq.stage2Redirect.valid
432  val pc_from_csr = io.robio.toCSR.isXRet || DelayN(rob.io.exception.valid, 4)
433  val rob_flush_pc = RegEnable(Mux(flushRedirect.bits.flushItself(),
434    flushPC, // replay inst
435    flushPC + Mux(flushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
436  ), flushRedirect.valid)
437  val flushTarget = Mux(pc_from_csr, io.robio.toCSR.trapTarget, rob_flush_pc)
438  when (frontendFlushValid) {
439    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
440    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegNext(flushTarget)
441  }
442
443
444  val pendingRedirect = RegInit(false.B)
445  when (stage2Redirect.valid) {
446    pendingRedirect := true.B
447  }.elsewhen (RegNext(io.frontend.toFtq.redirect.valid)) {
448    pendingRedirect := false.B
449  }
450
451  decode.io.in <> io.frontend.cfVec
452  decode.io.stallReason.in <> io.frontend.stallReason
453  decode.io.csrCtrl := RegNext(io.csrCtrl)
454  decode.io.intRat <> rat.io.intReadPorts
455  decode.io.fpRat <> rat.io.fpReadPorts
456
457  // memory dependency predict
458  // when decode, send fold pc to mdp
459  for (i <- 0 until DecodeWidth) {
460    val mdp_foldpc = Mux(
461      decode.io.out(i).fire,
462      decode.io.in(i).bits.foldpc,
463      rename.io.in(i).bits.cf.foldpc
464    )
465    ssit.io.raddr(i) := mdp_foldpc
466    waittable.io.raddr(i) := mdp_foldpc
467  }
468  // currently, we only update mdp info when isReplay
469  ssit.io.update <> RegNext(redirectGen.io.memPredUpdate)
470  ssit.io.csrCtrl := RegNext(io.csrCtrl)
471  waittable.io.update <> RegNext(redirectGen.io.memPredUpdate)
472  waittable.io.csrCtrl := RegNext(io.csrCtrl)
473
474  // snapshot check
475  val snpt = Module(new SnapshotGenerator(rename.io.out.head.bits.robIdx))
476  snpt.io.enq := rename.io.out.head.bits.snapshot && rename.io.out.head.fire
477  snpt.io.enqData.head := rename.io.out.head.bits.robIdx
478  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
479    Cat(rob.io.commits.commitValid.zip(rob.io.commits.robIdx).map(x => x._1 && x._2 === snpt.io.snapshots(snpt.io.deqPtr.value))).orR
480  snpt.io.flush := stage2Redirect.valid
481
482  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
483    snpt.io.valids(idx) && stage2Redirect.bits.robIdx >= snpt.io.snapshots(idx)).reduceTree(_ || _)
484  val snptSelect = MuxCase(0.U(log2Ceil(RenameSnapshotNum).W),
485    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
486      (snpt.io.valids(idx) && stage2Redirect.bits.robIdx >= snpt.io.snapshots(idx), idx)
487  ))
488
489  rob.io.snpt.snptEnq := DontCare
490  rob.io.snpt.snptDeq := snpt.io.deq
491  rob.io.snpt.useSnpt := useSnpt
492  rob.io.snpt.snptSelect := snptSelect
493  rat.io.snpt.snptEnq := rename.io.out.head.bits.snapshot && rename.io.out.head.fire
494  rat.io.snpt.snptDeq := snpt.io.deq
495  rat.io.snpt.useSnpt := useSnpt
496  rat.io.snpt.snptSelect := snptSelect
497  rename.io.snpt.snptEnq := DontCare
498  rename.io.snpt.snptDeq := snpt.io.deq
499  rename.io.snpt.useSnpt := useSnpt
500  rename.io.snpt.snptSelect := snptSelect
501
502  // prevent rob from generating snapshot when full here
503  val renameOut = Wire(chiselTypeOf(rename.io.out))
504  renameOut <> rename.io.out
505  when(isFull(snpt.io.enqPtr, snpt.io.deqPtr)) {
506    renameOut.head.bits.snapshot := false.B
507  }
508
509  // LFST lookup and update
510  dispatch.io.lfst := DontCare
511  if (LFSTEnable) {
512    val lfst = Module(new LFST)
513    lfst.io.redirect <> RegNext(io.redirect)
514    lfst.io.storeIssue <> RegNext(io.stIn)
515    lfst.io.csrCtrl <> RegNext(io.csrCtrl)
516    lfst.io.dispatch <> dispatch.io.lfst
517  }
518
519
520  rat.io.redirect := stage2Redirect.valid
521  rat.io.robCommits := rob.io.commits
522  rat.io.intRenamePorts := rename.io.intRenamePorts
523  rat.io.fpRenamePorts := rename.io.fpRenamePorts
524  rat.io.debug_int_rat <> io.debug_int_rat
525  rat.io.debug_fp_rat <> io.debug_fp_rat
526
527  // pipeline between decode and rename
528  for (i <- 0 until RenameWidth) {
529    // fusion decoder
530    val decodeHasException = io.frontend.cfVec(i).bits.exceptionVec(instrPageFault) || io.frontend.cfVec(i).bits.exceptionVec(instrAccessFault)
531    val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
532    fusionDecoder.io.in(i).valid := io.frontend.cfVec(i).valid && !(decodeHasException || disableFusion)
533    fusionDecoder.io.in(i).bits := io.frontend.cfVec(i).bits.instr
534    if (i > 0) {
535      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
536    }
537
538    // Pipeline
539    val renamePipe = PipelineNext(decode.io.out(i), rename.io.in(i).ready,
540      stage2Redirect.valid || pendingRedirect)
541    renamePipe.ready := rename.io.in(i).ready
542    rename.io.in(i).valid := renamePipe.valid && !fusionDecoder.io.clear(i)
543    rename.io.in(i).bits := renamePipe.bits
544    rename.io.intReadPorts(i) := rat.io.intReadPorts(i).map(_.data)
545    rename.io.fpReadPorts(i) := rat.io.fpReadPorts(i).map(_.data)
546    rename.io.waittable(i) := RegEnable(waittable.io.rdata(i), decode.io.out(i).fire)
547
548    if (i < RenameWidth - 1) {
549      // fusion decoder sees the raw decode info
550      fusionDecoder.io.dec(i) := renamePipe.bits.ctrl
551      rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
552
553      // update the first RenameWidth - 1 instructions
554      decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
555      when (fusionDecoder.io.out(i).valid) {
556        fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits.ctrl)
557        // TODO: remove this dirty code for ftq update
558        val sameFtqPtr = rename.io.in(i).bits.cf.ftqPtr.value === rename.io.in(i + 1).bits.cf.ftqPtr.value
559        val ftqOffset0 = rename.io.in(i).bits.cf.ftqOffset
560        val ftqOffset1 = rename.io.in(i + 1).bits.cf.ftqOffset
561        val ftqOffsetDiff = ftqOffset1 - ftqOffset0
562        val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
563        val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
564        val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
565        val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
566        rename.io.in(i).bits.ctrl.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
567        XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
568      }
569    }
570  }
571
572  rename.io.redirect := stage2Redirect
573  rename.io.robCommits <> rob.io.commits
574  rename.io.ssit <> ssit.io.rdata
575  rename.io.int_need_free := rat.io.int_need_free
576  rename.io.int_old_pdest := rat.io.int_old_pdest
577  rename.io.fp_old_pdest := rat.io.fp_old_pdest
578  rename.io.debug_int_rat <> rat.io.debug_int_rat
579  rename.io.debug_fp_rat <> rat.io.debug_fp_rat
580  rename.io.stallReason.in <> decode.io.stallReason.out
581
582  // pipeline between rename and dispatch
583  for (i <- 0 until RenameWidth) {
584    PipelineConnect(renameOut(i), dispatch.io.fromRename(i), dispatch.io.recv(i), stage2Redirect.valid)
585  }
586
587  dispatch.io.hartId := io.hartId
588  dispatch.io.redirect := stage2Redirect
589  dispatch.io.enqRob <> rob.io.enq
590  dispatch.io.toIntDq <> intDq.io.enq
591  dispatch.io.toFpDq <> fpDq.io.enq
592  dispatch.io.toLsDq <> lsDq.io.enq
593  dispatch.io.allocPregs <> io.allocPregs
594  dispatch.io.robHead := rob.io.debugRobHead
595  dispatch.io.stallReason <> rename.io.stallReason.out
596  dispatch.io.lqCanAccept := io.lqCanAccept
597  dispatch.io.sqCanAccept := io.sqCanAccept
598  dispatch.io.robHeadNotReady := rob.io.headNotReady
599  dispatch.io.robFull := rob.io.robFull
600  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
601
602  intDq.io.redirect <> redirectForExu
603  fpDq.io.redirect <> redirectForExu
604  lsDq.io.redirect <> redirectForExu
605
606  val dpqOut = intDq.io.deq ++ lsDq.io.deq ++ fpDq.io.deq
607  io.dispatch <> dpqOut
608
609  for (dp2 <- outer.dispatch2.map(_.module.io)) {
610    dp2.redirect := redirectForExu
611    if (dp2.readFpState.isDefined) {
612      dp2.readFpState.get := DontCare
613    }
614    if (dp2.readIntState.isDefined) {
615      dp2.readIntState.get := DontCare
616    }
617    if (dp2.enqLsq.isDefined) {
618      val lsqCtrl = Module(new LsqEnqCtrl)
619      lsqCtrl.io.redirect <> redirectForExu
620      lsqCtrl.io.enq <> dp2.enqLsq.get
621      lsqCtrl.io.lcommit := io.lqDeq
622      lsqCtrl.io.scommit := io.sqDeq
623      lsqCtrl.io.lqCancelCnt := io.lqCancelCnt
624      lsqCtrl.io.sqCancelCnt := io.sqCancelCnt
625      io.enqLsq <> lsqCtrl.io.enqLsq
626      rob.io.debugEnqLsq := io.enqLsq
627    }
628  }
629  for ((dp2In, i) <- outer.dispatch2.flatMap(_.module.io.in).zipWithIndex) {
630    dp2In.valid := dpqOut(i).valid
631    dp2In.bits := dpqOut(i).bits
632    // override ready here to avoid cross-module loop path
633    dpqOut(i).ready := dp2In.ready
634  }
635  for ((dp2Out, i) <- outer.dispatch2.flatMap(_.module.io.out).zipWithIndex) {
636    dp2Out.ready := io.rsReady(i)
637  }
638
639  val pingpong = RegInit(false.B)
640  pingpong := !pingpong
641  pcMem.io.raddr(0) := intDq.io.deqNext(0).cf.ftqPtr.value
642  pcMem.io.raddr(1) := intDq.io.deqNext(2).cf.ftqPtr.value
643  val jumpPcRead0 = pcMem.io.rdata(0).getPc(RegNext(intDq.io.deqNext(0).cf.ftqOffset))
644  val jumpPcRead1 = pcMem.io.rdata(1).getPc(RegNext(intDq.io.deqNext(2).cf.ftqOffset))
645  io.jumpPc := Mux(pingpong && (exuParameters.AluCnt > 2).B, jumpPcRead1, jumpPcRead0)
646  val jalrTargetReadPtr = Mux(pingpong && (exuParameters.AluCnt > 2).B,
647    io.dispatch(2).bits.cf.ftqPtr,
648    io.dispatch(0).bits.cf.ftqPtr)
649  pcMem.io.raddr(4) := (jalrTargetReadPtr + 1.U).value
650  val jalrTargetRead = pcMem.io.rdata(4).startAddr
651  val read_from_newest_entry = RegNext(jalrTargetReadPtr) === RegNext(io.frontend.fromFtq.newest_entry_ptr)
652  io.jalr_target := Mux(read_from_newest_entry, RegNext(io.frontend.fromFtq.newest_entry_target), jalrTargetRead)
653  for(i <- 0 until exuParameters.LduCnt){
654    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
655    pcMem.io.raddr(i + PCMEMIDX_LD) := io.ld_pc_read(i).ptr.value
656    io.ld_pc_read(i).data := pcMem.io.rdata(i + PCMEMIDX_LD).getPc(RegNext(io.ld_pc_read(i).offset))
657  }
658  if(EnableStorePrefetchSMS) {
659    for(i <- 0 until exuParameters.StuCnt){
660      // store read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
661      pcMem.io.raddr(i + PCMEMIDX_ST) := io.st_pc_read(i).ptr.value
662      io.st_pc_read(i).data := pcMem.io.rdata(i + PCMEMIDX_ST).getPc(RegNext(io.st_pc_read(i).offset))
663    }
664  }else {
665    for(i <- 0 until exuParameters.StuCnt){
666      io.st_pc_read(i).data := 0.U
667    }
668  }
669
670  rob.io.hartId := io.hartId
671  io.cpu_halt := DelayN(rob.io.cpu_halt, 5)
672  rob.io.redirect := stage2Redirect
673  outer.rob.generateWritebackIO(Some(outer), Some(this))
674
675  io.redirect := stage2Redirect
676
677  // rob to int block
678  io.robio.toCSR <> rob.io.csr
679  // When wfi is disabled, it will not block ROB commit.
680  rob.io.csr.wfiEvent := io.robio.toCSR.wfiEvent
681  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
682  io.robio.toCSR.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
683  io.robio.exception := rob.io.exception
684  io.robio.exception.bits.uop.cf.pc := flushPC
685
686  // rob to mem block
687  io.robio.lsq <> rob.io.lsq
688
689  rob.io.debug_ls := io.robio.debug_ls
690  rob.io.debugHeadLsIssue := io.robHeadLsIssue
691  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
692  io.robDeqPtr := rob.io.robDeqPtr
693
694  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
695  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
696  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
697  io.debugRolling := rob.io.debugRolling
698
699  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
700  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
701  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
702  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
703
704  val pfevent = Module(new PFEvent)
705  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
706  val csrevents = pfevent.io.hpmevent.slice(8,16)
707
708  val perfinfo = IO(new Bundle(){
709    val perfEventsRs      = Input(Vec(NumRs, new PerfEvent))
710    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
711    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
712  })
713
714  val perfFromUnits = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerfEvents)
715  val perfFromIO    = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++
716                        perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++
717                        perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value))
718  val perfBlock     = Seq()
719  // let index = 0 be no event
720  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
721
722  if (printEventCoding) {
723    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
724      println("CtrlBlock perfEvents Set", name, inc, i)
725    }
726  }
727
728  val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
729  val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
730  generatePerfEvent()
731}
732