CtrlBlock.scala 15.2 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

17 18
package xiangshan.backend

19
import chipsalliance.rocketchip.config.Parameters
20 21
import chisel3._
import chisel3.util._
Y
Yinan Xu 已提交
22
import utils._
23
import xiangshan._
24
import xiangshan.backend.decode.{DecodeStage, ImmUnion}
25 26 27 28
import xiangshan.backend.dispatch.{Dispatch, DispatchQueue}
import xiangshan.backend.rename.Rename
import xiangshan.backend.rob.{Rob, RobCSRIO, RobLsqIO}
import xiangshan.frontend.{FtqPtr, FtqRead}
Y
Yinan Xu 已提交
29
import xiangshan.mem.LsqEnqIO
W
William Wang 已提交
30
import difftest._
31

32
class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
Y
Yinan Xu 已提交
33
  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
34
  val stage2Redirect = Valid(new Redirect)
L
Lingrui98 已提交
35
  val stage3Redirect = ValidIO(new Redirect)
Y
Yinan Xu 已提交
36
  val robFlush = Valid(new Bundle {
37 38
    val ftqIdx = Output(new FtqPtr)
    val ftqOffset = Output(UInt(log2Up(PredictWidth).W))
39
    val replayInst = Output(Bool()) // not used for now
40 41 42
  })
}

43
class RedirectGenerator(implicit p: Parameters) extends XSModule
44
  with HasCircularQueuePtrHelper {
L
ljw 已提交
45
  val numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt
L
LinJiawei 已提交
46
  val io = IO(new Bundle() {
L
ljw 已提交
47
    val exuMispredict = Vec(numRedirect, Flipped(ValidIO(new ExuOutput)))
L
ljw 已提交
48
    val loadReplay = Flipped(ValidIO(new Redirect))
49
    val flush = Input(Bool())
Z
zoujr 已提交
50
    val stage1PcRead = Vec(numRedirect+1, new FtqRead(UInt(VAddrBits.W)))
L
LinJiawei 已提交
51
    val stage2Redirect = ValidIO(new Redirect)
L
LinJiawei 已提交
52
    val stage3Redirect = ValidIO(new Redirect)
53
    val memPredUpdate = Output(new MemPredUpdateReq)
Z
zoujr 已提交
54
    val memPredPcRead = new FtqRead(UInt(VAddrBits.W)) // read req send form stage 2
L
LinJiawei 已提交
55 56 57 58
  })
  /*
        LoadQueue  Jump  ALU0  ALU1  ALU2  ALU3   exception    Stage1
          |         |      |    |     |     |         |
L
LinJiawei 已提交
59
          |============= reg & compare =====|         |       ========
L
LinJiawei 已提交
60 61 62 63
                            |                         |
                            |                         |
                            |                         |        Stage2
                            |                         |
L
LinJiawei 已提交
64 65 66 67 68 69 70 71
                    redirect (flush backend)          |
                    |                                 |
               === reg ===                            |       ========
                    |                                 |
                    |----- mux (exception first) -----|        Stage3
                            |
                redirect (send to frontend)
   */
L
ljw 已提交
72 73 74 75
  private class Wrapper(val n: Int) extends Bundle {
    val redirect = new Redirect
    val valid = Bool()
    val idx = UInt(log2Up(n).W)
76
  }
77
  def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = {
Y
Yinan Xu 已提交
78
    val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.robIdx, xs(i).bits.robIdx)))
79 80 81 82 83 84
    val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j =>
      (if (j < i) !xs(j).valid || compareVec(i)(j)
      else if (j == i) xs(i).valid
      else !xs(j).valid || !compareVec(j)(i))
    )).andR))
    resultOnehot
L
LinJiawei 已提交
85 86
  }

87
  val redirects = io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits
Y
Yinan Xu 已提交
88 89
  val stage1FtqReadPcs =
    (io.stage1PcRead zip redirects).map{ case (r, redirect) =>
90 91
      r(redirect.ftqIdx, redirect.ftqOffset)
    }
