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 val lpaddr = Output(Vec(CommitWidth, UInt(64.W))) 195 val ltype = Output(Vec(CommitWidth, UInt(32.W))) 196 val lfu = Output(Vec(CommitWidth, UInt(4.W))) 197 } 198 }) 199 difftestIO <> DontCare 200 201 val ftq = Module(new Ftq) 202 val trapIO = IO(new TrapIO()) 203 trapIO <> DontCare 204 205 val decode = Module(new DecodeStage) 206 val rename = Module(new Rename) 207 val dispatch = Module(new Dispatch) 208 val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts)) 209 val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts)) 210 val redirectGen = Module(new RedirectGenerator) 211 212 val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt 213 214 val roq = Module(new Roq(roqWbSize)) 215 216 val backendRedirect = redirectGen.io.stage2Redirect 217 val frontendRedirect = redirectGen.io.stage3Redirect 218 val flush = roq.io.flushOut.valid 219 220 redirectGen.io.exuMispredict.zip(io.fromIntBlock.exuRedirect).map({case (x, y) => 221 x.valid := y.valid && y.bits.redirect.cfiUpdate.isMisPred 222 x.bits := y.bits 223 }) 224 redirectGen.io.loadRelay := io.fromLsBlock.replay 225 redirectGen.io.flush := flush 226 227 ftq.io.enq <> io.frontend.fetchInfo 228 for(i <- 0 until CommitWidth){ 229 ftq.io.roq_commits(i).valid := roq.io.commits.valid(i) && !roq.io.commits.isWalk 230 ftq.io.roq_commits(i).bits := roq.io.commits.info(i) 231 } 232 ftq.io.redirect <> backendRedirect 233 ftq.io.flush := flush 234 ftq.io.flushIdx := roq.io.flushOut.bits.ftqIdx 235 ftq.io.flushOffset := roq.io.flushOut.bits.ftqOffset 236 ftq.io.frontendRedirect <> frontendRedirect 237 ftq.io.exuWriteback <> io.fromIntBlock.exuRedirect 238 239 ftq.io.ftqRead(1) <> redirectGen.io.stage2FtqRead 240 ftq.io.ftqRead(2).ptr := roq.io.flushOut.bits.ftqIdx 241 val flushPC = GetPcByFtq( 242 ftq.io.ftqRead(2).entry.ftqPC, 243 RegEnable(roq.io.flushOut.bits.ftqOffset, roq.io.flushOut.valid), 244 ftq.io.ftqRead(2).entry.hasLastPrev 245 ) 246 247 val flushRedirect = Wire(Valid(new Redirect)) 248 flushRedirect.valid := RegNext(flush) 249 flushRedirect.bits := DontCare 250 flushRedirect.bits.ftqIdx := RegEnable(roq.io.flushOut.bits.ftqIdx, flush) 251 flushRedirect.bits.interrupt := true.B 252 flushRedirect.bits.cfiUpdate.target := Mux(io.roqio.toCSR.isXRet || roq.io.exception.valid, 253 io.roqio.toCSR.trapTarget, 254 flushPC + 4.U // flush pipe 255 ) 256 257 io.frontend.redirect_cfiUpdate := Mux(flushRedirect.valid, flushRedirect, frontendRedirect) 258 io.frontend.commit_cfiUpdate := ftq.io.commit_ftqEntry 259 io.frontend.ftqEnqPtr := ftq.io.enqPtr 260 io.frontend.ftqLeftOne := ftq.io.leftOne 261 262 decode.io.in <> io.frontend.cfVec 263 264 val jumpInst = dispatch.io.enqIQCtrl(0).bits 265 val ftqOffsetReg = Reg(UInt(log2Up(PredictWidth).W)) 266 ftqOffsetReg := jumpInst.cf.ftqOffset 267 ftq.io.ftqRead(0).ptr := jumpInst.cf.ftqPtr // jump 268 io.toIntBlock.jumpPc := GetPcByFtq( 269 ftq.io.ftqRead(0).entry.ftqPC, ftqOffsetReg, ftq.io.ftqRead(0).entry.hasLastPrev 270 ) 271 io.toIntBlock.jalr_target := ftq.io.ftqRead(0).entry.target 272 273 // pipeline between decode and dispatch 274 for (i <- 0 until RenameWidth) { 275 PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready, 276 backendRedirect.valid || flush || io.frontend.redirect_cfiUpdate.valid) 277 } 278 279 rename.io.redirect <> backendRedirect 280 rename.io.flush := flush 281 rename.io.roqCommits <> roq.io.commits 282 rename.io.out <> dispatch.io.fromRename 283 rename.io.renameBypass <> dispatch.io.renameBypass 284 285 dispatch.io.redirect <> backendRedirect 286 dispatch.io.flush := flush 287 dispatch.io.enqRoq <> roq.io.enq 288 dispatch.io.enqLsq <> io.toLsBlock.enqLsq 289 dispatch.io.readIntRf <> io.toIntBlock.readRf 290 dispatch.io.readFpRf <> io.toFpBlock.readRf 291 dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) => 292 intBusyTable.io.allocPregs(i).valid := preg.isInt 293 fpBusyTable.io.allocPregs(i).valid := preg.isFp 294 intBusyTable.io.allocPregs(i).bits := preg.preg 295 fpBusyTable.io.allocPregs(i).bits := preg.preg 296 } 297 dispatch.io.numExist <> io.fromIntBlock.numExist ++ io.fromFpBlock.numExist ++ io.fromLsBlock.numExist 298 dispatch.io.enqIQCtrl <> io.toIntBlock.enqIqCtrl ++ io.toFpBlock.enqIqCtrl ++ io.toLsBlock.enqIqCtrl 299// dispatch.io.enqIQData <> io.toIntBlock.enqIqData ++ io.toFpBlock.enqIqData ++ io.toLsBlock.enqIqData 300 301 302 fpBusyTable.io.flush := flush 303 intBusyTable.io.flush := flush 304 for((wb, setPhyRegRdy) <- io.fromIntBlock.wbRegs.zip(intBusyTable.io.wbPregs)){ 305 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen 306 setPhyRegRdy.bits := wb.bits.uop.pdest 307 } 308 for((wb, setPhyRegRdy) <- io.fromFpBlock.wbRegs.zip(fpBusyTable.io.wbPregs)){ 309 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen 310 setPhyRegRdy.bits := wb.bits.uop.pdest 311 } 312 intBusyTable.io.read <> dispatch.io.readIntState 313 fpBusyTable.io.read <> dispatch.io.readFpState 314 315 roq.io.redirect <> backendRedirect 316 roq.io.exeWbResults.zip( 317 io.fromIntBlock.wbRegs ++ io.fromFpBlock.wbRegs ++ io.fromLsBlock.stOut 318 ).foreach{ 319 case(x, y) => 320 x.bits := y.bits 321 x.valid := y.valid 322 } 323 324 // TODO: is 'backendRedirect' necesscary? 325 io.toIntBlock.redirect <> backendRedirect 326 io.toIntBlock.flush <> flush 327 io.toFpBlock.redirect <> backendRedirect 328 io.toFpBlock.flush <> flush 329 io.toLsBlock.redirect <> backendRedirect 330 io.toLsBlock.flush <> RegNext(flush) 331 332 if (env.DualCoreDifftest) { 333 difftestIO.fromRoq <> roq.difftestIO 334 trapIO <> roq.trapIO 335 } 336 337 dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex 338 dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex 339 340 // roq to int block 341 io.roqio.toCSR <> roq.io.csr 342 io.roqio.exception := roq.io.exception 343 io.roqio.exception.bits.uop.cf.pc := flushPC 344 // roq to mem block 345 io.roqio.lsq <> roq.io.lsq 346} 347