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 chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import xiangshan._ 24import xiangshan.backend.decode.{DecodeStage, ImmUnion} 25import xiangshan.backend.dispatch.{Dispatch, DispatchQueue} 26import xiangshan.backend.rename.{Rename, RenameTableWrapper} 27import xiangshan.backend.rob.{Rob, RobCSRIO, RobLsqIO} 28import xiangshan.frontend.{FtqPtr, FtqRead} 29import xiangshan.mem.LsqEnqIO 30import difftest._ 31 32class CtrlToFtqIO(implicit p: Parameters) extends XSBundle { 33 val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo)) 34 val stage2Redirect = Valid(new Redirect) 35 val stage3Redirect = ValidIO(new Redirect) 36 val robFlush = ValidIO(new Redirect) 37} 38 39class RedirectGenerator(implicit p: Parameters) extends XSModule 40 with HasCircularQueuePtrHelper { 41 val numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt 42 val io = IO(new Bundle() { 43 val exuMispredict = Vec(numRedirect, Flipped(ValidIO(new ExuOutput))) 44 val loadReplay = Flipped(ValidIO(new Redirect)) 45 val flush = Input(Bool()) 46 val stage1PcRead = Vec(numRedirect+1, new FtqRead(UInt(VAddrBits.W))) 47 val stage2Redirect = ValidIO(new Redirect) 48 val stage3Redirect = ValidIO(new Redirect) 49 val memPredUpdate = Output(new MemPredUpdateReq) 50 val memPredPcRead = new FtqRead(UInt(VAddrBits.W)) // read req send form stage 2 51 }) 52 /* 53 LoadQueue Jump ALU0 ALU1 ALU2 ALU3 exception Stage1 54 | | | | | | | 55 |============= reg & compare =====| | ======== 56 | | 57 | | 58 | | Stage2 59 | | 60 redirect (flush backend) | 61 | | 62 === reg === | ======== 63 | | 64 |----- mux (exception first) -----| Stage3 65 | 66 redirect (send to frontend) 67 */ 68 private class Wrapper(val n: Int) extends Bundle { 69 val redirect = new Redirect 70 val valid = Bool() 71 val idx = UInt(log2Up(n).W) 72 } 73 def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = { 74 val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.robIdx, xs(i).bits.robIdx))) 75 val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j => 76 (if (j < i) !xs(j).valid || compareVec(i)(j) 77 else if (j == i) xs(i).valid 78 else !xs(j).valid || !compareVec(j)(i)) 79 )).andR)) 80 resultOnehot 81 } 82 83 val redirects = io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits 84 val stage1FtqReadPcs = 85 (io.stage1PcRead zip redirects).map{ case (r, redirect) => 86 r(redirect.ftqIdx, redirect.ftqOffset) 87 } 88 89 def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = { 90 val redirect = Wire(Valid(new Redirect)) 91 redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred 92 redirect.bits := exuOut.bits.redirect 93 redirect 94 } 95 96 val jumpOut = io.exuMispredict.head 97 val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay) 98 val oldestOneHot = selectOldestRedirect(allRedirect) 99 val needFlushVec = VecInit(allRedirect.map(_.bits.robIdx.needFlush(io.stage2Redirect) || io.flush)) 100 val oldestValid = VecInit(oldestOneHot.zip(needFlushVec).map{ case (v, f) => v && !f }).asUInt.orR 101 val oldestExuOutput = Mux1H(io.exuMispredict.indices.map(oldestOneHot), io.exuMispredict) 102 val oldestRedirect = Mux1H(oldestOneHot, allRedirect) 103 104 val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid) 105 val s1_imm12_reg = RegNext(oldestExuOutput.bits.uop.ctrl.imm(11, 0)) 106 val s1_pd = RegNext(oldestExuOutput.bits.uop.cf.pd) 107 val s1_redirect_bits_reg = RegNext(oldestRedirect.bits) 108 val s1_redirect_valid_reg = RegNext(oldestValid) 109 val s1_redirect_onehot = RegNext(oldestOneHot) 110 111 // stage1 -> stage2 112 io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush 113 io.stage2Redirect.bits := s1_redirect_bits_reg 114 io.stage2Redirect.bits.cfiUpdate := DontCare 115 116 val s1_isReplay = s1_redirect_onehot.last 117 val s1_isJump = s1_redirect_onehot.head 118 val real_pc = Mux1H(s1_redirect_onehot, stage1FtqReadPcs) 119 val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN) 120 val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U) 121 val target = Mux(s1_isReplay, 122 real_pc, // replay from itself 123 Mux(s1_redirect_bits_reg.cfiUpdate.taken, 124 Mux(s1_isJump, s1_jumpTarget, brTarget), 125 snpc 126 ) 127 ) 128 129 // get pc from ftq 130 // valid only if redirect is caused by load violation 131 // store_pc is used to update store set 132 val store_pc = io.memPredPcRead(s1_redirect_bits_reg.stFtqIdx, s1_redirect_bits_reg.stFtqOffset) 133 134 // update load violation predictor if load violation redirect triggered 135 io.memPredUpdate.valid := RegNext(s1_isReplay && s1_redirect_valid_reg, init = false.B) 136 // update wait table 137 io.memPredUpdate.waddr := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth)) 138 io.memPredUpdate.wdata := true.B 139 // update store set 140 io.memPredUpdate.ldpc := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth)) 141 // store pc is ready 1 cycle after s1_isReplay is judged 142 io.memPredUpdate.stpc := XORFold(store_pc(VAddrBits-1, 1), MemPredPCWidth) 143 144 val s2_target = RegEnable(target, enable = s1_redirect_valid_reg) 145 val s2_pd = RegEnable(s1_pd, enable = s1_redirect_valid_reg) 146 val s2_pc = RegEnable(real_pc, enable = s1_redirect_valid_reg) 147 val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, enable = s1_redirect_valid_reg) 148 val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B) 149 150 io.stage3Redirect.valid := s2_redirect_valid_reg 151 io.stage3Redirect.bits := s2_redirect_bits_reg 152 val stage3CfiUpdate = io.stage3Redirect.bits.cfiUpdate 153 stage3CfiUpdate.pc := s2_pc 154 stage3CfiUpdate.pd := s2_pd 155 stage3CfiUpdate.predTaken := s2_redirect_bits_reg.cfiUpdate.predTaken 156 stage3CfiUpdate.target := s2_target 157 stage3CfiUpdate.taken := s2_redirect_bits_reg.cfiUpdate.taken 158 stage3CfiUpdate.isMisPred := s2_redirect_bits_reg.cfiUpdate.isMisPred 159 160 // recover runahead checkpoint if redirect 161 if (!env.FPGAPlatform) { 162 val runahead_redirect = Module(new DifftestRunaheadRedirectEvent) 163 runahead_redirect.io.clock := clock 164 runahead_redirect.io.coreid := hardId.U 165 runahead_redirect.io.valid := io.stage3Redirect.valid 166 runahead_redirect.io.pc := s2_pc // for debug only 167 runahead_redirect.io.target_pc := s2_target // for debug only 168 runahead_redirect.io.checkpoint_id := io.stage3Redirect.bits.debug_runahead_checkpoint_id // make sure it is right 169 } 170} 171 172class CtrlBlock(implicit p: Parameters) extends XSModule 173 with HasCircularQueuePtrHelper { 174 val io = IO(new Bundle { 175 val frontend = Flipped(new FrontendToCtrlIO) 176 val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq)) 177 val dispatch = Vec(3*dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 178 // from int block 179 val exuRedirect = Vec(exuParameters.AluCnt + exuParameters.JmpCnt, Flipped(ValidIO(new ExuOutput))) 180 val stIn = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuInput))) 181 val stOut = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuOutput))) 182 val memoryViolation = Flipped(ValidIO(new Redirect)) 183 val enqLsq = Flipped(new LsqEnqIO) 184 val jumpPc = Output(UInt(VAddrBits.W)) 185 val jalr_target = Output(UInt(VAddrBits.W)) 186 val robio = new Bundle { 187 // to int block 188 val toCSR = new RobCSRIO 189 val exception = ValidIO(new ExceptionInfo) 190 // to mem block 191 val lsq = new RobLsqIO 192 } 193 val csrCtrl = Input(new CustomCSRCtrlIO) 194 val perfInfo = Output(new Bundle{ 195 val ctrlInfo = new Bundle { 196 val robFull = Input(Bool()) 197 val intdqFull = Input(Bool()) 198 val fpdqFull = Input(Bool()) 199 val lsdqFull = Input(Bool()) 200 } 201 }) 202 val writeback = Vec(NRIntWritePorts + NRFpWritePorts, Flipped(ValidIO(new ExuOutput))) 203 // redirect out 204 val redirect = ValidIO(new Redirect) 205 val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 206 val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 207 }) 208 209 val decode = Module(new DecodeStage) 210 val rat = Module(new RenameTableWrapper) 211 val rename = Module(new Rename) 212 val dispatch = Module(new Dispatch) 213 val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth, "int")) 214 val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth, "fp")) 215 val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth, "ls")) 216 val redirectGen = Module(new RedirectGenerator) 217 218 val robWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt 219 val rob = Module(new Rob(robWbSize)) 220 221 val robPcRead = io.frontend.fromFtq.getRobFlushPcRead 222 val flushPC = robPcRead(rob.io.flushOut.bits.ftqIdx, rob.io.flushOut.bits.ftqOffset) 223 224 val flushRedirect = Wire(Valid(new Redirect)) 225 flushRedirect.valid := RegNext(rob.io.flushOut.valid) 226 flushRedirect.bits := RegEnable(rob.io.flushOut.bits, rob.io.flushOut.valid) 227 flushRedirect.bits.cfiUpdate.target := Mux(io.robio.toCSR.isXRet || rob.io.exception.valid, 228 io.robio.toCSR.trapTarget, 229 Mux(flushRedirect.bits.flushItself(), 230 flushPC, // replay inst 231 flushPC + 4.U // flush pipe 232 ) 233 ) 234 235 val flushRedirectReg = Wire(Valid(new Redirect)) 236 flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B) 237 flushRedirectReg.bits := RegEnable(flushRedirect.bits, enable = flushRedirect.valid) 238 239 val stage2Redirect = Mux(flushRedirect.valid, flushRedirect, redirectGen.io.stage2Redirect) 240 val stage3Redirect = Mux(flushRedirectReg.valid, flushRedirectReg, redirectGen.io.stage3Redirect) 241 242 val exuRedirect = io.exuRedirect.map(x => { 243 val valid = x.valid && x.bits.redirectValid 244 val killedByOlder = x.bits.uop.robIdx.needFlush(stage2Redirect) 245 val delayed = Wire(Valid(new ExuOutput)) 246 delayed.valid := RegNext(valid && !killedByOlder, init = false.B) 247 delayed.bits := RegEnable(x.bits, x.valid) 248 delayed 249 }) 250 val loadReplay = Wire(Valid(new Redirect)) 251 loadReplay.valid := RegNext(io.memoryViolation.valid && 252 !io.memoryViolation.bits.robIdx.needFlush(stage2Redirect), 253 init = false.B 254 ) 255 loadReplay.bits := RegEnable(io.memoryViolation.bits, io.memoryViolation.valid) 256 io.frontend.fromFtq.getRedirectPcRead <> redirectGen.io.stage1PcRead 257 io.frontend.fromFtq.getMemPredPcRead <> redirectGen.io.memPredPcRead 258 redirectGen.io.exuMispredict <> exuRedirect 259 redirectGen.io.loadReplay <> loadReplay 260 redirectGen.io.flush := RegNext(rob.io.flushOut.valid) 261 262 for(i <- 0 until CommitWidth){ 263 io.frontend.toFtq.rob_commits(i).valid := rob.io.commits.valid(i) && !rob.io.commits.isWalk 264 io.frontend.toFtq.rob_commits(i).bits := rob.io.commits.info(i) 265 } 266 io.frontend.toFtq.stage2Redirect <> stage2Redirect 267 io.frontend.toFtq.robFlush <> RegNext(rob.io.flushOut) 268 io.frontend.toFtq.stage3Redirect := stage3Redirect 269 270 decode.io.in <> io.frontend.cfVec 271 // currently, we only update wait table when isReplay 272 decode.io.memPredUpdate(0) <> RegNext(redirectGen.io.memPredUpdate) 273 decode.io.memPredUpdate(1) := DontCare 274 decode.io.memPredUpdate(1).valid := false.B 275 decode.io.csrCtrl := RegNext(io.csrCtrl) 276 277 rat.io.robCommits := rob.io.commits 278 for ((r, i) <- rat.io.intReadPorts.zipWithIndex) { 279 val raddr = decode.io.out(i).bits.ctrl.lsrc.take(2) :+ decode.io.out(i).bits.ctrl.ldest 280 r.map(_.addr).zip(raddr).foreach(x => x._1 := x._2) 281 rename.io.intReadPorts(i) := r.map(_.data) 282 r.foreach(_.hold := !rename.io.in(i).ready) 283 } 284 rat.io.intRenamePorts := rename.io.intRenamePorts 285 for ((r, i) <- rat.io.fpReadPorts.zipWithIndex) { 286 val raddr = decode.io.out(i).bits.ctrl.lsrc.take(3) :+ decode.io.out(i).bits.ctrl.ldest 287 r.map(_.addr).zip(raddr).foreach(x => x._1 := x._2) 288 rename.io.fpReadPorts(i) := r.map(_.data) 289 r.foreach(_.hold := !rename.io.in(i).ready) 290 } 291 rat.io.fpRenamePorts := rename.io.fpRenamePorts 292 rat.io.debug_int_rat <> io.debug_int_rat 293 rat.io.debug_fp_rat <> io.debug_fp_rat 294 295 // pipeline between decode and rename 296 for (i <- 0 until RenameWidth) { 297 PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready, 298 stage2Redirect.valid || stage3Redirect.valid) 299 } 300 301 rename.io.redirect <> stage2Redirect 302 rename.io.robCommits <> rob.io.commits 303 304 // pipeline between rename and dispatch 305 for (i <- 0 until RenameWidth) { 306 PipelineConnect(rename.io.out(i), dispatch.io.fromRename(i), dispatch.io.recv(i), stage2Redirect.valid) 307 } 308 dispatch.io.preDpInfo := RegEnable(rename.io.dispatchInfo, rename.io.out(0).fire) 309 310 dispatch.io.redirect <> stage2Redirect 311 dispatch.io.enqRob <> rob.io.enq 312 dispatch.io.enqLsq <> io.enqLsq 313 dispatch.io.toIntDq <> intDq.io.enq 314 dispatch.io.toFpDq <> fpDq.io.enq 315 dispatch.io.toLsDq <> lsDq.io.enq 316 dispatch.io.allocPregs <> io.allocPregs 317 dispatch.io.csrCtrl <> io.csrCtrl 318 dispatch.io.storeIssue <> io.stIn 319 dispatch.io.singleStep := false.B 320 321 intDq.io.redirect <> stage2Redirect 322 fpDq.io.redirect <> stage2Redirect 323 lsDq.io.redirect <> stage2Redirect 324 325 io.dispatch <> intDq.io.deq ++ lsDq.io.deq ++ fpDq.io.deq 326 327 val pingpong = RegInit(false.B) 328 pingpong := !pingpong 329 val jumpInst = Mux(pingpong && (exuParameters.AluCnt > 2).B, io.dispatch(2).bits, io.dispatch(0).bits) 330 val jumpPcRead = io.frontend.fromFtq.getJumpPcRead 331 io.jumpPc := jumpPcRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset) 332 val jumpTargetRead = io.frontend.fromFtq.target_read 333 io.jalr_target := jumpTargetRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset) 334 335 rob.io.redirect <> stage2Redirect 336 val exeWbResults = VecInit(io.writeback ++ io.stOut) 337 val timer = GTimer() 338 for((rob_wb, wb) <- rob.io.exeWbResults.zip(exeWbResults)) { 339 rob_wb.valid := RegNext(wb.valid && !wb.bits.uop.robIdx.needFlush(stage2Redirect)) 340 rob_wb.bits := RegNext(wb.bits) 341 rob_wb.bits.uop.debugInfo.writebackTime := timer 342 } 343 344 io.redirect <> stage2Redirect 345 346 // rob to int block 347 io.robio.toCSR <> rob.io.csr 348 io.robio.toCSR.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr) 349 io.robio.exception := rob.io.exception 350 io.robio.exception.bits.uop.cf.pc := flushPC 351 352 // rob to mem block 353 io.robio.lsq <> rob.io.lsq 354 355 io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull) 356 io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull) 357 io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull) 358 io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull) 359} 360