L
ljw 已提交
92 93

  def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = {
L
LinJiawei 已提交
94
    val redirect = Wire(Valid(new Redirect))
L
ljw 已提交
95 96
    redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred
    redirect.bits := exuOut.bits.redirect
L
LinJiawei 已提交
97
    redirect
L
ljw 已提交
98
  }
L
LinJiawei 已提交
99

L
ljw 已提交
100
  val jumpOut = io.exuMispredict.head
101 102
  val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay)
  val oldestOneHot = selectOldestRedirect(allRedirect)
Y
Yinan Xu 已提交
103
  val needFlushVec = VecInit(allRedirect.map(_.bits.robIdx.needFlush(io.stage2Redirect, io.flush)))
104
  val oldestValid = VecInit(oldestOneHot.zip(needFlushVec).map{ case (v, f) => v && !f }).asUInt.orR
105
  val oldestExuOutput = Mux1H(io.exuMispredict.indices.map(oldestOneHot), io.exuMispredict)
106
  val oldestRedirect = Mux1H(oldestOneHot, allRedirect)
107

L
LinJiawei 已提交
108
  val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid)
109 110 111 112 113
  val s1_imm12_reg = RegNext(oldestExuOutput.bits.uop.ctrl.imm(11, 0))
  val s1_pd = RegNext(oldestExuOutput.bits.uop.cf.pd)
  val s1_redirect_bits_reg = RegNext(oldestRedirect.bits)
  val s1_redirect_valid_reg = RegNext(oldestValid)
  val s1_redirect_onehot = RegNext(oldestOneHot)
L
LinJiawei 已提交
114 115

  // stage1 -> stage2
116
  io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush
L
LinJiawei 已提交
117 118 119
  io.stage2Redirect.bits := s1_redirect_bits_reg
  io.stage2Redirect.bits.cfiUpdate := DontCare

120 121
  val s1_isReplay = s1_redirect_onehot.last
  val s1_isJump = s1_redirect_onehot.head
122
  val real_pc = Mux1H(s1_redirect_onehot, stage1FtqReadPcs)
L
ljw 已提交
123 124
  val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN)
  val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U)
125
  val target = Mux(s1_isReplay,
126
    real_pc, // replay from itself
L
ljw 已提交
127 128
    Mux(s1_redirect_bits_reg.cfiUpdate.taken,
      Mux(s1_isJump, s1_jumpTarget, brTarget),
L
LinJiawei 已提交
129
      snpc
L
LinJiawei 已提交
130 131
    )
  )
132

133 134 135
  // get pc from ftq
  // valid only if redirect is caused by load violation
  // store_pc is used to update store set
136
  val store_pc = io.memPredPcRead(s1_redirect_bits_reg.stFtqIdx, s1_redirect_bits_reg.stFtqOffset)
137 138 139 140 141 142 143 144 145 146

  // update load violation predictor if load violation redirect triggered
  io.memPredUpdate.valid := RegNext(s1_isReplay && s1_redirect_valid_reg, init = false.B)
  // update wait table
  io.memPredUpdate.waddr := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth))
  io.memPredUpdate.wdata := true.B
  // update store set
  io.memPredUpdate.ldpc := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth))
  // store pc is ready 1 cycle after s1_isReplay is judged
  io.memPredUpdate.stpc := XORFold(store_pc(VAddrBits-1, 1), MemPredPCWidth)
147

L
ljw 已提交
148 149
  val s2_target = RegEnable(target, enable = s1_redirect_valid_reg)
  val s2_pd = RegEnable(s1_pd, enable = s1_redirect_valid_reg)
150
  val s2_pc = RegEnable(real_pc, enable = s1_redirect_valid_reg)
L
ljw 已提交
151 152 153
  val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, enable = s1_redirect_valid_reg)
  val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B)

L
LinJiawei 已提交
154 155 156
  io.stage3Redirect.valid := s2_redirect_valid_reg
  io.stage3Redirect.bits := s2_redirect_bits_reg
  val stage3CfiUpdate = io.stage3Redirect.bits.cfiUpdate
157
  stage3CfiUpdate.pc := s2_pc
L
LinJiawei 已提交
158
  stage3CfiUpdate.pd := s2_pd
