xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision a31b14cd6beef352717b95ee4694732c63f825c7)
1package xiangshan.backend
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.backend.decode.{DecodeStage, ImmUnion}
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, RoqExceptionInfo}
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 redirect = ValidIO(new Redirect)
41  val flush = Output(Bool())
42}
43
44class RedirectGenerator extends XSModule with HasCircularQueuePtrHelper {
45  val io = IO(new Bundle() {
46    val loadRelay = Flipped(ValidIO(new Redirect))
47    val exuMispredict = Vec(exuParameters.JmpCnt + exuParameters.AluCnt, Flipped(ValidIO(new ExuOutput)))
48    val flush = Input(Bool())
49    val stage2FtqRead = new FtqRead
50    val stage2Redirect = ValidIO(new Redirect)
51    val stage3Redirect = ValidIO(new Redirect)
52  })
53  /*
54        LoadQueue  Jump  ALU0  ALU1  ALU2  ALU3   exception    Stage1
55          |         |      |    |     |     |         |
56          |============= reg & compare =====|         |       ========
57                            |                         |
58                            |                         |
59                            |                         |        Stage2
60                            |                         |
61                    redirect (flush backend)          |
62                    |                                 |
63               === reg ===                            |       ========
64                    |                                 |
65                    |----- mux (exception first) -----|        Stage3
66                            |
67                redirect (send to frontend)
68   */
69  def selectOlderRedirect(x: Valid[Redirect], y: Valid[Redirect]): Valid[Redirect] = {
70    Mux(x.valid,
71      Mux(y.valid,
72        Mux(isAfter(x.bits.roqIdx, y.bits.roqIdx), y, x),
73        x
74      ),
75      y
76    )
77  }
78  def selectOlderExuOutWithFlag(x: Valid[ExuOutput], y: Valid[ExuOutput]): (Valid[ExuOutput], Bool) = {
79    val yIsOlder = Mux(x.valid,
80      Mux(y.valid,
81        Mux(isAfter(x.bits.redirect.roqIdx, y.bits.redirect.roqIdx), true.B, false.B),
82        false.B
83      ),
84      true.B
85    )
86    val sel = Mux(yIsOlder, y, x)
87    (sel, yIsOlder)
88  }
89  def selectOlderExuOut(x: Valid[ExuOutput], y: Valid[ExuOutput]): Valid[ExuOutput] = {
90    selectOlderExuOutWithFlag(x, y)._1
91  }
92  val jumpOut = io.exuMispredict.head
93  val oldestAluOut = ParallelOperation(io.exuMispredict.tail, selectOlderExuOut)
94  val (oldestExuOut, jumpIsOlder) = selectOlderExuOutWithFlag(oldestAluOut, jumpOut) // select between jump and alu
95
96  val oldestMispredict = selectOlderRedirect(io.loadRelay, {
97    val redirect = Wire(Valid(new Redirect))
98    redirect.valid := oldestExuOut.valid
99    redirect.bits := oldestExuOut.bits.redirect
100    redirect
101  })
102
103  XSDebug(oldestExuOut.valid, p"exuMispredict: ${Binary(Cat(io.exuMispredict.map(_.valid)))}\n")
104
105  val s1_isJump = RegNext(jumpIsOlder, init = false.B)
106  val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid)
107  val s1_imm12_reg = RegEnable(oldestExuOut.bits.uop.ctrl.imm(11, 0), oldestExuOut.valid)
108  val s1_pd = RegEnable(oldestExuOut.bits.uop.cf.pd, oldestExuOut.valid)
109  val s1_redirect_bits_reg = Reg(new Redirect)
110  val s1_redirect_valid_reg = RegInit(false.B)
111
112  // stage1 -> stage2
113  when(oldestMispredict.valid && !oldestMispredict.bits.roqIdx.needFlush(io.stage2Redirect, io.flush)){
114    s1_redirect_bits_reg := oldestMispredict.bits
115    s1_redirect_valid_reg := true.B
116  }.otherwise({
117    s1_redirect_valid_reg := false.B
118  })
119  io.stage2Redirect.valid := s1_redirect_valid_reg
120  io.stage2Redirect.bits := s1_redirect_bits_reg
121  io.stage2Redirect.bits.cfiUpdate := DontCare
122  // at stage2, we read ftq to get pc
123  io.stage2FtqRead.ptr := s1_redirect_bits_reg.ftqIdx
124
125  // stage3, calculate redirect target
126  val s2_isJump = RegNext(s1_isJump)
127  val s2_jumpTarget = RegEnable(s1_jumpTarget, s1_redirect_valid_reg)
128  val s2_imm12_reg = RegEnable(s1_imm12_reg, s1_redirect_valid_reg)
129  val s2_pd = RegEnable(s1_pd, s1_redirect_valid_reg)
130  val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, enable = s1_redirect_valid_reg)
131  val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B)
132
133  val ftqRead = io.stage2FtqRead.entry
134  val pc = GetPcByFtq(ftqRead.ftqPC, s2_redirect_bits_reg.ftqOffset, ftqRead.hasLastPrev)
135  val brTarget = pc + SignExt(ImmUnion.B.toImm32(s2_imm12_reg), XLEN)
136  val snpc = pc + Mux(s2_pd.isRVC, 2.U, 4.U)
137  val isReplay = RedirectLevel.flushItself(s2_redirect_bits_reg.level)
138  val target = Mux(isReplay,
139    pc, // repaly from itself
140    Mux(s2_redirect_bits_reg.cfiUpdate.taken,
141      Mux(s2_isJump, s2_jumpTarget, brTarget),
142      snpc
143    )
144  )
145  io.stage3Redirect.valid := s2_redirect_valid_reg
146  io.stage3Redirect.bits := s2_redirect_bits_reg
147  val stage3CfiUpdate = io.stage3Redirect.bits.cfiUpdate
148  stage3CfiUpdate.pc := pc
149  stage3CfiUpdate.pd := s2_pd
150  stage3CfiUpdate.rasSp := ftqRead.rasSp
151  stage3CfiUpdate.rasEntry := ftqRead.rasTop
152  stage3CfiUpdate.hist := ftqRead.hist
153  stage3CfiUpdate.predHist := ftqRead.predHist
154  stage3CfiUpdate.specCnt := ftqRead.specCnt(s2_redirect_bits_reg.ftqOffset)
155  stage3CfiUpdate.predTaken := s2_redirect_bits_reg.cfiUpdate.predTaken
156  stage3CfiUpdate.sawNotTakenBranch := VecInit((0 until PredictWidth).map{ i =>
157    if(i == 0) false.B else Cat(ftqRead.br_mask.take(i)).orR()
158  })(s2_redirect_bits_reg.ftqOffset)
159  stage3CfiUpdate.target := target
160  stage3CfiUpdate.taken := s2_redirect_bits_reg.cfiUpdate.taken
161  stage3CfiUpdate.isMisPred := s2_redirect_bits_reg.cfiUpdate.isMisPred
162}
163
164class CtrlBlock extends XSModule with HasCircularQueuePtrHelper {
165  val io = IO(new Bundle {
166    val frontend = Flipped(new FrontendToBackendIO)
167    val fromIntBlock = Flipped(new IntBlockToCtrlIO)
168    val fromFpBlock = Flipped(new FpBlockToCtrlIO)
169    val fromLsBlock = Flipped(new LsBlockToCtrlIO)
170    val toIntBlock = new CtrlToIntBlockIO
171    val toFpBlock = new CtrlToFpBlockIO
172    val toLsBlock = new CtrlToLsBlockIO
173    val roqio = new Bundle {
174      // to int block
175      val toCSR = new RoqCSRIO
176      val exception = ValidIO(new RoqExceptionInfo)
177      // to mem block
178      val lsq = new RoqLsqIO
179    }
180  })
181
182  val difftestIO = IO(new Bundle() {
183    val fromRoq = new Bundle() {
184      val commit = Output(UInt(32.W))
185      val thisPC = Output(UInt(XLEN.W))
186      val thisINST = Output(UInt(32.W))
187      val skip = Output(UInt(32.W))
188      val wen = Output(UInt(32.W))
189      val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
190      val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6
191      val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
192      val isRVC = Output(UInt(32.W))
193      val scFailed = Output(Bool())
194    }
195  })
196  difftestIO <> DontCare
197
198  val ftq = Module(new Ftq)
199  val trapIO = IO(new TrapIO())
200  trapIO <> DontCare
201
202  val decode = Module(new DecodeStage)
203  val rename = Module(new Rename)
204  val dispatch = Module(new Dispatch)
205  val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts))
206  val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts))
207  val redirectGen = Module(new RedirectGenerator)
208
209  val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt
210
211  val roq = Module(new Roq(roqWbSize))
212
213  val backendRedirect = redirectGen.io.stage2Redirect
214  val frontendRedirect = redirectGen.io.stage3Redirect
215  val flush = roq.io.flushOut.valid
216
217  redirectGen.io.exuMispredict.zip(io.fromIntBlock.exuRedirect).map({case (x, y) =>
218    x.valid := y.valid && y.bits.redirect.cfiUpdate.isMisPred
219    x.bits := y.bits
220  })
221  redirectGen.io.loadRelay := io.fromLsBlock.replay
222  redirectGen.io.flush := flush
223
224  ftq.io.enq <> io.frontend.fetchInfo
225  for(i <- 0 until CommitWidth){
226    ftq.io.roq_commits(i).valid := roq.io.commits.valid(i) && !roq.io.commits.isWalk
227    ftq.io.roq_commits(i).bits := roq.io.commits.info(i)
228  }
229  ftq.io.redirect <> backendRedirect
230  ftq.io.flush := flush
231  ftq.io.flushIdx := roq.io.flushOut.bits.ftqIdx
232  ftq.io.flushOffset := roq.io.flushOut.bits.ftqOffset
233  ftq.io.frontendRedirect <> frontendRedirect
234  ftq.io.exuWriteback <> io.fromIntBlock.exuRedirect
235
236  ftq.io.ftqRead(1) <> redirectGen.io.stage2FtqRead
237  ftq.io.ftqRead(2).ptr := roq.io.flushOut.bits.ftqIdx
238  val flushPC = GetPcByFtq(
239    ftq.io.ftqRead(2).entry.ftqPC,
240    RegEnable(roq.io.flushOut.bits.ftqOffset, roq.io.flushOut.valid),
241    ftq.io.ftqRead(2).entry.hasLastPrev
242  )
243
244  val flushRedirect = Wire(Valid(new Redirect))
245  flushRedirect.valid := RegNext(flush)
246  flushRedirect.bits := DontCare
247  flushRedirect.bits.ftqIdx := RegEnable(roq.io.flushOut.bits.ftqIdx, flush)
248  flushRedirect.bits.interrupt := true.B
249  flushRedirect.bits.cfiUpdate.target := Mux(io.roqio.toCSR.isXRet || roq.io.exception.valid,
250    io.roqio.toCSR.trapTarget,
251    flushPC + 4.U // flush pipe
252  )
253
254  io.frontend.redirect_cfiUpdate := Mux(flushRedirect.valid, flushRedirect, frontendRedirect)
255  io.frontend.commit_cfiUpdate := ftq.io.commit_ftqEntry
256  io.frontend.ftqEnqPtr := ftq.io.enqPtr
257  io.frontend.ftqLeftOne := ftq.io.leftOne
258
259  decode.io.in <> io.frontend.cfVec
260
261  val jumpInst = dispatch.io.enqIQCtrl(0).bits
262  val ftqOffsetReg = Reg(UInt(log2Up(PredictWidth).W))
263  ftqOffsetReg := jumpInst.cf.ftqOffset
264  ftq.io.ftqRead(0).ptr := jumpInst.cf.ftqPtr // jump
265  io.toIntBlock.jumpPc := GetPcByFtq(
266    ftq.io.ftqRead(0).entry.ftqPC, ftqOffsetReg, ftq.io.ftqRead(0).entry.hasLastPrev
267  )
268  io.toIntBlock.jalr_target := ftq.io.ftqRead(0).entry.target
269
270  // pipeline between decode and dispatch
271  for (i <- 0 until RenameWidth) {
272    PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready,
273      backendRedirect.valid || flush || io.frontend.redirect_cfiUpdate.valid)
274  }
275
276  rename.io.redirect <> backendRedirect
277  rename.io.flush := flush
278  rename.io.roqCommits <> roq.io.commits
279  rename.io.out <> dispatch.io.fromRename
280  rename.io.renameBypass <> dispatch.io.renameBypass
281
282  dispatch.io.redirect <> backendRedirect
283  dispatch.io.flush := flush
284  dispatch.io.enqRoq <> roq.io.enq
285  dispatch.io.enqLsq <> io.toLsBlock.enqLsq
286  dispatch.io.readIntRf <> io.toIntBlock.readRf
287  dispatch.io.readFpRf <> io.toFpBlock.readRf
288  dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) =>
289    intBusyTable.io.allocPregs(i).valid := preg.isInt
290    fpBusyTable.io.allocPregs(i).valid := preg.isFp
291    intBusyTable.io.allocPregs(i).bits := preg.preg
292    fpBusyTable.io.allocPregs(i).bits := preg.preg
293  }
294  dispatch.io.numExist <> io.fromIntBlock.numExist ++ io.fromFpBlock.numExist ++ io.fromLsBlock.numExist
295  dispatch.io.enqIQCtrl <> io.toIntBlock.enqIqCtrl ++ io.toFpBlock.enqIqCtrl ++ io.toLsBlock.enqIqCtrl
296//  dispatch.io.enqIQData <> io.toIntBlock.enqIqData ++ io.toFpBlock.enqIqData ++ io.toLsBlock.enqIqData
297
298
299  fpBusyTable.io.flush := flush
300  intBusyTable.io.flush := flush
301  for((wb, setPhyRegRdy) <- io.fromIntBlock.wbRegs.zip(intBusyTable.io.wbPregs)){
302    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen
303    setPhyRegRdy.bits := wb.bits.uop.pdest
304  }
305  for((wb, setPhyRegRdy) <- io.fromFpBlock.wbRegs.zip(fpBusyTable.io.wbPregs)){
306    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen
307    setPhyRegRdy.bits := wb.bits.uop.pdest
308  }
309  intBusyTable.io.read <> dispatch.io.readIntState
310  fpBusyTable.io.read <> dispatch.io.readFpState
311
312  roq.io.redirect <> backendRedirect
313  roq.io.exeWbResults.zip(
314    io.fromIntBlock.wbRegs ++ io.fromFpBlock.wbRegs ++ io.fromLsBlock.stOut
315  ).foreach{
316    case(x, y) =>
317      x.bits := y.bits
318      x.valid := y.valid
319  }
320
321  // TODO: is 'backendRedirect' necesscary?
322  io.toIntBlock.redirect <> backendRedirect
323  io.toIntBlock.flush <> flush
324  io.toFpBlock.redirect <> backendRedirect
325  io.toFpBlock.flush <> flush
326  io.toLsBlock.redirect <> backendRedirect
327  io.toLsBlock.flush <> RegNext(flush)
328
329  if (env.DualCoreDifftest) {
330    difftestIO.fromRoq <> roq.difftestIO
331    trapIO <> roq.trapIO
332  }
333
334  dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex
335  dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex
336
337  // roq to int block
338  io.roqio.toCSR <> roq.io.csr
339  io.roqio.exception := roq.io.exception
340  io.roqio.exception.bits.uop.cf.pc := flushPC
341  // roq to mem block
342  io.roqio.lsq <> roq.io.lsq
343}
344