xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision dfde261ec16dcebbbc95b82ef115b1948e841083)
1package xiangshan.backend
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.backend.decode.{DecodeStage, ImmUnion, WaitTableParameters}
8import xiangshan.backend.rename.{BusyTable, Rename}
9import xiangshan.backend.dispatch.Dispatch
10import xiangshan.backend.exu._
11import xiangshan.backend.exu.Exu.exuConfigs
12import xiangshan.backend.ftq.{Ftq, FtqRead, GetPcByFtq}
13import xiangshan.backend.regfile.RfReadPort
14import xiangshan.backend.roq.{Roq, RoqCSRIO, RoqLsqIO, RoqPtr}
15import xiangshan.mem.LsqEnqIO
16
17class CtrlToIntBlockIO extends XSBundle {
18  val enqIqCtrl = Vec(exuParameters.IntExuCnt, DecoupledIO(new MicroOp))
19  val readRf = Vec(NRIntReadPorts, Output(UInt(PhyRegIdxWidth.W)))
20  val jumpPc = Output(UInt(VAddrBits.W))
21  val jalr_target = Output(UInt(VAddrBits.W))
22  // int block only uses port 0~7
23  val readPortIndex = Vec(exuParameters.IntExuCnt, Output(UInt(log2Ceil(8 / 2).W))) // TODO parameterize 8 here
24  val redirect = ValidIO(new Redirect)
25  val flush = Output(Bool())
26}
27
28class CtrlToFpBlockIO extends XSBundle {
29  val enqIqCtrl = Vec(exuParameters.FpExuCnt, DecoupledIO(new MicroOp))
30  val readRf = Vec(NRFpReadPorts, Output(UInt(PhyRegIdxWidth.W)))
31  // fp block uses port 0~11
32  val readPortIndex = Vec(exuParameters.FpExuCnt, Output(UInt(log2Ceil((NRFpReadPorts - exuParameters.StuCnt) / 3).W)))
33  val redirect = ValidIO(new Redirect)
34  val flush = Output(Bool())
35}
36
37class CtrlToLsBlockIO extends XSBundle {
38  val enqIqCtrl = Vec(exuParameters.LsExuCnt, DecoupledIO(new MicroOp))
39  val enqLsq = Flipped(new LsqEnqIO)
40  val waitTableUpdate = Vec(StorePipelineWidth, Input(new WaitTableUpdateReq))
41  val redirect = ValidIO(new Redirect)
42  val flush = Output(Bool())
43}
44
45class RedirectGenerator extends XSModule with HasCircularQueuePtrHelper with WaitTableParameters {
46  val numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt
47  val io = IO(new Bundle() {
48    val exuMispredict = Vec(numRedirect, Flipped(ValidIO(new ExuOutput)))
49    val loadReplay = Flipped(ValidIO(new Redirect))
50    val flush = Input(Bool())
51    val stage1FtqRead = Vec(numRedirect + 1, new FtqRead)
52    val stage2FtqRead = new FtqRead
53    val stage2Redirect = ValidIO(new Redirect)
54    val stage3Redirect = ValidIO(new Redirect)
55    val waitTableUpdate = Output(new WaitTableUpdateReq)
56  })
57  /*
58        LoadQueue  Jump  ALU0  ALU1  ALU2  ALU3   exception    Stage1
59          |         |      |    |     |     |         |
60          |============= reg & compare =====|         |       ========
61                            |                         |
62                            |                         |
63                            |                         |        Stage2
64                            |                         |
65                    redirect (flush backend)          |
66                    |                                 |
67               === reg ===                            |       ========
68                    |                                 |
69                    |----- mux (exception first) -----|        Stage3
70                            |
71                redirect (send to frontend)
72   */
73  private class Wrapper(val n: Int) extends Bundle {
74    val redirect = new Redirect
75    val valid = Bool()
76    val idx = UInt(log2Up(n).W)
77  }
78  def selectOldestRedirect(xs: Seq[Valid[Redirect]]): (Valid[Redirect], UInt) = {
79    val wrappers = for((r, i) <- xs.zipWithIndex) yield {
80      val wrap = Wire(new Wrapper(xs.size))
81      wrap.redirect := r.bits
82      wrap.valid := r.valid
83      wrap.idx := i.U
84      wrap
85    }
86    val oldest = ParallelOperation[Wrapper](wrappers, (x, y) => {
87      Mux(x.valid,
88        Mux(y.valid, Mux(isAfter(x.redirect.roqIdx, y.redirect.roqIdx), y, x), x), y
89      )
90    })
91    val result = Wire(Valid(new Redirect))
92    result.valid := oldest.valid
93    result.bits := oldest.redirect
94    (result, oldest.idx)
95  }
96
97  for((ptr, redirect) <- io.stage1FtqRead.map(_.ptr).zip(
98    io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits
99  )){ ptr := redirect.ftqIdx }
100
101  def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = {
102    val redirect = Wire(Valid(new Redirect))
103    redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred
104    redirect.bits := exuOut.bits.redirect
105    redirect
106  }
107
108  val jumpOut = io.exuMispredict.head
109  val aluOut = VecInit(io.exuMispredict.tail)
110  val (oldestAluRedirect, oldestAluIdx) = selectOldestRedirect(aluOut.map(getRedirect))
111  val (oldestExuRedirect, jumpIsOlder) = selectOldestRedirect(Seq(
112    oldestAluRedirect, getRedirect(jumpOut)
113  ))
114  val oldestExuOutput = Mux(jumpIsOlder.asBool(), jumpOut, aluOut(oldestAluIdx))
115  val (oldestRedirect, _) = selectOldestRedirect(Seq(io.loadReplay, oldestExuRedirect))
116
117  val s1_isJump = RegNext(jumpIsOlder.asBool(), init = false.B)
118  val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid)
119  val s1_imm12_reg = RegEnable(oldestExuOutput.bits.uop.ctrl.imm(11, 0), oldestExuOutput.valid)
120  val s1_pd = RegEnable(oldestExuOutput.bits.uop.cf.pd, oldestExuOutput.valid)
121  val s1_redirect_bits_reg = Reg(new Redirect)
122  val s1_redirect_valid_reg = RegInit(false.B)
123  val s1_aluIdx = RegEnable(oldestAluIdx, oldestAluRedirect.valid)
124
125  // stage1 -> stage2
126  when(oldestRedirect.valid && !oldestRedirect.bits.roqIdx.needFlush(io.stage2Redirect, io.flush)){
127    s1_redirect_bits_reg := oldestRedirect.bits
128    s1_redirect_valid_reg := true.B
129  }.otherwise({
130    s1_redirect_valid_reg := false.B
131  })
132  io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush
133  io.stage2Redirect.bits := s1_redirect_bits_reg
134  io.stage2Redirect.bits.cfiUpdate := DontCare
135  // at stage2, we read ftq to get pc
136  io.stage2FtqRead.ptr := s1_redirect_bits_reg.ftqIdx
137
138  val isReplay = RedirectLevel.flushItself(s1_redirect_bits_reg.level)
139  val ftqRead = Mux(isReplay,
140    io.stage1FtqRead.last.entry,
141    Mux(
142      s1_isJump,
143      io.stage1FtqRead.head.entry,
144      VecInit(io.stage1FtqRead.tail.take(exuParameters.AluCnt).map(_.entry))(s1_aluIdx)
145    )
146  )
147  val cfiUpdate_pc = Cat(
148    ftqRead.ftqPC.head(VAddrBits - s1_redirect_bits_reg.ftqOffset.getWidth - instOffsetBits),
149    s1_redirect_bits_reg.ftqOffset,
150    0.U(instOffsetBits.W)
151  )
152  val real_pc = GetPcByFtq(ftqRead.ftqPC, s1_redirect_bits_reg.ftqOffset,
153    ftqRead.lastPacketPC.valid,
154    ftqRead.lastPacketPC.bits
155  )
156  val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN)
157  val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U)
158  val target = Mux(isReplay,
159    real_pc, // repaly from itself
160    Mux(s1_redirect_bits_reg.cfiUpdate.taken,
161      Mux(s1_isJump, s1_jumpTarget, brTarget),
162      snpc
163    )
164  )
165
166  // update waittable if load violation redirect triggered
167  io.waitTableUpdate.valid := RegNext(isReplay && s1_redirect_valid_reg, init = false.B)
168  io.waitTableUpdate.waddr := RegNext(XORFold(real_pc(VAddrBits-1, 1), WaitTableAddrWidth))
169  io.waitTableUpdate.wdata := true.B
170
171  io.stage2FtqRead.ptr := s1_redirect_bits_reg.ftqIdx
172
173  val s2_target = RegEnable(target, enable = s1_redirect_valid_reg)
174  val s2_pd = RegEnable(s1_pd, enable = s1_redirect_valid_reg)
175  val s2_cfiUpdata_pc = RegEnable(cfiUpdate_pc, enable = s1_redirect_valid_reg)
176  val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, enable = s1_redirect_valid_reg)
177  val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B)
178  val s2_ftqRead = io.stage2FtqRead.entry
179
180  io.stage3Redirect.valid := s2_redirect_valid_reg
181  io.stage3Redirect.bits := s2_redirect_bits_reg
182  val stage3CfiUpdate = io.stage3Redirect.bits.cfiUpdate
183  stage3CfiUpdate.pc := s2_cfiUpdata_pc
184  stage3CfiUpdate.pd := s2_pd
185  stage3CfiUpdate.rasSp := s2_ftqRead.rasSp
186  stage3CfiUpdate.rasEntry := s2_ftqRead.rasTop
187  stage3CfiUpdate.predHist := s2_ftqRead.predHist
188  stage3CfiUpdate.specCnt := s2_ftqRead.specCnt
189  stage3CfiUpdate.hist := s2_ftqRead.hist
190  stage3CfiUpdate.predTaken := s2_redirect_bits_reg.cfiUpdate.predTaken
191  stage3CfiUpdate.sawNotTakenBranch := VecInit((0 until PredictWidth).map{ i =>
192    if(i == 0) false.B else Cat(s2_ftqRead.br_mask.take(i)).orR()
193  })(s2_redirect_bits_reg.ftqOffset)
194  stage3CfiUpdate.target := s2_target
195  stage3CfiUpdate.taken := s2_redirect_bits_reg.cfiUpdate.taken
196  stage3CfiUpdate.isMisPred := s2_redirect_bits_reg.cfiUpdate.isMisPred
197}
198
199class CtrlBlock extends XSModule with HasCircularQueuePtrHelper {
200  val io = IO(new Bundle {
201    val frontend = Flipped(new FrontendToBackendIO)
202    val fromIntBlock = Flipped(new IntBlockToCtrlIO)
203    val fromFpBlock = Flipped(new FpBlockToCtrlIO)
204    val fromLsBlock = Flipped(new LsBlockToCtrlIO)
205    val toIntBlock = new CtrlToIntBlockIO
206    val toFpBlock = new CtrlToFpBlockIO
207    val toLsBlock = new CtrlToLsBlockIO
208    val roqio = new Bundle {
209      // to int block
210      val toCSR = new RoqCSRIO
211      val exception = ValidIO(new ExceptionInfo)
212      // to mem block
213      val lsq = new RoqLsqIO
214    }
215    val csrCtrl = Input(new CustomCSRCtrlIO)
216  })
217
218  val difftestIO = IO(new Bundle() {
219    val fromRoq = new Bundle() {
220      val commit = Output(UInt(32.W))
221      val thisPC = Output(UInt(XLEN.W))
222      val thisINST = Output(UInt(32.W))
223      val skip = Output(UInt(32.W))
224      val wen = Output(UInt(32.W))
225      val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
226      val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6
227      val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
228      val isRVC = Output(UInt(32.W))
229      val scFailed = Output(Bool())
230      val lpaddr = Output(Vec(CommitWidth, UInt(64.W)))
231      val ltype = Output(Vec(CommitWidth, UInt(32.W)))
232      val lfu = Output(Vec(CommitWidth, UInt(4.W)))
233    }
234  })
235  difftestIO <> DontCare
236
237  val ftq = Module(new Ftq)
238  val trapIO = IO(new TrapIO())
239  trapIO <> DontCare
240
241  val decode = Module(new DecodeStage)
242  val rename = Module(new Rename)
243  val dispatch = Module(new Dispatch)
244  val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts))
245  val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts))
246  val redirectGen = Module(new RedirectGenerator)
247
248  val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt
249  val roq = Module(new Roq(roqWbSize))
250
251  val backendRedirect = redirectGen.io.stage2Redirect
252  val frontendRedirect = redirectGen.io.stage3Redirect
253  val flush = roq.io.flushOut.valid
254  val flushReg = RegNext(flush)
255
256  val exuRedirect = io.fromIntBlock.exuRedirect.map(x => {
257    val valid = x.valid && x.bits.redirectValid
258    val killedByOlder = x.bits.uop.roqIdx.needFlush(backendRedirect, flushReg)
259    val delayed = Wire(Valid(new ExuOutput))
260    delayed.valid := RegNext(valid && !killedByOlder, init = false.B)
261    delayed.bits := RegEnable(x.bits, x.valid)
262    delayed
263  })
264  VecInit(ftq.io.ftqRead.tail.dropRight(1)) <> redirectGen.io.stage1FtqRead
265  ftq.io.cfiRead <> redirectGen.io.stage2FtqRead
266  redirectGen.io.exuMispredict <> exuRedirect
267  redirectGen.io.loadReplay := io.fromLsBlock.replay
268  redirectGen.io.flush := flushReg
269
270  ftq.io.enq <> io.frontend.fetchInfo
271  for(i <- 0 until CommitWidth){
272    ftq.io.roq_commits(i).valid := roq.io.commits.valid(i) && !roq.io.commits.isWalk
273    ftq.io.roq_commits(i).bits := roq.io.commits.info(i)
274  }
275  ftq.io.redirect <> backendRedirect
276  ftq.io.flush := flushReg
277  ftq.io.flushIdx := RegNext(roq.io.flushOut.bits.ftqIdx)
278  ftq.io.flushOffset := RegNext(roq.io.flushOut.bits.ftqOffset)
279  ftq.io.frontendRedirect <> frontendRedirect
280  ftq.io.exuWriteback <> exuRedirect
281
282  ftq.io.ftqRead.last.ptr := roq.io.flushOut.bits.ftqIdx
283  val flushPC = GetPcByFtq(
284    ftq.io.ftqRead.last.entry.ftqPC,
285    RegEnable(roq.io.flushOut.bits.ftqOffset, roq.io.flushOut.valid),
286    ftq.io.ftqRead.last.entry.lastPacketPC.valid,
287    ftq.io.ftqRead.last.entry.lastPacketPC.bits
288  )
289
290  val flushRedirect = Wire(Valid(new Redirect))
291  flushRedirect.valid := flushReg
292  flushRedirect.bits := DontCare
293  flushRedirect.bits.ftqIdx := RegEnable(roq.io.flushOut.bits.ftqIdx, flush)
294  flushRedirect.bits.interrupt := true.B
295  flushRedirect.bits.cfiUpdate.target := Mux(io.roqio.toCSR.isXRet || roq.io.exception.valid,
296    io.roqio.toCSR.trapTarget,
297    flushPC + 4.U // flush pipe
298  )
299
300  io.frontend.redirect_cfiUpdate := Mux(flushRedirect.valid, flushRedirect, frontendRedirect)
301  io.frontend.commit_cfiUpdate := ftq.io.commit_ftqEntry
302  io.frontend.ftqEnqPtr := ftq.io.enqPtr
303  io.frontend.ftqLeftOne := ftq.io.leftOne
304
305  decode.io.in <> io.frontend.cfVec
306  // currently, we only update wait table when isReplay
307  decode.io.waitTableUpdate(0) <> RegNext(redirectGen.io.waitTableUpdate)
308  decode.io.waitTableUpdate(1) := DontCare
309  decode.io.waitTableUpdate(1).valid := false.B
310  // decode.io.waitTableUpdate <> io.toLsBlock.waitTableUpdate
311  decode.io.csrCtrl := RegNext(io.csrCtrl)
312
313
314  val jumpInst = dispatch.io.enqIQCtrl(0).bits
315  val ftqOffsetReg = Reg(UInt(log2Up(PredictWidth).W))
316  ftqOffsetReg := jumpInst.cf.ftqOffset
317  ftq.io.ftqRead(0).ptr := jumpInst.cf.ftqPtr // jump
318  io.toIntBlock.jumpPc := GetPcByFtq(
319    ftq.io.ftqRead(0).entry.ftqPC, ftqOffsetReg,
320    ftq.io.ftqRead(0).entry.lastPacketPC.valid,
321    ftq.io.ftqRead(0).entry.lastPacketPC.bits
322  )
323  io.toIntBlock.jalr_target := ftq.io.ftqRead(0).entry.target
324
325  // pipeline between decode and dispatch
326  for (i <- 0 until RenameWidth) {
327    PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready,
328      io.frontend.redirect_cfiUpdate.valid)
329  }
330
331  rename.io.redirect <> backendRedirect
332  rename.io.flush := flushReg
333  rename.io.roqCommits <> roq.io.commits
334  rename.io.out <> dispatch.io.fromRename
335  rename.io.renameBypass <> dispatch.io.renameBypass
336  rename.io.dispatchInfo <> dispatch.io.preDpInfo
337
338  dispatch.io.redirect <> backendRedirect
339  dispatch.io.flush := flushReg
340  dispatch.io.enqRoq <> roq.io.enq
341  dispatch.io.enqLsq <> io.toLsBlock.enqLsq
342  dispatch.io.readIntRf <> io.toIntBlock.readRf
343  dispatch.io.readFpRf <> io.toFpBlock.readRf
344  dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) =>
345    intBusyTable.io.allocPregs(i).valid := preg.isInt
346    fpBusyTable.io.allocPregs(i).valid := preg.isFp
347    intBusyTable.io.allocPregs(i).bits := preg.preg
348    fpBusyTable.io.allocPregs(i).bits := preg.preg
349  }
350  dispatch.io.numExist <> io.fromIntBlock.numExist ++ io.fromFpBlock.numExist ++ io.fromLsBlock.numExist
351  dispatch.io.enqIQCtrl <> io.toIntBlock.enqIqCtrl ++ io.toFpBlock.enqIqCtrl ++ io.toLsBlock.enqIqCtrl
352//  dispatch.io.enqIQData <> io.toIntBlock.enqIqData ++ io.toFpBlock.enqIqData ++ io.toLsBlock.enqIqData
353
354
355  fpBusyTable.io.flush := flushReg
356  intBusyTable.io.flush := flushReg
357  for((wb, setPhyRegRdy) <- io.fromIntBlock.wbRegs.zip(intBusyTable.io.wbPregs)){
358    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen
359    setPhyRegRdy.bits := wb.bits.uop.pdest
360  }
361  for((wb, setPhyRegRdy) <- io.fromFpBlock.wbRegs.zip(fpBusyTable.io.wbPregs)){
362    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen
363    setPhyRegRdy.bits := wb.bits.uop.pdest
364  }
365  intBusyTable.io.read <> dispatch.io.readIntState
366  fpBusyTable.io.read <> dispatch.io.readFpState
367
368  roq.io.redirect <> backendRedirect
369  roq.io.exeWbResults <> (io.fromIntBlock.wbRegs ++ io.fromFpBlock.wbRegs ++ io.fromLsBlock.stOut)
370
371  // TODO: is 'backendRedirect' necesscary?
372  io.toIntBlock.redirect <> backendRedirect
373  io.toIntBlock.flush <> flushReg
374  io.toFpBlock.redirect <> backendRedirect
375  io.toFpBlock.flush <> flushReg
376  io.toLsBlock.redirect <> backendRedirect
377  io.toLsBlock.flush <> flushReg
378
379  if (!env.FPGAPlatform) {
380    difftestIO.fromRoq <> roq.difftestIO
381    trapIO <> roq.trapIO
382  }
383
384  dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex
385  dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex
386
387  // roq to int block
388  io.roqio.toCSR <> roq.io.csr
389  io.roqio.exception := roq.io.exception
390  io.roqio.exception.bits.uop.cf.pc := flushPC
391  // roq to mem block
392  io.roqio.lsq <> roq.io.lsq
393}
394