1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* 4* XiangShan is licensed under Mulan PSL v2. 5* You can use this software according to the terms and conditions of the Mulan PSL v2. 6* You may obtain a copy of Mulan PSL v2 at: 7* http://license.coscl.org.cn/MulanPSL2 8* 9* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12* 13* See the Mulan PSL v2 for more details. 14***************************************************************************************/ 15 16package xiangshan.backend 17 18import chipsalliance.rocketchip.config.Parameters 19import chisel3._ 20import chisel3.util._ 21import utils._ 22import xiangshan._ 23import xiangshan.backend.decode.{DecodeStage, ImmUnion} 24import xiangshan.backend.rename.{BusyTable, Rename} 25import xiangshan.backend.dispatch.Dispatch 26import xiangshan.backend.exu._ 27import xiangshan.frontend.{FtqRead, FtqToCtrlIO, FtqPtr} 28import xiangshan.backend.roq.{Roq, RoqCSRIO, RoqLsqIO, RoqPtr} 29import xiangshan.mem.LsqEnqIO 30 31class CtrlToFtqIO(implicit p: Parameters) extends XSBundle { 32 val roq_commits = Vec(CommitWidth, Valid(new RoqCommitInfo)) 33 val stage2Redirect = Valid(new Redirect) 34 val roqFlush = Valid(new Bundle { 35 val ftqIdx = Output(new FtqPtr) 36 val ftqOffset = Output(UInt(log2Up(PredictWidth).W)) 37 }) 38 39 val loadReplay = Valid(new Redirect) 40 val stage3Redirect = ValidIO(new Redirect) 41} 42 43class RedirectGenerator(implicit p: Parameters) extends XSModule 44 with HasCircularQueuePtrHelper { 45 val numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt 46 val io = IO(new Bundle() { 47 val exuMispredict = Vec(numRedirect, Flipped(ValidIO(new ExuOutput))) 48 val loadReplay = Flipped(ValidIO(new Redirect)) 49 val flush = Input(Bool()) 50 val stage1PcRead = Vec(numRedirect+1, new FtqRead(UInt(VAddrBits.W))) 51 val stage2Redirect = ValidIO(new Redirect) 52 val stage3Redirect = ValidIO(new Redirect) 53 val memPredUpdate = Output(new MemPredUpdateReq) 54 val memPredPcRead = new FtqRead(UInt(VAddrBits.W)) // read req send form stage 2 55 }) 56 /* 57 LoadQueue Jump ALU0 ALU1 ALU2 ALU3 exception Stage1 58 | | | | | | | 59 |============= reg & compare =====| | ======== 60 | | 61 | | 62 | | Stage2 63 | | 64 redirect (flush backend) | 65 | | 66 === reg === | ======== 67 | | 68 |----- mux (exception first) -----| Stage3 69 | 70 redirect (send to frontend) 71 */ 72 private class Wrapper(val n: Int) extends Bundle { 73 val redirect = new Redirect 74 val valid = Bool() 75 val idx = UInt(log2Up(n).W) 76 } 77 def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = { 78 val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.roqIdx, xs(i).bits.roqIdx))) 79 val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j => 80 (if (j < i) !xs(j).valid || compareVec(i)(j) 81 else if (j == i) xs(i).valid 82 else !xs(j).valid || !compareVec(j)(i)) 83 )).andR)) 84 resultOnehot 85 } 86 87 val redirects = io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits 88 val stage1FtqReadPcs = 89 (io.stage1PcRead zip redirects).map{ case (r, redirect) => 90 r(redirect.ftqIdx, redirect.ftqOffset) 91 } 92 93 def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = { 94 val redirect = Wire(Valid(new Redirect)) 95 redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred 96 redirect.bits := exuOut.bits.redirect 97 redirect 98 } 99 100 val jumpOut = io.exuMispredict.head 101 val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay) 102 val oldestOneHot = selectOldestRedirect(allRedirect) 103 val needFlushVec = VecInit(allRedirect.map(_.bits.roqIdx.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 108 val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid) 109 val s1_imm12_reg = RegNext(oldestExuOutput.bits.uop.ctrl.imm(11, 0)) 110 val s1_pd = RegNext(oldestExuOutput.bits.uop.cf.pd) 111 val s1_redirect_bits_reg = RegNext(oldestRedirect.bits) 112 val s1_redirect_valid_reg = RegNext(oldestValid) 113 val s1_redirect_onehot = RegNext(oldestOneHot) 114 115 // stage1 -> stage2 116 io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush 117 io.stage2Redirect.bits := s1_redirect_bits_reg 118 io.stage2Redirect.bits.cfiUpdate := DontCare 119 120 val s1_isReplay = s1_redirect_onehot.last 121 val s1_isJump = s1_redirect_onehot.head 122 val real_pc = Mux1H(s1_redirect_onehot, stage1FtqReadPcs) 123 val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN) 124 val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U) 125 val target = Mux(s1_isReplay, 126 real_pc, // repaly from itself 127 Mux(s1_redirect_bits_reg.cfiUpdate.taken, 128 Mux(s1_isJump, s1_jumpTarget, brTarget), 129 snpc 130 ) 131 ) 132 133 // get pc from ftq 134 // valid only if redirect is caused by load violation 135 // 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 // update load violation predictor if load violation redirect triggered 139 io.memPredUpdate.valid := RegNext(s1_isReplay && s1_redirect_valid_reg, init = false.B) 140 // update wait table 141 io.memPredUpdate.waddr := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth)) 142 io.memPredUpdate.wdata := true.B 143 // update store set 144 io.memPredUpdate.ldpc := RegNext(XORFold(real_pc(VAddrBits-1, 1), MemPredPCWidth)) 145 // store pc is ready 1 cycle after s1_isReplay is judged 146 io.memPredUpdate.stpc := XORFold(store_pc(VAddrBits-1, 1), MemPredPCWidth) 147 148 val s2_target = RegEnable(target, enable = s1_redirect_valid_reg) 149 val s2_pd = RegEnable(s1_pd, enable = s1_redirect_valid_reg) 150 val s2_pc = RegEnable(real_pc, enable = s1_redirect_valid_reg) 151 val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, enable = s1_redirect_valid_reg) 152 val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B) 153 154 io.stage3Redirect.valid := s2_redirect_valid_reg 155 io.stage3Redirect.bits := s2_redirect_bits_reg 156 val stage3CfiUpdate = io.stage3Redirect.bits.cfiUpdate 157 stage3CfiUpdate.pc := s2_pc 158 stage3CfiUpdate.pd := s2_pd 159 // stage3CfiUpdate.rasSp := s2_ftqRead.rasSp 160 // stage3CfiUpdate.rasEntry := s2_ftqRead.rasTop 161 // stage3CfiUpdate.predHist := s2_ftqRead.predHist 162 // stage3CfiUpdate.specCnt := s2_ftqRead.specCnt 163 // stage3CfiUpdate.hist := s2_hist 164 stage3CfiUpdate.predTaken := s2_redirect_bits_reg.cfiUpdate.predTaken 165 // stage3CfiUpdate.br_hit := s2_sawNotTakenBranch 166 stage3CfiUpdate.target := s2_target 167 stage3CfiUpdate.taken := s2_redirect_bits_reg.cfiUpdate.taken 168 stage3CfiUpdate.isMisPred := s2_redirect_bits_reg.cfiUpdate.isMisPred 169} 170 171class CtrlBlock(implicit p: Parameters) extends XSModule 172 with HasCircularQueuePtrHelper { 173 val io = IO(new Bundle { 174 val frontend = Flipped(new FrontendToCtrlIO) 175 val enqIQ = Vec(exuParameters.CriticalExuCnt, DecoupledIO(new MicroOp)) 176 // from int block 177 val exuRedirect = Vec(exuParameters.AluCnt + exuParameters.JmpCnt, Flipped(ValidIO(new ExuOutput))) 178 val stIn = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuInput))) 179 val stOut = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuOutput))) 180 val memoryViolation = Flipped(ValidIO(new Redirect)) 181 val enqLsq = Flipped(new LsqEnqIO) 182 val jumpPc = Output(UInt(VAddrBits.W)) 183 val jalr_target = Output(UInt(VAddrBits.W)) 184 val roqio = new Bundle { 185 // to int block 186 val toCSR = new RoqCSRIO 187 val exception = ValidIO(new ExceptionInfo) 188 // to mem block 189 val lsq = new RoqLsqIO 190 } 191 val csrCtrl = Input(new CustomCSRCtrlIO) 192 val perfInfo = Output(new Bundle{ 193 val ctrlInfo = new Bundle { 194 val roqFull = Input(Bool()) 195 val intdqFull = Input(Bool()) 196 val fpdqFull = Input(Bool()) 197 val lsdqFull = Input(Bool()) 198 } 199 }) 200 val writeback = Vec(NRIntWritePorts + NRFpWritePorts, Flipped(ValidIO(new ExuOutput))) 201 // redirect out 202 val redirect = ValidIO(new Redirect) 203 val flush = Output(Bool()) 204 val readIntRf = Vec(NRIntReadPorts, Output(UInt(PhyRegIdxWidth.W))) 205 val readFpRf = Vec(NRFpReadPorts, Output(UInt(PhyRegIdxWidth.W))) 206 val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 207 val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 208 }) 209 210 val decode = Module(new DecodeStage) 211 val rename = Module(new Rename) 212 val dispatch = Module(new Dispatch) 213 val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts)) 214 val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts)) 215 val redirectGen = Module(new RedirectGenerator) 216 217 val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt 218 val roq = Module(new Roq(roqWbSize)) 219 220 val stage2Redirect = redirectGen.io.stage2Redirect 221 val stage3Redirect = redirectGen.io.stage3Redirect 222 val flush = roq.io.flushOut.valid 223 val flushReg = RegNext(flush) 224 225 val exuRedirect = io.exuRedirect.map(x => { 226 val valid = x.valid && x.bits.redirectValid 227 val killedByOlder = x.bits.uop.roqIdx.needFlush(stage2Redirect, flushReg) 228 val delayed = Wire(Valid(new ExuOutput)) 229 delayed.valid := RegNext(valid && !killedByOlder, init = false.B) 230 delayed.bits := RegEnable(x.bits, x.valid) 231 delayed 232 }) 233 val loadReplay = Wire(Valid(new Redirect)) 234 loadReplay.valid := RegNext(io.memoryViolation.valid && 235 !io.memoryViolation.bits.roqIdx.needFlush(stage2Redirect, flushReg), 236 init = false.B 237 ) 238 loadReplay.bits := RegEnable(io.memoryViolation.bits, io.memoryViolation.valid) 239 io.frontend.fromFtq.getRedirectPcRead <> redirectGen.io.stage1PcRead 240 io.frontend.fromFtq.getMemPredPcRead <> redirectGen.io.memPredPcRead 241 redirectGen.io.exuMispredict <> exuRedirect 242 redirectGen.io.loadReplay <> loadReplay 243 redirectGen.io.flush := flushReg 244 245 for(i <- 0 until CommitWidth){ 246 io.frontend.toFtq.roq_commits(i).valid := roq.io.commits.valid(i) && !roq.io.commits.isWalk 247 io.frontend.toFtq.roq_commits(i).bits := roq.io.commits.info(i) 248 } 249 io.frontend.toFtq.stage2Redirect <> stage2Redirect 250 io.frontend.toFtq.roqFlush <> RegNext(roq.io.flushOut) 251 io.frontend.toFtq.stage3Redirect <> stage3Redirect 252 io.frontend.toFtq.loadReplay <> loadReplay 253 254 val roqPcRead = io.frontend.fromFtq.getRoqFlushPcRead 255 val flushPC = roqPcRead(roq.io.flushOut.bits.ftqIdx, roq.io.flushOut.bits.ftqOffset) 256 257 val flushRedirect = Wire(Valid(new Redirect)) 258 flushRedirect.valid := flushReg 259 flushRedirect.bits := DontCare 260 flushRedirect.bits.ftqIdx := RegEnable(roq.io.flushOut.bits.ftqIdx, flush) 261 flushRedirect.bits.interrupt := true.B 262 flushRedirect.bits.cfiUpdate.target := Mux(io.roqio.toCSR.isXRet || roq.io.exception.valid, 263 io.roqio.toCSR.trapTarget, 264 flushPC + 4.U // flush pipe 265 ) 266 val flushRedirectReg = Wire(Valid(new Redirect)) 267 flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B) 268 flushRedirectReg.bits := RegEnable(flushRedirect.bits, enable = flushRedirect.valid) 269 270 io.frontend.redirect_cfiUpdate := Mux(flushRedirectReg.valid, flushRedirectReg, stage3Redirect) 271 272 decode.io.in <> io.frontend.cfVec 273 // currently, we only update wait table when isReplay 274 decode.io.memPredUpdate(0) <> RegNext(redirectGen.io.memPredUpdate) 275 decode.io.memPredUpdate(1) := DontCare 276 decode.io.memPredUpdate(1).valid := false.B 277 // decode.io.memPredUpdate <> io.toLsBlock.memPredUpdate 278 decode.io.csrCtrl := RegNext(io.csrCtrl) 279 280 281 val jumpInst = dispatch.io.enqIQCtrl(0).bits 282 val jumpPcRead = io.frontend.fromFtq.getJumpPcRead 283 io.jumpPc := jumpPcRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset) 284 val jumpTargetRead = io.frontend.fromFtq.target_read 285 io.jalr_target := jumpTargetRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset) 286 287 // pipeline between decode and dispatch 288 for (i <- 0 until RenameWidth) { 289 PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready, 290 flushReg || io.frontend.redirect_cfiUpdate.valid) 291 } 292 293 rename.io.redirect <> stage2Redirect 294 rename.io.flush := flushReg 295 rename.io.roqCommits <> roq.io.commits 296 rename.io.out <> dispatch.io.fromRename 297 rename.io.renameBypass <> dispatch.io.renameBypass 298 rename.io.dispatchInfo <> dispatch.io.preDpInfo 299 rename.io.csrCtrl <> RegNext(io.csrCtrl) 300 301 dispatch.io.redirect <> stage2Redirect 302 dispatch.io.flush := flushReg 303 dispatch.io.enqRoq <> roq.io.enq 304 dispatch.io.enqLsq <> io.enqLsq 305 dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) => 306 intBusyTable.io.allocPregs(i).valid := preg.isInt 307 fpBusyTable.io.allocPregs(i).valid := preg.isFp 308 intBusyTable.io.allocPregs(i).bits := preg.preg 309 fpBusyTable.io.allocPregs(i).bits := preg.preg 310 } 311 dispatch.io.enqIQCtrl := DontCare 312 io.enqIQ <> dispatch.io.enqIQCtrl 313 dispatch.io.csrCtrl <> io.csrCtrl 314 dispatch.io.storeIssue <> io.stIn 315 dispatch.io.readIntRf <> io.readIntRf 316 dispatch.io.readFpRf <> io.readFpRf 317 318 fpBusyTable.io.flush := flushReg 319 intBusyTable.io.flush := flushReg 320 for((wb, setPhyRegRdy) <- io.writeback.take(NRIntWritePorts).zip(intBusyTable.io.wbPregs)){ 321 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen 322 setPhyRegRdy.bits := wb.bits.uop.pdest 323 } 324 for((wb, setPhyRegRdy) <- io.writeback.drop(NRIntWritePorts).zip(fpBusyTable.io.wbPregs)){ 325 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen 326 setPhyRegRdy.bits := wb.bits.uop.pdest 327 } 328 intBusyTable.io.read <> dispatch.io.readIntState 329 fpBusyTable.io.read <> dispatch.io.readFpState 330 331 roq.io.redirect <> stage2Redirect 332 val exeWbResults = VecInit(io.writeback ++ io.stOut) 333 for((roq_wb, wb) <- roq.io.exeWbResults.zip(exeWbResults)) { 334 roq_wb.valid := RegNext(wb.valid && !wb.bits.uop.roqIdx.needFlush(stage2Redirect, flushReg)) 335 roq_wb.bits := RegNext(wb.bits) 336 } 337 338 // TODO: is 'backendRedirect' necesscary? 339 io.redirect <> stage2Redirect 340 io.flush <> flushReg 341 io.debug_int_rat <> rename.io.debug_int_rat 342 io.debug_fp_rat <> rename.io.debug_fp_rat 343 344// dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex 345// dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex 346 347 // roq to int block 348 io.roqio.toCSR <> roq.io.csr 349 io.roqio.toCSR.perfinfo.retiredInstr <> RegNext(roq.io.csr.perfinfo.retiredInstr) 350 io.roqio.exception := roq.io.exception 351 io.roqio.exception.bits.uop.cf.pc := flushPC 352 // roq to mem block 353 io.roqio.lsq <> roq.io.lsq 354 355 io.perfInfo.ctrlInfo.roqFull := RegNext(roq.io.roqFull) 356 io.perfInfo.ctrlInfo.intdqFull := RegNext(dispatch.io.ctrlInfo.intdqFull) 357 io.perfInfo.ctrlInfo.fpdqFull := RegNext(dispatch.io.ctrlInfo.fpdqFull) 358 io.perfInfo.ctrlInfo.lsdqFull := RegNext(dispatch.io.ctrlInfo.lsdqFull) 359} 360