L
LinJiawei 已提交
159
  stage3CfiUpdate.predTaken := s2_redirect_bits_reg.cfiUpdate.predTaken
L
ljw 已提交
160
  stage3CfiUpdate.target := s2_target
L
LinJiawei 已提交
161 162
  stage3CfiUpdate.taken := s2_redirect_bits_reg.cfiUpdate.taken
  stage3CfiUpdate.isMisPred := s2_redirect_bits_reg.cfiUpdate.isMisPred
W
William Wang 已提交
163 164 165 166 167 168 169 170 171 172 173

  // recover runahead checkpoint if redirect
  if (!env.FPGAPlatform) {
    val runahead_redirect = Module(new DifftestRunaheadRedirectEvent)
    runahead_redirect.io.clock := clock
    runahead_redirect.io.coreid := hardId.U
    runahead_redirect.io.valid := io.stage3Redirect.valid
    runahead_redirect.io.pc :=  s2_pc // for debug only
    runahead_redirect.io.target_pc := s2_target // for debug only
    runahead_redirect.io.checkpoint_id := io.stage3Redirect.bits.debug_runahead_checkpoint_id // make sure it is right
  }
L
LinJiawei 已提交
174 175
}

176
class CtrlBlock(implicit p: Parameters) extends XSModule
177
  with HasCircularQueuePtrHelper {
178
  val io = IO(new Bundle {
179
    val frontend = Flipped(new FrontendToCtrlIO)
180 181
    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
    val dispatch = Vec(3*dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
182 183 184 185 186 187 188 189
    // from int block
    val exuRedirect = Vec(exuParameters.AluCnt + exuParameters.JmpCnt, Flipped(ValidIO(new ExuOutput)))
    val stIn = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuInput)))
    val stOut = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuOutput)))
    val memoryViolation = Flipped(ValidIO(new Redirect))
    val enqLsq = Flipped(new LsqEnqIO)
    val jumpPc = Output(UInt(VAddrBits.W))
    val jalr_target = Output(UInt(VAddrBits.W))
Y
Yinan Xu 已提交
190
    val robio = new Bundle {
Y
Yinan Xu 已提交
191
      // to int block
Y
Yinan Xu 已提交
192
      val toCSR = new RobCSRIO
193
      val exception = ValidIO(new ExceptionInfo)
Y
Yinan Xu 已提交
194
      // to mem block
Y
Yinan Xu 已提交
195
      val lsq = new RobLsqIO
Y
Yinan Xu 已提交
196
    }
197
    val csrCtrl = Input(new CustomCSRCtrlIO)
198 199
    val perfInfo = Output(new Bundle{
      val ctrlInfo = new Bundle {
Y
Yinan Xu 已提交
200
        val robFull   = Input(Bool())
201 202 203 204 205
        val intdqFull = Input(Bool())
        val fpdqFull  = Input(Bool())
        val lsdqFull  = Input(Bool())
      }
    })
206
    val writeback = Vec(NRIntWritePorts + NRFpWritePorts, Flipped(ValidIO(new ExuOutput)))
207 208 209 210 211
    // redirect out
    val redirect = ValidIO(new Redirect)
    val flush = Output(Bool())
    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
212 213 214 215
  })

  val decode = Module(new DecodeStage)
  val rename = Module(new Rename)
216
  val dispatch = Module(new Dispatch)
217 218 219
  val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth, "int"))
  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth, "fp"))
  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth, "ls"))
L
LinJiawei 已提交
220
  val redirectGen = Module(new RedirectGenerator)
221

Y
Yinan Xu 已提交
222 223
  val robWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt
  val rob = Module(new Rob(robWbSize))
224

225 226
  val stage2Redirect = redirectGen.io.stage2Redirect
  val stage3Redirect = redirectGen.io.stage3Redirect
Y
Yinan Xu 已提交
227
  val flush = rob.io.flushOut.valid
228
  val flushReg = RegNext(flush)
L
LinJiawei 已提交
229

