1package xiangshan.backend.issue 2 3import chipsalliance.rocketchip.config.Parameters 4import chisel3._ 5import chisel3.util._ 6import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 7import xiangshan._ 8import xiangshan.backend.Bundles 9import xiangshan.backend.datapath.DataConfig.VAddrData 10import xiangshan.backend.regfile.RfWritePortWithConfig 11import xiangshan.backend.rename.BusyTable 12import xiangshan.mem.{LsqEnqCtrl, LsqEnqIO, MemWaitUpdateReq, SqPtr} 13import xiangshan.backend.Bundles.{DynInst, IssueQueueWakeUpBundle} 14 15sealed trait SchedulerType 16 17case class IntScheduler() extends SchedulerType 18case class MemScheduler() extends SchedulerType 19case class VfScheduler() extends SchedulerType 20case class NoScheduler() extends SchedulerType 21 22class Scheduler(val params: SchdBlockParams)(implicit p: Parameters) extends LazyModule with HasXSParameter { 23 val numIntStateWrite = backendParams.numIntWb 24 val numVfStateWrite = backendParams.numVfWb 25 26 val dispatch2Iq = LazyModule(new Dispatch2Iq(params)) 27 val issueQueue = params.issueBlockParams.map(x => LazyModule(new IssueQueue(x).suggestName(x.getIQName))) 28 29 lazy val module = params.schdType match { 30 case IntScheduler() => new SchedulerArithImp(this)(params, p) 31 case MemScheduler() => new SchedulerMemImp(this)(params, p) 32 case VfScheduler() => new SchedulerArithImp(this)(params, p) 33 case _ => null 34 } 35} 36 37class SchedulerIO()(implicit params: SchdBlockParams, p: Parameters) extends XSBundle { 38 // params alias 39 private val LoadQueueSize = VirtualLoadQueueSize 40 41 val fromTop = new Bundle { 42 val hartId = Input(UInt(8.W)) 43 } 44 val fromWbFuBusyTable = new Bundle{ 45 val fuBusyTableRead = MixedVec(params.issueBlockParams.map(x => Input(x.genWbFuBusyTableReadBundle))) 46 } 47 val toWbFuBusyTable = new Bundle{ 48 val intFuBusyTableWrite = MixedVec(params.issueBlockParams.map(x => x.genWbFuBusyTableWriteBundle)) 49 val vfFuBusyTableWrite = MixedVec(params.issueBlockParams.map(x => x.genWbFuBusyTableWriteBundle)) 50 } 51 val fromCtrlBlock = new Bundle { 52 val pcVec = Input(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W))) 53 val targetVec = Input(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W))) 54 val flush = Flipped(ValidIO(new Redirect)) 55 } 56 val fromDispatch = new Bundle { 57 val allocPregs = Vec(RenameWidth, Input(new ResetPregStateReq)) 58 val uops = Vec(params.numUopIn, Flipped(DecoupledIO(new DynInst))) 59 } 60 val intWriteBack = MixedVec(Vec(backendParams.intPregParams.numWrite, 61 new RfWritePortWithConfig(backendParams.intPregParams.dataCfg, backendParams.intPregParams.addrWidth))) 62 val vfWriteBack = MixedVec(Vec(backendParams.vfPregParams.numWrite, 63 new RfWritePortWithConfig(backendParams.vfPregParams.dataCfg, backendParams.vfPregParams.addrWidth))) 64 val toDataPath: MixedVec[MixedVec[DecoupledIO[Bundles.IssueQueueIssueBundle]]] = MixedVec(params.issueBlockParams.map(_.genIssueDecoupledBundle)) 65 val fromDataPath: MixedVec[MixedVec[Bundles.OGRespBundle]] = MixedVec(params.issueBlockParams.map(x => Flipped(x.genOGRespBundle))) 66 67 val memIO = if (params.isMemSchd) Some(new Bundle { 68 val lsqEnqIO = Flipped(new LsqEnqIO) 69 }) else None 70 val fromMem = if (params.isMemSchd) Some(new Bundle { 71 val ldaFeedback = Flipped(Vec(params.LduCnt, new MemRSFeedbackIO)) 72 val staFeedback = Flipped(Vec(params.StaCnt, new MemRSFeedbackIO)) 73 val stIssuePtr = Input(new SqPtr()) 74 val lcommit = Input(UInt(log2Up(CommitWidth + 1).W)) 75 val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) // connected to `memBlock.io.sqDeq` instead of ROB 76 // from lsq 77 val lqCancelCnt = Input(UInt(log2Up(LoadQueueSize + 1).W)) 78 val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W)) 79 val memWaitUpdateReq = Flipped(new MemWaitUpdateReq) 80 }) else None 81 val toMem = if (params.isMemSchd) Some(new Bundle { 82 val loadFastMatch = Output(Vec(params.LduCnt, new IssueQueueLoadBundle)) 83 }) else None 84} 85 86abstract class SchedulerImpBase(wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters) 87 extends LazyModuleImp(wrapper) 88 with HasXSParameter 89{ 90 val io = IO(new SchedulerIO()) 91 92 // alias 93 private val schdType = params.schdType 94 private val (numRfRead, numRfWrite) = params.numRfReadWrite.getOrElse((0, 0)) 95 private val numPregs = params.numPregs 96 97 // Modules 98 val dispatch2Iq: Dispatch2IqImp = wrapper.dispatch2Iq.module 99 val issueQueues: Seq[IssueQueueImp] = wrapper.issueQueue.map(_.module) 100 101 // BusyTable Modules 102 val intBusyTable = schdType match { 103 case IntScheduler() | MemScheduler() => Some(Module(new BusyTable(dispatch2Iq.numIntStateRead, wrapper.numIntStateWrite))) 104 case _ => None 105 } 106 107 val vfBusyTable = schdType match { 108 case VfScheduler() | MemScheduler() => Some(Module(new BusyTable(dispatch2Iq.numVfStateRead, wrapper.numVfStateWrite))) 109 case _ => None 110 } 111 112 dispatch2Iq.io match { case dp2iq => 113 dp2iq.redirect <> io.fromCtrlBlock.flush 114 dp2iq.in <> io.fromDispatch.uops 115 dp2iq.readIntState.foreach(_ <> intBusyTable.get.io.read) 116 dp2iq.readVfState.foreach(_ <> vfBusyTable.get.io.read) 117 } 118 119 intBusyTable match { 120 case Some(bt) => 121 bt.io.allocPregs.zip(io.fromDispatch.allocPregs).foreach { case (btAllocPregs, dpAllocPregs) => 122 btAllocPregs.valid := dpAllocPregs.isInt 123 btAllocPregs.bits := dpAllocPregs.preg 124 } 125 bt.io.wbPregs.zipWithIndex.foreach { case (wb, i) => 126 wb.valid := io.intWriteBack(i).wen && io.intWriteBack(i).intWen 127 wb.bits := io.intWriteBack(i).addr 128 } 129 case None => 130 } 131 132 vfBusyTable match { 133 case Some(bt) => 134 bt.io.allocPregs.zip(io.fromDispatch.allocPregs).foreach { case (btAllocPregs, dpAllocPregs) => 135 btAllocPregs.valid := dpAllocPregs.isFp 136 btAllocPregs.bits := dpAllocPregs.preg 137 } 138 bt.io.wbPregs.zipWithIndex.foreach { case (wb, i) => 139 wb.valid := io.vfWriteBack(i).wen && (io.vfWriteBack(i).fpWen || io.vfWriteBack(i).vecWen) 140 wb.bits := io.vfWriteBack(i).addr 141 } 142 case None => 143 } 144 145 val wakeupFromWBVec = Wire(Vec(params.numWakeupFromWB, ValidIO(new IssueQueueWakeUpBundle(params.pregIdxWidth)))) 146 val writeback = params.schdType match { 147 case IntScheduler() => io.intWriteBack 148 case MemScheduler() => io.intWriteBack ++ io.vfWriteBack 149 case VfScheduler() => io.vfWriteBack 150 case _ => Seq() 151 } 152 wakeupFromWBVec.zip(writeback).foreach { case (sink, source) => 153 sink.valid := source.wen 154 sink.bits.rfWen := source.intWen 155 sink.bits.fpWen := source.fpWen 156 sink.bits.vecWen := source.vecWen 157 sink.bits.pdest := source.addr 158 } 159 160 io.toDataPath.zipWithIndex.foreach { case (toDp, i) => 161 toDp <> issueQueues(i).io.deq 162 } 163} 164 165class SchedulerArithImp(override val wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters) 166 extends SchedulerImpBase(wrapper) 167 with HasXSParameter 168{ 169// dontTouch(io.vfWbFuBusyTable) 170 println(s"[SchedulerArithImp] " + 171 s"has intBusyTable: ${intBusyTable.nonEmpty}, " + 172 s"has vfBusyTable: ${vfBusyTable.nonEmpty}") 173 174 issueQueues.zipWithIndex.foreach { case (iq, i) => 175 iq.io.flush <> io.fromCtrlBlock.flush 176 iq.io.enq <> dispatch2Iq.io.out(i) 177 iq.io.wakeup := wakeupFromWBVec 178 iq.io.deqResp.zipWithIndex.foreach { case (deqResp, j) => 179 deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready 180 deqResp.bits.respType := RSFeedbackType.issueSuccess 181 deqResp.bits.addrOH := iq.io.deq(j).bits.addrOH 182 deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B) 183 deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType 184 185 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready 186 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType 187 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.respType := RSFeedbackType.issueSuccess 188 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B) 189 190 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready 191 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType 192 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).deqResp.bits.respType := RSFeedbackType.issueSuccess 193 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B) 194 } 195 iq.io.og0Resp.zipWithIndex.foreach { case (og0Resp, j) => 196 og0Resp.valid := io.fromDataPath(i)(j).og0resp.valid 197 og0Resp.bits.respType := io.fromDataPath(i)(j).og0resp.bits.respType 198 og0Resp.bits.addrOH := io.fromDataPath(i)(j).og0resp.bits.addrOH 199 og0Resp.bits.rfWen := io.fromDataPath(i)(j).og0resp.bits.rfWen 200 og0Resp.bits.fuType := io.fromDataPath(i)(j).og0resp.bits.fuType 201 202 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.valid := io.fromDataPath(i)(j).og0resp.valid 203 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.fuType := io.fromDataPath(i)(j).og0resp.bits.fuType 204 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.respType := io.fromDataPath(i)(j).og0resp.bits.respType 205 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.rfWen := io.fromDataPath(i)(j).og0resp.bits.rfWen 206 207 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og0Resp.valid := io.fromDataPath(i)(j).og0resp.valid 208 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og0Resp.bits.fuType := io.fromDataPath(i)(j).og0resp.bits.fuType 209 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og0Resp.bits.respType := io.fromDataPath(i)(j).og0resp.bits.respType 210 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og0Resp.bits.rfWen := io.fromDataPath(i)(j).og0resp.bits.rfWen 211 } 212 iq.io.og1Resp.zipWithIndex.foreach { case (og1Resp, j) => 213 og1Resp.valid := io.fromDataPath(i)(j).og1resp.valid 214 og1Resp.bits.respType := io.fromDataPath(i)(j).og1resp.bits.respType 215 og1Resp.bits.addrOH := io.fromDataPath(i)(j).og1resp.bits.addrOH 216 og1Resp.bits.rfWen := io.fromDataPath(i)(j).og1resp.bits.rfWen 217 og1Resp.bits.fuType := io.fromDataPath(i)(j).og1resp.bits.fuType 218 219 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.valid := io.fromDataPath(i)(j).og1resp.valid 220 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.fuType := io.fromDataPath(i)(j).og1resp.bits.fuType 221 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.respType := io.fromDataPath(i)(j).og1resp.bits.respType 222 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.rfWen := io.fromDataPath(i)(j).og1resp.bits.rfWen 223 224 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og1Resp.valid := io.fromDataPath(i)(j).og1resp.valid 225 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og1Resp.bits.fuType := io.fromDataPath(i)(j).og1resp.bits.fuType 226 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og1Resp.bits.respType := io.fromDataPath(i)(j).og1resp.bits.respType 227 io.toWbFuBusyTable.vfFuBusyTableWrite(i)(j).og1Resp.bits.rfWen := io.fromDataPath(i)(j).og1resp.bits.rfWen 228 } 229 230 iq.io.wbBusyTableRead := io.fromWbFuBusyTable.fuBusyTableRead(i) 231 } 232 233 val iqJumpBundleVec: Seq[IssueQueueJumpBundle] = issueQueues.map { 234 case imp: IssueQueueIntImp => imp.io.enqJmp 235 case _ => None 236 }.filter(_.nonEmpty).flatMap(_.get) 237 println(s"[Scheduler] iqJumpBundleVec: ${iqJumpBundleVec}") 238 239 iqJumpBundleVec.zip(io.fromCtrlBlock.pcVec zip io.fromCtrlBlock.targetVec).foreach { case (iqJmp, (pc, target)) => 240 iqJmp.pc := pc 241 iqJmp.target := target 242 } 243} 244 245class SchedulerMemImp(override val wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters) 246 extends SchedulerImpBase(wrapper) 247 with HasXSParameter 248{ 249 println(s"[SchedulerMemImp] " + 250 s"has intBusyTable: ${intBusyTable.nonEmpty}, " + 251 s"has vfBusyTable: ${vfBusyTable.nonEmpty}") 252 253 val memAddrIQs = issueQueues.filter(iq => iq.params.StdCnt == 0) 254 val stAddrIQs = issueQueues.filter(iq => iq.params.StaCnt > 0) // included in memAddrIQs 255 val ldAddrIQs = issueQueues.filter(iq => iq.params.LduCnt > 0) 256 val stDataIQs = issueQueues.filter(iq => iq.params.StdCnt > 0) 257 require(memAddrIQs.nonEmpty && stDataIQs.nonEmpty) 258 259 issueQueues.zipWithIndex.foreach { case (iq, i) => 260 iq.io.deqResp.zipWithIndex.foreach { case (deqResp, j) => 261 deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready 262 deqResp.bits.respType := RSFeedbackType.issueSuccess 263 deqResp.bits.addrOH := iq.io.deq(j).bits.addrOH 264 deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B) 265 deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType 266 267 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready 268 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType 269 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.respType := RSFeedbackType.issueSuccess 270 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B) 271 } 272 iq.io.og0Resp.zipWithIndex.foreach { case (og0Resp, j) => 273 og0Resp.valid := io.fromDataPath(i)(j).og0resp.valid 274 og0Resp.bits.respType := io.fromDataPath(i)(j).og0resp.bits.respType 275 og0Resp.bits.addrOH := io.fromDataPath(i)(j).og0resp.bits.addrOH 276 og0Resp.bits.rfWen := io.fromDataPath(i)(j).og0resp.bits.rfWen 277 og0Resp.bits.fuType := io.fromDataPath(i)(j).og0resp.bits.fuType 278 279 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.valid := io.fromDataPath(i)(j).og0resp.valid 280 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.fuType := io.fromDataPath(i)(j).og0resp.bits.fuType 281 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.respType := io.fromDataPath(i)(j).og0resp.bits.respType 282 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og0Resp.bits.rfWen := io.fromDataPath(i)(j).og0resp.bits.rfWen 283 } 284 iq.io.og1Resp.zipWithIndex.foreach { case (og1Resp, j) => 285 og1Resp.valid := io.fromDataPath(i)(j).og1resp.valid 286 og1Resp.bits.respType := io.fromDataPath(i)(j).og1resp.bits.respType 287 og1Resp.bits.addrOH := io.fromDataPath(i)(j).og1resp.bits.addrOH 288 og1Resp.bits.rfWen := io.fromDataPath(i)(j).og1resp.bits.rfWen 289 og1Resp.bits.fuType := io.fromDataPath(i)(j).og1resp.bits.fuType 290 291 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.valid := io.fromDataPath(i)(j).og1resp.valid 292 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.fuType := io.fromDataPath(i)(j).og1resp.bits.fuType 293 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.respType := io.fromDataPath(i)(j).og1resp.bits.respType 294 io.toWbFuBusyTable.intFuBusyTableWrite(i)(j).og1Resp.bits.rfWen := io.fromDataPath(i)(j).og1resp.bits.rfWen 295 } 296 iq.io.wbBusyTableRead := io.fromWbFuBusyTable.fuBusyTableRead(i) 297 } 298 299 memAddrIQs.zipWithIndex.foreach { case (iq, i) => 300 iq.io.flush <> io.fromCtrlBlock.flush 301 iq.io.enq <> dispatch2Iq.io.out(i) 302 iq.io.wakeup := wakeupFromWBVec 303 } 304 305 ldAddrIQs.foreach { 306 case imp: IssueQueueMemAddrImp => imp.io.memIO.get.feedbackIO <> io.fromMem.get.ldaFeedback 307 case _ => 308 } 309 310 stAddrIQs.foreach { 311 case imp: IssueQueueMemAddrImp => imp.io.memIO.get.feedbackIO <> io.fromMem.get.staFeedback 312 case _ => 313 } 314 315 dispatch2Iq.io.out(1).zip(stAddrIQs(0).io.enq).zip(stDataIQs(0).io.enq).foreach{ case((di, staIQ), stdIQ) => 316 val isAllReady = staIQ.ready && stdIQ.ready 317 di.ready := isAllReady 318 staIQ.valid := di.valid && isAllReady 319 stdIQ.valid := di.valid && isAllReady 320 } 321 322 require(stAddrIQs.size == stDataIQs.size, s"number of store address IQs(${stAddrIQs.size}) " + 323 s"should be equal to number of data IQs(${stDataIQs})") 324 stDataIQs.zip(stAddrIQs).zipWithIndex.foreach { case ((stdIQ, staIQ), i) => 325 stdIQ.io.flush <> io.fromCtrlBlock.flush 326 327 stdIQ.io.enq.zip(staIQ.io.enq).foreach { case (stdIQEnq, staIQEnq) => 328 stdIQEnq.bits := staIQEnq.bits 329 // Store data reuses store addr src(1) in dispatch2iq 330 // [dispatch2iq] --src*------src*(0)--> [staIQ] 331 // \ 332 // ---src*(1)--> [stdIQ] 333 // Since the src(1) of sta is easier to get, stdIQEnq.bits.src*(0) is assigned to staIQEnq.bits.src*(1) 334 // instead of dispatch2Iq.io.out(x).bits.src*(1) 335 stdIQEnq.bits.srcState(0) := staIQEnq.bits.srcState(1) 336 stdIQEnq.bits.srcType(0) := staIQEnq.bits.srcType(1) 337 stdIQEnq.bits.psrc(0) := staIQEnq.bits.psrc(1) 338 stdIQEnq.bits.sqIdx := staIQEnq.bits.sqIdx 339 } 340 stdIQ.io.wakeup := wakeupFromWBVec 341 } 342 343 val lsqEnqCtrl = Module(new LsqEnqCtrl) 344 345 lsqEnqCtrl.io.redirect <> io.fromCtrlBlock.flush 346 lsqEnqCtrl.io.enq <> dispatch2Iq.io.enqLsqIO.get 347 lsqEnqCtrl.io.lcommit := io.fromMem.get.lcommit 348 lsqEnqCtrl.io.scommit := io.fromMem.get.scommit 349 lsqEnqCtrl.io.lqCancelCnt := io.fromMem.get.lqCancelCnt 350 lsqEnqCtrl.io.sqCancelCnt := io.fromMem.get.sqCancelCnt 351 io.memIO.get.lsqEnqIO <> lsqEnqCtrl.io.enqLsq 352} 353