CtrlBlock.scala 15.1 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}
L
LinJiawei 已提交
25
import xiangshan.backend.rename.{BusyTable, Rename}
26 27
import xiangshan.backend.dispatch.Dispatch
import xiangshan.backend.exu._
28
import xiangshan.frontend.{FtqRead, FtqToCtrlIO, FtqPtr}
29
import xiangshan.backend.roq.{Roq, RoqCSRIO, RoqLsqIO, RoqPtr}
Y
Yinan Xu 已提交
30
import xiangshan.mem.LsqEnqIO
31

32 33 34
class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
  val roq_commits = Vec(CommitWidth, Valid(new RoqCommitInfo))
  val stage2Redirect = Valid(new Redirect)
L
Lingrui98 已提交
35
  val stage3Redirect = ValidIO(new Redirect)
36 37 38
  val roqFlush = Valid(new Bundle {
    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 78 79 80 81 82 83 84
  def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = {
    val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.roqIdx, xs(i).bits.roqIdx)))
    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 88
  val redirects = io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits
  val stage1FtqReadPcs = 
L
Lingrui98 已提交
89
    (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 103 104
  val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay)
  val oldestOneHot = selectOldestRedirect(allRedirect)
  val needFlushVec = VecInit(allRedirect.map(_.bits.roqIdx.needFlush(io.stage2Redirect, io.flush)))
  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, // repaly 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
L
LinJiawei 已提交
163 164
}

165
class CtrlBlock(implicit p: Parameters) extends XSModule
166
  with HasCircularQueuePtrHelper {
167
  val io = IO(new Bundle {
168
    val frontend = Flipped(new FrontendToCtrlIO)
169
    val enqIQ = Vec(exuParameters.CriticalExuCnt, DecoupledIO(new MicroOp))
170 171 172 173 174 175 176 177
    // 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 已提交
178 179 180
    val roqio = new Bundle {
      // to int block
      val toCSR = new RoqCSRIO
181
      val exception = ValidIO(new ExceptionInfo)
Y
Yinan Xu 已提交
182
      // to mem block
183
      val lsq = new RoqLsqIO
Y
Yinan Xu 已提交
184
    }
185
    val csrCtrl = Input(new CustomCSRCtrlIO)
186 187 188 189 190 191 192 193
    val perfInfo = Output(new Bundle{
      val ctrlInfo = new Bundle {
        val roqFull   = Input(Bool())
        val intdqFull = Input(Bool())
        val fpdqFull  = Input(Bool())
        val lsdqFull  = Input(Bool())
      }
    })
194
    val writeback = Vec(NRIntWritePorts + NRFpWritePorts, Flipped(ValidIO(new ExuOutput)))
195 196 197 198 199 200 201
    // redirect out
    val redirect = ValidIO(new Redirect)
    val flush = Output(Bool())
    val readIntRf = Vec(NRIntReadPorts, Output(UInt(PhyRegIdxWidth.W)))
    val readFpRf = Vec(NRFpReadPorts, Output(UInt(PhyRegIdxWidth.W)))
    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
202 203 204 205
  })

  val decode = Module(new DecodeStage)
  val rename = Module(new Rename)
206
  val dispatch = Module(new Dispatch)
Y
Yinan Xu 已提交
207 208
  val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts))
  val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts))
L
LinJiawei 已提交
209
  val redirectGen = Module(new RedirectGenerator)
210

L
LinJiawei 已提交
211
  val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt
212
  val roq = Module(new Roq(roqWbSize))
213

214 215
  val stage2Redirect = redirectGen.io.stage2Redirect
  val stage3Redirect = redirectGen.io.stage3Redirect
216
  val flush = roq.io.flushOut.valid
217
  val flushReg = RegNext(flush)
L
LinJiawei 已提交
218

219
  val exuRedirect = io.exuRedirect.map(x => {
L
ljw 已提交
220
    val valid = x.valid && x.bits.redirectValid
221
    val killedByOlder = x.bits.uop.roqIdx.needFlush(stage2Redirect, flushReg)
L
ljw 已提交
222 223 224 225
    val delayed = Wire(Valid(new ExuOutput))
    delayed.valid := RegNext(valid && !killedByOlder, init = false.B)
    delayed.bits := RegEnable(x.bits, x.valid)
    delayed
L
LinJiawei 已提交
226
  })