230
  val exuRedirect = io.exuRedirect.map(x => {
L
ljw 已提交
231
    val valid = x.valid && x.bits.redirectValid
Y
Yinan Xu 已提交
232
    val killedByOlder = x.bits.uop.robIdx.needFlush(stage2Redirect, flushReg)
L
ljw 已提交
233 234 235 236
    val delayed = Wire(Valid(new ExuOutput))
    delayed.valid := RegNext(valid && !killedByOlder, init = false.B)
    delayed.bits := RegEnable(x.bits, x.valid)
    delayed
L
LinJiawei 已提交
237
  })
L
ljw 已提交
238
  val loadReplay = Wire(Valid(new Redirect))
239
  loadReplay.valid := RegNext(io.memoryViolation.valid &&
Y
Yinan Xu 已提交
240
    !io.memoryViolation.bits.robIdx.needFlush(stage2Redirect, flushReg),
L
ljw 已提交
241 242
    init = false.B
  )
243
  loadReplay.bits := RegEnable(io.memoryViolation.bits, io.memoryViolation.valid)
244 245
  io.frontend.fromFtq.getRedirectPcRead <> redirectGen.io.stage1PcRead
  io.frontend.fromFtq.getMemPredPcRead <> redirectGen.io.memPredPcRead
L
ljw 已提交
246
  redirectGen.io.exuMispredict <> exuRedirect
L
ljw 已提交
247
  redirectGen.io.loadReplay <> loadReplay
248
  redirectGen.io.flush := flushReg
249

L
LinJiawei 已提交
250
  for(i <- 0 until CommitWidth){
Y
Yinan Xu 已提交
251 252
    io.frontend.toFtq.rob_commits(i).valid := rob.io.commits.valid(i) && !rob.io.commits.isWalk
    io.frontend.toFtq.rob_commits(i).bits := rob.io.commits.info(i)
L
LinJiawei 已提交
253
  }
254
  io.frontend.toFtq.stage2Redirect <> stage2Redirect
Y
Yinan Xu 已提交
255
  io.frontend.toFtq.robFlush <> RegNext(rob.io.flushOut)
L
Lingrui98 已提交
256

Y
Yinan Xu 已提交
257 258
  val robPcRead = io.frontend.fromFtq.getRobFlushPcRead
  val flushPC = robPcRead(rob.io.flushOut.bits.ftqIdx, rob.io.flushOut.bits.ftqOffset)
259 260

  val flushRedirect = Wire(Valid(new Redirect))
261
  flushRedirect.valid := flushReg
262
  flushRedirect.bits := DontCare
Y
Yinan Xu 已提交
263
  flushRedirect.bits.ftqIdx := RegEnable(rob.io.flushOut.bits.ftqIdx, flush)
264
  flushRedirect.bits.interrupt := true.B
Y
Yinan Xu 已提交
265 266 267
  flushRedirect.bits.cfiUpdate.target := Mux(io.robio.toCSR.isXRet || rob.io.exception.valid,
    io.robio.toCSR.trapTarget,
    Mux(RegEnable(rob.io.flushOut.bits.replayInst, flush),
W
William Wang 已提交
268 269 270
      flushPC, // replay inst
      flushPC + 4.U // flush pipe
    )
271
  )
Y
Yinan Xu 已提交
272
  when (flushRedirect.valid && RegEnable(rob.io.flushOut.bits.replayInst, flush)) {
273 274
    XSDebug("replay inst (%x) from rob\n", flushPC);
  }
L
ljw 已提交
275 276 277
  val flushRedirectReg = Wire(Valid(new Redirect))
  flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B)
  flushRedirectReg.bits := RegEnable(flushRedirect.bits, enable = flushRedirect.valid)
L
LinJiawei 已提交
278

279
  io.frontend.toFtq.stage3Redirect := Mux(flushRedirectReg.valid, flushRedirectReg, stage3Redirect)
Y
Yinan Xu 已提交
280

281
  decode.io.in <> io.frontend.cfVec
282
  // currently, we only update wait table when isReplay
283 284 285
  decode.io.memPredUpdate(0) <> RegNext(redirectGen.io.memPredUpdate)
  decode.io.memPredUpdate(1) := DontCare
  decode.io.memPredUpdate(1).valid := false.B