L
ljw 已提交
227
  val loadReplay = Wire(Valid(new Redirect))
228
  loadReplay.valid := RegNext(io.memoryViolation.valid &&
229
    !io.memoryViolation.bits.roqIdx.needFlush(stage2Redirect, flushReg),
L
ljw 已提交
230 231
    init = false.B
  )
232
  loadReplay.bits := RegEnable(io.memoryViolation.bits, io.memoryViolation.valid)
233 234
  io.frontend.fromFtq.getRedirectPcRead <> redirectGen.io.stage1PcRead
  io.frontend.fromFtq.getMemPredPcRead <> redirectGen.io.memPredPcRead
L
ljw 已提交
235
  redirectGen.io.exuMispredict <> exuRedirect
L
ljw 已提交
236
  redirectGen.io.loadReplay <> loadReplay
237
  redirectGen.io.flush := flushReg
238

L
LinJiawei 已提交
239
  for(i <- 0 until CommitWidth){
L
Lingrui98 已提交
240 241
    io.frontend.toFtq.roq_commits(i).valid := roq.io.commits.valid(i) && !roq.io.commits.isWalk
    io.frontend.toFtq.roq_commits(i).bits := roq.io.commits.info(i)
L
LinJiawei 已提交
242
  }
243 244
  io.frontend.toFtq.stage2Redirect <> stage2Redirect
  io.frontend.toFtq.roqFlush <> RegNext(roq.io.flushOut)
L
Lingrui98 已提交
245

246 247
  val roqPcRead = io.frontend.fromFtq.getRoqFlushPcRead
  val flushPC = roqPcRead(roq.io.flushOut.bits.ftqIdx, roq.io.flushOut.bits.ftqOffset)
248 249

  val flushRedirect = Wire(Valid(new Redirect))
250
  flushRedirect.valid := flushReg
251 252 253
  flushRedirect.bits := DontCare
  flushRedirect.bits.ftqIdx := RegEnable(roq.io.flushOut.bits.ftqIdx, flush)
  flushRedirect.bits.interrupt := true.B
L
LinJiawei 已提交
254 255
  flushRedirect.bits.cfiUpdate.target := Mux(io.roqio.toCSR.isXRet || roq.io.exception.valid,
    io.roqio.toCSR.trapTarget,
W
William Wang 已提交
256 257 258 259
    Mux(RegEnable(roq.io.flushOut.bits.replayInst, flush),
      flushPC, // replay inst
      flushPC + 4.U // flush pipe
    )
260
  )
261 262 263
  when (flushRedirect.valid && RegEnable(roq.io.flushOut.bits.replayInst, flush)) {
    XSDebug("replay inst (%x) from rob\n", flushPC);
  }
L
ljw 已提交
264 265 266
  val flushRedirectReg = Wire(Valid(new Redirect))
  flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B)
  flushRedirectReg.bits := RegEnable(flushRedirect.bits, enable = flushRedirect.valid)
L
LinJiawei 已提交
267

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

270
  decode.io.in <> io.frontend.cfVec
271
  // currently, we only update wait table when isReplay
272 273 274 275
  decode.io.memPredUpdate(0) <> RegNext(redirectGen.io.memPredUpdate)
  decode.io.memPredUpdate(1) := DontCare
  decode.io.memPredUpdate(1).valid := false.B
  // decode.io.memPredUpdate <> io.toLsBlock.memPredUpdate
276 277
  decode.io.csrCtrl := RegNext(io.csrCtrl)

278

L
LinJiawei 已提交
279
  val jumpInst = dispatch.io.enqIQCtrl(0).bits
280
  val jumpPcRead = io.frontend.fromFtq.getJumpPcRead
281
  io.jumpPc := jumpPcRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
282
  val jumpTargetRead = io.frontend.fromFtq.target_read
283
  io.jalr_target := jumpTargetRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
L
LinJiawei 已提交
284

285 286
  // pipeline between decode and dispatch
  for (i <- 0 until RenameWidth) {
L
LinJiawei 已提交
287
    PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready,
288
      flushReg || io.frontend.toFtq.stage3Redirect.valid)
289
  }
290