286 287
  decode.io.csrCtrl := RegNext(io.csrCtrl)

288

289
  val jumpInst = io.dispatch(0).bits
290
  val jumpPcRead = io.frontend.fromFtq.getJumpPcRead
291
  io.jumpPc := jumpPcRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
292
  val jumpTargetRead = io.frontend.fromFtq.target_read
293
  io.jalr_target := jumpTargetRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
L
LinJiawei 已提交
294

295 296
  // pipeline between decode and rename
  val redirectValid = stage2Redirect.valid || flushReg
297
  for (i <- 0 until RenameWidth) {
L
LinJiawei 已提交
298
    PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready,
299
      flushReg || io.frontend.toFtq.stage3Redirect.valid)
300
  }
301

302
  rename.io.redirect <> stage2Redirect
303
  rename.io.flush := flushReg
Y
Yinan Xu 已提交
304
  rename.io.robCommits <> rob.io.commits
305

306 307 308 309 310 311 312 313
  // pipeline between rename and dispatch
  for (i <- 0 until RenameWidth) {
    PipelineConnect(rename.io.out(i), dispatch.io.fromRename(i), dispatch.io.recv(i), redirectValid)
  }
  dispatch.io.renameBypass := RegEnable(rename.io.renameBypass, rename.io.out(0).fire)
  dispatch.io.preDpInfo := RegEnable(rename.io.dispatchInfo, rename.io.out(0).fire)

  dispatch.io.flush <> flushReg
314
  dispatch.io.redirect <> stage2Redirect
Y
Yinan Xu 已提交
315
  dispatch.io.enqRob <> rob.io.enq
316
  dispatch.io.enqLsq <> io.enqLsq
317 318 319 320
  dispatch.io.toIntDq <> intDq.io.enq
  dispatch.io.toFpDq <> fpDq.io.enq
  dispatch.io.toLsDq <> lsDq.io.enq
  dispatch.io.allocPregs <> io.allocPregs
321
  dispatch.io.csrCtrl <> io.csrCtrl
322
  dispatch.io.storeIssue <> io.stIn
323 324 325 326 327 328 329 330 331 332
  dispatch.io.singleStep := false.B

  intDq.io.redirect <> stage2Redirect
  intDq.io.flush <> flushReg
  fpDq.io.redirect <> stage2Redirect
  fpDq.io.flush <> flushReg
  lsDq.io.redirect <> stage2Redirect
  lsDq.io.flush <> flushReg

  io.dispatch <> intDq.io.deq ++ lsDq.io.deq ++ fpDq.io.deq
Y
Yinan Xu 已提交
333

Y
Yinan Xu 已提交
334
  rob.io.redirect <> stage2Redirect
335
  val exeWbResults = VecInit(io.writeback ++ io.stOut)
336
  val timer = GTimer()
Y
Yinan Xu 已提交
337 338 339 340
  for((rob_wb, wb) <- rob.io.exeWbResults.zip(exeWbResults)) {
    rob_wb.valid := RegNext(wb.valid && !wb.bits.uop.robIdx.needFlush(stage2Redirect, flushReg))
    rob_wb.bits := RegNext(wb.bits)
    rob_wb.bits.uop.debugInfo.writebackTime := timer
L
ljw 已提交
341
  }
L
LinJiawei 已提交
342

343
  io.redirect <> stage2Redirect
344 345 346 347
  io.flush <> flushReg
  io.debug_int_rat <> rename.io.debug_int_rat
  io.debug_fp_rat <> rename.io.debug_fp_rat

Y
Yinan Xu 已提交
348 349 350 351 352
  // rob to int block
  io.robio.toCSR <> rob.io.csr
  io.robio.toCSR.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
  io.robio.exception := rob.io.exception
  io.robio.exception.bits.uop.cf.pc := flushPC
353

Y
Yinan Xu 已提交
354 355
  // rob to mem block
  io.robio.lsq <> rob.io.lsq
356

Y
Yinan Xu 已提交
357
  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
358 359 360
  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
361
}