291
  rename.io.redirect <> stage2Redirect
292
  rename.io.flush := flushReg
293 294
  rename.io.roqCommits <> roq.io.commits
  rename.io.out <> dispatch.io.fromRename
295
  rename.io.renameBypass <> dispatch.io.renameBypass
296
  rename.io.dispatchInfo <> dispatch.io.preDpInfo
297

298
  dispatch.io.redirect <> stage2Redirect
299
  dispatch.io.flush := flushReg
300
  dispatch.io.enqRoq <> roq.io.enq
301
  dispatch.io.enqLsq <> io.enqLsq
302
  dispatch.io.singleStep := false.B
Y
Yinan Xu 已提交
303 304
  dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) =>
    intBusyTable.io.allocPregs(i).valid := preg.isInt
305
    fpBusyTable.io.allocPregs(i).valid := preg.isFp
Y
Yinan Xu 已提交
306 307 308
    intBusyTable.io.allocPregs(i).bits := preg.preg
    fpBusyTable.io.allocPregs(i).bits := preg.preg
  }
309
  dispatch.io.enqIQCtrl := DontCare
310
  io.enqIQ <> dispatch.io.enqIQCtrl
311
  dispatch.io.csrCtrl <> io.csrCtrl
312 313 314
  dispatch.io.storeIssue <> io.stIn
  dispatch.io.readIntRf <> io.readIntRf
  dispatch.io.readFpRf <> io.readFpRf
L
LinJiawei 已提交
315

316 317
  fpBusyTable.io.flush := flushReg
  intBusyTable.io.flush := flushReg
318
  for((wb, setPhyRegRdy) <- io.writeback.take(NRIntWritePorts).zip(intBusyTable.io.wbPregs)){
319
    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen
Y
Yinan Xu 已提交
320 321
    setPhyRegRdy.bits := wb.bits.uop.pdest
  }
322
  for((wb, setPhyRegRdy) <- io.writeback.drop(NRIntWritePorts).zip(fpBusyTable.io.wbPregs)){
Y
Yinan Xu 已提交
323 324 325
    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen
    setPhyRegRdy.bits := wb.bits.uop.pdest
  }
Y
Yinan Xu 已提交
326 327
  intBusyTable.io.read <> dispatch.io.readIntState
  fpBusyTable.io.read <> dispatch.io.readFpState
Y
Yinan Xu 已提交
328

329
  roq.io.redirect <> stage2Redirect
330
  val exeWbResults = VecInit(io.writeback ++ io.stOut)
L
ljw 已提交
331
  for((roq_wb, wb) <- roq.io.exeWbResults.zip(exeWbResults)) {
332
    roq_wb.valid := RegNext(wb.valid && !wb.bits.uop.roqIdx.needFlush(stage2Redirect, flushReg))
L
ljw 已提交
333 334
    roq_wb.bits := RegNext(wb.bits)
  }
L
LinJiawei 已提交
335 336

  // TODO: is 'backendRedirect' necesscary?
337
  io.redirect <> stage2Redirect
338 339 340 341 342 343
  io.flush <> flushReg
  io.debug_int_rat <> rename.io.debug_int_rat
  io.debug_fp_rat <> rename.io.debug_fp_rat

//  dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex
//  dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex
344

Y
Yinan Xu 已提交
345 346
  // roq to int block
  io.roqio.toCSR <> roq.io.csr
347
  io.roqio.toCSR.perfinfo.retiredInstr <> RegNext(roq.io.csr.perfinfo.retiredInstr)
348
  io.roqio.exception := roq.io.exception
349
  io.roqio.exception.bits.uop.cf.pc := flushPC
Y
Yinan Xu 已提交
350
  // roq to mem block
351
  io.roqio.lsq <> roq.io.lsq
352 353 354 355 356

  io.perfInfo.ctrlInfo.roqFull := RegNext(roq.io.roqFull)
  io.perfInfo.ctrlInfo.intdqFull := RegNext(dispatch.io.ctrlInfo.intdqFull)
  io.perfInfo.ctrlInfo.fpdqFull := RegNext(dispatch.io.ctrlInfo.fpdqFull)
  io.perfInfo.ctrlInfo.lsdqFull := RegNext(dispatch.io.ctrlInfo.lsdqFull)
357
}